From 4c7f30e690a9ee55922ca596f528c1783f9edc89 Mon Sep 17 00:00:00 2001 From: Steve Karolewics Date: Thu, 7 May 2020 19:21:17 -0700 Subject: [PATCH] Created a D3D11 version of the shader linkage zoo test --- .../demos/d3d11/d3d11_shader_linkage_zoo.cpp | 334 ++++++++++++++++++ util/test/demos/demos.vcxproj | 1 + util/test/demos/demos.vcxproj.filters | 3 + .../tests/D3D11/D3D11_Shader_Linkage_Zoo.py | 49 +++ 4 files changed, 387 insertions(+) create mode 100644 util/test/demos/d3d11/d3d11_shader_linkage_zoo.cpp create mode 100644 util/test/tests/D3D11/D3D11_Shader_Linkage_Zoo.py diff --git a/util/test/demos/d3d11/d3d11_shader_linkage_zoo.cpp b/util/test/demos/d3d11/d3d11_shader_linkage_zoo.cpp new file mode 100644 index 000000000..4b88758f0 --- /dev/null +++ b/util/test/demos/d3d11/d3d11_shader_linkage_zoo.cpp @@ -0,0 +1,334 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2020 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include "d3d11_test.h" + +RD_TEST(D3D11_Shader_Linkage_Zoo, D3D11GraphicsTest) +{ + static constexpr const char *Description = + "Tests various shader linkage scenarios to ensure proper handling of data between shader " + "stages."; + + enum class VarType : uint32_t + { + Float = 0, + UInt = 1, + Count = 2, + }; + const std::string varTypeName[(uint32_t)VarType::Count] = {"float", "uint"}; + + struct ShaderLinkageEntry + { + bool nointerp; + VarType type; + uint32_t components; + uint32_t arraySize; + std::string semantic; + bool consumedByPS; + }; + + std::string BuildStruct(const std::vector &outputs) + { + std::string structDef = R"EOSHADER( +struct v2f +{ + float4 pos : SV_POSITION; +)EOSHADER"; + + for(size_t i = 0; i < outputs.size(); ++i) + { + structDef += " "; + if(outputs[i].nointerp) + structDef += "nointerpolation "; + structDef += varTypeName[(uint32_t)outputs[i].type]; + structDef += std::to_string(outputs[i].components); + structDef += " "; + structDef += "element" + std::to_string(i); + if(outputs[i].arraySize != 0) + structDef += "[" + std::to_string(outputs[i].arraySize) + "]"; + structDef += " : " + outputs[i].semantic + ";\n"; + } + + structDef += "};"; + return structDef; + } + + std::string BuildVS(const std::vector &outputs) + { + std::string vs = R"EOSHADER( +struct vertin +{ + float3 pos : POSITION; + float4 col : COLOR0; + float2 uv : TEXCOORD0; +}; +)EOSHADER"; + + vs += BuildStruct(outputs); + + vs += R"EOSHADER( + +v2f main(vertin IN, uint vid : SV_VertexID) +{ + v2f OUT = (v2f)0; + OUT.pos = float4(IN.pos, 1.0f); +)EOSHADER"; + + float counterFloat = 0.0f; + uint32_t counterUInt = 0; + for(size_t i = 0; i < outputs.size(); ++i) + { + uint32_t count = std::max(1U, outputs[i].arraySize); + for(uint32_t j = 0; j < count; ++j) + { + vs += " OUT.element" + std::to_string(i); + if(outputs[i].arraySize != 0) + vs += "[" + std::to_string(j) + "]"; + vs += " = "; + vs += varTypeName[(uint32_t)outputs[i].type]; + vs += std::to_string(outputs[i].components); + vs += "("; + for(uint32_t k = 0; k < outputs[i].components; ++k) + { + if(k != 0) + vs += ", "; + vs += std::to_string(outputs[i].type == VarType::Float ? counterFloat++ : counterUInt++); + } + vs += ");\n"; + } + } + + vs += "\n return OUT;\n}\n"; + + return vs; + } + + std::string BuildPS(const std::vector &inputs) + { + std::string ps = BuildStruct(inputs); + + ps += R"EOSHADER( + +float4 main(v2f IN) : SV_Target0 +{ + float4 outF = float4(0.0f, 0.0f, 0.0f, 0.0f); + uint4 outU = uint4(0, 0, 0, 0); + +)EOSHADER"; + + const std::string varAccess[] = {" outF", " outU"}; + const std::string componentAccess[] = {".x", ".xy", ".xyz", ".xyzw"}; + + for(size_t i = 0; i < inputs.size(); ++i) + { + if(inputs[i].consumedByPS) + { + if(inputs[i].arraySize == 0) + { + ps += varAccess[(uint32_t)inputs[i].type]; + ps += componentAccess[inputs[i].components - 1]; + ps += " += IN.element" + std::to_string(i) + ";\n"; + } + else + { + // Access each element + for(uint32_t j = 0; j < inputs[i].arraySize; ++j) + { + ps += varAccess[(uint32_t)inputs[i].type]; + ps += componentAccess[inputs[i].components - 1]; + ps += " += IN.element" + std::to_string(i); + ps += "[" + std::to_string(j) + "];\n"; + } + } + } + } + + ps += "\n return outF + (float4)outU;\n}\n"; + return ps; + } + + struct TestCase + { + ID3D11VertexShaderPtr vs; + ID3D11PixelShaderPtr ps; + ID3D11InputLayoutPtr inputLayout; + }; + + TestCase BuildTestCase(const std::vector &elements) + { + ID3DBlobPtr vsblob = Compile(BuildVS(elements), "main", "vs_5_0"); + ID3DBlobPtr psblob = Compile(BuildPS(elements), "main", "ps_5_0"); + TestCase ret; + ret.vs = CreateVS(vsblob); + ret.ps = CreatePS(psblob); + + D3D11_INPUT_ELEMENT_DESC layoutdesc[] = { + {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0}, + }; + + CHECK_HR(dev->CreateInputLayout(layoutdesc, ARRAY_COUNT(layoutdesc), vsblob->GetBufferPointer(), + vsblob->GetBufferSize(), &ret.inputLayout)); + + return ret; + } + + int main() + { + // initialise, create window, create device, etc + if(!Init()) + return 3; + + ID3D11BufferPtr vb = MakeBuffer().Vertex().Data(DefaultTri); + + ID3D11Texture2DPtr fltTex = + MakeTexture(DXGI_FORMAT_R32G32B32A32_FLOAT, screenWidth, screenHeight).RTV(); + ID3D11RenderTargetViewPtr fltRT = MakeRTV(fltTex); + + std::vector tests; + + // No additional semantics + tests.push_back(BuildTestCase({})); + + // A single semantic of various types, interpolation modes, and components + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{true, VarType::Float, 1, 0, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 4, 0, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 4, 0, "TEXCOORD0", false}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 1, 0, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 4, 0, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 4, 0, "TEXCOORD0", false}})); + tests.push_back(BuildTestCase({{true, VarType::UInt, 4, 0, "TEXCOORD0", true}})); + + // A single semantic with various array sizes + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 1, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 2, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 5, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 1, 1, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 1, 2, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 1, 5, "TEXCOORD0", true}})); + + // Multiple semantics that pack together + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 0, "TEXCOORD0", true}, + {false, VarType::Float, 2, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::UInt, 2, 0, "TEXCOORD0", true}, + {false, VarType::UInt, 2, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{true, VarType::Float, 2, 0, "TEXCOORD0", true}, + {true, VarType::Float, 2, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 3, 0, "TEXCOORD0", true}, + {false, VarType::Float, 1, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::Float, 3, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::Float, 2, 0, "TEXCOORD1", true}, + {false, VarType::Float, 1, 0, "TEXCOORD2", true}})); + // These pack into v1.x, v2.xy, and v1.y + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::UInt, 2, 0, "TEXCOORD1", true}, + {false, VarType::Float, 1, 0, "TEXCOORD2", true}})); + + // Multiple semantics that don't pack together + tests.push_back(BuildTestCase({{false, VarType::Float, 3, 0, "TEXCOORD0", true}, + {false, VarType::Float, 2, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 0, "TEXCOORD0", true}, + {false, VarType::Float, 3, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 4, 0, "TEXCOORD0", true}, + {false, VarType::Float, 1, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::Float, 4, 0, "TEXCOORD1", true}})); + + // Multiple semantics that will pack together "out of order" thanks to FXC's rules + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 0, "TEXCOORD0", true}, + {false, VarType::Float, 3, 0, "TEXCOORD1", true}, + {false, VarType::Float, 2, 0, "TEXCOORD2", true}})); + + // Semantics that don't pack together due to being arrays + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 2, "TEXCOORD0", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 1, "TEXCOORD0", true}, + {false, VarType::Float, 2, 1, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 1, "TEXCOORD0", true}, + {false, VarType::Float, 2, 0, "TEXCOORD1", true}})); + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 0, "TEXCOORD0", true}, + {false, VarType::Float, 2, 1, "TEXCOORD1", true}})); + + // Tests focusing on different interpolation modes + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 0, "TEXCOORD0", true}, + {true, VarType::Float, 2, 0, "TEXCOORD1", true}})); + // The following test is currently broken: the semantics live in v1.x and v1.y, but during + // debugging our initial inputs shader places them in an array[2], resulting in v1.x and v2.x + tests.push_back(BuildTestCase({{false, VarType::UInt, 1, 0, "TEXCOORD0", true}, + {true, VarType::UInt, 1, 0, "TEXCOORD1", true}})); + // The following test is currently broken: the semantics live in v1.x and v2.x, but during + // debugging our initial inputs shader places them in an array[2], resulting in the correct + // register placement, but incorrect interpolation modes since uints are always nointerpolation + tests.push_back(BuildTestCase({{false, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::UInt, 1, 0, "TEXCOORD1", true}})); + // The following test is currently broken: the semantics live in v1.x and v1.y, despite having + // different types since the interpolation mode is the same. During debugging our initial inputs + // shader places them in an array[2], resulting in the wrong register placement + tests.push_back(BuildTestCase({{true, VarType::Float, 1, 0, "TEXCOORD0", true}, + {false, VarType::UInt, 1, 0, "TEXCOORD1", true}})); + + // Bespoke tests for broken scenarios discovered through bug reports: + + // The following test is currently broken: the semantics live in v1.xy, v2.x, and v3.xyz due + // to each being an array. During debugging, out initial input shader defines the last semantic + // without an array size, so FXC packs it into v2.yzw + tests.push_back(BuildTestCase({{false, VarType::Float, 2, 1, "TEXCOORD0", true}, + {false, VarType::Float, 1, 1, "TEXCOORD1", false}, + {false, VarType::Float, 3, 1, "TEXCOORD2", true}})); + + while(Running()) + { + ClearRenderTargetView(fltRT, {0.2f, 0.2f, 0.2f, 1.0f}); + ClearRenderTargetView(bbRTV, {0.2f, 0.2f, 0.2f, 1.0f}); + + IASetVertexBuffer(vb, sizeof(DefaultA2V), 0); + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + RSSetViewport({0.0f, 0.0f, (float)screenWidth, (float)screenHeight, 0.0f, 1.0f}); + + ctx->OMSetRenderTargets(1, &fltRT.GetInterfacePtr(), NULL); + + for(size_t i = 0; i < tests.size(); ++i) + { + setMarker("draw" + std::to_string(i)); + + ctx->IASetInputLayout(tests[i].inputLayout); + + ctx->VSSetShader(tests[i].vs, NULL, 0); + ctx->PSSetShader(tests[i].ps, NULL, 0); + + ctx->Draw(3, 0); + } + + Present(); + } + + return 0; + } +}; + +REGISTER_TEST(); diff --git a/util/test/demos/demos.vcxproj b/util/test/demos/demos.vcxproj index 1bb984e19..0fa1b7f04 100644 --- a/util/test/demos/demos.vcxproj +++ b/util/test/demos/demos.vcxproj @@ -154,6 +154,7 @@ + diff --git a/util/test/demos/demos.vcxproj.filters b/util/test/demos/demos.vcxproj.filters index 8b28774eb..88a0ad17f 100644 --- a/util/test/demos/demos.vcxproj.filters +++ b/util/test/demos/demos.vcxproj.filters @@ -487,6 +487,9 @@ D3D12\demos + + D3D11\demos + diff --git a/util/test/tests/D3D11/D3D11_Shader_Linkage_Zoo.py b/util/test/tests/D3D11/D3D11_Shader_Linkage_Zoo.py new file mode 100644 index 000000000..520816878 --- /dev/null +++ b/util/test/tests/D3D11/D3D11_Shader_Linkage_Zoo.py @@ -0,0 +1,49 @@ +import renderdoc as rd +from typing import List +import rdtest + + +class D3D11_Shader_Linkage_Zoo(rdtest.TestCase): + demos_test_name = 'D3D11_Shader_Linkage_Zoo' + + def check_capture(self): + failed = False + + test_marker: rd.DrawcallDescription = self.find_draw("draw") + while test_marker is not None: + drawcall = test_marker.next + event_name = test_marker.name + test_marker: rd.DrawcallDescription = self.find_draw("draw", drawcall.eventId) + + self.controller.SetFrameEvent(drawcall.eventId, False) + pipe: rd.PipeState = self.controller.GetPipelineState() + + # Debug the shader + trace: rd.ShaderDebugTrace = self.controller.DebugPixel(200, 150, rd.ReplayController.NoPreference, + rd.ReplayController.NoPreference) + if trace.debugger is None: + failed = True + rdtest.log.error("Test {} could not be debugged.".format(event_name)) + continue + + cycles, variables = self.process_trace(trace) + + output = self.find_output_source_var(trace, rd.ShaderBuiltin.ColorOutput, 0) + + debugged = self.evaluate_source_var(output, variables) + + try: + self.check_pixel_value(pipe.GetOutputTargets()[0].resourceId, 200, 150, debugged.value.fv[0:4]) + except rdtest.TestFailureException as ex: + failed = True + rdtest.log.error("Test {} did not match. {}".format(event_name, str(ex))) + continue + finally: + self.controller.FreeTrace(trace) + + rdtest.log.success("Test {} matched as expected".format(event_name)) + + if failed: + raise rdtest.TestFailureException("Some tests were not as expected") + + rdtest.log.success("All tests matched")