From d832fb554d20c4a87724478ca175e2b22bec8a9b Mon Sep 17 00:00:00 2001 From: turanszkij Date: Thu, 5 Mar 2020 20:08:47 +0000 Subject: [PATCH] improvements for #90 --- WickedEngine/wiAudio.cpp | 22 +- WickedEngine/wiGraphicsDevice_DX11.cpp | 120 ++++--- WickedEngine/wiGraphicsDevice_DX12.cpp | 95 ++++-- WickedEngine/wiGraphicsDevice_Vulkan.cpp | 383 ++++++++++++----------- WickedEngine/wiGraphicsDevice_Vulkan.h | 1 + WickedEngine/wiNetwork_Windows.cpp | 12 +- WickedEngine/wiRawInput.cpp | 4 +- WickedEngine/wiVersion.cpp | 2 +- 8 files changed, 360 insertions(+), 279 deletions(-) diff --git a/WickedEngine/wiAudio.cpp b/WickedEngine/wiAudio.cpp index bb002410c..486de539a 100644 --- a/WickedEngine/wiAudio.cpp +++ b/WickedEngine/wiAudio.cpp @@ -177,6 +177,14 @@ namespace wiAudio sourceVoice->DestroyVoice(); } }; + SoundInternal* to_internal(const Sound* param) + { + return static_cast(param->internal_state.get()); + } + SoundInstanceInternal* to_internal(const SoundInstance* param) + { + return static_cast(param->internal_state.get()); + } void Initialize() { @@ -366,7 +374,7 @@ namespace wiAudio { if (instance != nullptr && instance->IsValid()) { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); HRESULT hr = instanceinternal->sourceVoice->Start(); assert(SUCCEEDED(hr)); } @@ -375,7 +383,7 @@ namespace wiAudio { if (instance != nullptr && instance->IsValid()) { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); HRESULT hr = instanceinternal->sourceVoice->Stop(); // preserves cursor position assert(SUCCEEDED(hr)); } @@ -384,7 +392,7 @@ namespace wiAudio { if (instance != nullptr && instance->IsValid()) { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); HRESULT hr = instanceinternal->sourceVoice->Stop(); // preserves cursor position assert(SUCCEEDED(hr)); hr = instanceinternal->sourceVoice->FlushSourceBuffers(); // reset submitted audio buffer @@ -402,7 +410,7 @@ namespace wiAudio } else { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); HRESULT hr = instanceinternal->sourceVoice->SetVolume(volume); assert(SUCCEEDED(hr)); } @@ -416,7 +424,7 @@ namespace wiAudio } else { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); instanceinternal->sourceVoice->GetVolume(&volume); } return volume; @@ -425,7 +433,7 @@ namespace wiAudio { if (instance != nullptr && instance->IsValid()) { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); HRESULT hr = instanceinternal->sourceVoice->ExitLoop(); assert(SUCCEEDED(hr)); } @@ -447,7 +455,7 @@ namespace wiAudio { if (instance != nullptr && instance->IsValid()) { - const auto& instanceinternal = std::static_pointer_cast(instance->internal_state); + auto instanceinternal = to_internal(instance); X3DAUDIO_LISTENER listener = {}; listener.Position = instance3D.listenerPos; diff --git a/WickedEngine/wiGraphicsDevice_DX11.cpp b/WickedEngine/wiGraphicsDevice_DX11.cpp index 50bed8856..cb2e1810f 100644 --- a/WickedEngine/wiGraphicsDevice_DX11.cpp +++ b/WickedEngine/wiGraphicsDevice_DX11.cpp @@ -10,7 +10,6 @@ #include #include -using namespace std; using namespace Microsoft::WRL; namespace wiGraphics @@ -1153,6 +1152,43 @@ namespace DX11_Internal std::shared_ptr device; ComPtr resource; }; + + Resource_DX11* to_internal(const GPUResource* param) + { + return static_cast(param->internal_state.get()); + } + Resource_DX11* to_internal(const GPUBuffer* param) + { + return static_cast(param->internal_state.get()); + } + Texture_DX11* to_internal(const Texture* param) + { + return static_cast(param->internal_state.get()); + } + InputLayout_DX11* to_internal(const InputLayout* param) + { + return static_cast(param->internal_state.get()); + } + BlendState_DX11* to_internal(const BlendState* param) + { + return static_cast(param->internal_state.get()); + } + DepthStencilState_DX11* to_internal(const DepthStencilState* param) + { + return static_cast(param->internal_state.get()); + } + RasterizerState_DX11* to_internal(const RasterizerState* param) + { + return static_cast(param->internal_state.get()); + } + Sampler_DX11* to_internal(const Sampler* param) + { + return static_cast(param->internal_state.get()); + } + Query_DX11* to_internal(const GPUQuery* param) + { + return static_cast(param->internal_state.get()); + } } using namespace DX11_Internal; @@ -1214,7 +1250,7 @@ GraphicsDevice_DX11::GraphicsDevice_DX11(wiPlatform::window_type window, bool fu } if (FAILED(hr)) { - stringstream ss(""); + std::stringstream ss(""); ss << "Failed to create the graphics device! ERROR: " << std::hex << hr; wiHelper::messageBox(ss.str(), "Error!"); exit(1); @@ -1855,7 +1891,7 @@ bool GraphicsDevice_DX11::CreateRenderPass(const RenderPassDesc* pDesc, RenderPa int GraphicsDevice_DX11::CreateSubresource(Texture* texture, SUBRESOURCE_TYPE type, uint32_t firstSlice, uint32_t sliceCount, uint32_t firstMip, uint32_t mipCount) { - const auto& internal_state = static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); switch (type) { @@ -2252,8 +2288,8 @@ int GraphicsDevice_DX11::CreateSubresource(Texture* texture, SUBRESOURCE_TYPE ty bool GraphicsDevice_DX11::DownloadResource(const GPUResource* resourceToDownload, const GPUResource* resourceDest, void* dataDest) { assert(resourceToDownload->type == resourceDest->type); - auto internal_state_src = static_pointer_cast(resourceToDownload->internal_state); - auto internal_state_dst = static_pointer_cast(resourceDest->internal_state); + auto internal_state_src = to_internal(resourceToDownload); + auto internal_state_dst = to_internal(resourceDest); if (resourceToDownload->IsBuffer()) { @@ -2334,7 +2370,7 @@ bool GraphicsDevice_DX11::DownloadResource(const GPUResource* resourceToDownload void GraphicsDevice_DX11::SetName(GPUResource* pResource, const std::string& name) { - const auto& internal_state = static_pointer_cast(pResource->internal_state); + auto internal_state = to_internal(pResource); internal_state->resource->SetPrivateData(WKPDID_D3DDebugObjectName, (uint32_t)name.length(), name.c_str()); } @@ -2454,7 +2490,7 @@ void GraphicsDevice_DX11::commit_allocations(CommandList cmd) if (frame_allocators[cmd].dirty) { - const auto& internal_state = static_pointer_cast(frame_allocators[cmd].buffer.internal_state); + auto internal_state = std::static_pointer_cast(frame_allocators[cmd].buffer.internal_state); deviceContexts[cmd]->Unmap(internal_state->resource.Get(), 0); frame_allocators[cmd].dirty = false; } @@ -2474,7 +2510,7 @@ void GraphicsDevice_DX11::RenderPassBegin(const RenderPass* renderpass, CommandL const RenderPassAttachment& attachment = desc.attachments[i]; const Texture* texture = attachment.texture; int subresource = attachment.subresource; - const auto& internal_state = static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); if (attachment.type == RenderPassAttachment::RENDERTARGET) { @@ -2568,7 +2604,7 @@ void GraphicsDevice_DX11::BindResource(SHADERSTAGE stage, const GPUResource* res { if (resource != nullptr && resource->IsValid()) { - const auto& internal_state = static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(resource); ID3D11ShaderResourceView* SRV; if (subresource < 0) @@ -2613,7 +2649,7 @@ void GraphicsDevice_DX11::BindResources(SHADERSTAGE stage, const GPUResource *co ID3D11ShaderResourceView* srvs[16]; for (uint32_t i = 0; i < count; ++i) { - srvs[i] = resources[i] != nullptr && resources[i]->IsValid() ? static_pointer_cast(resources[i]->internal_state)->srv.Get() : nullptr; + srvs[i] = resources[i] != nullptr && resources[i]->IsValid() ? to_internal(resources[i])->srv.Get() : nullptr; } switch (stage) @@ -2645,7 +2681,7 @@ void GraphicsDevice_DX11::BindUAV(SHADERSTAGE stage, const GPUResource* resource { if (resource != nullptr && resource->IsValid()) { - const auto& internal_state = static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(resource); ID3D11UnorderedAccessView* UAV; if (subresource < 0) @@ -2676,7 +2712,7 @@ void GraphicsDevice_DX11::BindUAVs(SHADERSTAGE stage, const GPUResource *const* ID3D11UnorderedAccessView* uavs[8]; for (uint32_t i = 0; i < count; ++i) { - uavs[i] = resources[i] != nullptr && resources[i]->IsValid() ? static_pointer_cast(resources[i]->internal_state)->uav.Get() : nullptr; + uavs[i] = resources[i] != nullptr && resources[i]->IsValid() ? to_internal(resources[i])->uav.Get() : nullptr; if(stage != CS) { @@ -2716,7 +2752,7 @@ void GraphicsDevice_DX11::BindSampler(SHADERSTAGE stage, const Sampler* sampler, { if (sampler != nullptr && sampler->IsValid()) { - const auto& internal_state = static_pointer_cast(sampler->internal_state); + auto internal_state = to_internal(sampler); ID3D11SamplerState* SAM = internal_state->resource.Get(); switch (stage) @@ -2747,7 +2783,7 @@ void GraphicsDevice_DX11::BindSampler(SHADERSTAGE stage, const Sampler* sampler, } void GraphicsDevice_DX11::BindConstantBuffer(SHADERSTAGE stage, const GPUBuffer* buffer, uint32_t slot, CommandList cmd) { - ID3D11Buffer* res = buffer != nullptr && buffer->IsValid() ? (ID3D11Buffer*)static_pointer_cast(buffer->internal_state)->resource.Get() : nullptr; + ID3D11Buffer* res = buffer != nullptr && buffer->IsValid() ? (ID3D11Buffer*)to_internal(buffer)->resource.Get() : nullptr; switch (stage) { case wiGraphics::VS: @@ -2779,13 +2815,13 @@ void GraphicsDevice_DX11::BindVertexBuffers(const GPUBuffer *const* vertexBuffer ID3D11Buffer* res[8] = { 0 }; for (uint32_t i = 0; i < count; ++i) { - res[i] = vertexBuffers[i] != nullptr && vertexBuffers[i]->IsValid() ? (ID3D11Buffer*)static_pointer_cast(vertexBuffers[i]->internal_state)->resource.Get() : nullptr; + res[i] = vertexBuffers[i] != nullptr && vertexBuffers[i]->IsValid() ? (ID3D11Buffer*)to_internal(vertexBuffers[i])->resource.Get() : nullptr; } deviceContexts[cmd]->IASetVertexBuffers(slot, count, res, strides, (offsets != nullptr ? offsets : reinterpret_cast(__nullBlob))); } void GraphicsDevice_DX11::BindIndexBuffer(const GPUBuffer* indexBuffer, const INDEXBUFFER_FORMAT format, uint32_t offset, CommandList cmd) { - ID3D11Buffer* res = indexBuffer != nullptr && indexBuffer->IsValid() ? (ID3D11Buffer*)static_pointer_cast(indexBuffer->internal_state)->resource.Get() : nullptr; + ID3D11Buffer* res = indexBuffer != nullptr && indexBuffer->IsValid() ? (ID3D11Buffer*)to_internal(indexBuffer)->resource.Get() : nullptr; deviceContexts[cmd]->IASetIndexBuffer(res, (format == INDEXBUFFER_FORMAT::INDEXFORMAT_16BIT ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT), offset); } void GraphicsDevice_DX11::BindStencilRef(uint32_t value, CommandList cmd) @@ -2803,38 +2839,38 @@ void GraphicsDevice_DX11::BindPipelineState(const PipelineState* pso, CommandLis { const PipelineStateDesc& desc = pso != nullptr ? pso->GetDesc() : PipelineStateDesc(); - ID3D11VertexShader* vs = desc.vs == nullptr ? nullptr : static_pointer_cast(desc.vs->internal_state)->resource.Get(); + ID3D11VertexShader* vs = desc.vs == nullptr ? nullptr : static_cast(desc.vs->internal_state.get())->resource.Get(); if (vs != prev_vs[cmd]) { deviceContexts[cmd]->VSSetShader(vs, nullptr, 0); prev_vs[cmd] = vs; } - ID3D11PixelShader* ps = desc.ps == nullptr ? nullptr : static_pointer_cast(desc.ps->internal_state)->resource.Get(); + ID3D11PixelShader* ps = desc.ps == nullptr ? nullptr : static_cast(desc.ps->internal_state.get())->resource.Get(); if (ps != prev_ps[cmd]) { deviceContexts[cmd]->PSSetShader(ps, nullptr, 0); prev_ps[cmd] = ps; } - ID3D11HullShader* hs = desc.hs == nullptr ? nullptr : static_pointer_cast(desc.hs->internal_state)->resource.Get(); + ID3D11HullShader* hs = desc.hs == nullptr ? nullptr : static_cast(desc.hs->internal_state.get())->resource.Get(); if (hs != prev_hs[cmd]) { deviceContexts[cmd]->HSSetShader(hs, nullptr, 0); prev_hs[cmd] = hs; } - ID3D11DomainShader* ds = desc.ds == nullptr ? nullptr : static_pointer_cast(desc.ds->internal_state)->resource.Get(); + ID3D11DomainShader* ds = desc.ds == nullptr ? nullptr : static_cast(desc.ds->internal_state.get())->resource.Get(); if (ds != prev_ds[cmd]) { deviceContexts[cmd]->DSSetShader(ds, nullptr, 0); prev_ds[cmd] = ds; } - ID3D11GeometryShader* gs = desc.gs == nullptr ? nullptr : static_pointer_cast(desc.gs->internal_state)->resource.Get(); + ID3D11GeometryShader* gs = desc.gs == nullptr ? nullptr : static_cast(desc.gs->internal_state.get())->resource.Get(); if (gs != prev_gs[cmd]) { deviceContexts[cmd]->GSSetShader(gs, nullptr, 0); prev_gs[cmd] = gs; } - ID3D11BlendState* bs = desc.bs == nullptr ? nullptr : static_pointer_cast(desc.bs->internal_state)->resource.Get(); + ID3D11BlendState* bs = desc.bs == nullptr ? nullptr : to_internal(desc.bs)->resource.Get(); if (bs != prev_bs[cmd] || desc.sampleMask != prev_samplemask[cmd] || blendFactor[cmd].x != prev_blendfactor[cmd].x || blendFactor[cmd].y != prev_blendfactor[cmd].y || @@ -2849,14 +2885,14 @@ void GraphicsDevice_DX11::BindPipelineState(const PipelineState* pso, CommandLis prev_samplemask[cmd] = desc.sampleMask; } - ID3D11RasterizerState* rs = desc.rs == nullptr ? nullptr : static_pointer_cast(desc.rs->internal_state)->resource.Get(); + ID3D11RasterizerState* rs = desc.rs == nullptr ? nullptr : to_internal(desc.rs)->resource.Get(); if (rs != prev_rs[cmd]) { deviceContexts[cmd]->RSSetState(rs); prev_rs[cmd] = rs; } - ID3D11DepthStencilState* dss = desc.dss == nullptr ? nullptr : static_pointer_cast(desc.dss->internal_state)->resource.Get(); + ID3D11DepthStencilState* dss = desc.dss == nullptr ? nullptr : to_internal(desc.dss)->resource.Get(); if (dss != prev_dss[cmd] || stencilRef[cmd] != prev_stencilRef[cmd]) { deviceContexts[cmd]->OMSetDepthStencilState(dss, stencilRef[cmd]); @@ -2864,7 +2900,7 @@ void GraphicsDevice_DX11::BindPipelineState(const PipelineState* pso, CommandLis prev_stencilRef[cmd] = stencilRef[cmd]; } - ID3D11InputLayout* il = desc.il == nullptr ? nullptr : static_pointer_cast(desc.il->internal_state)->resource.Get(); + ID3D11InputLayout* il = desc.il == nullptr ? nullptr : to_internal(desc.il)->resource.Get(); if (il != prev_il[cmd]) { deviceContexts[cmd]->IASetInputLayout(il); @@ -2905,7 +2941,7 @@ void GraphicsDevice_DX11::BindPipelineState(const PipelineState* pso, CommandLis } void GraphicsDevice_DX11::BindComputeShader(const Shader* cs, CommandList cmd) { - ID3D11ComputeShader* _cs = cs == nullptr ? nullptr : static_pointer_cast(cs->internal_state)->resource.Get(); + ID3D11ComputeShader* _cs = cs == nullptr ? nullptr : static_cast(cs->internal_state.get())->resource.Get(); if (_cs != prev_cs[cmd]) { deviceContexts[cmd]->CSSetShader(_cs, nullptr, 0); @@ -2940,13 +2976,13 @@ void GraphicsDevice_DX11::DrawInstancedIndirect(const GPUBuffer* args, uint32_t { commit_allocations(cmd); - deviceContexts[cmd]->DrawInstancedIndirect((ID3D11Buffer*)static_pointer_cast(args->internal_state)->resource.Get(), args_offset); + deviceContexts[cmd]->DrawInstancedIndirect((ID3D11Buffer*)to_internal(args)->resource.Get(), args_offset); } void GraphicsDevice_DX11::DrawIndexedInstancedIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { commit_allocations(cmd); - deviceContexts[cmd]->DrawIndexedInstancedIndirect((ID3D11Buffer*)static_pointer_cast(args->internal_state)->resource.Get(), args_offset); + deviceContexts[cmd]->DrawIndexedInstancedIndirect((ID3D11Buffer*)to_internal(args)->resource.Get(), args_offset); } void GraphicsDevice_DX11::Dispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, CommandList cmd) { @@ -2958,28 +2994,28 @@ void GraphicsDevice_DX11::DispatchIndirect(const GPUBuffer* args, uint32_t args_ { commit_allocations(cmd); - deviceContexts[cmd]->DispatchIndirect((ID3D11Buffer*)static_pointer_cast(args->internal_state)->resource.Get(), args_offset); + deviceContexts[cmd]->DispatchIndirect((ID3D11Buffer*)to_internal(args)->resource.Get(), args_offset); } void GraphicsDevice_DX11::CopyResource(const GPUResource* pDst, const GPUResource* pSrc, CommandList cmd) { assert(pDst != nullptr && pSrc != nullptr); - auto internal_state_src = static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal(pSrc); + auto internal_state_dst = to_internal(pDst); deviceContexts[cmd]->CopyResource(internal_state_dst->resource.Get(), internal_state_src->resource.Get()); } void GraphicsDevice_DX11::CopyTexture2D_Region(const Texture* pDst, uint32_t dstMip, uint32_t dstX, uint32_t dstY, const Texture* pSrc, uint32_t srcMip, CommandList cmd) { assert(pDst != nullptr && pSrc != nullptr); - auto internal_state_src = static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal(pSrc); + auto internal_state_dst = to_internal(pDst); deviceContexts[cmd]->CopySubresourceRegion(internal_state_dst->resource.Get(), D3D11CalcSubresource(dstMip, 0, pDst->GetDesc().MipLevels), dstX, dstY, 0, internal_state_src->resource.Get(), D3D11CalcSubresource(srcMip, 0, pSrc->GetDesc().MipLevels), nullptr); } void GraphicsDevice_DX11::MSAAResolve(const Texture* pDst, const Texture* pSrc, CommandList cmd) { assert(pDst != nullptr && pSrc != nullptr); - auto internal_state_src = static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal(pSrc); + auto internal_state_dst = to_internal(pDst); deviceContexts[cmd]->ResolveSubresource(internal_state_dst->resource.Get(), 0, internal_state_src->resource.Get(), 0, _ConvertFormat(pDst->desc.Format)); } void GraphicsDevice_DX11::UpdateBuffer(const GPUBuffer* buffer, const void* data, CommandList cmd, int dataSize) @@ -2992,7 +3028,7 @@ void GraphicsDevice_DX11::UpdateBuffer(const GPUBuffer* buffer, const void* data return; } - const auto& internal_state = static_pointer_cast(buffer->internal_state); + auto internal_state = to_internal(buffer); dataSize = std::min((int)buffer->desc.ByteWidth, dataSize); @@ -3023,19 +3059,19 @@ void GraphicsDevice_DX11::UpdateBuffer(const GPUBuffer* buffer, const void* data void GraphicsDevice_DX11::QueryBegin(const GPUQuery* query, CommandList cmd) { - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); deviceContexts[cmd]->Begin(internal_state->resource.Get()); } void GraphicsDevice_DX11::QueryEnd(const GPUQuery* query, CommandList cmd) { - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); deviceContexts[cmd]->End(internal_state->resource.Get()); } bool GraphicsDevice_DX11::QueryRead(const GPUQuery* query, GPUQueryResult* result) { const uint32_t _flags = D3D11_ASYNC_GETDATA_DONOTFLUSH; - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); ID3D11Query* QUERY = internal_state->resource.Get(); HRESULT hr = S_OK; @@ -3078,7 +3114,7 @@ GraphicsDevice::GPUAllocation GraphicsDevice_DX11::AllocateGPU(size_t dataSize, { return result; } - const auto& internal_state = static_pointer_cast(allocator.buffer.internal_state); + auto internal_state = std::static_pointer_cast(allocator.buffer.internal_state); allocator.dirty = true; @@ -3106,7 +3142,7 @@ GraphicsDevice::GPUAllocation GraphicsDevice_DX11::AllocateGPU(size_t dataSize, void GraphicsDevice_DX11::EventBegin(const std::string& name, CommandList cmd) { - userDefinedAnnotations[cmd]->BeginEvent(wstring(name.begin(), name.end()).c_str()); + userDefinedAnnotations[cmd]->BeginEvent(std::wstring(name.begin(), name.end()).c_str()); } void GraphicsDevice_DX11::EventEnd(CommandList cmd) { @@ -3114,7 +3150,7 @@ void GraphicsDevice_DX11::EventEnd(CommandList cmd) } void GraphicsDevice_DX11::SetMarker(const std::string& name, CommandList cmd) { - userDefinedAnnotations[cmd]->SetMarker(wstring(name.begin(),name.end()).c_str()); + userDefinedAnnotations[cmd]->SetMarker(std::wstring(name.begin(),name.end()).c_str()); } } diff --git a/WickedEngine/wiGraphicsDevice_DX12.cpp b/WickedEngine/wiGraphicsDevice_DX12.cpp index 5b1dab93c..ae6e04b49 100644 --- a/WickedEngine/wiGraphicsDevice_DX12.cpp +++ b/WickedEngine/wiGraphicsDevice_DX12.cpp @@ -24,7 +24,6 @@ // Uncomment this to enable DX12 renderpass feature: //#define DX12_REAL_RENDERPASS -using namespace std; using namespace Microsoft::WRL; namespace wiGraphics @@ -1016,6 +1015,35 @@ namespace DX12_Internal } }; + Resource_DX12* to_internal(const GPUResource* param) + { + return static_cast(param->internal_state.get()); + } + Resource_DX12* to_internal(const GPUBuffer* param) + { + return static_cast(param->internal_state.get()); + } + Texture_DX12* to_internal(const Texture* param) + { + return static_cast(param->internal_state.get()); + } + Sampler_DX12* to_internal(const Sampler* param) + { + return static_cast(param->internal_state.get()); + } + Query_DX12* to_internal(const GPUQuery* param) + { + return static_cast(param->internal_state.get()); + } + PipelineState_DX12* to_internal(const Shader* param) + { + assert(param->stage == CS); // only compute shader has pipeline state! + return static_cast(param->internal_state.get()); + } + PipelineState_DX12* to_internal(const PipelineState* param) + { + return static_cast(param->internal_state.get()); + } } using namespace DX12_Internal; @@ -1211,7 +1239,7 @@ using namespace DX12_Internal; { continue; } - const auto& internal_state = static_pointer_cast(buffer->internal_state); + auto internal_state = to_internal(buffer); D3D12_CPU_DESCRIPTOR_HANDLE dst = heap.start_cpu; dst.ptr += (heap.ringOffset + slot) * device->resource_descriptor_size; @@ -1224,7 +1252,7 @@ using namespace DX12_Internal; DynamicResourceState& state = it->second; state.binding[stage] = true; D3D12_CONSTANT_BUFFER_VIEW_DESC cbv; - cbv.BufferLocation = static_pointer_cast(state.allocation.buffer->internal_state)->resource->GetGPUVirtualAddress(); + cbv.BufferLocation = to_internal(state.allocation.buffer)->resource->GetGPUVirtualAddress(); cbv.BufferLocation += (D3D12_GPU_VIRTUAL_ADDRESS)state.allocation.offset; cbv.SizeInBytes = (uint32_t)Align((size_t)buffer->desc.ByteWidth, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT); @@ -1247,7 +1275,7 @@ using namespace DX12_Internal; { continue; } - const auto& internal_state = static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(resource); D3D12_CPU_DESCRIPTOR_HANDLE src = {}; if (subresource < 0) @@ -1277,7 +1305,7 @@ using namespace DX12_Internal; { continue; } - const auto& internal_state = static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(resource); D3D12_CPU_DESCRIPTOR_HANDLE src = {}; if (subresource < 0) @@ -1348,7 +1376,7 @@ using namespace DX12_Internal; { continue; } - const auto& internal_state = static_pointer_cast(sampler->internal_state); + auto internal_state = to_internal(sampler); D3D12_CPU_DESCRIPTOR_HANDLE src = internal_state->descriptor; @@ -1543,7 +1571,7 @@ using namespace DX12_Internal; hr = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)); if (FAILED(hr)) { - stringstream ss(""); + std::stringstream ss(""); ss << "Failed to create the graphics device! ERROR: " << std::hex << hr; wiHelper::messageBox(ss.str(), "Error!"); assert(0); @@ -2545,7 +2573,7 @@ using namespace DX12_Internal; int GraphicsDevice_DX12::CreateSubresource(Texture* texture, SUBRESOURCE_TYPE type, uint32_t firstSlice, uint32_t sliceCount, uint32_t firstMip, uint32_t mipCount) { - const auto& internal_state = static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); switch (type) { @@ -2923,8 +2951,8 @@ using namespace DX12_Internal; void GraphicsDevice_DX12::SetName(GPUResource* pResource, const std::string& name) { - const auto& internal_state = static_pointer_cast(pResource->internal_state); - internal_state->resource->SetName(wstring(name.begin(), name.end()).c_str()); + auto internal_state = to_internal(pResource); + internal_state->resource->SetName(std::wstring(name.begin(), name.end()).c_str()); } @@ -3301,7 +3329,7 @@ using namespace DX12_Internal; const RenderPassAttachment& attachment = desc.attachments[i]; const Texture* texture = attachment.texture; int subresource = attachment.subresource; - const auto& internal_state = static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); D3D12_CLEAR_VALUE clear_value; clear_value.Format = _ConvertFormat(texture->desc.Format); @@ -3411,7 +3439,7 @@ using namespace DX12_Internal; const RenderPassAttachment& attachment = desc.attachments[i]; const Texture* texture = attachment.texture; int subresource = attachment.subresource; - const auto& internal_state = static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); if (attachment.type == RenderPassAttachment::RENDERTARGET) { @@ -3478,7 +3506,7 @@ using namespace DX12_Internal; { continue; } - const auto& internal_state = static_pointer_cast(attachment.texture->internal_state); + auto internal_state = to_internal(attachment.texture); D3D12_RESOURCE_BARRIER& barrierdesc = barrierdescs[numBarriers++]; @@ -3599,7 +3627,7 @@ using namespace DX12_Internal; { if (vertexBuffers[i] != nullptr) { - res[i].BufferLocation = vertexBuffers[i]->IsValid() ? static_pointer_cast(vertexBuffers[i]->internal_state)->resource->GetGPUVirtualAddress() : 0; + res[i].BufferLocation = vertexBuffers[i]->IsValid() ? to_internal(vertexBuffers[i])->resource->GetGPUVirtualAddress() : 0; res[i].SizeInBytes = vertexBuffers[i]->desc.ByteWidth; if (offsets != nullptr) { @@ -3616,7 +3644,7 @@ using namespace DX12_Internal; D3D12_INDEX_BUFFER_VIEW res = {}; if (indexBuffer != nullptr) { - const auto& internal_state = static_pointer_cast(indexBuffer->internal_state); + auto internal_state = to_internal(indexBuffer); res.BufferLocation = internal_state->resource->GetGPUVirtualAddress() + (D3D12_GPU_VIRTUAL_ADDRESS)offset; res.Format = (format == INDEXBUFFER_FORMAT::INDEXFORMAT_16BIT ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT); @@ -3901,8 +3929,7 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::BindComputeShader(const Shader* cs, CommandList cmd) { - assert(cs->stage == CS); - const auto& internal_state = static_pointer_cast(cs->internal_state); + auto internal_state = to_internal(cs); prev_pipeline_hash[cmd] = 0; // note: same bind point for compute and graphics in dx12! GetDirectCommandList(cmd)->SetPipelineState(internal_state->resource.Get()); @@ -3929,13 +3956,13 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::DrawInstancedIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { - const auto& internal_state = static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); GetFrameResources().descriptors[cmd].validate(cmd); GetDirectCommandList(cmd)->ExecuteIndirect(drawInstancedIndirectCommandSignature.Get(), 1, internal_state->resource.Get(), args_offset, nullptr, 0); } void GraphicsDevice_DX12::DrawIndexedInstancedIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { - const auto& internal_state = static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); GetFrameResources().descriptors[cmd].validate(cmd); GetDirectCommandList(cmd)->ExecuteIndirect(drawIndexedInstancedIndirectCommandSignature.Get(), 1, internal_state->resource.Get(), args_offset, nullptr, 0); } @@ -3946,20 +3973,20 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::DispatchIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { - const auto& internal_state = static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); GetFrameResources().descriptors[cmd].validate(cmd); GetDirectCommandList(cmd)->ExecuteIndirect(dispatchIndirectCommandSignature.Get(), 1, internal_state->resource.Get(), args_offset, nullptr, 0); } void GraphicsDevice_DX12::CopyResource(const GPUResource* pDst, const GPUResource* pSrc, CommandList cmd) { - auto internal_state_src = static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal(pSrc); + auto internal_state_dst = to_internal(pDst); GetDirectCommandList(cmd)->CopyResource(internal_state_dst->resource.Get(), internal_state_src->resource.Get()); } void GraphicsDevice_DX12::CopyTexture2D_Region(const Texture* pDst, uint32_t dstMip, uint32_t dstX, uint32_t dstY, const Texture* pSrc, uint32_t srcMip, CommandList cmd) { - auto internal_state_src = static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal(pSrc); + auto internal_state_dst = to_internal(pDst); D3D12_RESOURCE_DESC src_desc = internal_state_src->resource->GetDesc(); D3D12_RESOURCE_DESC dst_desc = internal_state_dst->resource->GetDesc(); @@ -4017,8 +4044,8 @@ using namespace DX12_Internal; else { // Contents will be transferred to device memory: - auto internal_state_src = static_pointer_cast(GetFrameResources().resourceBuffer[cmd].buffer.internal_state); - auto internal_state_dst = static_pointer_cast(buffer->internal_state); + auto internal_state_src = std::static_pointer_cast(GetFrameResources().resourceBuffer[cmd].buffer.internal_state); + auto internal_state_dst = to_internal(buffer); D3D12_RESOURCE_BARRIER barrier = {}; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; @@ -4054,7 +4081,7 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::QueryBegin(const GPUQuery* query, CommandList cmd) { - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); switch (query->desc.Type) { @@ -4071,7 +4098,7 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::QueryEnd(const GPUQuery* query, CommandList cmd) { - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); switch (query->desc.Type) { @@ -4091,7 +4118,7 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::QueryRead(const GPUQuery* query, GPUQueryResult* result) { - const auto& internal_state = static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); D3D12_RANGE range; range.Begin = (size_t)internal_state->query_index * sizeof(size_t); @@ -4146,14 +4173,14 @@ using namespace DX12_Internal; { barrierdesc.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; barrierdesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrierdesc.UAV.pResource = barrier.memory.resource == nullptr ? nullptr : static_pointer_cast(barrier.memory.resource->internal_state)->resource.Get(); + barrierdesc.UAV.pResource = barrier.memory.resource == nullptr ? nullptr : to_internal(barrier.memory.resource)->resource.Get(); } break; case GPUBarrier::IMAGE_BARRIER: { barrierdesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrierdesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrierdesc.Transition.pResource = static_pointer_cast(barrier.image.texture->internal_state)->resource.Get(); + barrierdesc.Transition.pResource = to_internal(barrier.image.texture)->resource.Get(); barrierdesc.Transition.StateBefore = _ConvertImageLayout(barrier.image.layout_before); barrierdesc.Transition.StateAfter = _ConvertImageLayout(barrier.image.layout_after); barrierdesc.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; @@ -4163,7 +4190,7 @@ using namespace DX12_Internal; { barrierdesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrierdesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrierdesc.Transition.pResource = static_pointer_cast(barrier.buffer.buffer->internal_state)->resource.Get(); + barrierdesc.Transition.pResource = to_internal(barrier.buffer.buffer)->resource.Get(); barrierdesc.Transition.StateBefore = _ConvertBufferState(barrier.buffer.state_before); barrierdesc.Transition.StateAfter = _ConvertBufferState(barrier.buffer.state_after); barrierdesc.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; @@ -4200,7 +4227,7 @@ using namespace DX12_Internal; void GraphicsDevice_DX12::EventBegin(const std::string& name, CommandList cmd) { - PIXBeginEvent(GetDirectCommandList(cmd), 0xFF000000, wstring(name.begin(), name.end()).c_str()); + PIXBeginEvent(GetDirectCommandList(cmd), 0xFF000000, std::wstring(name.begin(), name.end()).c_str()); } void GraphicsDevice_DX12::EventEnd(CommandList cmd) { @@ -4208,7 +4235,7 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::SetMarker(const std::string& name, CommandList cmd) { - PIXSetMarker(GetDirectCommandList(cmd), 0xFFFF0000, wstring(name.begin(), name.end()).c_str()); + PIXSetMarker(GetDirectCommandList(cmd), 0xFFFF0000, std::wstring(name.begin(), name.end()).c_str()); } diff --git a/WickedEngine/wiGraphicsDevice_Vulkan.cpp b/WickedEngine/wiGraphicsDevice_Vulkan.cpp index 9d75c36de..3813beac3 100644 --- a/WickedEngine/wiGraphicsDevice_Vulkan.cpp +++ b/WickedEngine/wiGraphicsDevice_Vulkan.cpp @@ -862,6 +862,22 @@ namespace Vulkan_Internal } } }; + struct Shader_Vulkan + { + std::shared_ptr device; + VkShaderModule shaderModule = VK_NULL_HANDLE; + VkPipeline pipeline_cs = VK_NULL_HANDLE; + VkPipelineShaderStageCreateInfo stageInfo = {}; + + ~Shader_Vulkan() + { + uint64_t framecount = device->GetFrameCount(); + device->destroylocker.lock(); + if (shaderModule) device->destroyer_shadermodules.push_back(std::make_pair(shaderModule, framecount)); + if (pipeline_cs) device->destroyer_pipelines.push_back(std::make_pair(pipeline_cs, framecount)); + device->destroylocker.unlock(); + } + }; struct PipelineState_Vulkan { std::shared_ptr device; @@ -880,6 +896,8 @@ namespace Vulkan_Internal std::shared_ptr device; VkRenderPass renderpass = VK_NULL_HANDLE; VkFramebuffer framebuffer = VK_NULL_HANDLE; + VkRenderPassBeginInfo beginInfo; + VkClearValue clearColors[9] = {}; ~RenderPass_Vulkan() { @@ -890,6 +908,35 @@ namespace Vulkan_Internal device->destroylocker.unlock(); } }; + + Buffer_Vulkan* to_internal(const GPUBuffer* param) + { + return static_cast(param->internal_state.get()); + } + Texture_Vulkan* to_internal(const Texture* param) + { + return static_cast(param->internal_state.get()); + } + Sampler_Vulkan* to_internal(const Sampler* param) + { + return static_cast(param->internal_state.get()); + } + Query_Vulkan* to_internal(const GPUQuery* param) + { + return static_cast(param->internal_state.get()); + } + Shader_Vulkan* to_internal(const Shader* param) + { + return static_cast(param->internal_state.get()); + } + PipelineState_Vulkan* to_internal(const PipelineState* param) + { + return static_cast(param->internal_state.get()); + } + RenderPass_Vulkan* to_internal(const RenderPass* param) + { + return static_cast(param->internal_state.get()); + } } using namespace Vulkan_Internal; @@ -1290,7 +1337,7 @@ using namespace Vulkan_Internal; { continue; } - const auto& internal_state = std::static_pointer_cast(buffer->internal_state); + auto internal_state = to_internal(buffer); bufferInfos[writeCount] = {}; bufferInfos[writeCount].range = buffer->desc.ByteWidth; @@ -1301,7 +1348,7 @@ using namespace Vulkan_Internal; if (it != device->dynamic_constantbuffers[cmd].end()) { DynamicResourceState& state = it->second; - bufferInfos[writeCount].buffer = std::static_pointer_cast(state.allocation.buffer->internal_state)->resource; + bufferInfos[writeCount].buffer = to_internal(state.allocation.buffer)->resource; bufferInfos[writeCount].offset = state.allocation.offset; state.binding[stage] = true; } @@ -1343,7 +1390,8 @@ using namespace Vulkan_Internal; if (resource->IsTexture()) { // Texture: - const auto& internal_state = std::static_pointer_cast(resource->internal_state); + const Texture* texture = (const Texture*)resource; + auto internal_state = to_internal(texture); const uint32_t binding = VULKAN_DESCRIPTOR_SET_OFFSET_SRV_TEXTURE + slot; @@ -1368,7 +1416,7 @@ using namespace Vulkan_Internal; { // Buffer: const GPUBuffer* buffer = (const GPUBuffer*)resource; - const auto& internal_state = std::static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(buffer); if (buffer->desc.Format == FORMAT_UNKNOWN) { @@ -1432,7 +1480,8 @@ using namespace Vulkan_Internal; { // Texture: const uint32_t binding = VULKAN_DESCRIPTOR_SET_OFFSET_UAV_TEXTURE + slot; - const auto& internal_state = std::static_pointer_cast(resource->internal_state); + const Texture* texture = (const Texture*)resource; + auto internal_state = to_internal(texture); imageInfos[writeCount] = {}; imageInfos[writeCount].imageView = subresource < 0 ? internal_state->uav : internal_state->subresources_uav[subresource]; @@ -1455,7 +1504,7 @@ using namespace Vulkan_Internal; { // Buffer: const GPUBuffer* buffer = (const GPUBuffer*)resource; - const auto& internal_state = std::static_pointer_cast(resource->internal_state); + auto internal_state = to_internal(buffer); if (buffer->desc.Format == FORMAT_UNKNOWN) { @@ -1514,7 +1563,7 @@ using namespace Vulkan_Internal; { continue; } - const auto& internal_state = std::static_pointer_cast(sampler->internal_state); + auto internal_state = to_internal(sampler); imageInfos[writeCount] = {}; imageInfos[writeCount].imageView = VK_NULL_HANDLE; @@ -2379,6 +2428,12 @@ using namespace Vulkan_Internal; vkDestroyQueryPool(device, querypool_timestamp, nullptr); vkDestroyQueryPool(device, querypool_occlusion, nullptr); + vkDestroyBuffer(device, nullBuffer, nullptr); + vkDestroyBufferView(device, nullBufferView, nullptr); + vkDestroyImage(device, nullImage, nullptr); + vkDestroyImageView(device, nullImageView, nullptr); + vkDestroySampler(device, nullSampler, nullptr); + vmaDestroyAllocator(allocator); vkDestroyDevice(device, nullptr); @@ -2818,7 +2873,9 @@ using namespace Vulkan_Internal; } bool GraphicsDevice_Vulkan::CreateShader(SHADERSTAGE stage, const void *pShaderBytecode, size_t BytecodeLength, Shader *pShader) { - pShader->internal_state = shared_from_this(); + auto internal_state = std::make_shared(); + internal_state->device = shared_from_this(); + pShader->internal_state = internal_state; pShader->code.resize(BytecodeLength); std::memcpy(pShader->code.data(), pShaderBytecode, BytecodeLength); @@ -2826,12 +2883,43 @@ using namespace Vulkan_Internal; VkResult res = VK_SUCCESS; + VkShaderModuleCreateInfo moduleInfo = {}; + moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + moduleInfo.codeSize = pShader->code.size(); + moduleInfo.pCode = reinterpret_cast(pShader->code.data()); + res = vkCreateShaderModule(device, &moduleInfo, nullptr, &internal_state->shaderModule); + assert(res == VK_SUCCESS); + + internal_state->stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + internal_state->stageInfo.module = internal_state->shaderModule; + internal_state->stageInfo.pName = "main"; + switch (stage) + { + case wiGraphics::VS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; + break; + case wiGraphics::HS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; + break; + case wiGraphics::DS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; + break; + case wiGraphics::GS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_GEOMETRY_BIT; + break; + case wiGraphics::PS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; + break; + case wiGraphics::CS: + internal_state->stageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; + break; + default: + assert(0); + break; + } + if (stage == CS) { - auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); - pShader->internal_state = internal_state; - VkComputePipelineCreateInfo pipelineInfo = {}; pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; pipelineInfo.layout = defaultPipelineLayout_Compute; @@ -2839,27 +2927,10 @@ using namespace Vulkan_Internal; // Create compute pipeline state in place: - - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; - - VkShaderModule shaderModule = {}; - moduleInfo.codeSize = pShader->code.size(); - moduleInfo.pCode = reinterpret_cast(pShader->code.data()); - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - pipelineInfo.stage = stageInfo; + pipelineInfo.stage = internal_state->stageInfo; - res = vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &internal_state->resource); + res = vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &internal_state->pipeline_cs); assert(res == VK_SUCCESS); } @@ -3171,7 +3242,7 @@ using namespace Vulkan_Internal; const Texture* texture = desc.attachments[i].texture; const TextureDesc& texdesc = texture->desc; int subresource = desc.attachments[i].subresource; - auto texture_internal_state = std::static_pointer_cast(texture->internal_state); + auto texture_internal_state = to_internal(texture); attachmentDescriptions[validAttachmentCount].format = _ConvertFormat(texdesc.Format); attachmentDescriptions[validAttachmentCount].samples = (VkSampleCountFlagBits)texdesc.SampleCount; @@ -3316,12 +3387,49 @@ using namespace Vulkan_Internal; res = vkCreateFramebuffer(device, &framebufferInfo, nullptr, &internal_state->framebuffer); assert(res == VK_SUCCESS); + + internal_state->beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; + internal_state->beginInfo.renderPass = internal_state->renderpass; + internal_state->beginInfo.framebuffer = internal_state->framebuffer; + + if (renderpass->desc.numAttachments > 0) + { + const TextureDesc& texdesc = desc.attachments[0].texture->desc; + + internal_state->beginInfo.renderArea.offset = { 0, 0 }; + internal_state->beginInfo.renderArea.extent.width = texdesc.Width; + internal_state->beginInfo.renderArea.extent.height = texdesc.Height; + internal_state->beginInfo.clearValueCount = renderpass->desc.numAttachments; + internal_state->beginInfo.pClearValues = internal_state->clearColors; + + for (uint32_t i = 0; i < desc.numAttachments; ++i) + { + const ClearValue& clear = desc.attachments[i].texture->desc.clear; + if (desc.attachments[i].type == RenderPassAttachment::RENDERTARGET) + { + internal_state->clearColors[i].color.float32[0] = clear.color[0]; + internal_state->clearColors[i].color.float32[1] = clear.color[1]; + internal_state->clearColors[i].color.float32[2] = clear.color[2]; + internal_state->clearColors[i].color.float32[3] = clear.color[3]; + } + else if (desc.attachments[i].type == RenderPassAttachment::DEPTH_STENCIL) + { + internal_state->clearColors[i].depthStencil.depth = clear.depthstencil.depth; + internal_state->clearColors[i].depthStencil.stencil = clear.depthstencil.stencil; + } + else + { + assert(0); + } + } + } + return res == VK_SUCCESS ? true : false; } int GraphicsDevice_Vulkan::CreateSubresource(Texture* texture, SUBRESOURCE_TYPE type, uint32_t firstSlice, uint32_t sliceCount, uint32_t firstMip, uint32_t mipCount) { - const auto& internal_state = std::static_pointer_cast(texture->internal_state); + auto internal_state = to_internal(texture); VkImageViewCreateInfo view_desc = {}; view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; @@ -3521,12 +3629,12 @@ using namespace Vulkan_Internal; if (pResource->IsTexture()) { info.objectType = VK_OBJECT_TYPE_IMAGE; - info.objectHandle = (uint64_t)std::static_pointer_cast(pResource->internal_state)->resource; + info.objectHandle = (uint64_t)to_internal((const Texture*)pResource)->resource; } else if (pResource->IsBuffer()) { info.objectType = VK_OBJECT_TYPE_BUFFER; - info.objectHandle = (uint64_t)std::static_pointer_cast(pResource->internal_state)->resource; + info.objectHandle = (uint64_t)to_internal((const GPUBuffer*)pResource)->resource; } VkResult res = setDebugUtilsObjectNameEXT(device, &info); @@ -3786,6 +3894,19 @@ using namespace Vulkan_Internal; break; } } + while (!destroyer_shadermodules.empty()) + { + if (destroyer_shadermodules.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_shadermodules.front(); + destroyer_shadermodules.pop_front(); + vkDestroyShaderModule(device, item.first, nullptr); + } + else + { + break; + } + } while (!destroyer_pipelines.empty()) { if (destroyer_pipelines.front().second + BACKBUFFER_COUNT < FRAMECOUNT) @@ -4002,51 +4123,10 @@ using namespace Vulkan_Internal; void GraphicsDevice_Vulkan::RenderPassBegin(const RenderPass* renderpass, CommandList cmd) { - const auto& internal_state = std::static_pointer_cast(renderpass->internal_state); - active_renderpass[cmd] = renderpass; - VkRenderPassBeginInfo renderPassInfo = {}; - renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - renderPassInfo.renderPass = internal_state->renderpass; - renderPassInfo.framebuffer = internal_state->framebuffer; - - const RenderPassDesc& desc = renderpass->desc; - - VkClearValue clearColors[9] = {}; - if (renderpass->desc.numAttachments > 0) - { - const TextureDesc& texdesc = desc.attachments[0].texture->desc; - - renderPassInfo.renderArea.offset = { 0, 0 }; - renderPassInfo.renderArea.extent.width = texdesc.Width; - renderPassInfo.renderArea.extent.height = texdesc.Height; - renderPassInfo.clearValueCount = renderpass->desc.numAttachments; - renderPassInfo.pClearValues = clearColors; - - for (uint32_t i = 0; i < desc.numAttachments; ++i) - { - const ClearValue& clear = desc.attachments[i].texture->desc.clear; - if (desc.attachments[i].type == RenderPassAttachment::RENDERTARGET) - { - clearColors[i].color.float32[0] = clear.color[0]; - clearColors[i].color.float32[1] = clear.color[1]; - clearColors[i].color.float32[2] = clear.color[2]; - clearColors[i].color.float32[3] = clear.color[3]; - } - else if(desc.attachments[i].type == RenderPassAttachment::DEPTH_STENCIL) - { - clearColors[i].depthStencil.depth = clear.depthstencil.depth; - clearColors[i].depthStencil.stencil = clear.depthstencil.stencil; - } - else - { - assert(0); - } - } - } - - vkCmdBeginRenderPass(GetDirectCommandList(cmd), &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); + auto internal_state = to_internal(renderpass); + vkCmdBeginRenderPass(GetDirectCommandList(cmd), &internal_state->beginInfo, VK_SUBPASS_CONTENTS_INLINE); } void GraphicsDevice_Vulkan::RenderPassEnd(CommandList cmd) { @@ -4162,7 +4242,7 @@ using namespace Vulkan_Internal; } else { - const auto& internal_state = std::static_pointer_cast(vertexBuffers[i]->internal_state); + auto internal_state = to_internal(vertexBuffers[i]); vbuffers[i] = internal_state->resource; if (offsets != nullptr) { @@ -4177,7 +4257,7 @@ using namespace Vulkan_Internal; { if (indexBuffer != nullptr) { - const auto& internal_state = std::static_pointer_cast(indexBuffer->internal_state); + auto internal_state = to_internal(indexBuffer); vkCmdBindIndexBuffer(GetDirectCommandList(cmd), internal_state->resource, (VkDeviceSize)offset, format == INDEXFORMAT_16BIT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); } } @@ -4224,111 +4304,36 @@ using namespace Vulkan_Internal; VkGraphicsPipelineCreateInfo pipelineInfo = {}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.layout = defaultPipelineLayout_Graphics; - pipelineInfo.renderPass = active_renderpass[cmd] == nullptr ? defaultRenderPass : std::static_pointer_cast(active_renderpass[cmd]->internal_state)->renderpass; + pipelineInfo.renderPass = active_renderpass[cmd] == nullptr ? defaultRenderPass : to_internal(active_renderpass[cmd])->renderpass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // Shaders: - std::vector shaderStages; - - if (pso->desc.vs != nullptr && !pso->desc.vs->code.empty()) + uint32_t shaderStageCount = 0; + VkPipelineShaderStageCreateInfo shaderStages[SHADERSTAGE_COUNT - 1]; + if (pso->desc.vs != nullptr && pso->desc.vs->IsValid()) { - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - moduleInfo.codeSize = pso->desc.vs->code.size(); - moduleInfo.pCode = reinterpret_cast(pso->desc.vs->code.data()); - VkShaderModule shaderModule; - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - shaderStages.push_back(stageInfo); + shaderStages[shaderStageCount++] = to_internal(pso->desc.vs)->stageInfo; } - - if (pso->desc.hs != nullptr && !pso->desc.hs->code.empty()) + if (pso->desc.hs != nullptr && pso->desc.hs->IsValid()) { - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - moduleInfo.codeSize = pso->desc.hs->code.size(); - moduleInfo.pCode = reinterpret_cast(pso->desc.hs->code.data()); - VkShaderModule shaderModule; - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - shaderStages.push_back(stageInfo); + shaderStages[shaderStageCount++] = to_internal(pso->desc.hs)->stageInfo; } - - if (pso->desc.ds != nullptr && !pso->desc.ds->code.empty()) + if (pso->desc.ds != nullptr && pso->desc.ds->IsValid()) { - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - moduleInfo.codeSize = pso->desc.ds->code.size(); - moduleInfo.pCode = reinterpret_cast(pso->desc.ds->code.data()); - VkShaderModule shaderModule; - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - shaderStages.push_back(stageInfo); + shaderStages[shaderStageCount++] = to_internal(pso->desc.ds)->stageInfo; } - - if (pso->desc.gs != nullptr && !pso->desc.gs->code.empty()) + if (pso->desc.gs != nullptr && pso->desc.gs->IsValid()) { - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - moduleInfo.codeSize = pso->desc.gs->code.size(); - moduleInfo.pCode = reinterpret_cast(pso->desc.gs->code.data()); - VkShaderModule shaderModule; - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_GEOMETRY_BIT; - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - shaderStages.push_back(stageInfo); + shaderStages[shaderStageCount++] = to_internal(pso->desc.gs)->stageInfo; } - - if (pso->desc.ps != nullptr && !pso->desc.ps->code.empty()) + if (pso->desc.ps != nullptr && pso->desc.ps->IsValid()) { - VkShaderModuleCreateInfo moduleInfo = {}; - moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - moduleInfo.codeSize = pso->desc.ps->code.size(); - moduleInfo.pCode = reinterpret_cast(pso->desc.ps->code.data()); - VkShaderModule shaderModule; - res = vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule); - assert(res == VK_SUCCESS); - - VkPipelineShaderStageCreateInfo stageInfo = {}; - stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; - stageInfo.module = shaderModule; - stageInfo.pName = "main"; - - shaderStages.push_back(stageInfo); + shaderStages[shaderStageCount++] = to_internal(pso->desc.ps)->stageInfo; } - - pipelineInfo.stageCount = static_cast(shaderStages.size()); - pipelineInfo.pStages = shaderStages.data(); + pipelineInfo.stageCount = shaderStageCount; + pipelineInfo.pStages = shaderStages; // Fixed function states: @@ -4662,8 +4667,8 @@ using namespace Vulkan_Internal; void GraphicsDevice_Vulkan::BindComputeShader(const Shader* cs, CommandList cmd) { assert(cs->stage == CS); - const auto& internal_state = std::static_pointer_cast(cs->internal_state); - vkCmdBindPipeline(GetDirectCommandList(cmd), VK_PIPELINE_BIND_POINT_COMPUTE, internal_state->resource); + auto internal_state = to_internal(cs); + vkCmdBindPipeline(GetDirectCommandList(cmd), VK_PIPELINE_BIND_POINT_COMPUTE, internal_state->pipeline_cs); } void GraphicsDevice_Vulkan::Draw(uint32_t vertexCount, uint32_t startVertexLocation, CommandList cmd) { @@ -4688,13 +4693,13 @@ using namespace Vulkan_Internal; void GraphicsDevice_Vulkan::DrawInstancedIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { GetFrameResources().descriptors[cmd].validate(cmd); - const auto& internal_state = std::static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); vkCmdDrawIndirect(GetDirectCommandList(cmd), internal_state->resource, (VkDeviceSize)args_offset, 1, (uint32_t)sizeof(IndirectDrawArgsInstanced)); } void GraphicsDevice_Vulkan::DrawIndexedInstancedIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { GetFrameResources().descriptors[cmd].validate(cmd); - const auto& internal_state = std::static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); vkCmdDrawIndexedIndirect(GetDirectCommandList(cmd), internal_state->resource, (VkDeviceSize)args_offset, 1, (uint32_t)sizeof(IndirectDrawArgsIndexedInstanced)); } void GraphicsDevice_Vulkan::Dispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, CommandList cmd) @@ -4705,18 +4710,18 @@ using namespace Vulkan_Internal; void GraphicsDevice_Vulkan::DispatchIndirect(const GPUBuffer* args, uint32_t args_offset, CommandList cmd) { GetFrameResources().descriptors[cmd].validate(cmd); - const auto& internal_state = std::static_pointer_cast(args->internal_state); + auto internal_state = to_internal(args); vkCmdDispatchIndirect(GetDirectCommandList(cmd), internal_state->resource, (VkDeviceSize)args_offset); } void GraphicsDevice_Vulkan::CopyResource(const GPUResource* pDst, const GPUResource* pSrc, CommandList cmd) { if (pDst->type == GPUResource::GPU_RESOURCE_TYPE::TEXTURE && pSrc->type == GPUResource::GPU_RESOURCE_TYPE::TEXTURE) { - auto internal_state_src = std::static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = std::static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal((const Texture*)pSrc); + auto internal_state_dst = to_internal((const Texture*)pDst); - const TextureDesc& src_desc = ((Texture*)pSrc)->GetDesc(); - const TextureDesc& dst_desc = ((Texture*)pDst)->GetDesc(); + const TextureDesc& src_desc = ((const Texture*)pSrc)->GetDesc(); + const TextureDesc& dst_desc = ((const Texture*)pDst)->GetDesc(); VkImageCopy copy = {}; copy.extent.width = dst_desc.Width; @@ -4771,11 +4776,11 @@ using namespace Vulkan_Internal; } else if (pDst->type == GPUResource::GPU_RESOURCE_TYPE::BUFFER && pSrc->type == GPUResource::GPU_RESOURCE_TYPE::BUFFER) { - auto internal_state_src = std::static_pointer_cast(pSrc->internal_state); - auto internal_state_dst = std::static_pointer_cast(pDst->internal_state); + auto internal_state_src = to_internal((const GPUBuffer*)pSrc); + auto internal_state_dst = to_internal((const GPUBuffer*)pDst); - const GPUBufferDesc& src_desc = ((GPUBuffer*)pSrc)->GetDesc(); - const GPUBufferDesc& dst_desc = ((GPUBuffer*)pDst)->GetDesc(); + const GPUBufferDesc& src_desc = ((const GPUBuffer*)pSrc)->GetDesc(); + const GPUBufferDesc& dst_desc = ((const GPUBuffer*)pDst)->GetDesc(); VkBufferCopy copy = {}; copy.srcOffset = 0; @@ -4804,7 +4809,7 @@ using namespace Vulkan_Internal; { return; } - const auto& internal_state = std::static_pointer_cast(buffer->internal_state); + auto internal_state = to_internal(buffer); dataSize = std::min((int)buffer->desc.ByteWidth, dataSize); dataSize = (dataSize >= 0 ? dataSize : buffer->desc.ByteWidth); @@ -4910,7 +4915,7 @@ using namespace Vulkan_Internal; } void GraphicsDevice_Vulkan::QueryBegin(const GPUQuery *query, CommandList cmd) { - const auto& internal_state = std::static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); switch (query->desc.Type) { @@ -4924,7 +4929,7 @@ using namespace Vulkan_Internal; } void GraphicsDevice_Vulkan::QueryEnd(const GPUQuery *query, CommandList cmd) { - const auto& internal_state = std::static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); switch (query->desc.Type) { @@ -4941,7 +4946,7 @@ using namespace Vulkan_Internal; } bool GraphicsDevice_Vulkan::QueryRead(const GPUQuery* query, GPUQueryResult* result) { - const auto& internal_state = std::static_pointer_cast(query->internal_state); + auto internal_state = to_internal(query); VkResult res = VK_SUCCESS; @@ -4996,7 +5001,7 @@ using namespace Vulkan_Internal; case GPUBarrier::IMAGE_BARRIER: { const TextureDesc& desc = barrier.image.texture->GetDesc(); - const auto& internal_state = std::static_pointer_cast(barrier.image.texture->internal_state); + auto internal_state = to_internal(barrier.image.texture); VkImageMemoryBarrier& barrierdesc = imagebarriers[imagebarrier_count++]; barrierdesc.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -5028,7 +5033,7 @@ using namespace Vulkan_Internal; break; case GPUBarrier::BUFFER_BARRIER: { - const auto& internal_state = std::static_pointer_cast(barrier.buffer.buffer->internal_state); + auto internal_state = to_internal(barrier.buffer.buffer); VkBufferMemoryBarrier& barrierdesc = bufferbarriers[bufferbarrier_count++]; barrierdesc.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; diff --git a/WickedEngine/wiGraphicsDevice_Vulkan.h b/WickedEngine/wiGraphicsDevice_Vulkan.h index f4f2c34ac..42b087198 100644 --- a/WickedEngine/wiGraphicsDevice_Vulkan.h +++ b/WickedEngine/wiGraphicsDevice_Vulkan.h @@ -318,6 +318,7 @@ namespace wiGraphics std::deque, uint64_t>> destroyer_buffers; std::deque> destroyer_bufferviews; std::deque> destroyer_samplers; + std::deque> destroyer_shadermodules; std::deque> destroyer_pipelines; std::deque> destroyer_renderpasses; std::deque> destroyer_framebuffers; diff --git a/WickedEngine/wiNetwork_Windows.cpp b/WickedEngine/wiNetwork_Windows.cpp index f4cb5cc40..6c00aa4f0 100644 --- a/WickedEngine/wiNetwork_Windows.cpp +++ b/WickedEngine/wiNetwork_Windows.cpp @@ -35,6 +35,10 @@ namespace wiNetwork } } }; + SocketInternal* to_internal(const Socket* param) + { + return static_cast(param->internal_state.get()); + } void Initialize() { @@ -86,7 +90,7 @@ namespace wiNetwork target.sin_addr.S_un.S_un_b.s_b3 = connection->ipaddress[2]; target.sin_addr.S_un.S_un_b.s_b4 = connection->ipaddress[3]; - const auto& socketinternal = std::static_pointer_cast(sock->internal_state); + auto socketinternal = to_internal(sock); int result = sendto(socketinternal->handle, (const char*)data, (int)dataSize, 0, (const sockaddr*)& target, sizeof(target)); if (result == SOCKET_ERROR) @@ -112,7 +116,7 @@ namespace wiNetwork target.sin_port = htons(port); target.sin_addr.S_un.S_addr = htonl(INADDR_ANY); - const auto& socketinternal = std::static_pointer_cast(sock->internal_state); + auto socketinternal = to_internal(sock); int result = bind(socketinternal->handle, (const sockaddr*)& target, sizeof(target)); if (result == SOCKET_ERROR) @@ -133,7 +137,7 @@ namespace wiNetwork { if (socket != nullptr && sock->IsValid()) { - const auto& socketinternal = std::static_pointer_cast(sock->internal_state); + auto socketinternal = to_internal(sock); fd_set readfds; FD_ZERO(&readfds); @@ -160,7 +164,7 @@ namespace wiNetwork { if (socket != nullptr && sock->IsValid()) { - const auto& socketinternal = std::static_pointer_cast(sock->internal_state); + auto socketinternal = to_internal(sock); sockaddr_in sender; int targetsize = sizeof(sender); diff --git a/WickedEngine/wiRawInput.cpp b/WickedEngine/wiRawInput.cpp index 1a9fb5af5..8e408dab0 100644 --- a/WickedEngine/wiRawInput.cpp +++ b/WickedEngine/wiRawInput.cpp @@ -442,7 +442,7 @@ namespace wiRawInput } bool GetControllerState(wiInput::ControllerState* state, int index) { - if (index < controllers.size() && controllers[index].handle && !controllers[index].is_xinput) + if (index < (int)controllers.size() && controllers[index].handle && !controllers[index].is_xinput) { if (state != nullptr) { @@ -454,7 +454,7 @@ namespace wiRawInput } void SetControllerFeedback(const wiInput::ControllerFeedback& data, int index) { - if (index < controllers.size() && controllers[index].handle && !controllers[index].is_xinput) + if (index < (int)controllers.size() && controllers[index].handle && !controllers[index].is_xinput) { HANDLE hid_device = CreateFile( controllers[index].name.c_str(), diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index cdf9c7cef..25824cc34 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 38; // minor bug fixes, alterations, refactors, updates - const int revision = 0; + const int revision = 1; long GetVersion()