2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-03-05 12:33:02 +00:00

fix: use buffer binding point instead of index

This commit is contained in:
Jan Laupetin
2026-03-01 01:13:35 +01:00
parent b2f51b2ae1
commit 400c6ca78a
2 changed files with 91 additions and 15 deletions

View File

@@ -408,12 +408,20 @@ namespace
{ {
assert(m_shader_info); assert(m_shader_info);
auto bufferIndex = 0uz;
auto usedConstantIndex = 0uz; auto usedConstantIndex = 0uz;
const auto bufferCount = m_shader_info->m_constant_buffers.size(); for (const auto& buffer : m_shader_info->m_constant_buffers)
while (bufferIndex < bufferCount)
{ {
const auto& buffer = m_shader_info->m_constant_buffers[bufferIndex]; const auto bufferBinding =
std::ranges::find_if(m_shader_info->m_bound_resources,
[buffer](const d3d11::BoundResource& boundResource)
{
return boundResource.m_type == d3d11::BoundResourceType::CBUFFER && boundResource.m_name == buffer.m_name;
});
if (bufferBinding == m_shader_info->m_bound_resources.end())
{
errorMessage = std::format("Failed to find binding for constant buffer {}", buffer.m_name);
return false;
}
auto variableIterator = buffer.m_variables.begin(); auto variableIterator = buffer.m_variables.begin();
const auto variableEnd = buffer.m_variables.end(); const auto variableEnd = buffer.m_variables.end();
@@ -439,15 +447,13 @@ namespace
const auto variableElementSize = variableIterator->m_size / variableElementCount; const auto variableElementSize = variableIterator->m_size / variableElementCount;
commonDestination.dx11.m_location.constant_buffer_offset = variableIterator->m_offset + variableElementSize * inputArgumentIndex; commonDestination.dx11.m_location.constant_buffer_offset = variableIterator->m_offset + variableElementSize * inputArgumentIndex;
commonDestination.dx11.m_size = variableElementSize; commonDestination.dx11.m_size = variableElementSize;
commonDestination.dx11.m_buffer = static_cast<unsigned>(bufferIndex); commonDestination.dx11.m_buffer = bufferBinding->m_bind_point;
isTransposed = variableIterator->m_variable_class == d3d11::VariableClass::MATRIX_COLUMNS; isTransposed = variableIterator->m_variable_class == d3d11::VariableClass::MATRIX_COLUMNS;
m_const_arg_added[usedConstantIndex] = true; m_const_arg_added[usedConstantIndex] = true;
return true; return true;
} }
bufferIndex++;
} }
return false; return false;
@@ -514,12 +520,20 @@ namespace
result::Expected<NoResult, std::string> AutoCreateMissingArgs() override result::Expected<NoResult, std::string> AutoCreateMissingArgs() override
{ {
size_t bufferIndex = 0;
size_t usedConstantCount = 0; size_t usedConstantCount = 0;
size_t textureCount = 0; size_t textureCount = 0;
for (const auto& buffer : m_shader_info->m_constant_buffers) for (const auto& buffer : m_shader_info->m_constant_buffers)
{ {
const auto bufferBinding =
std::ranges::find_if(m_shader_info->m_bound_resources,
[buffer](const d3d11::BoundResource& boundResource)
{
return boundResource.m_type == d3d11::BoundResourceType::CBUFFER && boundResource.m_name == buffer.m_name;
});
if (bufferBinding == m_shader_info->m_bound_resources.end())
return result::Unexpected(std::format("Failed to find binding for constant buffer {}", buffer.m_name));
for (const auto& variable : buffer.m_variables) for (const auto& variable : buffer.m_variables)
{ {
if (!variable.m_is_used) if (!variable.m_is_used)
@@ -528,12 +542,10 @@ namespace
if (m_const_arg_added[usedConstantCount++]) if (m_const_arg_added[usedConstantCount++])
continue; continue;
auto result = AutoCreateConstantArg(variable, bufferIndex); auto result = AutoCreateConstantArg(variable, bufferBinding->m_bind_point);
if (!result) if (!result)
return std::move(result); return std::move(result);
} }
bufferIndex++;
} }
for (const auto& maybeTextureResource : m_shader_info->m_bound_resources) for (const auto& maybeTextureResource : m_shader_info->m_bound_resources)

View File

@@ -8,6 +8,7 @@
#include "SearchPath/MockSearchPath.h" #include "SearchPath/MockSearchPath.h"
#include "Shader/ShaderCommon.h" #include "Shader/ShaderCommon.h"
#include "Utils/MemoryManager.h" #include "Utils/MemoryManager.h"
#include "catch2/generators/catch_generators.hpp"
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <filesystem> #include <filesystem>
@@ -72,7 +73,8 @@ TEST_CASE("TechniqueCompilerT6", "[t6][techset][compiler]")
SECTION("Can compile simple technique") SECTION("Can compile simple technique")
{ {
searchPath.AddFileData("techniques/pimp_technique_zprepass_example.tech", R"TECHNIQUE( const auto [inputName, inputData] = GENERATE(Catch::Generators::table<const char*, const char*>({
{"auto-create args", R"TECHNIQUE(
{ {
stateMap "passthrough"; stateMap "passthrough";
@@ -86,7 +88,28 @@ TEST_CASE("TechniqueCompilerT6", "[t6][techset][compiler]")
vertex.position = code.position; vertex.position = code.position;
} }
)TECHNIQUE"); )TECHNIQUE"},
{"manual args", R"TECHNIQUE(
{
stateMap "passthrough";
vertexShader 4.0 "simple.hlsl"
{
worldMatrix = constant.worldMatrix;
viewProjectionMatrix = constant.viewProjectionMatrix;
}
pixelShader 4.0 "simple.hlsl"
{
}
vertex.position = code.position;
}
)TECHNIQUE"},
}));
CAPTURE(inputName);
searchPath.AddFileData("techniques/pimp_technique_zprepass_example.tech", inputData);
GivenVertexShaderFile("simple.hlsl", searchPath); GivenVertexShaderFile("simple.hlsl", searchPath);
GivenPixelShaderFile("simple.hlsl", searchPath); GivenPixelShaderFile("simple.hlsl", searchPath);
@@ -147,7 +170,8 @@ TEST_CASE("TechniqueCompilerT6", "[t6][techset][compiler]")
SECTION("Can compile advanced technique") SECTION("Can compile advanced technique")
{ {
searchPath.AddFileData("techniques/example_lit_sun_shadow.tech", R"TECHNIQUE( const auto [inputName, inputData] = GENERATE(Catch::Generators::table<const char*, const char*>({
{"auto-create args", R"TECHNIQUE(
{ {
stateMap "passthrough"; stateMap "passthrough";
@@ -176,7 +200,47 @@ TEST_CASE("TechniqueCompilerT6", "[t6][techset][compiler]")
vertex.texcoord[4] = code.texcoord[3]; vertex.texcoord[4] = code.texcoord[3];
vertex.texcoord[5] = code.normalTransform[0]; vertex.texcoord[5] = code.normalTransform[0];
} }
)TECHNIQUE"); )TECHNIQUE"},
{"manual args", R"TECHNIQUE(
{
stateMap "passthrough";
vertexShader 4.0 "advanced.hlsl"
{
worldMatrix = constant.worldMatrix;
viewProjectionMatrix = constant.viewProjectionMatrix;
shadowLookupMatrix = constant.shadowLookupMatrix;
}
pixelShader 4.0 "advanced.hlsl"
{
normalMapSampler = material.normalMap;
normalMapSampler1 = material.normalMap1;
colorMapSampler = material.colorMap;
colorMapSampler1 = material.colorMap1;
colorMapSampler3 = material.colorMap3;
colorMapSampler2 = material.colorMap2;
shadowmapSamplerSun = sampler.shadowmapSamplerSun;
shadowmapSwitchPartition = constant.shadowmapSwitchPartition;
sunShadowmapPixelSize = constant.sunShadowmapPixelSize;
alphaRevealParms1 = material.alphaRevealParms1;
}
vertex.position = code.position;
vertex.color[0] = code.color;
vertex.texcoord[0] = code.texcoord[0];
vertex.normal = code.normal;
vertex.texcoord[2] = code.tangent;
vertex.texcoord[1] = code.texcoord[1];
vertex.texcoord[3] = code.texcoord[2];
vertex.texcoord[4] = code.texcoord[3];
vertex.texcoord[5] = code.normalTransform[0];
}
)TECHNIQUE"},
}));
CAPTURE(inputName);
searchPath.AddFileData("techniques/example_lit_sun_shadow.tech", inputData);
GivenVertexShaderFile("advanced.hlsl", searchPath); GivenVertexShaderFile("advanced.hlsl", searchPath);
GivenPixelShaderFile("advanced.hlsl", searchPath); GivenPixelShaderFile("advanced.hlsl", searchPath);