载入中...
搜索中...
未找到
Shader.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <map>
9#include <span>
10#include <string>
11#include <vector>
12#include "common/Result.h"
13#include "graphics/BlendMode.h"
15
16// Incomplete glm::mat4 is enough for const-reference parameters.
17#include <glm/fwd.hpp>
18
19namespace eve::graphics {
20class Texture;
21namespace vulkan {
22class Graphics;
23}
24
42public:
43 static constexpr int kMaxFloats = 32;
44 static constexpr std::size_t kMaxMeshTextures = 4;
45 static constexpr uint32_t kPushConstantBytes = uint32_t(kMaxFloats * sizeof(float));
46
48 enum class Kind { eSprite2D, eMesh3D };
49
58
61
62 Shader(const Shader &) = delete;
63 Shader &operator=(const Shader &) = delete;
64
65 Kind getKind() const { return kind_; }
66 void setKind(Kind k) { kind_ = k; }
67
74 void setXray(bool x) { isXray_ = x; }
75 bool isXray() const { return isXray_; }
76
78 BlendMode meshBlend = BlendMode::Opaque;
80 bool meshDepthWrite = true;
82 bool meshDoubleSided = true;
84 const MeshShaderRasterState &meshRasterState() const noexcept { return meshRaster_; }
85
87 int declareFloat(const std::string &name);
88 int declareVec2(const std::string &name);
89 int declareVec3(const std::string &name);
90 int declareVec4(const std::string &name);
91 int declareMatrix(const std::string &name);
92
93 void sendFloat(const std::string &name, float x);
94 void sendVec2(const std::string &name, float x, float y);
95 void sendVec3(const std::string &name, float x, float y, float z);
96 void sendVec4(const std::string &name, float x, float y, float z, float w);
97 void sendMatrix(const std::string &name, const glm::mat4 &m);
98
100 int sendToVar(const std::string &name, const void *data, size_t size);
101 int getFromVar(const std::string &name, void *data, size_t size) const;
102
103 bool hasUniform(const std::string &name) const;
104 int getUniformIndex(const std::string &name) const;
105 int getUniformFloatCount(const std::string &name) const;
106
107 const float *pushConstantData() const { return floats_.data(); }
115 void setPushConstantBlock(const std::array<float, kMaxFloats> &values) noexcept;
116 uint32_t pushConstantSize() const { return uint32_t(usedFloats_ * sizeof(float)); }
122 [[nodiscard]] Result<void> sendFloatBlock(std::span<const float> values);
123 int usedFloats() const { return usedFloats_; }
124
133 [[nodiscard]] Result<void> setMeshTexture(std::size_t slot, Texture *texture);
140 Texture *meshTexture(std::size_t slot) const {
141 return slot < meshTextures_.size() ? meshTextures_[slot] : nullptr;
142 }
143
144 const std::vector<uint32_t> &vertexSpirv() const { return vertSpv_; }
145 const std::vector<uint32_t> &fragmentSpirv() const { return fragSpv_; }
146
147 void setSpirv(std::vector<uint32_t> vert, std::vector<uint32_t> frag) {
148 vertSpv_ = std::move(vert);
149 fragSpv_ = std::move(frag);
150 }
151
153 void *gpuHandle = nullptr;
154
155private:
156 friend class vulkan::Graphics;
157 MeshShaderRasterState meshRaster_;
158 struct Uniform {
159 int index = 0;
160 int floatCount = 0;
161 };
162
163 int declareUniform(const std::string &name, int floatCount);
164 Uniform *findUniform(const std::string &name);
165 const Uniform *findUniform(const std::string &name) const;
166
167 Kind kind_ = Kind::eSprite2D;
168 bool isXray_ = false;
169 std::vector<uint32_t> vertSpv_;
170 std::vector<uint32_t> fragSpv_;
171 std::map<std::string, Uniform> uniforms_;
172 std::array<float, kMaxFloats> floats_{};
173 std::array<Texture *, kMaxMeshTextures> meshTextures_{};
174 int usedFloats_ = 0;
175};
176
177} // 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
#define EVENGINE_API_BACKENDS
Definition Export.h:108
vk::ShaderModule vert
vk::ShaderModule frag
Texture * texture
std::set< std::string > values
std::string slot
Move-only, checked operation results for the common layer.
float size
Definition TreeMesh.cpp:158
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Custom GPU program.
Definition Shader.h:41
void setSpirv(std::vector< uint32_t > vert, std::vector< uint32_t > frag)
Definition Shader.h:147
Shader & operator=(const Shader &)=delete
Kind getKind() const
Definition Shader.h:65
int usedFloats() const
Definition Shader.h:123
const std::vector< uint32_t > & fragmentSpirv() const
Definition Shader.h:145
Kind
Kind public API.
Definition Shader.h:48
void setXray(bool x)
X-ray mesh shader. When true, drawMeshShader renders this mesh a second pass over the already-lit sce...
Definition Shader.h:74
const MeshShaderRasterState & meshRasterState() const noexcept
Read the committed raster snapshot; render-thread affinity matches the owning Graphics.
Definition Shader.h:84
bool isXray() const
Definition Shader.h:75
Shader(const Shader &)=delete
const std::vector< uint32_t > & vertexSpirv() const
Definition Shader.h:144
void setKind(Kind k)
Definition Shader.h:66
Texture * meshTexture(std::size_t slot) const
Return the borrowed texture assigned to a custom-mesh slot, or null.
Definition Shader.h:140
uint32_t pushConstantSize() const
Definition Shader.h:116
const float * pushConstantData() const
Definition Shader.h:107
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
EVENGINE_API_BACKENDS public API.
Definition Graphics.h:360
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
BlendMode
Render-neutral 2D blend mode shared by graphics-facing modules.
Definition RenderTypes.h:11
Value snapshot of custom mesh rasterization state, independent of blending/culling.