diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /tools/gfx/cpu | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'tools/gfx/cpu')
| -rw-r--r-- | tools/gfx/cpu/cpu-base.h | 45 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-buffer.cpp | 2 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-buffer.h | 5 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-device.cpp | 493 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-device.h | 22 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-helper-functions.cpp | 1 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-helper-functions.h | 1 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-pipeline-state.cpp | 22 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-query.cpp | 4 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-query.h | 4 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-resource-views.cpp | 36 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-resource-views.h | 28 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-shader-object-layout.cpp | 64 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-shader-object-layout.h | 20 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-shader-object.cpp | 72 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-shader-object.h | 28 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-shader-program.h | 5 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-texture.cpp | 48 | ||||
| -rw-r--r-- | tools/gfx/cpu/cpu-texture.h | 35 |
19 files changed, 500 insertions, 435 deletions
diff --git a/tools/gfx/cpu/cpu-base.h b/tools/gfx/cpu/cpu-base.h index 6f30662ec..5c9cca5d9 100644 --- a/tools/gfx/cpu/cpu-base.h +++ b/tools/gfx/cpu/cpu-base.h @@ -2,15 +2,14 @@ // Shared header file for CPU implementation #pragma once -#include "slang.h" -#include "slang-com-ptr.h" -#include "slang-com-helper.h" -#include "core/slang-basic.h" -#include "core/slang-blob.h" - #include "../immediate-renderer-base.h" -#include "../slang-context.h" #include "../mutable-shader-object.h" +#include "../slang-context.h" +#include "core/slang-basic.h" +#include "core/slang-blob.h" +#include "slang-com-helper.h" +#include "slang-com-ptr.h" +#include "slang.h" #define SLANG_PRELUDE_NAMESPACE slang_prelude #include "prelude/slang-cpp-types.h" @@ -20,21 +19,21 @@ using namespace Slang; namespace cpu { - class BufferResourceImpl; - class TextureResourceImpl; - class ResourceViewImpl; - class BufferResourceViewImpl; - class TextureResourceViewImpl; - class ShaderObjectLayoutImpl; - class EntryPointLayoutImpl; - class RootShaderObjectLayoutImpl; - class ShaderObjectImpl; - class MutableShaderObjectImpl; - class EntryPointShaderObjectImpl; - class RootShaderObjectImpl; - class ShaderProgramImpl; - class PipelineStateImpl; - class QueryPoolImpl; - class DeviceImpl; +class BufferResourceImpl; +class TextureResourceImpl; +class ResourceViewImpl; +class BufferResourceViewImpl; +class TextureResourceViewImpl; +class ShaderObjectLayoutImpl; +class EntryPointLayoutImpl; +class RootShaderObjectLayoutImpl; +class ShaderObjectImpl; +class MutableShaderObjectImpl; +class EntryPointShaderObjectImpl; +class RootShaderObjectImpl; +class ShaderProgramImpl; +class PipelineStateImpl; +class QueryPoolImpl; +class DeviceImpl; } // namespace cpu } // namespace gfx diff --git a/tools/gfx/cpu/cpu-buffer.cpp b/tools/gfx/cpu/cpu-buffer.cpp index 2c9c3077d..a1184fd95 100644 --- a/tools/gfx/cpu/cpu-buffer.cpp +++ b/tools/gfx/cpu/cpu-buffer.cpp @@ -36,7 +36,7 @@ SLANG_NO_THROW DeviceAddress SLANG_MCALL BufferResourceImpl::getDeviceAddress() } SLANG_NO_THROW Result SLANG_MCALL - BufferResourceImpl::map(MemoryRange* rangeToRead, void** outPointer) +BufferResourceImpl::map(MemoryRange* rangeToRead, void** outPointer) { SLANG_UNUSED(rangeToRead); if (outPointer) diff --git a/tools/gfx/cpu/cpu-buffer.h b/tools/gfx/cpu/cpu-buffer.h index 6273b18b9..9b1866c74 100644 --- a/tools/gfx/cpu/cpu-buffer.h +++ b/tools/gfx/cpu/cpu-buffer.h @@ -14,7 +14,8 @@ class BufferResourceImpl : public BufferResource public: BufferResourceImpl(const Desc& _desc) : BufferResource(_desc) - {} + { + } ~BufferResourceImpl(); @@ -27,7 +28,7 @@ public: virtual SLANG_NO_THROW DeviceAddress SLANG_MCALL getDeviceAddress() override; virtual SLANG_NO_THROW Result SLANG_MCALL - map(MemoryRange* rangeToRead, void** outPointer) override; + map(MemoryRange* rangeToRead, void** outPointer) override; virtual SLANG_NO_THROW Result SLANG_MCALL unmap(MemoryRange* writtenRange) override; }; diff --git a/tools/gfx/cpu/cpu-device.cpp b/tools/gfx/cpu/cpu-device.cpp index bd747d998..6f7ff0f16 100644 --- a/tools/gfx/cpu/cpu-device.cpp +++ b/tools/gfx/cpu/cpu-device.cpp @@ -1,8 +1,6 @@ // cpu-device.cpp #include "cpu-device.h" -#include <chrono> - #include "cpu-buffer.h" #include "cpu-pipeline-state.h" #include "cpu-query.h" @@ -11,293 +9,300 @@ #include "cpu-shader-program.h" #include "cpu-texture.h" +#include <chrono> + namespace gfx { using namespace Slang; namespace cpu { - DeviceImpl::~DeviceImpl() +DeviceImpl::~DeviceImpl() +{ + m_currentPipeline = nullptr; + m_currentRootObject = nullptr; +} + +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::initialize(const Desc& desc) +{ + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + desc.extendedDescCount, + desc.extendedDescs, + SLANG_SHADER_HOST_CALLABLE, + "sm_5_1", + makeArray(slang::PreprocessorMacroDesc{"__CPU__", "1"}).getView())); + + SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); + + // Initialize DeviceInfo { - m_currentPipeline = nullptr; - m_currentRootObject = nullptr; + m_info.deviceType = DeviceType::CPU; + m_info.bindingStyle = BindingStyle::CUDA; + m_info.projectionStyle = ProjectionStyle::DirectX; + m_info.apiName = "CPU"; + static const float kIdentity[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + ::memcpy(m_info.identityProjectionMatrix, kIdentity, sizeof(kIdentity)); + m_info.adapterName = "CPU"; + m_info.timestampFrequency = 1000000000; } - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::initialize(const Desc& desc) + // Can support pointers (or something akin to that) { - SLANG_RETURN_ON_FAIL(slangContext.initialize( - desc.slang, - desc.extendedDescCount, - desc.extendedDescs, - SLANG_SHADER_HOST_CALLABLE, - "sm_5_1", - makeArray(slang::PreprocessorMacroDesc{ "__CPU__", "1" }).getView())); - - SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); - - // Initialize DeviceInfo - { - m_info.deviceType = DeviceType::CPU; - m_info.bindingStyle = BindingStyle::CUDA; - m_info.projectionStyle = ProjectionStyle::DirectX; - m_info.apiName = "CPU"; - static const float kIdentity[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; - ::memcpy(m_info.identityProjectionMatrix, kIdentity, sizeof(kIdentity)); - m_info.adapterName = "CPU"; - m_info.timestampFrequency = 1000000000; - } - - // Can support pointers (or something akin to that) - { - m_features.add("has-ptr"); - } - - return SLANG_OK; + m_features.add("has-ptr"); } - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureResource( - const ITextureResource::Desc& desc, - const ITextureResource::SubresourceData* initData, - ITextureResource** outResource) - { - TextureResource::Desc srcDesc = fixupTextureDesc(desc); + return SLANG_OK; +} - RefPtr<TextureResourceImpl> texture = new TextureResourceImpl(srcDesc); +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureResource( + const ITextureResource::Desc& desc, + const ITextureResource::SubresourceData* initData, + ITextureResource** outResource) +{ + TextureResource::Desc srcDesc = fixupTextureDesc(desc); - SLANG_RETURN_ON_FAIL(texture->init(initData)); + RefPtr<TextureResourceImpl> texture = new TextureResourceImpl(srcDesc); - returnComPtr(outResource, texture); - return SLANG_OK; - } + SLANG_RETURN_ON_FAIL(texture->init(initData)); - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferResource( - const IBufferResource::Desc& descIn, - const void* initData, - IBufferResource** outResource) - { - auto desc = fixupBufferDesc(descIn); - RefPtr<BufferResourceImpl> resource = new BufferResourceImpl(desc); - SLANG_RETURN_ON_FAIL(resource->init()); - if (initData) - { - SLANG_RETURN_ON_FAIL(resource->setData(0, desc.sizeInBytes, initData)); - } - returnComPtr(outResource, resource); - return SLANG_OK; - } + returnComPtr(outResource, texture); + return SLANG_OK; +} - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureView( - ITextureResource* inTexture, IResourceView::Desc const& desc, IResourceView** outView) +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferResource( + const IBufferResource::Desc& descIn, + const void* initData, + IBufferResource** outResource) +{ + auto desc = fixupBufferDesc(descIn); + RefPtr<BufferResourceImpl> resource = new BufferResourceImpl(desc); + SLANG_RETURN_ON_FAIL(resource->init()); + if (initData) { - auto texture = static_cast<TextureResourceImpl*>(inTexture); - RefPtr<TextureResourceViewImpl> view = new TextureResourceViewImpl(desc, texture); - returnComPtr(outView, view); - return SLANG_OK; + SLANG_RETURN_ON_FAIL(resource->setData(0, desc.sizeInBytes, initData)); } + returnComPtr(outResource, resource); + return SLANG_OK; +} - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferView( - IBufferResource* inBuffer, - IBufferResource* counterBuffer, - IResourceView::Desc const& desc, - IResourceView** outView) - { - auto buffer = static_cast<BufferResourceImpl*>(inBuffer); - RefPtr<BufferResourceViewImpl> view = new BufferResourceViewImpl(desc, buffer); - returnComPtr(outView, view); - return SLANG_OK; - } +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureView( + ITextureResource* inTexture, + IResourceView::Desc const& desc, + IResourceView** outView) +{ + auto texture = static_cast<TextureResourceImpl*>(inTexture); + RefPtr<TextureResourceViewImpl> view = new TextureResourceViewImpl(desc, texture); + returnComPtr(outView, view); + return SLANG_OK; +} - Result DeviceImpl::createShaderObjectLayout( - slang::ISession* session, - slang::TypeLayoutReflection* typeLayout, - ShaderObjectLayoutBase** outLayout) - { - RefPtr<ShaderObjectLayoutImpl> cpuLayout = new ShaderObjectLayoutImpl(this, session, typeLayout); - returnRefPtrMove(outLayout, cpuLayout); +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferView( + IBufferResource* inBuffer, + IBufferResource* counterBuffer, + IResourceView::Desc const& desc, + IResourceView** outView) +{ + auto buffer = static_cast<BufferResourceImpl*>(inBuffer); + RefPtr<BufferResourceViewImpl> view = new BufferResourceViewImpl(desc, buffer); + returnComPtr(outView, view); + return SLANG_OK; +} - return SLANG_OK; - } +Result DeviceImpl::createShaderObjectLayout( + slang::ISession* session, + slang::TypeLayoutReflection* typeLayout, + ShaderObjectLayoutBase** outLayout) +{ + RefPtr<ShaderObjectLayoutImpl> cpuLayout = + new ShaderObjectLayoutImpl(this, session, typeLayout); + returnRefPtrMove(outLayout, cpuLayout); - Result DeviceImpl::createShaderObject( - ShaderObjectLayoutBase* layout, - IShaderObject** outObject) - { - auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout); + return SLANG_OK; +} - RefPtr<ShaderObjectImpl> result = new ShaderObjectImpl(); - SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout)); - returnComPtr(outObject, result); +Result DeviceImpl::createShaderObject(ShaderObjectLayoutBase* layout, IShaderObject** outObject) +{ + auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout); - return SLANG_OK; - } + RefPtr<ShaderObjectImpl> result = new ShaderObjectImpl(); + SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout)); + returnComPtr(outObject, result); - Result DeviceImpl::createMutableShaderObject( - ShaderObjectLayoutBase* layout, - IShaderObject** outObject) - { - auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout); + return SLANG_OK; +} - RefPtr<MutableShaderObjectImpl> result = new MutableShaderObjectImpl(); - SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout)); - returnComPtr(outObject, result); +Result DeviceImpl::createMutableShaderObject( + ShaderObjectLayoutBase* layout, + IShaderObject** outObject) +{ + auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout); - return SLANG_OK; - } + RefPtr<MutableShaderObjectImpl> result = new MutableShaderObjectImpl(); + SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout)); + returnComPtr(outObject, result); - Result DeviceImpl::createRootShaderObject(IShaderProgram* program, ShaderObjectBase** outObject) - { - auto cpuProgram = static_cast<ShaderProgramImpl*>(program); - auto cpuProgramLayout = cpuProgram->layout; + return SLANG_OK; +} - RefPtr<RootShaderObjectImpl> result = new RootShaderObjectImpl(); - SLANG_RETURN_ON_FAIL(result->init(this, cpuProgramLayout)); - returnRefPtrMove(outObject, result); - return SLANG_OK; - } +Result DeviceImpl::createRootShaderObject(IShaderProgram* program, ShaderObjectBase** outObject) +{ + auto cpuProgram = static_cast<ShaderProgramImpl*>(program); + auto cpuProgramLayout = cpuProgram->layout; - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createProgram( - const IShaderProgram::Desc& desc, - IShaderProgram** outProgram, - ISlangBlob** outDiagnosticBlob) - { - RefPtr<ShaderProgramImpl> cpuProgram = new ShaderProgramImpl(); - cpuProgram->init(desc); - auto slangGlobalScope = cpuProgram->linkedProgram; - if (slangGlobalScope) - { - auto slangProgramLayout = slangGlobalScope->getLayout(); - if (!slangProgramLayout) - return SLANG_FAIL; - - RefPtr<RootShaderObjectLayoutImpl> cpuProgramLayout = new RootShaderObjectLayoutImpl(this, slangGlobalScope->getSession(), slangProgramLayout); - cpuProgramLayout->m_programLayout = slangProgramLayout; - - cpuProgram->layout = cpuProgramLayout; - } - - returnComPtr(outProgram, cpuProgram); - return SLANG_OK; - } + RefPtr<RootShaderObjectImpl> result = new RootShaderObjectImpl(); + SLANG_RETURN_ON_FAIL(result->init(this, cpuProgramLayout)); + returnRefPtrMove(outObject, result); + return SLANG_OK; +} - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createComputePipelineState( - const ComputePipelineStateDesc& desc, IPipelineState** outState) +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createProgram( + const IShaderProgram::Desc& desc, + IShaderProgram** outProgram, + ISlangBlob** outDiagnosticBlob) +{ + RefPtr<ShaderProgramImpl> cpuProgram = new ShaderProgramImpl(); + cpuProgram->init(desc); + auto slangGlobalScope = cpuProgram->linkedProgram; + if (slangGlobalScope) { - RefPtr<PipelineStateImpl> state = new PipelineStateImpl(); - state->init(desc); - returnComPtr(outState, state); - return Result(); - } + auto slangProgramLayout = slangGlobalScope->getLayout(); + if (!slangProgramLayout) + return SLANG_FAIL; - SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createQueryPool( - const IQueryPool::Desc& desc, IQueryPool** outPool) - { - RefPtr<QueryPoolImpl> pool = new QueryPoolImpl(); - pool->init(desc); - returnComPtr(outPool, pool); - return SLANG_OK; - } + RefPtr<RootShaderObjectLayoutImpl> cpuProgramLayout = new RootShaderObjectLayoutImpl( + this, + slangGlobalScope->getSession(), + slangProgramLayout); + cpuProgramLayout->m_programLayout = slangProgramLayout; - void DeviceImpl::writeTimestamp(IQueryPool* pool, GfxIndex index) - { - static_cast<QueryPoolImpl*>(pool)->m_queries[index] = - std::chrono::high_resolution_clock::now().time_since_epoch().count(); + cpuProgram->layout = cpuProgramLayout; } - SLANG_NO_THROW const DeviceInfo& SLANG_MCALL DeviceImpl::getDeviceInfo() const - { - return m_info; - } + returnComPtr(outProgram, cpuProgram); + return SLANG_OK; +} - SLANG_NO_THROW Result SLANG_MCALL - DeviceImpl::createSamplerState(ISamplerState::Desc const& desc, ISamplerState** outSampler) - { - SLANG_UNUSED(desc); - *outSampler = nullptr; - return SLANG_OK; - } +SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createComputePipelineState( + const ComputePipelineStateDesc& desc, + IPipelineState** outState) +{ + RefPtr<PipelineStateImpl> state = new PipelineStateImpl(); + state->init(desc); + returnComPtr(outState, state); + return Result(); +} - void* DeviceImpl::map(IBufferResource* buffer, MapFlavor flavor) - { - SLANG_UNUSED(flavor); - auto bufferImpl = static_cast<BufferResourceImpl*>(buffer); - return bufferImpl->m_data; - } - void DeviceImpl::unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) - { - SLANG_UNUSED(buffer); - SLANG_UNUSED(offsetWritten); - SLANG_UNUSED(sizeWritten); - } +SLANG_NO_THROW Result SLANG_MCALL +DeviceImpl::createQueryPool(const IQueryPool::Desc& desc, IQueryPool** outPool) +{ + RefPtr<QueryPoolImpl> pool = new QueryPoolImpl(); + pool->init(desc); + returnComPtr(outPool, pool); + return SLANG_OK; +} - void DeviceImpl::setPipelineState(IPipelineState* state) - { - m_currentPipeline = static_cast<PipelineStateImpl*>(state); - } +void DeviceImpl::writeTimestamp(IQueryPool* pool, GfxIndex index) +{ + static_cast<QueryPoolImpl*>(pool)->m_queries[index] = + std::chrono::high_resolution_clock::now().time_since_epoch().count(); +} - void DeviceImpl::bindRootShaderObject(IShaderObject* object) - { - m_currentRootObject = static_cast<RootShaderObjectImpl*>(object); - } +SLANG_NO_THROW const DeviceInfo& SLANG_MCALL DeviceImpl::getDeviceInfo() const +{ + return m_info; +} - void DeviceImpl::dispatchCompute(int x, int y, int z) - { - int entryPointIndex = 0; - int targetIndex = 0; - - // Specialize the compute kernel based on the shader object bindings. - RefPtr<PipelineStateBase> newPipeline; - maybeSpecializePipeline(m_currentPipeline, m_currentRootObject, newPipeline); - m_currentPipeline = static_cast<PipelineStateImpl*>(newPipeline.Ptr()); - - auto program = m_currentPipeline->getProgram(); - auto entryPointLayout = - m_currentRootObject->getLayout()->getEntryPoint(entryPointIndex); - auto entryPointName = entryPointLayout->getEntryPointName(); - - auto entryPointObject = m_currentRootObject->getEntryPoint(entryPointIndex); - - ComPtr<ISlangSharedLibrary> sharedLibrary; - ComPtr<ISlangBlob> diagnostics; - auto compileResult = program->slangGlobalScope->getEntryPointHostCallable( - entryPointIndex, targetIndex, sharedLibrary.writeRef(), diagnostics.writeRef()); - if (diagnostics) - { - getDebugCallback()->handleMessage( - compileResult == SLANG_OK ? DebugMessageType::Warning : DebugMessageType::Error, - DebugMessageSource::Slang, - (char*)diagnostics->getBufferPointer()); - } - if (SLANG_FAILED(compileResult)) return; - - auto func = (slang_prelude::ComputeFunc)sharedLibrary->findSymbolAddressByName(entryPointName); - - slang_prelude::ComputeVaryingInput varyingInput; - varyingInput.startGroupID.x = 0; - varyingInput.startGroupID.y = 0; - varyingInput.startGroupID.z = 0; - varyingInput.endGroupID.x = x; - varyingInput.endGroupID.y = y; - varyingInput.endGroupID.z = z; - - auto globalParamsData = m_currentRootObject->getDataBuffer(); - auto entryPointParamsData = entryPointObject->getDataBuffer(); - func(&varyingInput, entryPointParamsData, globalParamsData); - } +SLANG_NO_THROW Result SLANG_MCALL +DeviceImpl::createSamplerState(ISamplerState::Desc const& desc, ISamplerState** outSampler) +{ + SLANG_UNUSED(desc); + *outSampler = nullptr; + return SLANG_OK; +} - void DeviceImpl::copyBuffer( - IBufferResource* dst, - size_t dstOffset, - IBufferResource* src, - size_t srcOffset, - size_t size) +void* DeviceImpl::map(IBufferResource* buffer, MapFlavor flavor) +{ + SLANG_UNUSED(flavor); + auto bufferImpl = static_cast<BufferResourceImpl*>(buffer); + return bufferImpl->m_data; +} +void DeviceImpl::unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) +{ + SLANG_UNUSED(buffer); + SLANG_UNUSED(offsetWritten); + SLANG_UNUSED(sizeWritten); +} + +void DeviceImpl::setPipelineState(IPipelineState* state) +{ + m_currentPipeline = static_cast<PipelineStateImpl*>(state); +} + +void DeviceImpl::bindRootShaderObject(IShaderObject* object) +{ + m_currentRootObject = static_cast<RootShaderObjectImpl*>(object); +} + +void DeviceImpl::dispatchCompute(int x, int y, int z) +{ + int entryPointIndex = 0; + int targetIndex = 0; + + // Specialize the compute kernel based on the shader object bindings. + RefPtr<PipelineStateBase> newPipeline; + maybeSpecializePipeline(m_currentPipeline, m_currentRootObject, newPipeline); + m_currentPipeline = static_cast<PipelineStateImpl*>(newPipeline.Ptr()); + + auto program = m_currentPipeline->getProgram(); + auto entryPointLayout = m_currentRootObject->getLayout()->getEntryPoint(entryPointIndex); + auto entryPointName = entryPointLayout->getEntryPointName(); + + auto entryPointObject = m_currentRootObject->getEntryPoint(entryPointIndex); + + ComPtr<ISlangSharedLibrary> sharedLibrary; + ComPtr<ISlangBlob> diagnostics; + auto compileResult = program->slangGlobalScope->getEntryPointHostCallable( + entryPointIndex, + targetIndex, + sharedLibrary.writeRef(), + diagnostics.writeRef()); + if (diagnostics) { - auto dstImpl = static_cast<BufferResourceImpl*>(dst); - auto srcImpl = static_cast<BufferResourceImpl*>(src); - memcpy( - (uint8_t*)dstImpl->m_data + dstOffset, - (uint8_t*)srcImpl->m_data + srcOffset, - size); + getDebugCallback()->handleMessage( + compileResult == SLANG_OK ? DebugMessageType::Warning : DebugMessageType::Error, + DebugMessageSource::Slang, + (char*)diagnostics->getBufferPointer()); } + if (SLANG_FAILED(compileResult)) + return; + + auto func = (slang_prelude::ComputeFunc)sharedLibrary->findSymbolAddressByName(entryPointName); + + slang_prelude::ComputeVaryingInput varyingInput; + varyingInput.startGroupID.x = 0; + varyingInput.startGroupID.y = 0; + varyingInput.startGroupID.z = 0; + varyingInput.endGroupID.x = x; + varyingInput.endGroupID.y = y; + varyingInput.endGroupID.z = z; + + auto globalParamsData = m_currentRootObject->getDataBuffer(); + auto entryPointParamsData = entryPointObject->getDataBuffer(); + func(&varyingInput, entryPointParamsData, globalParamsData); +} + +void DeviceImpl::copyBuffer( + IBufferResource* dst, + size_t dstOffset, + IBufferResource* src, + size_t srcOffset, + size_t size) +{ + auto dstImpl = static_cast<BufferResourceImpl*>(dst); + auto srcImpl = static_cast<BufferResourceImpl*>(src); + memcpy((uint8_t*)dstImpl->m_data + dstOffset, (uint8_t*)srcImpl->m_data + srcOffset, size); +} } // namespace cpu diff --git a/tools/gfx/cpu/cpu-device.h b/tools/gfx/cpu/cpu-device.h index c7b80e26d..1bfafb1a1 100644 --- a/tools/gfx/cpu/cpu-device.h +++ b/tools/gfx/cpu/cpu-device.h @@ -1,7 +1,6 @@ // cpu-device.h #pragma once #include "cpu-base.h" - #include "cpu-pipeline-state.h" #include "cpu-shader-object.h" @@ -30,7 +29,9 @@ public: IBufferResource** outResource) override; virtual SLANG_NO_THROW Result SLANG_MCALL createTextureView( - ITextureResource* inTexture, IResourceView::Desc const& desc, IResourceView** outView) override; + ITextureResource* inTexture, + IResourceView::Desc const& desc, + IResourceView** outView) override; virtual SLANG_NO_THROW Result SLANG_MCALL createBufferView( IBufferResource* inBuffer, @@ -43,15 +44,15 @@ public: slang::TypeLayoutReflection* typeLayout, ShaderObjectLayoutBase** outLayout) override; - virtual Result createShaderObject( - ShaderObjectLayoutBase* layout, - IShaderObject** outObject) override; + virtual Result createShaderObject(ShaderObjectLayoutBase* layout, IShaderObject** outObject) + override; virtual Result createMutableShaderObject( ShaderObjectLayoutBase* layout, IShaderObject** outObject) override; - virtual Result createRootShaderObject(IShaderProgram* program, ShaderObjectBase** outObject) override; + virtual Result createRootShaderObject(IShaderProgram* program, ShaderObjectBase** outObject) + override; virtual SLANG_NO_THROW Result SLANG_MCALL createProgram( const IShaderProgram::Desc& desc, @@ -59,17 +60,18 @@ public: ISlangBlob** outDiagnosticBlob) override; virtual SLANG_NO_THROW Result SLANG_MCALL createComputePipelineState( - const ComputePipelineStateDesc& desc, IPipelineState** outState) override; + const ComputePipelineStateDesc& desc, + IPipelineState** outState) override; - virtual SLANG_NO_THROW Result SLANG_MCALL createQueryPool( - const IQueryPool::Desc& desc, IQueryPool** outPool) override; + virtual SLANG_NO_THROW Result SLANG_MCALL + createQueryPool(const IQueryPool::Desc& desc, IQueryPool** outPool) override; virtual void writeTimestamp(IQueryPool* pool, GfxIndex index) override; virtual SLANG_NO_THROW const DeviceInfo& SLANG_MCALL getDeviceInfo() const override; virtual SLANG_NO_THROW Result SLANG_MCALL - createSamplerState(ISamplerState::Desc const& desc, ISamplerState** outSampler) override; + createSamplerState(ISamplerState::Desc const& desc, ISamplerState** outSampler) override; virtual void submitGpuWork() override {} virtual void waitForGpu() override {} diff --git a/tools/gfx/cpu/cpu-helper-functions.cpp b/tools/gfx/cpu/cpu-helper-functions.cpp index f21be8d66..ea9308b35 100644 --- a/tools/gfx/cpu/cpu-helper-functions.cpp +++ b/tools/gfx/cpu/cpu-helper-functions.cpp @@ -8,5 +8,4 @@ namespace gfx using namespace Slang; - } // namespace gfx diff --git a/tools/gfx/cpu/cpu-helper-functions.h b/tools/gfx/cpu/cpu-helper-functions.h index ba8d4c88e..2c6de19d8 100644 --- a/tools/gfx/cpu/cpu-helper-functions.h +++ b/tools/gfx/cpu/cpu-helper-functions.h @@ -7,5 +7,4 @@ namespace gfx using namespace Slang; - } // namespace gfx diff --git a/tools/gfx/cpu/cpu-pipeline-state.cpp b/tools/gfx/cpu/cpu-pipeline-state.cpp index 7d2b6a636..d0de98532 100644 --- a/tools/gfx/cpu/cpu-pipeline-state.cpp +++ b/tools/gfx/cpu/cpu-pipeline-state.cpp @@ -10,18 +10,18 @@ using namespace Slang; namespace cpu { - ShaderProgramImpl* PipelineStateImpl::getProgram() - { - return static_cast<ShaderProgramImpl*>(m_program.Ptr()); - } +ShaderProgramImpl* PipelineStateImpl::getProgram() +{ + return static_cast<ShaderProgramImpl*>(m_program.Ptr()); +} - void PipelineStateImpl::init(const ComputePipelineStateDesc& inDesc) - { - PipelineStateDesc pipelineDesc; - pipelineDesc.type = PipelineType::Compute; - pipelineDesc.compute = inDesc; - initializeBase(pipelineDesc); - } +void PipelineStateImpl::init(const ComputePipelineStateDesc& inDesc) +{ + PipelineStateDesc pipelineDesc; + pipelineDesc.type = PipelineType::Compute; + pipelineDesc.compute = inDesc; + initializeBase(pipelineDesc); +} } // namespace cpu } // namespace gfx diff --git a/tools/gfx/cpu/cpu-query.cpp b/tools/gfx/cpu/cpu-query.cpp index 3f87d7a65..9ed416c42 100644 --- a/tools/gfx/cpu/cpu-query.cpp +++ b/tools/gfx/cpu/cpu-query.cpp @@ -14,8 +14,8 @@ Result QueryPoolImpl::init(const IQueryPool::Desc& desc) return SLANG_OK; } -SLANG_NO_THROW Result SLANG_MCALL QueryPoolImpl::getResult( - GfxIndex queryIndex, GfxCount count, uint64_t* data) +SLANG_NO_THROW Result SLANG_MCALL +QueryPoolImpl::getResult(GfxIndex queryIndex, GfxCount count, uint64_t* data) { for (GfxCount i = 0; i < count; i++) { diff --git a/tools/gfx/cpu/cpu-query.h b/tools/gfx/cpu/cpu-query.h index 172dcea41..fd4ac4754 100644 --- a/tools/gfx/cpu/cpu-query.h +++ b/tools/gfx/cpu/cpu-query.h @@ -14,8 +14,8 @@ class QueryPoolImpl : public QueryPoolBase public: List<uint64_t> m_queries; Result init(const IQueryPool::Desc& desc); - virtual SLANG_NO_THROW Result SLANG_MCALL getResult( - GfxIndex queryIndex, GfxCount count, uint64_t* data) override; + virtual SLANG_NO_THROW Result SLANG_MCALL + getResult(GfxIndex queryIndex, GfxCount count, uint64_t* data) override; }; } // namespace cpu diff --git a/tools/gfx/cpu/cpu-resource-views.cpp b/tools/gfx/cpu/cpu-resource-views.cpp index ccf253cab..b9d04beff 100644 --- a/tools/gfx/cpu/cpu-resource-views.cpp +++ b/tools/gfx/cpu/cpu-resource-views.cpp @@ -77,8 +77,10 @@ void TextureResourceViewImpl::SampleLevel( int32_t baseCoordCount = baseShape->baseCoordCount; int32_t integerMipLevel = int32_t(level + 0.5f); - if (integerMipLevel >= desc.numMipLevels) integerMipLevel = desc.numMipLevels - 1; - if (integerMipLevel < 0) integerMipLevel = 0; + if (integerMipLevel >= desc.numMipLevels) + integerMipLevel = desc.numMipLevels - 1; + if (integerMipLevel < 0) + integerMipLevel = 0; auto& mipLevelInfo = texture->m_mipLevels[integerMipLevel]; @@ -90,8 +92,10 @@ void TextureResourceViewImpl::SampleLevel( { elementIndex = int32_t(coords[coordIndex++] + 0.5f); } - if (elementIndex >= effectiveArrayElementCount) elementIndex = effectiveArrayElementCount - 1; - if (elementIndex < 0) elementIndex = 0; + if (elementIndex >= effectiveArrayElementCount) + elementIndex = effectiveArrayElementCount - 1; + if (elementIndex < 0) + elementIndex = 0; // Note: for now we are just going to do nearest-neighbor sampling // @@ -107,8 +111,10 @@ void TextureResourceViewImpl::SampleLevel( int32_t integerCoord = int32_t(coord * (extent - 1) + 0.5f); - if (integerCoord >= extent) integerCoord = extent - 1; - if (integerCoord < 0) integerCoord = 0; + if (integerCoord >= extent) + integerCoord = extent - 1; + if (integerCoord < 0) + integerCoord = 0; texelOffset += integerCoord * mipLevelInfo.strides[axis]; } @@ -145,16 +151,20 @@ void* TextureResourceViewImpl::_getTexelPtr(int32_t const* texelCoords) { elementIndex = texelCoords[coordIndex++]; } - if (elementIndex >= effectiveArrayElementCount) elementIndex = effectiveArrayElementCount - 1; - if (elementIndex < 0) elementIndex = 0; + if (elementIndex >= effectiveArrayElementCount) + elementIndex = effectiveArrayElementCount - 1; + if (elementIndex < 0) + elementIndex = 0; int32_t mipLevel = 0; if (!hasMipLevels) { mipLevel = texelCoords[coordIndex++]; } - if (mipLevel >= desc.numMipLevels) mipLevel = desc.numMipLevels - 1; - if (mipLevel < 0) mipLevel = 0; + if (mipLevel >= desc.numMipLevels) + mipLevel = desc.numMipLevels - 1; + if (mipLevel < 0) + mipLevel = 0; auto& mipLevelInfo = texture->m_mipLevels[mipLevel]; @@ -163,8 +173,10 @@ void* TextureResourceViewImpl::_getTexelPtr(int32_t const* texelCoords) for (int32_t axis = 0; axis < rank; ++axis) { int32_t coord = texelCoords[axis]; - if (coord >= mipLevelInfo.extents[axis]) coord = mipLevelInfo.extents[axis] - 1; - if (coord < 0) coord = 0; + if (coord >= mipLevelInfo.extents[axis]) + coord = mipLevelInfo.extents[axis] - 1; + if (coord < 0) + coord = 0; texelOffset += texelCoords[axis] * mipLevelInfo.strides[axis]; } diff --git a/tools/gfx/cpu/cpu-resource-views.h b/tools/gfx/cpu/cpu-resource-views.h index bec1339a3..c08946811 100644 --- a/tools/gfx/cpu/cpu-resource-views.h +++ b/tools/gfx/cpu/cpu-resource-views.h @@ -1,7 +1,6 @@ // cpu-resource-views.h #pragma once #include "cpu-base.h" - #include "cpu-buffer.h" #include "cpu-texture.h" @@ -34,9 +33,9 @@ class BufferResourceViewImpl : public ResourceViewImpl { public: BufferResourceViewImpl(Desc const& desc, BufferResourceImpl* buffer) - : ResourceViewImpl(Kind::Buffer, desc) - , m_buffer(buffer) - {} + : ResourceViewImpl(Kind::Buffer, desc), m_buffer(buffer) + { + } BufferResourceImpl* getBuffer() const; @@ -48,9 +47,9 @@ class TextureResourceViewImpl : public ResourceViewImpl, public slang_prelude::I { public: TextureResourceViewImpl(Desc const& desc, TextureResourceImpl* texture) - : ResourceViewImpl(Kind::Texture, desc) - , m_texture(texture) - {} + : ResourceViewImpl(Kind::Texture, desc), m_texture(texture) + { + } TextureResourceImpl* getTexture() const; @@ -62,9 +61,18 @@ public: void Load(const int32_t* texelCoords, void* outData, size_t dataSize) SLANG_OVERRIDE; - void Sample(slang_prelude::SamplerState samplerState, const float* coords, void* outData, size_t dataSize) SLANG_OVERRIDE; - - void SampleLevel(slang_prelude::SamplerState samplerState, const float* coords, float level, void* outData, size_t dataSize) SLANG_OVERRIDE; + void Sample( + slang_prelude::SamplerState samplerState, + const float* coords, + void* outData, + size_t dataSize) SLANG_OVERRIDE; + + void SampleLevel( + slang_prelude::SamplerState samplerState, + const float* coords, + float level, + void* outData, + size_t dataSize) SLANG_OVERRIDE; // // IRWTexture interface diff --git a/tools/gfx/cpu/cpu-shader-object-layout.cpp b/tools/gfx/cpu/cpu-shader-object-layout.cpp index 4f4c33a6a..aeeadfc23 100644 --- a/tools/gfx/cpu/cpu-shader-object-layout.cpp +++ b/tools/gfx/cpu/cpu-shader-object-layout.cpp @@ -8,13 +8,16 @@ using namespace Slang; namespace cpu { -ShaderObjectLayoutImpl::ShaderObjectLayoutImpl(RendererBase* renderer, slang::ISession* session, slang::TypeLayoutReflection* layout) +ShaderObjectLayoutImpl::ShaderObjectLayoutImpl( + RendererBase* renderer, + slang::ISession* session, + slang::TypeLayoutReflection* layout) { initBase(renderer, session, layout); m_subObjectCount = 0; m_resourceCount = 0; - + m_elementTypeLayout = _unwrapParameterGroups(layout, m_containerType); m_size = m_elementTypeLayout->getSize(); @@ -44,7 +47,8 @@ ShaderObjectLayoutImpl::ShaderObjectLayoutImpl(RendererBase* renderer, slang::IS // linear search over the descriptor ranges for a specific binding range. // auto uniformOffset = m_elementTypeLayout->getDescriptorSetDescriptorRangeIndexOffset( - descriptorSetIndex, rangeIndexInDescriptorSet); + descriptorSetIndex, + rangeIndexInDescriptorSet); Index baseIndex = 0; Index subObjectIndex = 0; @@ -103,8 +107,10 @@ ShaderObjectLayoutImpl::ShaderObjectLayoutImpl(RendererBase* renderer, slang::IS RefPtr<ShaderObjectLayoutImpl> subObjectLayout; if (slangBindingType != slang::BindingType::ExistentialValue) { - subObjectLayout = - new ShaderObjectLayoutImpl(renderer, m_slangSession, slangLeafTypeLayout->getElementTypeLayout()); + subObjectLayout = new ShaderObjectLayoutImpl( + renderer, + m_slangSession, + slangLeafTypeLayout->getElementTypeLayout()); } SubObjectRangeInfo subObjectRange; @@ -119,35 +125,50 @@ size_t ShaderObjectLayoutImpl::getSize() return m_size; } -Index ShaderObjectLayoutImpl::getResourceCount() const { return m_resourceCount; } -Index ShaderObjectLayoutImpl::getSubObjectCount() const { return m_subObjectCount; } -List<SubObjectRangeInfo>& ShaderObjectLayoutImpl::getSubObjectRanges() { return subObjectRanges; } -BindingRangeInfo ShaderObjectLayoutImpl::getBindingRange(Index index) { return m_bindingRanges[index]; } -Index ShaderObjectLayoutImpl::getBindingRangeCount() const { return m_bindingRanges.getCount(); } +Index ShaderObjectLayoutImpl::getResourceCount() const +{ + return m_resourceCount; +} +Index ShaderObjectLayoutImpl::getSubObjectCount() const +{ + return m_subObjectCount; +} +List<SubObjectRangeInfo>& ShaderObjectLayoutImpl::getSubObjectRanges() +{ + return subObjectRanges; +} +BindingRangeInfo ShaderObjectLayoutImpl::getBindingRange(Index index) +{ + return m_bindingRanges[index]; +} +Index ShaderObjectLayoutImpl::getBindingRangeCount() const +{ + return m_bindingRanges.getCount(); +} const char* EntryPointLayoutImpl::getEntryPointName() { return m_entryPointLayout->getName(); } -RootShaderObjectLayoutImpl::RootShaderObjectLayoutImpl(RendererBase* renderer, slang::ISession* session, slang::ProgramLayout* programLayout) +RootShaderObjectLayoutImpl::RootShaderObjectLayoutImpl( + RendererBase* renderer, + slang::ISession* session, + slang::ProgramLayout* programLayout) : ShaderObjectLayoutImpl(renderer, session, programLayout->getGlobalParamsTypeLayout()) , m_programLayout(programLayout) { - for (UInt i =0; i< programLayout->getEntryPointCount(); i++) + for (UInt i = 0; i < programLayout->getEntryPointCount(); i++) { - m_entryPointLayouts.add(new EntryPointLayoutImpl( - renderer, - session, - programLayout->getEntryPointByIndex(i))); + m_entryPointLayouts.add( + new EntryPointLayoutImpl(renderer, session, programLayout->getEntryPointByIndex(i))); } - } int RootShaderObjectLayoutImpl::getKernelIndex(UnownedStringSlice kernelName) { - auto entryPointCount = (int) m_programLayout->getEntryPointCount(); - for(int i = 0; i < entryPointCount; i++) + auto entryPointCount = (int)m_programLayout->getEntryPointCount(); + for (int i = 0; i < entryPointCount; i++) { auto entryPoint = m_programLayout->getEntryPointByIndex(i); if (kernelName == entryPoint->getName()) @@ -164,7 +185,10 @@ void RootShaderObjectLayoutImpl::getKernelThreadGroupSize(int kernelIndex, UInt* entryPoint->getComputeThreadGroupSize(3, threadGroupSizes); } -EntryPointLayoutImpl* RootShaderObjectLayoutImpl::getEntryPoint(Index index) { return m_entryPointLayouts[index]; } +EntryPointLayoutImpl* RootShaderObjectLayoutImpl::getEntryPoint(Index index) +{ + return m_entryPointLayouts[index]; +} } // namespace cpu } // namespace gfx diff --git a/tools/gfx/cpu/cpu-shader-object-layout.h b/tools/gfx/cpu/cpu-shader-object-layout.h index 3bf2e2aa7..b5439f370 100644 --- a/tools/gfx/cpu/cpu-shader-object-layout.h +++ b/tools/gfx/cpu/cpu-shader-object-layout.h @@ -43,7 +43,6 @@ struct SubObjectRangeInfo class ShaderObjectLayoutImpl : public ShaderObjectLayoutBase { public: - // TODO: Once memory lifetime stuff is handled, there is // no specific need to even track binding or sub-object // ranges for CPU. @@ -55,7 +54,10 @@ public: Index m_subObjectCount = 0; Index m_resourceCount = 0; - ShaderObjectLayoutImpl(RendererBase* renderer, slang::ISession* session, slang::TypeLayoutReflection* layout); + ShaderObjectLayoutImpl( + RendererBase* renderer, + slang::ISession* session, + slang::TypeLayoutReflection* layout); size_t getSize(); Index getResourceCount() const; @@ -72,12 +74,13 @@ private: public: EntryPointLayoutImpl( - RendererBase* renderer, - slang::ISession* session, - slang::EntryPointLayout* entryPointLayout) + RendererBase* renderer, + slang::ISession* session, + slang::EntryPointLayout* entryPointLayout) : ShaderObjectLayoutImpl(renderer, session, entryPointLayout->getTypeLayout()) , m_entryPointLayout(entryPointLayout) - {} + { + } const char* getEntryPointName(); }; @@ -88,7 +91,10 @@ public: slang::ProgramLayout* m_programLayout = nullptr; List<RefPtr<EntryPointLayoutImpl>> m_entryPointLayouts; - RootShaderObjectLayoutImpl(RendererBase* renderer, slang::ISession* session, slang::ProgramLayout* programLayout); + RootShaderObjectLayoutImpl( + RendererBase* renderer, + slang::ISession* session, + slang::ProgramLayout* programLayout); int getKernelIndex(UnownedStringSlice kernelName); void getKernelThreadGroupSize(int kernelIndex, UInt* threadGroupSizes); diff --git a/tools/gfx/cpu/cpu-shader-object.cpp b/tools/gfx/cpu/cpu-shader-object.cpp index 67ffd6af5..3c9dd3cb2 100644 --- a/tools/gfx/cpu/cpu-shader-object.cpp +++ b/tools/gfx/cpu/cpu-shader-object.cpp @@ -54,7 +54,6 @@ ResourceViewBase* CPUShaderObjectData::getResourceView( viewDesc.type = IResourceView::Type::UnorderedAccess; viewDesc.format = Format::Unknown; m_bufferView = new BufferResourceViewImpl(viewDesc, m_bufferResource); - } m_bufferResource->getDesc()->sizeInBytes = m_ordinaryData.getCount(); m_bufferResource->m_data = m_ordinaryData.getBuffer(); @@ -129,7 +128,7 @@ SLANG_NO_THROW GfxCount SLANG_MCALL ShaderObjectImpl::getEntryPointCount() } SLANG_NO_THROW Result SLANG_MCALL - ShaderObjectImpl::getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) +ShaderObjectImpl::getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) { *outEntryPoint = nullptr; return SLANG_OK; @@ -146,7 +145,7 @@ SLANG_NO_THROW size_t SLANG_MCALL ShaderObjectImpl::getSize() } SLANG_NO_THROW Result SLANG_MCALL - ShaderObjectImpl::setData(ShaderOffset const& offset, void const* data, size_t size) +ShaderObjectImpl::setData(ShaderOffset const& offset, void const* data, size_t size) { size = Math::Min(size, size_t(m_data.getCount() - offset.uniformOffset)); memcpy((char*)m_data.getBuffer() + offset.uniformOffset, data, size); @@ -154,7 +153,7 @@ SLANG_NO_THROW Result SLANG_MCALL } SLANG_NO_THROW Result SLANG_MCALL - ShaderObjectImpl::setResource(ShaderOffset const& offset, IResourceView* inView) +ShaderObjectImpl::setResource(ShaderOffset const& offset, IResourceView* inView) { auto layout = getLayout(); @@ -172,40 +171,40 @@ SLANG_NO_THROW Result SLANG_MCALL switch (view->getViewKind()) { case ResourceViewImpl::Kind::Texture: - { - auto textureView = static_cast<TextureResourceViewImpl*>(view); + { + auto textureView = static_cast<TextureResourceViewImpl*>(view); - slang_prelude::IRWTexture* textureObj = textureView; - SLANG_RETURN_ON_FAIL(setData(offset, &textureObj, sizeof(textureObj))); - } - break; + slang_prelude::IRWTexture* textureObj = textureView; + SLANG_RETURN_ON_FAIL(setData(offset, &textureObj, sizeof(textureObj))); + } + break; case ResourceViewImpl::Kind::Buffer: - { - auto bufferView = static_cast<BufferResourceViewImpl*>(view); - auto buffer = bufferView->getBuffer(); - auto desc = *buffer->getDesc(); + { + auto bufferView = static_cast<BufferResourceViewImpl*>(view); + auto buffer = bufferView->getBuffer(); + auto desc = *buffer->getDesc(); - void* dataPtr = buffer->m_data; - size_t size = desc.sizeInBytes; - if (desc.elementSize > 1) - size /= desc.elementSize; + void* dataPtr = buffer->m_data; + size_t size = desc.sizeInBytes; + if (desc.elementSize > 1) + size /= desc.elementSize; - auto ptrOffset = offset; - SLANG_RETURN_ON_FAIL(setData(ptrOffset, &dataPtr, sizeof(dataPtr))); + auto ptrOffset = offset; + SLANG_RETURN_ON_FAIL(setData(ptrOffset, &dataPtr, sizeof(dataPtr))); - auto sizeOffset = offset; - sizeOffset.uniformOffset += sizeof(dataPtr); - SLANG_RETURN_ON_FAIL(setData(sizeOffset, &size, sizeof(size))); - } - break; + auto sizeOffset = offset; + sizeOffset.uniformOffset += sizeof(dataPtr); + SLANG_RETURN_ON_FAIL(setData(sizeOffset, &size, sizeof(size))); + } + break; } return SLANG_OK; } SLANG_NO_THROW Result SLANG_MCALL - ShaderObjectImpl::setObject(ShaderOffset const& offset, IShaderObject* object) +ShaderObjectImpl::setObject(ShaderOffset const& offset, IShaderObject* object) { SLANG_RETURN_ON_FAIL(Super::setObject(offset, object)); @@ -217,21 +216,20 @@ SLANG_NO_THROW Result SLANG_MCALL switch (bindingRange.bindingType) { default: - { - void* bufferPtr = subObject->m_data.getBuffer(); - SLANG_RETURN_ON_FAIL(setData(offset, &bufferPtr, sizeof(void*))); - } - break; + { + void* bufferPtr = subObject->m_data.getBuffer(); + SLANG_RETURN_ON_FAIL(setData(offset, &bufferPtr, sizeof(void*))); + } + break; case slang::BindingType::ExistentialValue: case slang::BindingType::RawBuffer: - case slang::BindingType::MutableRawBuffer: - break; + case slang::BindingType::MutableRawBuffer: break; } return SLANG_OK; } SLANG_NO_THROW Result SLANG_MCALL - ShaderObjectImpl::setSampler(ShaderOffset const& offset, ISamplerState* sampler) +ShaderObjectImpl::setSampler(ShaderOffset const& offset, ISamplerState* sampler) { SLANG_UNUSED(sampler); SLANG_UNUSED(offset); @@ -239,7 +237,9 @@ SLANG_NO_THROW Result SLANG_MCALL } SLANG_NO_THROW Result SLANG_MCALL ShaderObjectImpl::setCombinedTextureSampler( - ShaderOffset const& offset, IResourceView* textureView, ISamplerState* sampler) + ShaderOffset const& offset, + IResourceView* textureView, + ISamplerState* sampler) { SLANG_UNUSED(sampler); setResource(offset, textureView); @@ -294,7 +294,7 @@ SLANG_NO_THROW GfxCount SLANG_MCALL RootShaderObjectImpl::getEntryPointCount() } SLANG_NO_THROW Result SLANG_MCALL - RootShaderObjectImpl::getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) +RootShaderObjectImpl::getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) { returnComPtr(outEntryPoint, m_entryPoints[index]); return SLANG_OK; diff --git a/tools/gfx/cpu/cpu-shader-object.h b/tools/gfx/cpu/cpu-shader-object.h index 2be703dc5..dc7199509 100644 --- a/tools/gfx/cpu/cpu-shader-object.h +++ b/tools/gfx/cpu/cpu-shader-object.h @@ -1,7 +1,6 @@ // cpu-shader-object.h #pragma once #include "cpu-base.h" - #include "cpu-shader-object-layout.h" namespace gfx @@ -36,38 +35,43 @@ public: class ShaderObjectImpl : public ShaderObjectBaseImpl<ShaderObjectImpl, ShaderObjectLayoutImpl, CPUShaderObjectData> { - typedef ShaderObjectBaseImpl<ShaderObjectImpl, ShaderObjectLayoutImpl, CPUShaderObjectData> Super; + typedef ShaderObjectBaseImpl<ShaderObjectImpl, ShaderObjectLayoutImpl, CPUShaderObjectData> + Super; public: List<RefPtr<ResourceViewImpl>> m_resources; virtual SLANG_NO_THROW Result SLANG_MCALL - init(IDevice* device, ShaderObjectLayoutImpl* typeLayout); + init(IDevice* device, ShaderObjectLayoutImpl* typeLayout); virtual SLANG_NO_THROW GfxCount SLANG_MCALL getEntryPointCount() override; virtual SLANG_NO_THROW Result SLANG_MCALL - getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) override; + getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) override; virtual SLANG_NO_THROW const void* SLANG_MCALL getRawData() override; virtual SLANG_NO_THROW size_t SLANG_MCALL getSize() override; virtual SLANG_NO_THROW Result SLANG_MCALL - setData(ShaderOffset const& offset, void const* data, size_t size) override; + setData(ShaderOffset const& offset, void const* data, size_t size) override; virtual SLANG_NO_THROW Result SLANG_MCALL - setResource(ShaderOffset const& offset, IResourceView* inView) override; + setResource(ShaderOffset const& offset, IResourceView* inView) override; virtual SLANG_NO_THROW Result SLANG_MCALL - setObject(ShaderOffset const& offset, IShaderObject* object) override; + setObject(ShaderOffset const& offset, IShaderObject* object) override; virtual SLANG_NO_THROW Result SLANG_MCALL - setSampler(ShaderOffset const& offset, ISamplerState* sampler) override; + setSampler(ShaderOffset const& offset, ISamplerState* sampler) override; virtual SLANG_NO_THROW Result SLANG_MCALL setCombinedTextureSampler( - ShaderOffset const& offset, IResourceView* textureView, ISamplerState* sampler) override; + ShaderOffset const& offset, + IResourceView* textureView, + ISamplerState* sampler) override; char* getDataBuffer(); }; -class MutableShaderObjectImpl : public MutableShaderObject<MutableShaderObjectImpl, ShaderObjectLayoutImpl> -{}; +class MutableShaderObjectImpl + : public MutableShaderObject<MutableShaderObjectImpl, ShaderObjectLayoutImpl> +{ +}; class EntryPointShaderObjectImpl : public ShaderObjectImpl { @@ -92,7 +96,7 @@ public: virtual SLANG_NO_THROW GfxCount SLANG_MCALL getEntryPointCount() override; virtual SLANG_NO_THROW Result SLANG_MCALL - getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) override; + getEntryPoint(GfxIndex index, IShaderObject** outEntryPoint) override; virtual Result collectSpecializationArgs(ExtendedShaderObjectTypeList& args) override; }; diff --git a/tools/gfx/cpu/cpu-shader-program.h b/tools/gfx/cpu/cpu-shader-program.h index f2f159538..6c92151fa 100644 --- a/tools/gfx/cpu/cpu-shader-program.h +++ b/tools/gfx/cpu/cpu-shader-program.h @@ -1,7 +1,6 @@ // cpu-shader-program.h #pragma once #include "cpu-base.h" - #include "cpu-shader-object-layout.h" namespace gfx @@ -16,9 +15,7 @@ class ShaderProgramImpl : public ShaderProgramBase public: RefPtr<RootShaderObjectLayoutImpl> layout; - ~ShaderProgramImpl() - { - } + ~ShaderProgramImpl() {} }; } // namespace cpu diff --git a/tools/gfx/cpu/cpu-texture.cpp b/tools/gfx/cpu/cpu-texture.cpp index d5367bd96..9077084f2 100644 --- a/tools/gfx/cpu/cpu-texture.cpp +++ b/tools/gfx/cpu/cpu-texture.cpp @@ -18,7 +18,7 @@ void _unpackFloatTexel(void const* texelData, void* outData, size_t outSize) { auto input = (float const*)texelData; - float temp[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; + float temp[4] = {0.0f, 0.0f, 0.0f, 1.0f}; for (int i = 0; i < N; ++i) temp[i] = input[i]; @@ -30,7 +30,7 @@ void _unpackFloat16Texel(void const* texelData, void* outData, size_t outSize) { auto input = (int16_t const*)texelData; - float temp[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; + float temp[4] = {0.0f, 0.0f, 0.0f, 1.0f}; for (int i = 0; i < N; ++i) temp[i] = HalfToFloat(input[i]); @@ -47,7 +47,7 @@ void _unpackUnorm8Texel(void const* texelData, void* outData, size_t outSize) { auto input = (uint8_t const*)texelData; - float temp[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; + float temp[4] = {0.0f, 0.0f, 0.0f, 1.0f}; for (int i = 0; i < N; ++i) temp[i] = _unpackUnorm8Value(input[i]); @@ -72,7 +72,7 @@ void _unpackUInt16Texel(void const* texelData, void* outData, size_t outSize) { auto input = (uint16_t const*)texelData; - uint32_t temp[4] = { 0, 0, 0, 0 }; + uint32_t temp[4] = {0, 0, 0, 0}; for (int i = 0; i < N; ++i) temp[i] = input[i]; @@ -84,7 +84,7 @@ void _unpackUInt32Texel(void const* texelData, void* outData, size_t outSize) { auto input = (uint32_t const*)texelData; - uint32_t temp[4] = { 0, 0, 0, 0 }; + uint32_t temp[4] = {0, 0, 0, 0}; for (int i = 0; i < N; ++i) temp[i] = input[i]; @@ -117,16 +117,16 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa uint32_t texelSize = uint32_t(texelInfo.blockSizeInBytes / texelInfo.pixelsPerBlock); m_texelSize = texelSize; - int32_t formatBlockSize[kMaxRank] = { 1, 1, 1 }; + int32_t formatBlockSize[kMaxRank] = {1, 1, 1}; auto baseShapeInfo = _getBaseShapeInfo(desc.type); m_baseShape = baseShapeInfo; - if(!baseShapeInfo) + if (!baseShapeInfo) return SLANG_FAIL; auto formatInfo = _getFormatInfo(desc.format); m_formatInfo = formatInfo; - if(!formatInfo) + if (!formatInfo) return SLANG_FAIL; int32_t rank = baseShapeInfo->rank; @@ -139,7 +139,7 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa extents[1] = desc.size.height; extents[2] = desc.size.depth; - for(int32_t axis = rank; axis < kMaxRank; ++axis) + for (int32_t axis = rank; axis < kMaxRank; ++axis) extents[axis] = 1; int32_t levelCount = desc.numMipLevels; @@ -147,26 +147,27 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa m_mipLevels.setCount(levelCount); int64_t totalDataSize = 0; - for( int32_t levelIndex = 0; levelIndex < levelCount; ++levelIndex ) + for (int32_t levelIndex = 0; levelIndex < levelCount; ++levelIndex) { auto& level = m_mipLevels[levelIndex]; - for( int32_t axis = 0; axis < kMaxRank; ++axis ) + for (int32_t axis = 0; axis < kMaxRank; ++axis) { int32_t extent = extents[axis] >> levelIndex; - if(extent < 1) extent = 1; + if (extent < 1) + extent = 1; level.extents[axis] = extent; } level.strides[0] = texelSize; - for( int32_t axis = 1; axis < kMaxRank+1; ++axis) + for (int32_t axis = 1; axis < kMaxRank + 1; ++axis) { - level.strides[axis] = level.strides[axis-1]*level.extents[axis-1]; + level.strides[axis] = level.strides[axis - 1] * level.extents[axis - 1]; } int64_t levelDataSize = texelSize; levelDataSize *= effectiveArrayElementCount; - for( int32_t axis = 0; axis < rank; ++axis) + for (int32_t axis = 0; axis < rank; ++axis) levelDataSize *= int64_t(level.extents[axis]); level.offset = totalDataSize; @@ -176,12 +177,13 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa void* textureData = malloc((size_t)totalDataSize); m_data = textureData; - if( initData ) + if (initData) { int32_t subResourceCounter = 0; - for(int32_t arrayElementIndex = 0; arrayElementIndex < effectiveArrayElementCount; ++arrayElementIndex) + for (int32_t arrayElementIndex = 0; arrayElementIndex < effectiveArrayElementCount; + ++arrayElementIndex) { - for(int32_t mipLevel = 0; mipLevel < m_desc.numMipLevels; ++mipLevel) + for (int32_t mipLevel = 0; mipLevel < m_desc.numMipLevels; ++mipLevel) { int32_t subResourceIndex = subResourceCounter++; @@ -189,7 +191,7 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa auto dstLayerStride = m_mipLevels[mipLevel].strides[2]; auto dstArrayStride = m_mipLevels[mipLevel].strides[3]; - auto textureRowSize = m_mipLevels[mipLevel].extents[0]*texelSize; + auto textureRowSize = m_mipLevels[mipLevel].extents[0] * texelSize; auto rowCount = m_mipLevels[mipLevel].extents[1]; auto depthLayerCount = m_mipLevels[mipLevel].extents[2]; @@ -199,17 +201,17 @@ Result TextureResourceImpl::init(ITextureResource::SubresourceData const* initDa ptrdiff_t srcLayerStride = ptrdiff_t(srcImage.strideZ); char* dstLevel = (char*)textureData + m_mipLevels[mipLevel].offset; - char* dstImage = dstLevel + dstArrayStride*arrayElementIndex; + char* dstImage = dstLevel + dstArrayStride * arrayElementIndex; - const char* srcLayer = (const char*) srcImage.data; + const char* srcLayer = (const char*)srcImage.data; char* dstLayer = dstImage; - for(int32_t depthLayer = 0; depthLayer < depthLayerCount; ++depthLayer) + for (int32_t depthLayer = 0; depthLayer < depthLayerCount; ++depthLayer) { const char* srcRow = srcLayer; char* dstRow = dstLayer; - for(int32_t row = 0; row < rowCount; ++row) + for (int32_t row = 0; row < rowCount; ++row) { memcpy(dstRow, srcRow, textureRowSize); diff --git a/tools/gfx/cpu/cpu-texture.h b/tools/gfx/cpu/cpu-texture.h index 5ae896d0f..e65f8f4be 100644 --- a/tools/gfx/cpu/cpu-texture.h +++ b/tools/gfx/cpu/cpu-texture.h @@ -16,14 +16,14 @@ struct CPUTextureBaseShapeInfo int32_t implicitArrayElementCount; }; -static const CPUTextureBaseShapeInfo kCPUTextureBaseShapeInfos[(int)ITextureResource::Type::_Count] = -{ - /* Unknown */ { 0, 0, 0 }, - /* Buffer */ { 1, 1, 1 }, - /* Texture1D */ { 1, 1, 1 }, - /* Texture2D */ { 2, 2, 1 }, - /* Texture3D */ { 3, 3, 1 }, - /* TextureCube */ { 2, 3, 6 }, +static const CPUTextureBaseShapeInfo + kCPUTextureBaseShapeInfos[(int)ITextureResource::Type::_Count] = { + /* Unknown */ {0, 0, 0}, + /* Buffer */ {1, 1, 1}, + /* Texture1D */ {1, 1, 1}, + /* Texture2D */ {2, 2, 1}, + /* Texture3D */ {3, 3, 1}, + /* TextureCube */ {2, 3, 6}, }; static CPUTextureBaseShapeInfo const* _getBaseShapeInfo(ITextureResource::Type baseShape); @@ -82,7 +82,10 @@ struct CPUFormatInfoMap auto& info = m_infos[Index(format)]; info.unpackFunc = func; } - SLANG_FORCE_INLINE const CPUTextureFormatInfo& get(Format format) const { return m_infos[Index(format)]; } + SLANG_FORCE_INLINE const CPUTextureFormatInfo& get(Format format) const + { + return m_infos[Index(format)]; + } CPUTextureFormatInfo m_infos[Index(Format::_Count)]; }; @@ -97,12 +100,16 @@ static CPUTextureFormatInfo const* _getFormatInfo(Format format) class TextureResourceImpl : public TextureResource { - enum { kMaxRank = 3 }; + enum + { + kMaxRank = 3 + }; public: TextureResourceImpl(const TextureResource::Desc& desc) : TextureResource(desc) - {} + { + } ~TextureResourceImpl(); Result init(ITextureResource::SubresourceData const* initData); @@ -119,11 +126,11 @@ public: struct MipLevel { int32_t extents[kMaxRank]; - int64_t strides[kMaxRank+1]; + int64_t strides[kMaxRank + 1]; int64_t offset; }; - List<MipLevel> m_mipLevels; - void* m_data = nullptr; + List<MipLevel> m_mipLevels; + void* m_data = nullptr; }; } // namespace cpu |
