summaryrefslogtreecommitdiffstats
path: root/examples
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 /examples
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 'examples')
-rw-r--r--examples/gpu-printing/main.cpp56
-rw-r--r--examples/hello-world/main.cpp1
-rw-r--r--examples/shader-object/main.cpp1
-rw-r--r--examples/shader-toy/main.cpp1
4 files changed, 6 insertions, 53 deletions
diff --git a/examples/gpu-printing/main.cpp b/examples/gpu-printing/main.cpp
index 3fd1e460a..0315d4827 100644
--- a/examples/gpu-printing/main.cpp
+++ b/examples/gpu-printing/main.cpp
@@ -56,9 +56,7 @@ ComPtr<slang::ISession> gSlangSession;
ComPtr<slang::IModule> gSlangModule;
ComPtr<gfx::IShaderProgram> gProgram;
-ComPtr<gfx::IPipelineLayout> gPipelineLayout;
ComPtr<gfx::IPipelineState> gPipelineState;
-ComPtr<gfx::IDescriptorSet> gDescriptorSet;
Slang::Dictionary<int, std::string> gHashedStrings;
@@ -74,21 +72,9 @@ ComPtr<gfx::IShaderProgram> loadComputeProgram(slang::IModule* slangModule, char
gGPUPrinting.loadStrings(linkedProgram->getLayout());
- ComPtr<ISlangBlob> codeBlob;
- linkedProgram->getEntryPointCode(0, 0, codeBlob.writeRef());
-
- char const* code = (char const*) codeBlob->getBufferPointer();
- char const* codeEnd = code + codeBlob->getBufferSize();
-
- gfx::IShaderProgram::KernelDesc kernelDescs[] =
- {
- { gfx::StageType::Compute, code, codeEnd },
- };
-
gfx::IShaderProgram::Desc programDesc = {};
programDesc.pipelineType = gfx::PipelineType::Compute;
- programDesc.kernels = &kernelDescs[0];
- programDesc.kernelCount = 2;
+ programDesc.slangProgram = linkedProgram;
auto shaderProgram = gDevice->createProgram(programDesc);
@@ -107,40 +93,7 @@ Result execute()
gProgram = loadComputeProgram(gSlangModule, "computeMain");
if(!gProgram) return SLANG_FAIL;
- IDescriptorSetLayout::SlotRangeDesc slotRanges[] =
- {
- IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::StorageBuffer),
- };
- IDescriptorSetLayout::Desc descriptorSetLayoutDesc;
- descriptorSetLayoutDesc.slotRangeCount = 1;
- descriptorSetLayoutDesc.slotRanges = &slotRanges[0];
- auto descriptorSetLayout = gDevice->createDescriptorSetLayout(descriptorSetLayoutDesc);
- if(!descriptorSetLayout) return SLANG_FAIL;
-
- IPipelineLayout::DescriptorSetDesc descriptorSets[] =
- {
- IPipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
- };
- IPipelineLayout::Desc pipelineLayoutDesc;
- pipelineLayoutDesc.renderTargetCount = 1;
- pipelineLayoutDesc.descriptorSetCount = 1;
- pipelineLayoutDesc.descriptorSets = &descriptorSets[0];
- auto pipelineLayout = gDevice->createPipelineLayout(pipelineLayoutDesc);
- if(!pipelineLayout) return SLANG_FAIL;
-
- gPipelineLayout = pipelineLayout;
-
- // Once we have the descriptor set layout, we can allocate
- // and fill in a descriptor set to hold our parameters.
- //
- auto descriptorSet =
- gDevice->createDescriptorSet(descriptorSetLayout, IDescriptorSet::Flag::Transient);
- if(!descriptorSet) return SLANG_FAIL;
-
- gDescriptorSet = descriptorSet;
-
ComputePipelineStateDesc desc;
- desc.pipelineLayout = gPipelineLayout;
desc.program = gProgram;
auto pipelineState = gDevice->createComputePipelineState(desc);
if(!pipelineState) return SLANG_FAIL;
@@ -164,12 +117,9 @@ Result execute()
auto queue = gDevice->createCommandQueue(queueDesc);
auto commandBuffer = queue->createCommandBuffer();
auto encoder = commandBuffer->encodeComputeCommands();
- // TODO: need to copy a zero into the start of the print buffer!
-
- gDescriptorSet->setResource(0, 0, printBufferView);
- encoder->setDescriptorSet(gPipelineLayout, 0, gDescriptorSet);
-
+ auto rootShaderObject = gDevice->createRootShaderObject(gProgram);
encoder->setPipelineState(gPipelineState);
+ encoder->bindRootShaderObject(rootShaderObject);
encoder->dispatchCompute(1, 1, 1);
encoder->endEncoding();
commandBuffer->close();
diff --git a/examples/hello-world/main.cpp b/examples/hello-world/main.cpp
index b3bee9c07..023c16304 100644
--- a/examples/hello-world/main.cpp
+++ b/examples/hello-world/main.cpp
@@ -244,6 +244,7 @@ Slang::Result initialize()
// platforms/APIs.
//
IDevice::Desc deviceDesc = {};
+ deviceDesc.deviceType = DeviceType::Vulkan;
gfx::Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if(SLANG_FAILED(res)) return res;
diff --git a/examples/shader-object/main.cpp b/examples/shader-object/main.cpp
index aaafc010a..4945de27b 100644
--- a/examples/shader-object/main.cpp
+++ b/examples/shader-object/main.cpp
@@ -136,6 +136,7 @@ int main()
// interacting with the graphics API.
Slang::ComPtr<gfx::IDevice> device;
IDevice::Desc deviceDesc = {};
+ deviceDesc.deviceType = DeviceType::Vulkan;
SLANG_RETURN_ON_FAIL(gfxCreateDevice(&deviceDesc, device.writeRef()));
// Now we can load the shader code.
diff --git a/examples/shader-toy/main.cpp b/examples/shader-toy/main.cpp
index c4424b294..8e377b42f 100644
--- a/examples/shader-toy/main.cpp
+++ b/examples/shader-toy/main.cpp
@@ -315,6 +315,7 @@ Result initialize()
gWindow->events.sizeChanged = Slang::Action<>(this, &ShaderToyApp::windowSizeChanged);
IDevice::Desc deviceDesc;
+ deviceDesc.deviceType = DeviceType::Vulkan;
Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if(SLANG_FAILED(res)) return res;