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