summaryrefslogtreecommitdiffstats
path: root/tools/platform/gui.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2021-03-24 13:57:55 -0700
committerGitHub <noreply@github.com>2021-03-24 13:57:55 -0700
commit98afb421f408aa8651afff3dba1b21fad71131fe (patch)
treea8610d12ac7d74a8772cb6ecdd810da8216baa67 /tools/platform/gui.cpp
parentd0f7b7f0ed1d0d1388ce944cd1ad906bbd9afb35 (diff)
Reimplement Vulkan shader objects. (#1764)
* Reimplement Vulkan shader objects. This change reimplements Vulkan shader objects in the `gfx` layer so that it is no longer layered on top of the `DescriptorSet` abstraction. Since this is the last implementation that uses `DescriptorSet`, the change also removes all `DescriptorSet` related API from public `gfx` interface. The Vulkan implementation now passes all test cases, but it still have two issues: 1. The PushConstant setting is not correct, this is because we don't seem to be able to get correct reflection data about the size of push constants for an entry-point. 2. The `shader-toy` example can't run on Vulkan, because it currently sets nullptr to `Texture` bindings, and this change doesn't properly handle setting resource to null in `ShaderObject`s yet. If we can use the `nullDescriptor` feature on vulkan, this implementation will be simple. However we still want to decide whether we want to use a Vulkan 1.2 feature for this. * Fix up
Diffstat (limited to 'tools/platform/gui.cpp')
-rw-r--r--tools/platform/gui.cpp103
1 files changed, 9 insertions, 94 deletions
diff --git a/tools/platform/gui.cpp b/tools/platform/gui.cpp
index 8e455cd12..e33a504bb 100644
--- a/tools/platform/gui.cpp
+++ b/tools/platform/gui.cpp
@@ -91,68 +91,16 @@ GUI::GUI(
} \
";
- SlangSession* slangSession = spCreateSession(nullptr);
- SlangCompileRequest* slangRequest = spCreateCompileRequest(slangSession);
-
- // TODO: These two lines need to change based on what the target graphics API
- // is, so we need a way for a `Renderer` to pass back its prefeerred code
- // format and profile name...
- //
- int targetIndex = spAddCodeGenTarget(slangRequest, SLANG_DXBC);
- spSetTargetProfile(slangRequest, targetIndex, spFindProfile(slangSession, "sm_4_0"));
-
- int translationUnitIndex = spAddTranslationUnit(slangRequest, SLANG_SOURCE_LANGUAGE_SLANG, nullptr);
- spAddTranslationUnitSourceString(slangRequest, translationUnitIndex, "gui.cpp.slang", shaderCode);
-
- char const* vertexEntryPointName = "vertexMain";
- char const* fragmentEntryPointName = "fragmentMain";
- int vertexIndex = spAddEntryPoint(slangRequest, translationUnitIndex, vertexEntryPointName, SLANG_STAGE_VERTEX);
- int fragmentIndex = spAddEntryPoint(slangRequest, translationUnitIndex, fragmentEntryPointName, SLANG_STAGE_FRAGMENT);
-
- const SlangResult compileRes = spCompile(slangRequest);
- if(auto diagnostics = spGetDiagnosticOutput(slangRequest))
- {
- printf("%s", diagnostics);
- }
- if(SLANG_FAILED(compileRes))
- {
- spDestroyCompileRequest(slangRequest);
- spDestroySession(slangSession);
- assert(!"unexpected");
- return;
- }
-
- ISlangBlob* vertexShaderBlob = nullptr;
- spGetEntryPointCodeBlob(slangRequest, vertexIndex, 0, &vertexShaderBlob);
-
- ISlangBlob* fragmentShaderBlob = nullptr;
- spGetEntryPointCodeBlob(slangRequest, fragmentIndex, 0, &fragmentShaderBlob);
-
- char const* vertexCode = (char const*) vertexShaderBlob->getBufferPointer();
- char const* vertexCodeEnd = vertexCode + vertexShaderBlob->getBufferSize();
-
- char const* fragmentCode = (char const*) fragmentShaderBlob->getBufferPointer();
- char const* fragmentCodeEnd = fragmentCode + fragmentShaderBlob->getBufferSize();
-
- spDestroyCompileRequest(slangRequest);
- spDestroySession(slangSession);
-
- gfx::IShaderProgram::KernelDesc kernelDescs[] =
- {
- { gfx::StageType::Vertex, vertexCode, vertexCodeEnd },
- { gfx::StageType::Fragment, fragmentCode, fragmentCodeEnd },
- };
+ auto slangSession = inDevice->getSlangSession();
+ // TODO: create slang program.
+ IShaderProgram* program = nullptr;
+#if 0
gfx::IShaderProgram::Desc programDesc = {};
programDesc.pipelineType = gfx::PipelineType::Graphics;
- programDesc.kernels = &kernelDescs[0];
- programDesc.kernelCount = 2;
-
- auto program = device->createProgram(programDesc);
-
- vertexShaderBlob->release();
- fragmentShaderBlob->release();
-
+ programDesc.slangProgram = slangProgram;
+ program = device->createProgram(programDesc);
+#endif
InputElementDesc inputElements[] = {
{"U", 0, Format::RG_Float32, offsetof(ImDrawVert, pos) },
{"U", 1, Format::RG_Float32, offsetof(ImDrawVert, uv) },
@@ -164,27 +112,6 @@ GUI::GUI(
//
- Slang::List<IDescriptorSetLayout::SlotRangeDesc> descriptorSetRanges;
- descriptorSetRanges.add(IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::UniformBuffer));
- descriptorSetRanges.add(IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::SampledImage));
- descriptorSetRanges.add(IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::Sampler));
-
- IDescriptorSetLayout::Desc descriptorSetLayoutDesc;
- descriptorSetLayoutDesc.slotRangeCount = descriptorSetRanges.getCount();
- descriptorSetLayoutDesc.slotRanges = descriptorSetRanges.getBuffer();
-
- descriptorSetLayout = device->createDescriptorSetLayout(descriptorSetLayoutDesc);
-
- Slang::List<IPipelineLayout::DescriptorSetDesc> pipelineDescriptorSets;
- pipelineDescriptorSets.add(IPipelineLayout::DescriptorSetDesc(descriptorSetLayout));
-
- IPipelineLayout::Desc pipelineLayoutDesc;
- pipelineLayoutDesc.descriptorSetCount = pipelineDescriptorSets.getCount();
- pipelineLayoutDesc.descriptorSets = pipelineDescriptorSets.getBuffer();
- pipelineLayoutDesc.renderTargetCount = 1;
-
- pipelineLayout = device->createPipelineLayout(pipelineLayoutDesc);
-
TargetBlendDesc targetBlendDesc;
targetBlendDesc.color.srcFactor = BlendFactor::SrcAlpha;
targetBlendDesc.color.dstFactor = BlendFactor::InvSrcAlpha;
@@ -194,7 +121,6 @@ GUI::GUI(
GraphicsPipelineStateDesc pipelineDesc;
pipelineDesc.framebufferLayout = framebufferLayout;
pipelineDesc.program = program;
- pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.inputLayout = inputLayout;
pipelineDesc.blend.targets = &targetBlendDesc;
pipelineDesc.blend.targetCount = 1;
@@ -379,19 +305,8 @@ void GUI::endFrame(IFramebuffer* framebuffer)
};
renderEncoder->setScissorRects(1, &rect);
- // TODO: This should be a dynamic/transient descriptor set...
- auto descriptorSet = device->createDescriptorSet(descriptorSetLayout, gfx::IDescriptorSet::Flag::Transient);
- descriptorSet->setConstantBuffer(0, 0, constantBuffer);
- descriptorSet->setResource(1, 0,
- (gfx::IResourceView*) command->TextureId);
- descriptorSet->setSampler(2, 0,
- samplerState);
-
- renderEncoder->setDescriptorSet(
- pipelineLayout,
- 0,
- descriptorSet);
-
+ // TODO: set parameter into root shader object.
+
renderEncoder->drawIndexed(command->ElemCount, indexOffset, vertexOffset);
}
indexOffset += command->ElemCount;