diff options
| -rw-r--r-- | tools/gfx/cpu/render-cpu.cpp | 13 | ||||
| -rw-r--r-- | tools/gfx/cuda/render-cuda.cpp | 6 | ||||
| -rw-r--r-- | tools/gfx/d3d11/render-d3d11.cpp | 82 | ||||
| -rw-r--r-- | tools/gfx/d3d12/render-d3d12.cpp | 6 | ||||
| -rw-r--r-- | tools/gfx/immediate-renderer-base.cpp | 4 | ||||
| -rw-r--r-- | tools/gfx/immediate-renderer-base.h | 2 | ||||
| -rw-r--r-- | tools/gfx/open-gl/render-gl.cpp | 13 | ||||
| -rw-r--r-- | tools/gfx/slang-context.h | 11 | ||||
| -rw-r--r-- | tools/gfx/vulkan/render-vk.cpp | 8 |
9 files changed, 110 insertions, 35 deletions
diff --git a/tools/gfx/cpu/render-cpu.cpp b/tools/gfx/cpu/render-cpu.cpp index 0c5f119b8..03628c166 100644 --- a/tools/gfx/cpu/render-cpu.cpp +++ b/tools/gfx/cpu/render-cpu.cpp @@ -1085,7 +1085,11 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL initialize(const Desc& desc) override { - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_HOST_CALLABLE, "sm_5_1")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_HOST_CALLABLE, + "sm_5_1", + makeArray(slang::PreprocessorMacroDesc{ "__CPU__", "1" }).getView())); SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); @@ -1244,7 +1248,12 @@ public: auto bufferImpl = static_cast<CPUBufferResource*>(buffer); return bufferImpl->m_data; } - virtual void unmap(IBufferResource* buffer) override { SLANG_UNUSED(buffer); } + virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) override + { + SLANG_UNUSED(buffer); + SLANG_UNUSED(offsetWritten); + SLANG_UNUSED(sizeWritten); + } }; SlangResult CPUShaderObject::init(IDevice* device, CPUShaderObjectLayout* typeLayout) diff --git a/tools/gfx/cuda/render-cuda.cpp b/tools/gfx/cuda/render-cuda.cpp index f60be8eda..263360018 100644 --- a/tools/gfx/cuda/render-cuda.cpp +++ b/tools/gfx/cuda/render-cuda.cpp @@ -1176,7 +1176,11 @@ public: public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL initialize(const Desc& desc) override { - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_PTX, "sm_5_1")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_PTX, + "sm_5_1", + makeArray(slang::PreprocessorMacroDesc{ "__CUDA_COMPUTE__", "1" }).getView())); SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); diff --git a/tools/gfx/d3d11/render-d3d11.cpp b/tools/gfx/d3d11/render-d3d11.cpp index d4a55543c..3e71ff3d4 100644 --- a/tools/gfx/d3d11/render-d3d11.cpp +++ b/tools/gfx/d3d11/render-d3d11.cpp @@ -117,7 +117,7 @@ public: const ComputePipelineStateDesc& desc, IPipelineState** outState) override; virtual void* map(IBufferResource* buffer, MapFlavor flavor) override; - virtual void unmap(IBufferResource* buffer) override; + virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) override; virtual void copyBuffer( IBufferResource* dst, size_t dstOffset, @@ -181,8 +181,10 @@ protected: } MapFlavor m_mapFlavor; + D3D11_USAGE m_d3dUsage; ComPtr<ID3D11Buffer> m_buffer; ComPtr<ID3D11Buffer> m_staging; + List<uint8_t> m_uploadStagingBuffer; }; class TextureResourceImpl : public TextureResource { @@ -1462,7 +1464,7 @@ protected: auto ordinaryData = device->map(m_ordinaryDataBuffer, gfx::MapFlavor::WriteDiscard); auto result = _writeOrdinaryData(ordinaryData, specializedOrdinaryDataSize, specializedLayout); - device->unmap(m_ordinaryDataBuffer); + device->unmap(m_ordinaryDataBuffer, 0, specializedOrdinaryDataSize); return result; } @@ -2013,7 +2015,11 @@ static bool _isSupportedNVAPIOp(IUnknown* dev, uint32_t op) SlangResult D3D11Device::initialize(const Desc& desc) { - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_DXBC, "sm_5_0")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_DXBC, + "sm_5_0", + makeArray(slang::PreprocessorMacroDesc{ "__D3D11__", "1" }).getView())); SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); @@ -2603,8 +2609,10 @@ Result D3D11Device::createBufferResource(const IBufferResource::Desc& descIn, co RefPtr<BufferResourceImpl> buffer(new BufferResourceImpl(srcDesc)); SLANG_RETURN_ON_FAIL(m_device->CreateBuffer(&bufferDesc, initData ? &subResourceData : nullptr, buffer->m_buffer.writeRef())); + buffer->m_d3dUsage = bufferDesc.Usage; - if (srcDesc.cpuAccessFlags & IResource::AccessFlag::Read) + if ((srcDesc.cpuAccessFlags & IResource::AccessFlag::Read) || + ((srcDesc.cpuAccessFlags & IResource::AccessFlag::Write) && bufferDesc.Usage != D3D11_USAGE_DYNAMIC)) { D3D11_BUFFER_DESC bufDesc = {}; bufDesc.BindFlags = 0; @@ -2614,7 +2622,6 @@ Result D3D11Device::createBufferResource(const IBufferResource::Desc& descIn, co SLANG_RETURN_ON_FAIL(m_device->CreateBuffer(&bufDesc, nullptr, buffer->m_staging.writeRef())); } - returnComPtr(outResource, buffer); return SLANG_OK; } @@ -2981,37 +2988,72 @@ void* D3D11Device::map(IBufferResource* bufferIn, MapFlavor flavor) break; case MapFlavor::HostRead: mapType = D3D11_MAP_READ; - - buffer = bufferResource->m_staging; - if (!buffer) - { - return nullptr; - } - - // Okay copy the data over - m_immediateContext->CopyResource(buffer, bufferResource->m_buffer); - break; default: return nullptr; } + bufferResource->m_mapFlavor = flavor; + + switch (flavor) + { + case MapFlavor::WriteDiscard: + case MapFlavor::HostWrite: + // If buffer is not dynamic, we need to use staging buffer. + if (bufferResource->m_d3dUsage != D3D11_USAGE_DYNAMIC) + { + bufferResource->m_uploadStagingBuffer.setCount(bufferResource->getDesc()->sizeInBytes); + return bufferResource->m_uploadStagingBuffer.getBuffer(); + } + break; + case MapFlavor::HostRead: + buffer = bufferResource->m_staging; + if (!buffer) + { + return nullptr; + } + + // Okay copy the data over + m_immediateContext->CopyResource(buffer, bufferResource->m_buffer); + + } + // We update our constant buffer per-frame, just for the purposes // of the example, but we don't actually load different data // per-frame (we always use an identity projection). D3D11_MAPPED_SUBRESOURCE mappedSub; SLANG_RETURN_NULL_ON_FAIL(m_immediateContext->Map(buffer, 0, mapType, 0, &mappedSub)); - bufferResource->m_mapFlavor = flavor; - return mappedSub.pData; } -void D3D11Device::unmap(IBufferResource* bufferIn) +void D3D11Device::unmap(IBufferResource* bufferIn, size_t offsetWritten, size_t sizeWritten) { BufferResourceImpl* bufferResource = static_cast<BufferResourceImpl*>(bufferIn); - ID3D11Buffer* buffer = (bufferResource->m_mapFlavor == MapFlavor::HostRead) ? bufferResource->m_staging : bufferResource->m_buffer; - m_immediateContext->Unmap(buffer, 0); + switch (bufferResource->m_mapFlavor) + { + case MapFlavor::WriteDiscard: + case MapFlavor::HostWrite: + // If buffer is not dynamic, the CPU has already written to the staging buffer, + // and we need to copy the content over to the GPU buffer. + if (bufferResource->m_d3dUsage != D3D11_USAGE_DYNAMIC && sizeWritten != 0) + { + D3D11_BOX dstBox = {}; + dstBox.left = (UINT)offsetWritten; + dstBox.right = (UINT)(offsetWritten + sizeWritten); + dstBox.back = 1; + dstBox.bottom = 1; + m_immediateContext->UpdateSubresource( + bufferResource->m_buffer, + 0, + &dstBox, + bufferResource->m_uploadStagingBuffer.getBuffer() + offsetWritten, + 0, + 0); + return; + } + } + m_immediateContext->Unmap(bufferResource->m_mapFlavor == MapFlavor::HostRead ? bufferResource->m_staging : bufferResource->m_buffer, 0); } #if 0 diff --git a/tools/gfx/d3d12/render-d3d12.cpp b/tools/gfx/d3d12/render-d3d12.cpp index 8b5721cda..427e98125 100644 --- a/tools/gfx/d3d12/render-d3d12.cpp +++ b/tools/gfx/d3d12/render-d3d12.cpp @@ -4254,7 +4254,11 @@ static bool _isSupportedNVAPIOp(ID3D12Device* dev, uint32_t op) Result D3D12Device::initialize(const Desc& desc) { - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_DXBC, "sm_5_1")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_DXBC, + "sm_5_1", + makeArray(slang::PreprocessorMacroDesc{ "__D3D12__", "1" }).getView())); SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); diff --git a/tools/gfx/immediate-renderer-base.cpp b/tools/gfx/immediate-renderer-base.cpp index 0ee2b7261..19a16eac1 100644 --- a/tools/gfx/immediate-renderer-base.cpp +++ b/tools/gfx/immediate-renderer-base.cpp @@ -471,7 +471,7 @@ void ImmediateRendererBase::uploadBufferData( { auto buffer = map(dst, gfx::MapFlavor::WriteDiscard); memcpy((uint8_t*)buffer + offset, data, size); - unmap(dst); + unmap(dst, offset, size); } SLANG_NO_THROW SlangResult SLANG_MCALL ImmediateRendererBase::readBufferResource( @@ -486,7 +486,7 @@ SLANG_NO_THROW SlangResult SLANG_MCALL ImmediateRendererBase::readBufferResource if (!content) return SLANG_FAIL; memcpy(blob->m_data.getBuffer(), content + offset, size); - unmap(buffer); + unmap(buffer, offset, size); returnComPtr(outBlob, blob); return SLANG_OK; } diff --git a/tools/gfx/immediate-renderer-base.h b/tools/gfx/immediate-renderer-base.h index 6d4365cab..3acceed15 100644 --- a/tools/gfx/immediate-renderer-base.h +++ b/tools/gfx/immediate-renderer-base.h @@ -74,7 +74,7 @@ public: virtual void submitGpuWork() = 0; virtual void waitForGpu() = 0; virtual void* map(IBufferResource* buffer, MapFlavor flavor) = 0; - virtual void unmap(IBufferResource* buffer) = 0; + virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) = 0; public: Slang::RefPtr<ImmediateCommandQueueBase> m_queue; diff --git a/tools/gfx/open-gl/render-gl.cpp b/tools/gfx/open-gl/render-gl.cpp index 5b1ea2c3e..cd4811911 100644 --- a/tools/gfx/open-gl/render-gl.cpp +++ b/tools/gfx/open-gl/render-gl.cpp @@ -152,7 +152,7 @@ public: ITextureResource* texture, ResourceState state, ISlangBlob** outBlob, size_t* outRowPitch, size_t* outPixelSize) override; virtual void* map(IBufferResource* buffer, MapFlavor flavor) override; - virtual void unmap(IBufferResource* buffer) override; + virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) override; virtual void setPrimitiveTopology(PrimitiveTopology topology) override; virtual void setVertexBuffers( @@ -1937,7 +1937,12 @@ HGLRC GLDevice::createGLContext(HDC hdc) SLANG_NO_THROW Result SLANG_MCALL GLDevice::initialize(const Desc& desc) { - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_GLSL, "glsl_440")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_GLSL, + "glsl_440", + makeArray( + slang::PreprocessorMacroDesc{ "__GL__", "1" }).getView())); SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc)); @@ -2557,8 +2562,10 @@ void* GLDevice::map(IBufferResource* bufferIn, MapFlavor flavor) return glMapBuffer(buffer->m_target, access); } -void GLDevice::unmap(IBufferResource* bufferIn) +void GLDevice::unmap(IBufferResource* bufferIn, size_t offsetWritten, size_t sizeWritten) { + SLANG_UNUSED(offsetWritten); + SLANG_UNUSED(sizeWritten); BufferResourceImpl* buffer = static_cast<BufferResourceImpl*>(bufferIn); glUnmapBuffer(buffer->m_target); } diff --git a/tools/gfx/slang-context.h b/tools/gfx/slang-context.h index b58d6cc18..39a457cf6 100644 --- a/tools/gfx/slang-context.h +++ b/tools/gfx/slang-context.h @@ -1,6 +1,7 @@ #pragma once #include "slang-gfx.h" +#include "core/slang-basic.h" namespace gfx { @@ -9,7 +10,8 @@ namespace gfx public: Slang::ComPtr<slang::IGlobalSession> globalSession; Slang::ComPtr<slang::ISession> session; - Result initialize(const gfx::IDevice::SlangDesc& desc, SlangCompileTarget compileTarget, const char* defaultProfileName) + Result initialize(const gfx::IDevice::SlangDesc& desc, SlangCompileTarget compileTarget, const char* defaultProfileName, + Slang::ConstArrayView<slang::PreprocessorMacroDesc> additionalMacros) { if (desc.slangGlobalSession) { @@ -24,8 +26,11 @@ namespace gfx slangSessionDesc.defaultMatrixLayoutMode = desc.defaultMatrixLayoutMode; slangSessionDesc.searchPathCount = desc.searchPathCount; slangSessionDesc.searchPaths = desc.searchPaths; - slangSessionDesc.preprocessorMacroCount = desc.preprocessorMacroCount; - slangSessionDesc.preprocessorMacros = desc.preprocessorMacros; + slangSessionDesc.preprocessorMacroCount = desc.preprocessorMacroCount + additionalMacros.getCount(); + Slang::List<slang::PreprocessorMacroDesc> macros; + macros.addRange(desc.preprocessorMacros, desc.preprocessorMacroCount); + macros.addRange(additionalMacros.getBuffer(), additionalMacros.getCount()); + slangSessionDesc.preprocessorMacros = macros.getBuffer(); slang::TargetDesc targetDesc = {}; targetDesc.format = compileTarget; auto targetProfile = desc.targetProfile; diff --git a/tools/gfx/vulkan/render-vk.cpp b/tools/gfx/vulkan/render-vk.cpp index 643c41394..d040fcebf 100644 --- a/tools/gfx/vulkan/render-vk.cpp +++ b/tools/gfx/vulkan/render-vk.cpp @@ -5120,7 +5120,11 @@ SlangResult VKDevice::initialize(const Desc& desc) SLANG_RETURN_ON_FAIL(m_deviceQueue.init(m_api, queue, m_queueFamilyIndex)); } - SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_SPIRV, "sm_5_1")); + SLANG_RETURN_ON_FAIL(slangContext.initialize( + desc.slang, + SLANG_SPIRV, + "sm_5_1", + makeArray(slang::PreprocessorMacroDesc{ "__VK__", "1" }).getView())); return SLANG_OK; } @@ -6469,4 +6473,4 @@ Result VKDevice::createComputePipelineState(const ComputePipelineStateDesc& inDe return SLANG_OK; } -} // renderer_test +} // renderer_test |
