载入中...
搜索中...
未找到
Shader.cpp
浏览该文件的文档.
1#include "graphics/Shader.h"
2#include "common/Exception.h"
3
4#include <algorithm>
5#include <cmath>
6#include <cstring>
7#include <glm/mat4x4.hpp>
8
9namespace eve::graphics {
10
12 if (slot >= meshTextures_.size())
14 DiagnosticCode::InvalidArgument, "custom mesh texture slot is outside [0,4)", {}, {}, "graphics.shader"));
15 meshTextures_[slot] = texture;
16 return Result<void>::success();
17}
18
19Shader::Shader() = default;
20
21Shader::~Shader() = default;
22
24 if (values.size() != static_cast<std::size_t>(usedFloats_) ||
25 !std::all_of(values.begin(), values.end(), [](float value) { return std::isfinite(value); }))
27 "Shader float block size or values invalid", "graphics.shader"));
28 std::copy(values.begin(), values.end(), floats_.begin());
29 return Result<void>::success();
30}
31
32void Shader::setPushConstantBlock(const std::array<float, kMaxFloats> &values) noexcept {
33 floats_ = values;
34 usedFloats_ = kMaxFloats;
35}
36
37int Shader::declareUniform(const std::string &name, int floatCount) {
38 if (name.empty()) throw Exception("Shader::declare: empty uniform name");
39 if (floatCount <= 0) throw Exception("Shader::declare: invalid float count");
40 if (auto *existing = findUniform(name)) {
41 if (existing->floatCount != floatCount)
42 throw Exception("Shader::declare: '%s' already declared with different size", name.c_str());
43 return existing->index;
44 }
45 if (usedFloats_ + floatCount > kMaxFloats)
46 throw Exception("Shader::declare: push-constant block full (max %d floats)", kMaxFloats);
47 Uniform u;
48 u.index = usedFloats_;
49 u.floatCount = floatCount;
50 uniforms_[name] = u;
51 usedFloats_ += floatCount;
52 return u.index;
53}
54
55int Shader::declareFloat(const std::string &name) { return declareUniform(name, 1); }
56int Shader::declareVec2(const std::string &name) { return declareUniform(name, 2); }
57int Shader::declareVec3(const std::string &name) { return declareUniform(name, 3); }
58int Shader::declareVec4(const std::string &name) { return declareUniform(name, 4); }
59int Shader::declareMatrix(const std::string &name) { return declareUniform(name, 16); }
60
61Shader::Uniform *Shader::findUniform(const std::string &name) {
62 auto it = uniforms_.find(name);
63 return it == uniforms_.end() ? nullptr : &it->second;
64}
65
66const Shader::Uniform *Shader::findUniform(const std::string &name) const {
67 auto it = uniforms_.find(name);
68 return it == uniforms_.end() ? nullptr : &it->second;
69}
70
71bool Shader::hasUniform(const std::string &name) const { return findUniform(name) != nullptr; }
72
73int Shader::getUniformIndex(const std::string &name) const {
74 auto *u = findUniform(name);
75 if (!u) throw Exception("Shader: unknown uniform '%s'", name.c_str());
76 return u->index;
77}
78
79int Shader::getUniformFloatCount(const std::string &name) const {
80 auto *u = findUniform(name);
81 if (!u) throw Exception("Shader: unknown uniform '%s'", name.c_str());
82 return u->floatCount;
83}
84
85int Shader::sendToVar(const std::string &name, const void *data, size_t size) {
86 auto *u = findUniform(name);
87 if (!u) throw Exception("Shader::send: unknown uniform '%s' (declare it first)", name.c_str());
88 if (!data) throw Exception("Shader::send: null data for '%s'", name.c_str());
89 const size_t expected = size_t(u->floatCount) * sizeof(float);
90 if (size != expected)
91 throw Exception("Shader::send: '%s' expects %zu bytes, got %zu", name.c_str(), expected, size);
92 std::memcpy(floats_.data() + u->index, data, size);
93 return int(size);
94}
95
96int Shader::getFromVar(const std::string &name, void *data, size_t size) const {
97 auto *u = findUniform(name);
98 if (!u) throw Exception("Shader::get: unknown uniform '%s'", name.c_str());
99 if (!data) throw Exception("Shader::get: null data for '%s'", name.c_str());
100 const size_t expected = size_t(u->floatCount) * sizeof(float);
101 if (size != expected)
102 throw Exception("Shader::get: '%s' expects %zu bytes, got %zu", name.c_str(), expected, size);
103 std::memcpy(data, floats_.data() + u->index, size);
104 return int(size);
105}
106
107void Shader::sendFloat(const std::string &name, float x) { sendToVar(name, &x, sizeof(x)); }
108
109void Shader::sendVec2(const std::string &name, float x, float y) {
110 const float v[2] = {x, y};
111 sendToVar(name, v, sizeof(v));
112}
113
114void Shader::sendVec3(const std::string &name, float x, float y, float z) {
115 const float v[3] = {x, y, z};
116 sendToVar(name, v, sizeof(v));
117}
118
119void Shader::sendVec4(const std::string &name, float x, float y, float z, float w) {
120 const float v[4] = {x, y, z, w};
121 sendToVar(name, v, sizeof(v));
122}
123
124void Shader::sendMatrix(const std::string &name, const glm::mat4 &m) {
125 sendToVar(name, &m[0][0], sizeof(float) * 16);
126}
127
128} // namespace eve::graphics
double z
double y
double m[16]
double x
float w
Definition AnimClip.cpp:738
std::string name
Definition ECS.cpp:154
float u
Definition Grass.cpp:233
float v
Texture * texture
std::set< std::string > values
Object::Ptr value
std::string slot
float size
Definition TreeMesh.cpp:158
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
bool hasUniform(const std::string &name) const
Definition Shader.cpp:71
void sendVec2(const std::string &name, float x, float y)
Definition Shader.cpp:109
int declareVec2(const std::string &name)
Definition Shader.cpp:56
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:107
void sendMatrix(const std::string &name, const glm::mat4 &m)
Definition Shader.cpp:124
int getFromVar(const std::string &name, void *data, size_t size) const
Definition Shader.cpp:96
void sendVec3(const std::string &name, float x, float y, float z)
Definition Shader.cpp:114
int declareMatrix(const std::string &name)
Definition Shader.cpp:59
int getUniformIndex(const std::string &name) const
Definition Shader.cpp:73
void setPushConstantBlock(const std::array< float, kMaxFloats > &values) noexcept
Replace the entire fixed push-constant ABI without name lookup or allocation.
Definition Shader.cpp:32
int getUniformFloatCount(const std::string &name) const
Definition Shader.cpp:79
int declareVec3(const std::string &name)
Definition Shader.cpp:57
int declareVec4(const std::string &name)
Definition Shader.cpp:58
static constexpr int kMaxFloats
Definition Shader.h:43
int declareFloat(const std::string &name)
Reserve sequential float slots in the push-constant block. Returns start index.
Definition Shader.cpp:55
Result< void > setMeshTexture(std::size_t slot, Texture *texture)
Assign a borrowed custom-mesh texture to a portable slot in [0,4).
Definition Shader.cpp:11
Result< void > sendFloatBlock(std::span< const float > values)
Replace all declared float slots atomically without name lookup.
Definition Shader.cpp:23
void sendVec4(const std::string &name, float x, float y, float z, float w)
Definition Shader.cpp:119
int sendToVar(const std::string &name, const void *data, size_t size)
Low-level blob write into a declared uniform (size must match declaration).
Definition Shader.cpp:85
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25