7 std::vector<TextureElement>& textureElements, std::vector<SamplerElement>& samplerElements)
9 textureElements.clear();
10 samplerElements.clear();
13 textureElements.reserve(textureCount);
14 samplerElements.reserve(samplerCount);
15 for (
size_t i = 0; i < textureCount; ++i) {
16 textureElements.push_back(resourceLayout.
getTexture(i));
18 for (
size_t i = 0; i < samplerCount; ++i) {
19 samplerElements.push_back(resourceLayout.
getSampler(i));
27 std::map<std::string, uint32_t> locations;
28 std::map<std::string, std::pair<uint32_t, uint32_t>> uniformsLocations;
29 std::map<std::string, std::pair<uint32_t, uint32_t>> constBufferLocations;
31 std::vector<TextureElement> texturesElements;
32 std::vector<SamplerElement> samplersElements;
50 const std::string& userCode)
53 std::vector<TextureElement> texturesElements;
54 std::vector<SamplerElement> samplersElements;
69 const std::map<uint32_t, VertexLayout>& vertexLayouts,
73 ss <<
"//FCT Default User Vertex Main\n";
74 ss <<
"ShaderOut main(ShaderIn sIn) {\n";
75 ss <<
" ShaderOut sOut;\n";
78 for (
const auto& [slot, layout] : vertexLayouts) {
79 for (
size_t i = 0; i < layout.getElementCount(); i++) {
86 ss <<
" sOut." << element->getSemantic() <<
" = sIn." << semantic <<
";\n";
91 ss <<
" sOut." << element->getSemantic() <<
" = sIn." << semantic <<
";\n";
93 ss <<
" //属性 " << semantic <<
" 在自动生成时被忽略 \n";
100 ss <<
" return sOut;\n";
108 std::stringstream ss;
109 ss <<
"//FCT Default User Pixel Main\n";
110 ss <<
"ShaderOut main(ShaderIn sIn) {\n";
111 ss <<
" ShaderOut sOut;\n";
119 const char* semantic = colorElement->getSemantic();
120 VtxType type = colorElement->getType();
123 ss <<
" sOut.target0 = sIn." << semantic <<
";\n";
125 ss <<
" sOut.target0 = float4(sIn." << semantic <<
", 1.0f);\n";
128 if (typeStr ==
"float4") {
129 ss <<
" sOut.target0 = sIn." << semantic <<
";\n";
130 }
else if (typeStr ==
"float3") {
131 ss <<
" sOut.target0 = float4(sIn." << semantic <<
", 1.0f);\n";
132 }
else if (typeStr ==
"float2") {
133 ss <<
" sOut.target0 = float4(sIn." << semantic <<
", 0.0f, 1.0f);\n";
134 }
else if (typeStr ==
"float") {
135 ss <<
" sOut.target0 = float4(sIn." << semantic <<
", 0.0f, 0.0f, 1.0f);\n";
137 ss <<
" sOut.target0 = float4(1.0f, 1.0f, 1.0f, 1.0f);\n";
141 ss <<
" // 没有找到颜色属性\n";
142 ss <<
" sOut.target0 = float4(1.0f, 1.0f, 1.0f, 1.0f);\n";
145 ss <<
" sOut.target1 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
146 ss <<
" sOut.target2 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
147 ss <<
" sOut.target3 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
148 ss <<
" sOut.target4 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
149 ss <<
" sOut.target5 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
150 ss <<
" sOut.target6 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
151 ss <<
" sOut.target7 = float4(0.0f, 0.0f, 0.0f, 0.0f);\n";
153 ss <<
" return sOut;\n";
160 const std::map<uint32_t, VertexLayout>& layouts,
161 std::map<std::string, uint32_t>& locationMap)
163 std::stringstream ss;
164 ss <<
"//FCT Input Structure\n";
165 ss <<
"struct ShaderIn {\n";
168 ss <<
" uint vertexID : SV_VertexID;\n";
169 ss <<
" uint instanceID : SV_InstanceID;\n\n";
172 std::set<std::string> usedSemantics;
173 for (
const auto& [slot, layout] : layouts) {
174 for (
size_t i = 0; i < layout.getElementCount(); i++) {
178 if (usedSemantics.find(semantic) != usedSemantics.end()) {
179 throw std::runtime_error(std::string(
"Semantic conflict detected: ") + semantic);
182 usedSemantics.insert(semantic);
186 int nextLocation = 0;
188 for (
const auto& [slot, layout] : layouts) {
189 ss <<
" // Vertex buffer slot " << slot <<
"\n";
191 for (
size_t i = 0; i < layout.getElementCount(); i++) {
196 locationMap[semantic] = nextLocation;
197 ss <<
" [[vk::location(" << nextLocation <<
")]] ";
198 ss << typeStr <<
" " << semantic <<
" : " << semantic <<
";\n";
203 if (slot != layouts.rbegin()->first) {
214 std::stringstream ss;
215 ss <<
"//FCT Input Structure\n";
216 ss <<
"struct ShaderIn {\n";
218 ss <<
" float4 position : SV_POSITION;\n";
227 std::stringstream ss;
228 ss <<
"//FCT Output Structure\n";
229 ss <<
"struct ShaderOut {\n";
231 ss <<
" float4 outPosition : SV_POSITION; \n";
240 std::stringstream ss;
241 ss <<
"//FCT Output Structure\n";
242 ss <<
"struct ShaderOut {\n";
243 ss <<
" float4 target0 : SV_Target0;\n";
244 ss <<
" float4 target1 : SV_Target1;\n";
245 ss <<
" float4 target2 : SV_Target2;\n";
246 ss <<
" float4 target3 : SV_Target3;\n";
247 ss <<
" float4 target4 : SV_Target4;\n";
248 ss <<
" float4 target5 : SV_Target5;\n";
249 ss <<
" float4 target6 : SV_Target6;\n";
250 ss <<
" float4 target7 : SV_Target7;\n";
258 std::stringstream ss;
259 ss <<
" //PixelLayout\n";
266 ss <<
" [[vk::location(" << i <<
")]] ";
267 ss << typeStr <<
" " << semantic <<
" : " << semantic <<
";\n";
274 const std::map<uint32_t, VertexLayout>& vertexLayouts,
277 std::stringstream ss;
278 ss <<
"ShaderOut main(ShaderIn);\n";
279 ss <<
"ShaderOut FCTEntry(ShaderIn sIn)\n";
281 ss <<
" ShaderOut sOut;\n";
284 bool hasPosition =
false;
300 ss <<
" sOut = main(sIn);\n";
302 if (hasPosition && posElement) {
307 ss <<
" sOut.outPosition = sOut." << semantic <<
";\n";
309 ss <<
" sOut.outPosition = float4(sOut." << semantic <<
", 1.0f);\n";
311 ss <<
" sOut.outPosition = float4(sOut." << semantic <<
", 0.0f, 1.0f);\n";
314 ss <<
" sOut.outPosition = float4(0.0f, 0.0f, 0.0f, 1.0f);\n";
318 ss <<
" return sOut;\n";
326 std::stringstream ss;
327 ss <<
"ShaderOut main(ShaderIn sIn);\n";
328 ss <<
"ShaderOut FCTEntry(ShaderIn sIn) {\n";
329 ss <<
" ShaderOut sOut;\n";
331 ss <<
" sOut = main(sIn);\n";
333 ss <<
" return sOut;\n";
366 if (storedTexture == texture) {
367 set = setBinding.first;
368 binding = setBinding.second;
379 return {set, binding};
408 if (storedSampler == sampler) {
409 set = setBinding.first;
410 binding = setBinding.second;
421 return {set, binding};
424 const std::vector<TextureElement>& textures,
const std::vector<SamplerElement>& samplers)
426 std::stringstream ss;
427 ss <<
"//FCT Textures and Samplers\n";
429 for (
const auto& texture : textures) {
434 std::string textureType;
435 switch (texture.getType()) {
437 textureType =
"Texture2D";
440 textureType =
"Texture3D";
443 textureType =
"TextureCube";
446 textureType =
"Texture2DArray";
449 textureType =
"Texture2D";
452 textureType =
"Texture2D";
456 ss <<
"[[vk::binding(" << binding <<
", " << set <<
")]] ";
457 ss << textureType <<
"<float4> " << texture.getName()
458 <<
" : register(t" << binding <<
", space" << set <<
");\n";
463 for (
const auto& sampler : samplers) {
468 ss <<
"[[vk::binding(" << binding <<
", " << set <<
")]] ";
469 ss <<
"SamplerState " << sampler.getName()
470 <<
" : register(s" << binding <<
", space" << set <<
");\n";
507 const std::vector<ConstLayout>& uniforms)
509 std::stringstream ss;
510 ss <<
"//FCT Constant Buffers\n";
512 for (
const auto& layout : uniforms) {
538 if (storedLayout == layout) {
539 set = setBinding.first;
540 binding = setBinding.second;
554 ss <<
"[[vk::binding(" << binding <<
", " << set <<
")]] ";
555 ss <<
"cbuffer " << layout.getName() <<
" : register(b" << binding <<
", space" << set <<
") {\n";
557 for (
size_t i = 0; i < layout.getElementCount(); i++) {
560 const char* name = element.
getName();
563 ss <<
" " << typeStr <<
" " << name <<
"[" << arraySize <<
"];\n";
565 ss <<
" " << typeStr <<
" " << name <<
";\n";
constexpr bool isArray() const noexcept
constexpr size_t getElementCount() const noexcept
constexpr ConstType getType() const noexcept
constexpr const char * getName() const noexcept
constexpr const VertexElement * getElementByType(VtxType type) const noexcept
constexpr size_t getElementCount() const noexcept
constexpr const VertexElement & getElement(size_t index) const noexcept
constexpr const VertexElement * getElementBySemantic(const char *semantic) const noexcept
void location(std::map< std::string, uint32_t > locations)
void addConstBufferLocation(ConstLayout layout, uint32_t set, uint32_t location)
void addTextureLocation(TextureElement texture, uint32_t set, uint32_t binding)
void addSamplerLocation(SamplerElement sampler, uint32_t set, uint32_t binding)
constexpr size_t getSamplerCount() const noexcept
constexpr const TextureElement & getTexture(size_t index) const noexcept
constexpr size_t getTextureCount() const noexcept
constexpr const SamplerElement & getSampler(size_t index) const noexcept
constexpr ShaderStages getShaderStages() const noexcept
constexpr UpdateFrequency getUpdateFrequency() const noexcept
std::string generateTexturesAndSamplers(RHI::ShaderBinary &binary, const std::vector< TextureElement > &textures, const std::vector< SamplerElement > &samplers)
std::string generateVertexShader(const std::map< uint32_t, VertexLayout > &vertexLayouts, const PixelLayout &pixelLayout, const std::vector< ConstLayout > &uniformLayouts, RHI::ShaderBinary &binary, ResourceLayout &resourceLayout, const std::string &userCode)
std::pair< uint32_t, uint32_t > allocateTextureBinding(const TextureElement &texture)
std::string generateShaderOut()
std::string generateShaderIn(const std::map< uint32_t, VertexLayout > &layouts, std::map< std::string, uint32_t > &locationMap)
std::string uniformTypeToShaderType(ConstType type)
std::vector< std::pair< SamplerElement, std::pair< uint32_t, uint32_t > > > m_samplerSetBindings
void ResourceLayoutToElements(ResourceLayout &resourceLayout, std::vector< TextureElement > &textureElements, std::vector< SamplerElement > &samplerElements)
std::vector< std::pair< ConstLayout, std::pair< uint32_t, uint32_t > > > m_layoutSetBindings
std::string generateDefaultPixelMain(const PixelLayout &pixelLayout)
std::string generateDefaultVertexMain(const std::map< uint32_t, VertexLayout > &vertexLayouts, const PixelLayout &pixelLayout)
std::string generatePixelShader(const PixelLayout &layout, const std::vector< ConstLayout > &uniformLayouts, RHI::ShaderBinary &binary, ResourceLayout &resourceLayout, const std::string &userCode)
std::pair< uint32_t, uint32_t > allocateSamplerBinding(const SamplerElement &sampler)
std::string generateVertexMain(const std::map< uint32_t, VertexLayout > &vertexLayouts, const PixelLayout &pixelLayout)
std::string generateConstBuffer(RHI::ShaderBinary &binary, const std::vector< ConstLayout > &uniforms)
std::string generatePixelMain(const PixelLayout &pixelLayout)
std::vector< std::pair< TextureElement, std::pair< uint32_t, uint32_t > > > m_textureSetBindings
std::string generateFromPixelLayout(const PixelLayout &layout)
std::unordered_map< UpdateFrequency, uint32_t > m_frequencyBindingCount
constexpr ShaderStages getShaderStages() const noexcept
constexpr UpdateFrequency getUpdateFrequency() const noexcept
constexpr VtxType getType() const noexcept
constexpr Format getFormat() const noexcept
constexpr const char * getSemantic() const noexcept
constexpr const char * FormatToShaderType(Format format) noexcept