diff options
Diffstat (limited to 'tests/cpu-program')
| -rw-r--r-- | tests/cpu-program/gfx-smoke.slang | 96 | ||||
| -rw-r--r-- | tests/cpu-program/gfx-smoke.slang.expected | 5 |
2 files changed, 96 insertions, 5 deletions
diff --git a/tests/cpu-program/gfx-smoke.slang b/tests/cpu-program/gfx-smoke.slang index fda6d5454..2ad33052f 100644 --- a/tests/cpu-program/gfx-smoke.slang +++ b/tests/cpu-program/gfx-smoke.slang @@ -3,14 +3,102 @@ __target_intrinsic(cpp, "printf(\"%s\\n\", ($0).getBuffer())") void writeln(String text); import gfx; +import slang; public __extern_cpp int main() { gfx.DeviceDesc deviceDesc = {}; deviceDesc.deviceType = gfx.DeviceType.CPU; - Optional<gfx.IDevice> obj; - if (gfx.succeeded(gfx.gfxCreateDevice(&deviceDesc, obj)) && obj.hasValue) - writeln("succ"); - else + Optional<gfx.IDevice> device; + gfx.gfxCreateDevice(&deviceDesc, device); + if (device == none) + { writeln("fail"); + return -1; + } + + gfx.CommandQueueDesc queueDesc = {}; + queueDesc.type = gfx.QueueType.Graphics; + Optional<gfx.ICommandQueue> queue; + device.value.createCommandQueue(&queueDesc, queue); + + gfx.ShaderProgramDesc2 programDesc = {}; + NativeString s = + "[shader(\"compute\")]" + "[numthreads(4, 1, 1)]" + "void computeMain(" + " uint3 sv_dispatchThreadID: SV_DispatchThreadID," + " uniform RWStructuredBuffer<float> buffer" + " )" + "{" + " var input = buffer[sv_dispatchThreadID.x];" + " buffer[sv_dispatchThreadID.x] = sv_dispatchThreadID.x;" + "}"; + programDesc.sourceData = s; + programDesc.sourceType = gfx.ShaderModuleSourceType.SlangSource; + programDesc.sourceDataSize = s.length; + programDesc.entryPointCount = 1; + NativeString entryPointName = "computeMain"; + programDesc.entryPointNames = &entryPointName; + Optional<gfx.IShaderProgram> program; + Optional<slang.ISlangBlob> diagBlob; + device.value.createProgram2(&programDesc, program, diagBlob); + + Optional<gfx.IPipelineState> pipeline; + gfx.ComputePipelineStateDesc pipelineDesc = {}; + pipelineDesc.program = NativeRef<gfx.IShaderProgram>(program.value); + device.value.createComputePipelineState(&pipelineDesc, pipeline); + + Optional<gfx.ITransientResourceHeap> transientHeap; + gfx.TransientResourceHeapDesc transientHeapDesc = {}; + transientHeapDesc.constantBufferDescriptorCount = 64; + transientHeapDesc.constantBufferSize = 1024; + transientHeapDesc.srvDescriptorCount = 1024; + transientHeapDesc.uavDescriptorCount = 1024; + transientHeapDesc.samplerDescriptorCount = 256; + transientHeapDesc.accelerationStructureDescriptorCount = 32; + device.value.createTransientResourceHeap(&transientHeapDesc, transientHeap); + + Optional<gfx.IBufferResource> buffer; + gfx.BufferResourceDesc bufferDesc = {}; + bufferDesc.memoryType = gfx.MemoryType.DeviceLocal; + bufferDesc.allowedStates.add(gfx.ResourceState.UnorderedAccess); + bufferDesc.defaultState = gfx.ResourceState.UnorderedAccess; + bufferDesc.elementSize = 4; + bufferDesc.sizeInBytes = 256; + bufferDesc.type = gfx.ResourceType.Buffer; + device.value.createBufferResource(&bufferDesc, nullptr, buffer); + + Optional<gfx.IResourceView> bufferView; + gfx.ResourceViewDesc viewDesc = {}; + viewDesc.type = gfx.ResourceViewType.UnorderedAccess; + device.value.createBufferView(buffer.value, none, &viewDesc, bufferView); + + Optional<gfx.ICommandBuffer> commandBuffer; + transientHeap.value.createCommandBuffer(commandBuffer); + Optional<gfx.IComputeCommandEncoder> encoder; + commandBuffer.value.encodeComputeCommands(encoder); + Optional<gfx.IShaderObject> rootObject; + encoder.value.bindPipeline(pipeline.value, rootObject); + Optional<gfx.IShaderObject> entryPointObject; + rootObject.value.getEntryPoint(0, entryPointObject); + gfx.ShaderOffset offset = {}; + entryPointObject.value.setResource(&offset, bufferView.value); + encoder.value.dispatchCompute(1, 1, 1); + encoder.value.endEncoding(); + commandBuffer.value.close(); + + NativeRef<gfx.ICommandBuffer> commandBufferRef = NativeRef<gfx.ICommandBuffer>(commandBuffer.value); + queue.value.executeCommandBuffers(1, &commandBufferRef, none, 0); + queue.value.waitOnHost(); + + Optional<slang.ISlangBlob> blob; + device.value.readBufferResource(buffer.value, 0, 16, blob); + + for (int i = 0; i < 4; i++) + { + float val = ((float *)blob.value.getBufferPointer())[i]; + writeln(String(val)); + } + return 0; }
\ No newline at end of file diff --git a/tests/cpu-program/gfx-smoke.slang.expected b/tests/cpu-program/gfx-smoke.slang.expected index 981dcfc29..aa0dd7680 100644 --- a/tests/cpu-program/gfx-smoke.slang.expected +++ b/tests/cpu-program/gfx-smoke.slang.expected @@ -2,5 +2,8 @@ result code = 0 standard error = { } standard output = { -succ +0 +1 +2 +3 } |
