diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-08-03 08:39:28 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-03 08:39:28 -0700 |
| commit | 68d705f6c805c9b4d31b386e065762e6db13ad18 (patch) | |
| tree | 97ffc0f24358101222d1bc62ac0c50affc55af12 /tools/render-test/main.cpp | |
| parent | 5ea746a571ced32a8975eb3a238c562b3d487149 (diff) | |
Major overhaul of Renderer abstraction, to support a new example (#624)
The original goal here was to bring up a second example program: `model-viewer`.
While the existing `hello-world` example is enough to get somebody up to speed with the basics of the Slang API (as a drop-in replacement for `D3DCompile` or similar), it doesn't really show any of the big-picture stuff that Slang is meant to enable.
There wasn't any use of D3D12/Vulkan descriptor tables/sets, and there wasn't any use of interfaces, generics, or `ParameterBlock`s in the shader code.
The `model-viewer` example addresses these issues. Its shader code involves generics, interfaces, and multiple `ParameterBlock`s, and the host-side code demonstrates a few key things for working with Slang:
* There is an application-level abstraction for parameter blocks, that combines the graphics-API descriptor set object with Slang type information
* There is a shader cache layer used to look up an appropriate variant of a rendering effect by using parameter block types to "plug in" global type variables
* There is a clear separation between the phases of compilation: a first phase that does semantic checking and enables reflection-based allocation of graphics API objects, followed by one or more code generation passes for specialized kernels.
This example is certainly not perfect, and it will need to be revamped more going forward. In particular:
* The output picture is ugly as sin. We need a plan for how to get this to load better content, perhaps even popping up an error message to note that the required input data isn't present in the basic repository.
* The shader code is too simplistic. There isn't any real material variety, and the `IMaterial` abstraction is completely wrong.
* The use of parameter blocks is facile because there are no resource parameters right now. Fixing that will likely expose issues around interfacing with Slang's reflection API.
* The whole example exposes the issue that Slang's current APIs aren't really designed for the benefit of two-phase compilation (since our many client application has been stuck on one-phase compilation).
* Global type parameters are actually a Bad Idea that we only did for compatibility with existing codebases. We should not be showing them off in an example of the Right Way to use Slang, but the language support for type parameters on entry points is still not complete.
Of course, the majority of the changes here are *not* inside the example applications, and instead involve a major overhaul of the `Renderer` abstraction that is used for both tests and examples. The main thrust of the change is to make the abstraction layer be closer to the D3D12/Vulkan model than to a D3D11-style model. This is important for the `model-viewer` example, since it aspires to show how Slang can be incorporated into a renderer that targets a modern API. The most important bit is actually the use of descriptor sets and "pipeline layouts" a la Vulkan, since without these Slang's `ParameterBlock` abstraction won't make a lot of sense.
Implementation of the abstraction for the various APIs has very much been on an as-needed basis. The current implementation is just enough for the two examples to work, plus enough to get all the tests to pass in both debug and release builds on Windows.
A big missing feature in the API abstraction right now is memory lifetime management. The code had been trending toward something D3D11-like where a constant buffer could be mapped per-frame with the implementation doing behind-the-scenes allocation for targets like D3D12/Vulkan. I'd like to shift more toward a model of just exposing "transient" allocations that are only valid for one frame, because these are more representation of how an efficient renderer for next-generation APIs will work. That transition isn't actually complete, though, so there are problems with the existing examples where `hello-world` is actually scribbling into memory that the GPU might still be using, while `model-viewer` is doing full-on heavy-weight allocations on a per-frame basis with no real concern for the performance implications.
All together, there are a lot of things here that need more work, but this branch has been way too long-lived already, and so I'd like to get this checked in as long as all the tests pass.
Diffstat (limited to 'tools/render-test/main.cpp')
| -rw-r--r-- | tools/render-test/main.cpp | 117 |
1 files changed, 78 insertions, 39 deletions
diff --git a/tools/render-test/main.cpp b/tools/render-test/main.cpp index 935b9bc98..4734c2c8f 100644 --- a/tools/render-test/main.cpp +++ b/tools/render-test/main.cpp @@ -29,6 +29,8 @@ namespace renderer_test { +using Slang::Result; + int gWindowWidth = 1024; int gWindowHeight = 768; @@ -45,7 +47,7 @@ struct Vertex float uv[2]; }; -static const Vertex kVertexData[] = +static const Vertex kVertexData[] = { { { 0, 0, 0.5 }, {1, 0, 0} , {0, 0} }, { { 0, 1, 0.5 }, {0, 0, 1} , {1, 0} }, @@ -61,15 +63,15 @@ class RenderTestApp // At initialization time, we are going to load and compile our Slang shader // code, and then create the API objects we need for rendering. - Result initialize(Renderer* renderer, ShaderCompiler* shaderCompiler); + Result initialize(Renderer* renderer, ShaderCompiler* shaderCompiler); void runCompute(); void renderFrame(); void finalize(); - BindingState* getBindingState() const { return m_bindingState; } + BindingStateImpl* getBindingState() const { return m_bindingState; } Result writeBindingOutput(const char* fileName); - + Result writeScreen(const char* filename); protected: @@ -85,7 +87,8 @@ class RenderTestApp RefPtr<InputLayout> m_inputLayout; RefPtr<BufferResource> m_vertexBuffer; RefPtr<ShaderProgram> m_shaderProgram; - RefPtr<BindingState> m_bindingState; + RefPtr<PipelineState> m_pipelineState; + RefPtr<BindingStateImpl> m_bindingState; ShaderInputLayout m_shaderInputLayout; ///< The binding layout int m_numAddedConstantBuffers; ///< Constant buffers can be added to the binding directly. Will be added at the end. @@ -117,22 +120,26 @@ SlangResult RenderTestApp::initialize(Renderer* renderer, ShaderCompiler* shader } { - BindingState::Desc bindingStateDesc; - SLANG_RETURN_ON_FAIL(ShaderRendererUtil::createBindingStateDesc(m_shaderInputLayout, m_renderer, bindingStateDesc)); - - //! Hack -> if bindings are specified, just set up the constant buffer binding - // Should probably be more sophisticated than this - with 'dynamic' constant buffer/s binding always being specified - // in the test file - - if ((gOptions.shaderType == Options::ShaderProgramType::Graphics || gOptions.shaderType == Options::ShaderProgramType::GraphicsCompute) - && bindingStateDesc.findBindingIndex(Resource::BindFlag::ConstantBuffer, 0) < 0) + //! Hack -> if doing a graphics test, add an extra binding for our dynamic constant buffer + // + // TODO: Should probably be more sophisticated than this - with 'dynamic' constant buffer/s binding always being specified + // in the test file + RefPtr<BufferResource> addedConstantBuffer; + switch(gOptions.shaderType) { - bindingStateDesc.addResource(BindingType::Buffer, m_constantBuffer, BindingState::RegisterRange::makeSingle(0) ); + default: + break; + case Options::ShaderProgramType::Graphics: + case Options::ShaderProgramType::GraphicsCompute: + addedConstantBuffer = m_constantBuffer; m_numAddedConstantBuffers++; + break; } - m_bindingState = m_renderer->createBindingState(bindingStateDesc); + BindingStateImpl* bindingState = nullptr; + SLANG_RETURN_ON_FAIL(ShaderRendererUtil::createBindingState(m_shaderInputLayout, m_renderer, addedConstantBuffer, &bindingState)); + m_bindingState = bindingState; } // Do other initialization that doesn't depend on the source language. @@ -156,6 +163,38 @@ SlangResult RenderTestApp::initialize(Renderer* renderer, ShaderCompiler* shader if(!m_vertexBuffer) return SLANG_FAIL; + { + switch(gOptions.shaderType) + { + default: + assert(!"unexpected test shader type"); + return SLANG_FAIL; + + case Options::ShaderProgramType::Compute: + { + ComputePipelineStateDesc desc; + desc.pipelineLayout = m_bindingState->pipelineLayout; + desc.program = m_shaderProgram; + + m_pipelineState = renderer->createComputePipelineState(desc); + } + break; + + case Options::ShaderProgramType::Graphics: + case Options::ShaderProgramType::GraphicsCompute: + { + GraphicsPipelineStateDesc desc; + desc.pipelineLayout = m_bindingState->pipelineLayout; + desc.program = m_shaderProgram; + desc.inputLayout = m_inputLayout; + desc.renderTargetCount = m_bindingState->m_numRenderTargets; + + m_pipelineState = renderer->createGraphicsPipelineState(desc); + } + break; + } + } + return SLANG_OK; } @@ -182,6 +221,16 @@ Result RenderTestApp::initializeShaders(ShaderCompiler* shaderCompiler) fclose(sourceFile); sourceText[sourceSize] = 0; + switch( gOptions.shaderType ) + { + default: + m_shaderInputLayout.numRenderTargets = 1; + break; + + case Options::ShaderProgramType::Compute: + m_shaderInputLayout.numRenderTargets = 0; + break; + } m_shaderInputLayout.Parse(sourceText); ShaderCompileRequest::SourceInfo sourceInfo; @@ -220,31 +269,27 @@ void RenderTestApp::renderFrame() { const ProjectionStyle projectionStyle = RendererUtil::getProjectionStyle(m_renderer->getRendererType()); RendererUtil::getIdentityProjection(projectionStyle, (float*)mappedData); - + m_renderer->unmap(m_constantBuffer); } - // Input Assembler (IA) + auto pipelineType = PipelineType::Graphics; - m_renderer->setInputLayout(m_inputLayout); - m_renderer->setPrimitiveTopology(PrimitiveTopology::TriangleList); + m_renderer->setPipelineState(pipelineType, m_pipelineState); + m_renderer->setPrimitiveTopology(PrimitiveTopology::TriangleList); m_renderer->setVertexBuffer(0, m_vertexBuffer, sizeof(Vertex)); - // Vertex Shader (VS) - // Pixel Shader (PS) - - m_renderer->setShaderProgram(m_shaderProgram); - m_renderer->setBindingState(m_bindingState); - // + m_bindingState->apply(m_renderer, pipelineType); m_renderer->draw(3); } void RenderTestApp::runCompute() { - m_renderer->setShaderProgram(m_shaderProgram); - m_renderer->setBindingState(m_bindingState); + auto pipelineType = PipelineType::Compute; + m_renderer->setPipelineState(pipelineType, m_pipelineState); + m_bindingState->apply(m_renderer, pipelineType); m_renderer->dispatchCompute(1, 1, 1); } @@ -265,18 +310,12 @@ Result RenderTestApp::writeBindingOutput(const char* fileName) return SLANG_FAIL; } - const BindingState::Desc& bindingStateDesc = m_bindingState->getDesc(); - // Must be the same amount of entries - assert(bindingStateDesc.m_bindings.Count() == m_shaderInputLayout.entries.Count() + m_numAddedConstantBuffers); - - const int numBindings = int(m_shaderInputLayout.entries.Count()); - - for (int i = 0; i < numBindings; ++i) + for(auto binding : m_bindingState->outputBindings) { + auto i = binding.entryIndex; const auto& layoutBinding = m_shaderInputLayout.entries[i]; - const auto& binding = bindingStateDesc.m_bindings[i]; - if (layoutBinding.isOutput) + assert(layoutBinding.isOutput); { if (binding.resource && binding.resource->isBuffer()) { @@ -524,11 +563,11 @@ SlangResult innerMain(int argc, char** argv) else { Result res = app.writeScreen(gOptions.outputPath); - + if (SLANG_FAILED(res)) { fprintf(stderr, "ERROR: failed to write screen capture to file\n"); - return res; + return res; } } return SLANG_OK; |
