From 49345ff7dff4831d361d355ebcfa7208e8cefc55 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 25 Jun 2024 17:06:37 -0300 Subject: [PATCH] Implementa ponteiros ComPtr --- framework/platform-dx/adapter.cpp | 24 +- framework/platform-dx/blendstate.cpp | 5 +- framework/platform-dx/buffer.cpp | 11 +- framework/platform-dx/depthstencilstate.cpp | 5 +- framework/platform-dx/device.cpp | 17 +- framework/platform-dx/effect.cpp | 16 +- framework/platform-dx/rasterizerstate.cpp | 5 +- framework/platform-dx/rendertarget.cpp | 11 +- framework/platform-dx/samplerstate.cpp | 7 +- framework/platform-dx/shader.cpp | 10 +- framework/platform-dx/sprite.cpp | 26 +- framework/platform-dx/swapchain.cpp | 9 +- framework/platform-dx/texture.cpp | 116 +++------ inc/xna/platform-dx/dx.hpp | 263 ++++---------------- 14 files changed, 143 insertions(+), 382 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index 8dcd0b0..51bc51b 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -12,14 +12,14 @@ namespace xna { } uptr GraphicsAdapter::DefaultAdapter() { - IDXGIFactory1* pFactory = nullptr; + comptr pFactory = nullptr; - if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(ExMessage::CreateComponent); - IDXGIAdapter1* pAdapter = nullptr; + comptr pAdapter = nullptr; - if (pFactory->EnumAdapters1(0, &pAdapter) != DXGI_ERROR_NOT_FOUND) { + if (pFactory->EnumAdapters1(0, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { auto adp = unew(); adp->impl->_index = 0; @@ -28,22 +28,19 @@ namespace xna { return adp; } - pFactory->Release(); - pFactory = nullptr; - return nullptr; } void GraphicsAdapter::Adapters(std::vector>& adapters) { - IDXGIFactory1* pFactory = nullptr; + comptr pFactory = nullptr; - if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(ExMessage::CreateComponent); - IDXGIAdapter1* pAdapter = nullptr; + comptr pAdapter = nullptr; UINT count = 0; - for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { + for (; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) { auto adp = unew(); adp->impl->_index = count; @@ -51,9 +48,6 @@ namespace xna { adapters.push_back(std::move(adp)); } - - pFactory->Release(); - pFactory = nullptr; } String GraphicsAdapter::Description() const { @@ -141,7 +135,7 @@ namespace xna { uptr GraphicsAdapter::SupportedDisplayModes() const { if (!impl->dxadapter) return nullptr; - const auto totalDisplay = getDisplayModesCount(impl->dxadapter); + const auto totalDisplay = getDisplayModesCount(impl->dxadapter.Get()); if (totalDisplay == 0) return nullptr; diff --git a/framework/platform-dx/blendstate.cpp b/framework/platform-dx/blendstate.cpp index bc9f84c..825f1d3 100644 --- a/framework/platform-dx/blendstate.cpp +++ b/framework/platform-dx/blendstate.cpp @@ -102,13 +102,12 @@ namespace xna { } if (impl->dxBlendState) { - impl->dxBlendState->Release(); impl->dxBlendState = nullptr; } const auto hr = m_device->impl->_device->CreateBlendState( &impl->dxDescription, - &impl->dxBlendState); + impl->dxBlendState.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -127,7 +126,7 @@ namespace xna { } m_device->impl->_context->OMSetBlendState( - impl->dxBlendState, + impl->dxBlendState.Get(), impl->blendFactor, impl->sampleMask); diff --git a/framework/platform-dx/buffer.cpp b/framework/platform-dx/buffer.cpp index 938aced..7992e90 100644 --- a/framework/platform-dx/buffer.cpp +++ b/framework/platform-dx/buffer.cpp @@ -22,14 +22,13 @@ namespace xna { } if (impl->_buffer) { - impl->_buffer->Release(); impl->_buffer = nullptr; } const auto hr = m_device->impl->_device->CreateBuffer( &impl->_description, &impl->_subResource, - &impl->_buffer); + impl->_buffer.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -56,7 +55,6 @@ namespace xna { } if (impl->_blob) { - impl->_blob->Release(); impl->_blob = nullptr; } @@ -80,7 +78,7 @@ namespace xna { Exception::Throw(ExMessage::ApplyComponent); } - m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0); + m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer.Get(), DXGI_FORMAT_R16_UINT, 0); return true; } @@ -109,7 +107,7 @@ namespace xna { UINT stride = impl->size; UINT offset = 0; m_device->impl->_context->IASetVertexBuffers(0, 1, - &impl->dxBuffer, &stride, &offset); + impl->dxBuffer.GetAddressOf(), &stride, &offset); return true; } @@ -132,7 +130,6 @@ namespace xna { } if (impl->_inputLayout) { - impl->_inputLayout->Release(); impl->_inputLayout = nullptr; } @@ -141,7 +138,7 @@ namespace xna { static_cast(impl->_description.size()), blob.impl->_blob->GetBufferPointer(), blob.impl->_blob->GetBufferSize(), - &impl->_inputLayout); + impl->_inputLayout.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); diff --git a/framework/platform-dx/depthstencilstate.cpp b/framework/platform-dx/depthstencilstate.cpp index d1245bb..98b5013 100644 --- a/framework/platform-dx/depthstencilstate.cpp +++ b/framework/platform-dx/depthstencilstate.cpp @@ -42,13 +42,12 @@ namespace xna { } if (impl->dxDepthStencil) { - impl->dxDepthStencil->Release(); impl->dxDepthStencil = nullptr; } const auto hr = m_device->impl->_device->CreateDepthStencilState( &impl->dxDescription, - &impl->dxDepthStencil); + impl->dxDepthStencil.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -67,7 +66,7 @@ namespace xna { Exception::Throw(ExMessage::UnintializedComponent); } - m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil, 0); + m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil.Get(), 0); return true; } diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 7d2836e..5e3804c 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -26,16 +26,16 @@ namespace xna { createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; #endif auto hr = D3D11CreateDevice( - impl._adapter->impl->dxadapter, + impl._adapter->impl->dxadapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, NULL, createDeviceFlags, NULL, 0, D3D11_SDK_VERSION, - &impl._device, + impl._device.GetAddressOf(), &impl._featureLevel, - &impl._context); + impl._context.GetAddressOf()); if (FAILED(hr)) { OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n"); @@ -48,9 +48,9 @@ namespace xna { NULL, 0, D3D11_SDK_VERSION, - &impl._device, + impl._device.GetAddressOf(), &impl._featureLevel, - &impl._context); + impl._context.GetAddressOf()); if FAILED(hr) Exception::Throw(ExMessage::CreateComponent); @@ -156,7 +156,10 @@ namespace xna { if (!impl) return false; const auto result = impl->_swapChain->Present(impl->_usevsync); - impl->_context->OMSetRenderTargets(1, &impl->_renderTarget2D->render_impl->_renderTargetView, nullptr); + impl->_context->OMSetRenderTargets( + 1, + impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(), + nullptr); return result; } @@ -172,7 +175,7 @@ namespace xna { impl->_backgroundColor[3] = v4.W; impl->_context->ClearRenderTargetView( - impl->_renderTarget2D->render_impl->_renderTargetView, + impl->_renderTarget2D->render_impl->_renderTargetView.Get(), impl->_backgroundColor); } diff --git a/framework/platform-dx/effect.cpp b/framework/platform-dx/effect.cpp index 6a5c797..9744ee2 100644 --- a/framework/platform-dx/effect.cpp +++ b/framework/platform-dx/effect.cpp @@ -18,9 +18,9 @@ namespace xna { //UINT FXFlags, 0, //ID3D11Device * pDevice, - device->impl->_device, + device->impl->_device.Get(), //ID3DX11Effect * *ppEffect - &impl->dxEffect + impl->dxEffect.ReleaseAndGetAddressOf() ); if FAILED(result) @@ -331,7 +331,7 @@ namespace xna { if (!impl->dxPass) throw std::runtime_error("EffectPass::Apply: The class was not initialized correctly"); - const auto hr = impl->dxPass->Apply(0, impl->dxContext); + const auto hr = impl->dxPass->Apply(0, impl->dxContext.Get()); if FAILED(hr) throw std::runtime_error("EffectPass::Apply: error to call Apply"); @@ -399,14 +399,12 @@ namespace xna { auto current = impl->dxTechnique->GetPassByIndex(i); auto pass = snew(); - pass->impl->dxPass = current; - pass->impl->dxPass->AddRef(); + pass->impl->dxPass.Attach(current); current->Release(); current = nullptr; pass->impl->dxContext = impl->dxContext; - pass->impl->dxContext->AddRef(); list[i] = pass; } @@ -580,8 +578,7 @@ namespace xna { break; auto efparam = snew(); - efparam->impl->dxVariable = el; - efparam->impl->dxVariable->AddRef(); + efparam->impl->dxVariable.Attach(el); el->Release(); el = nullptr; @@ -603,8 +600,7 @@ namespace xna { break; auto efparam = snew(); - efparam->impl->dxVariable = member; - efparam->impl->dxVariable->AddRef(); + efparam->impl->dxVariable.Attach(member); member->Release(); member = nullptr; diff --git a/framework/platform-dx/rasterizerstate.cpp b/framework/platform-dx/rasterizerstate.cpp index c9634a8..8a0f635 100644 --- a/framework/platform-dx/rasterizerstate.cpp +++ b/framework/platform-dx/rasterizerstate.cpp @@ -22,13 +22,12 @@ namespace xna { } if (impl->dxRasterizerState) { - impl->dxRasterizerState->Release(); impl->dxRasterizerState = nullptr; } const auto hr = m_device->impl->_device->CreateRasterizerState( &impl->dxDescription, - &impl->dxRasterizerState); + impl->dxRasterizerState.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -47,7 +46,7 @@ namespace xna { Exception::Throw(ExMessage::UnintializedComponent); } - m_device->impl->_context->RSSetState(impl->dxRasterizerState); + m_device->impl->_context->RSSetState(impl->dxRasterizerState.Get()); return true; } diff --git a/framework/platform-dx/rendertarget.cpp b/framework/platform-dx/rendertarget.cpp index 2f0bd14..e166b0d 100644 --- a/framework/platform-dx/rendertarget.cpp +++ b/framework/platform-dx/rendertarget.cpp @@ -16,19 +16,14 @@ namespace xna { bool RenderTarget2D::Initialize() { if (!impl || !m_device || !m_device->impl->_device) { Exception::Throw(ExMessage::InitializeComponent); - } - - if (impl->dxTexture2D) { - impl->dxTexture2D->Release(); - impl->dxTexture2D = nullptr; - } + } if (!m_device->impl->_swapChain->impl->GetBackBuffer(impl->dxTexture2D)) return false; auto& dxdevice = m_device->impl->_device; - const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D, NULL, &render_impl->_renderTargetView); + const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, render_impl->_renderTargetView.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -46,7 +41,7 @@ namespace xna { Exception::Throw(ExMessage::UnintializedComponent); auto& context = m_device->impl->_context; - context->OMSetRenderTargets(1, &render_impl->_renderTargetView, nullptr); + context->OMSetRenderTargets(1, render_impl->_renderTargetView.GetAddressOf(), nullptr); return true; } } \ No newline at end of file diff --git a/framework/platform-dx/samplerstate.cpp b/framework/platform-dx/samplerstate.cpp index fa2af6d..9436823 100644 --- a/framework/platform-dx/samplerstate.cpp +++ b/framework/platform-dx/samplerstate.cpp @@ -16,13 +16,12 @@ namespace xna { } if (impl->_samplerState) { - impl->_samplerState->Release(); impl->_samplerState = nullptr; } const auto hr = m_device->impl->_device->CreateSamplerState( &impl->_description, - &impl->_samplerState); + impl->_samplerState.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -41,7 +40,7 @@ namespace xna { Exception::Throw(ExMessage::UnintializedComponent); } - m_device->impl->_context->PSSetSamplers(0, 1, &impl->_samplerState); + m_device->impl->_context->PSSetSamplers(0, 1, impl->_samplerState.GetAddressOf()); return true; } @@ -62,7 +61,7 @@ namespace xna { if (!current || !current->impl || !current->impl->_samplerState) Exception::Throw(ExMessage::InvalidOperation); - states[i] = current->impl->_samplerState; + states[i] = current->impl->_samplerState.Get(); states[i]->AddRef(); } diff --git a/framework/platform-dx/shader.cpp b/framework/platform-dx/shader.cpp index 8ceda6b..d8e29ca 100644 --- a/framework/platform-dx/shader.cpp +++ b/framework/platform-dx/shader.cpp @@ -68,15 +68,14 @@ namespace xna { } if (impl->_vertexShader) { - impl->_vertexShader->Release(); - impl->_vertexShader = nullptr; + impl->_vertexShader.ReleaseAndGetAddressOf(); } const auto hr = m_device->impl->_device->CreateVertexShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, - &impl->_vertexShader); + impl->_vertexShader.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -92,15 +91,14 @@ namespace xna { } if (impl->_pixelShader) { - impl->_pixelShader->Release(); - impl->_pixelShader = nullptr; + impl->_pixelShader.ReleaseAndGetAddressOf(); } const auto hr = m_device->impl->_device->CreatePixelShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, - &impl->_pixelShader); + impl->_pixelShader.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index 95276ab..1925641 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -63,7 +63,7 @@ namespace xna { impl = unew(); impl->_dxSpriteFont = unew( //ID3D11ShaderResourceView* texture - texture->impl->dxShaderResource, + texture->impl->dxShaderResource.Get(), //Glyph const* glyphs dxGlyps.data(), //size_t glyphCount @@ -130,7 +130,7 @@ namespace xna { implementation = unew(); implementation->_dxspriteBatch = snew( //ID3D11DeviceContext* deviceContext - device->impl->_context + device->impl->_context.Get() ); Viewport(device->Viewport()); @@ -154,10 +154,10 @@ namespace xna { implementation->_dxspriteBatch->Begin( sort, - blendState ? blendState->impl->dxBlendState : nullptr, - samplerState ? samplerState->impl->_samplerState : nullptr, - depthStencil ? depthStencil->impl->dxDepthStencil : nullptr, - rasterizerState ? rasterizerState->impl->dxRasterizerState : nullptr, + blendState ? blendState->impl->dxBlendState.Get() : nullptr, + samplerState ? samplerState->impl->_samplerState.Get() : nullptr, + depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr, + rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr, nullptr, matrix ); @@ -182,7 +182,7 @@ namespace xna { XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; implementation->_dxspriteBatch->Draw( - texture.impl->dxShaderResource, + texture.impl->dxShaderResource.Get(), _position, _color ); @@ -209,7 +209,7 @@ namespace xna { }; implementation->_dxspriteBatch->Draw( - texture.impl->dxShaderResource, + texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color); @@ -239,7 +239,7 @@ namespace xna { const DxSpriteEffects _effects = static_cast(effects); implementation->_dxspriteBatch->Draw( - texture.impl->dxShaderResource, + texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -275,7 +275,7 @@ namespace xna { const XMFLOAT2 _scale = { scale.X, scale.Y }; implementation->_dxspriteBatch->Draw( - texture.impl->dxShaderResource, + texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -302,7 +302,7 @@ namespace xna { const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, _color); + implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { @@ -330,7 +330,7 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); + implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { @@ -362,7 +362,7 @@ namespace xna { const auto _effects = static_cast(effects); implementation->_dxspriteBatch->Draw( - texture.impl->dxShaderResource, + texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color, diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index 82469a1..e86941b 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -16,13 +16,12 @@ namespace xna { impl = nullptr; } - static bool internalInit(GraphicsDevice& device, HWND windowHandle, IDXGISwapChain1*& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) { + static bool internalInit(GraphicsDevice& device, HWND windowHandle, comptr& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) { if (!device.impl->_device || !windowHandle) return false; if (swapChain) { - swapChain->Release(); - swapChain = nullptr; + swapChain.ReleaseAndGetAddressOf(); } auto adapter = device.Adapter(); @@ -39,12 +38,12 @@ namespace xna { if (FAILED(hr)) return false; dxFactory2->CreateSwapChainForHwnd( - device.impl->_device, + device.impl->_device.Get(), windowHandle, &desc, &fdesc, nullptr, - &swapChain); + swapChain.GetAddressOf()); return true; diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index d4b9c29..f38cce0 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -9,44 +9,30 @@ namespace xna { { auto _this = device.shared_from_this(); auto texture2d = snew(_this); - ID3D11Resource* resource = nullptr; + comptr resource = nullptr; auto wstr = XnaHelper::ToWString(fileName); HRESULT result = DirectX::CreateWICTextureFromFile( - device.impl->_device, - device.impl->_context, + device.impl->_device.Get(), + device.impl->_context.Get(), wstr.c_str(), - &resource, - &texture2d->impl->dxShaderResource, + resource.GetAddressOf(), + texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(), 0U); - if (FAILED(result)) - { - if (resource) { - resource->Release(); - resource = nullptr; - } - + if (FAILED(result)) { return nullptr; } - result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D); + result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); if (FAILED(result)) { - if (resource) { - resource->Release(); - resource = nullptr; - } - return nullptr; } D3D11_TEXTURE2D_DESC desc; texture2d->impl->dxTexture2D->GetDesc(&desc); - texture2d->impl->dxDescription = desc; - - resource->Release(); - resource = nullptr; + texture2d->impl->dxDescription = desc; return texture2d; } @@ -57,25 +43,20 @@ namespace xna { Exception::Throw(ExMessage::InitializeComponent); } - auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); + auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); } - ID3D11Resource* resource = nullptr; - hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + comptr resource = nullptr; + hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::InvalidOperation); } - hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); - - if (resource) { - resource->Release(); - resource = nullptr; - } + hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, &impl->dxShaderResource); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -128,35 +109,25 @@ namespace xna { HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data) { if (!impl.dxTexture2D) { - auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D); + auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); } } - ID3D11Resource* resource = nullptr; - auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + comptr resource = nullptr; + auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::InvalidOperation); } constexpr int R8G8B8A8U_BYTE_SIZE = 4; - device.impl->_context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - - if (impl.dxShaderResource) { - impl.dxShaderResource->Release(); - impl.dxShaderResource = nullptr; - } + device.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; - hr = device.impl->_device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource); - - if (resource) { - resource->Release(); - resource = nullptr; - } + hr = device.impl->_device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -216,15 +187,15 @@ namespace xna { } if (!impl->dxTexture2D) { - auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); + auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); } } - ID3D11Resource* resource = nullptr; - auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + comptr resource = nullptr; + auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::InvalidOperation); @@ -233,30 +204,20 @@ namespace xna { D3D11_BOX box{}; if (rect) { - box.left = rect->X; - box.right = rect->X + rect->Width; - box.top = rect->Y; - box.bottom = rect->Y + rect->Height; + box.left = rect->Left(); + box.right = rect->Right(); + box.top = rect->Top(); + box.bottom = rect->Bottom(); box.back = level; box.front = 0; } constexpr int R8G8B8A8U_BYTE_SIZE = 4; - m_device->impl->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - - if (impl->dxShaderResource) { - impl->dxShaderResource->Release(); - impl->dxShaderResource = nullptr; - } + m_device->impl->_context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); impl->dxShaderDescription.Format = impl->dxDescription.Format; impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels; - hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); - - if (resource) { - resource->Release(); - resource = nullptr; - } + hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -286,34 +247,24 @@ namespace xna { { auto _this = device.shared_from_this(); auto texture2d = snew(_this); - ID3D11Resource* resource = nullptr; + comptr resource = nullptr; auto hr = DirectX::CreateWICTextureFromMemory( - device.impl->_device, - device.impl->_context, + device.impl->_device.Get(), + device.impl->_context.Get(), data.data(), data.size(), - &resource, - &texture2d->impl->dxShaderResource); + resource.GetAddressOf(), + texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf()); if (FAILED(hr)) { - if (resource) { - resource->Release(); - resource = nullptr; - } - return nullptr; } - hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D); + hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); if (FAILED(hr)) { - if (resource) { - resource->Release(); - resource = nullptr; - } - return nullptr; } @@ -321,9 +272,6 @@ namespace xna { texture2d->impl->dxTexture2D->GetDesc(&desc); texture2d->impl->dxDescription = desc; - resource->Release(); - resource = nullptr; - return texture2d; } diff --git a/inc/xna/platform-dx/dx.hpp b/inc/xna/platform-dx/dx.hpp index b741418..6b3dc4f 100644 --- a/inc/xna/platform-dx/dx.hpp +++ b/inc/xna/platform-dx/dx.hpp @@ -512,14 +512,8 @@ namespace xna { }; struct GraphicsAdapter::PlatformImplementation { - ~PlatformImplementation() { - if (dxadapter) { - dxadapter->Release(); - dxadapter = nullptr; - } - } + comptr dxadapter = nullptr; - IDXGIAdapter1* dxadapter = nullptr; private: friend class GraphicsAdapter; Uint _index{ 0 }; @@ -543,57 +537,29 @@ namespace xna { }; struct BlendState::PlatformImplementation { - ~PlatformImplementation() { - if (dxBlendState) { - dxBlendState->Release(); - dxBlendState = nullptr; - } - } - - ID3D11BlendState* dxBlendState = nullptr; + comptr dxBlendState = nullptr; D3D11_BLEND_DESC dxDescription{}; float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; UINT sampleMask{ 0xffffffff }; }; struct ConstantBuffer::PlatformImplementation { - ~PlatformImplementation() { - if (_buffer) { - _buffer->Release(); - _buffer = nullptr; - } - } - D3D11_BUFFER_DESC _description{}; D3D11_SUBRESOURCE_DATA _subResource{}; - ID3D11Buffer* _buffer = nullptr; + comptr _buffer = nullptr; DirectX::XMMATRIX _worldViewProjection{}; }; struct DataBuffer::PlatformImplementation { - ~PlatformImplementation() { - if (_blob) { - _blob->Release(); - _blob = nullptr; - } - } + comptr _blob = nullptr; - ID3DBlob* _blob = nullptr; - - void Set(ID3DBlob*& blob) { + void Set(comptr const& blob) { _blob = blob; } }; struct DepthStencilState::PlatformImplementation { - ~PlatformImplementation() { - if (dxDepthStencil) { - dxDepthStencil->Release(); - dxDepthStencil = nullptr; - } - } - - ID3D11DepthStencilState* dxDepthStencil = nullptr; + comptr dxDepthStencil = nullptr; D3D11_DEPTH_STENCIL_DESC dxDescription{}; }; @@ -646,14 +612,7 @@ namespace xna { }; struct IndexBuffer::PlatformImplementation { - ~PlatformImplementation() { - if (dxBuffer) { - dxBuffer->Release(); - dxBuffer = nullptr; - } - } - - ID3D11Buffer* dxBuffer = nullptr; + comptr dxBuffer = nullptr; }; struct Keyboard::PlatformImplementation { @@ -675,126 +634,58 @@ namespace xna { }; struct RasterizerState::PlatformImplementation { - ~PlatformImplementation() { - if (dxRasterizerState) { - dxRasterizerState->Release(); - dxRasterizerState = nullptr; - } - } - - ID3D11RasterizerState* dxRasterizerState = nullptr; + comptr dxRasterizerState = nullptr; D3D11_RASTERIZER_DESC dxDescription{}; }; struct SamplerState::PlatformImplementation { - ~PlatformImplementation() { - if (_samplerState) { - _samplerState->Release(); - _samplerState = nullptr; - } - } - - ID3D11SamplerState* _samplerState = nullptr; + comptr _samplerState = nullptr; D3D11_SAMPLER_DESC _description{}; }; struct VertexShader::PlatformImplementation { - ~PlatformImplementation() { - if (_vertexShader) { - _vertexShader->Release(); - _vertexShader = nullptr; - } - } - - ID3D11VertexShader* _vertexShader = nullptr; + comptr _vertexShader = nullptr; }; struct PixelShader::PlatformImplementation { - ~PlatformImplementation() { - if (_pixelShader) { - _pixelShader->Release(); - _pixelShader = nullptr; - } - } - - ID3D11PixelShader* _pixelShader = nullptr; + comptr _pixelShader = nullptr; }; struct SwapChain::PlatformImplementation { - ~PlatformImplementation() { - if (dxSwapChain) { - dxSwapChain->Release(); - dxSwapChain = nullptr; - } - } - - IDXGISwapChain1* dxSwapChain{ nullptr }; + comptr dxSwapChain{ nullptr }; DXGI_SWAP_CHAIN_DESC1 dxDescription{}; DXGI_SWAP_CHAIN_FULLSCREEN_DESC dxFullScreenDescription{}; - bool GetBackBuffer(ID3D11Texture2D*& texture2D) { + bool GetBackBuffer(comptr& texture2D) { if (!dxSwapChain) return false; - const auto hr = dxSwapChain->GetBuffer(0, __uuidof(texture2D), (void**)(&texture2D)); + const auto hr = dxSwapChain->GetBuffer(0, __uuidof(texture2D), (void**)texture2D.GetAddressOf()); return !FAILED(hr); } }; struct Texture2D::PlatformImplementation { - ~PlatformImplementation() { - if (dxTexture2D) { - dxTexture2D->Release(); - dxTexture2D = nullptr; - } - - if (dxShaderResource) { - dxShaderResource->Release(); - dxShaderResource = nullptr; - } - } - - ID3D11Texture2D* dxTexture2D{ nullptr }; - ID3D11ShaderResourceView* dxShaderResource{ nullptr }; + comptr dxTexture2D{ nullptr }; + comptr dxShaderResource{ nullptr }; D3D11_SUBRESOURCE_DATA dxSubResource{}; D3D11_TEXTURE2D_DESC dxDescription{}; D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{}; }; - struct RenderTarget2D::PlatformImplementation { - ~PlatformImplementation() { - if (_renderTargetView) { - _renderTargetView->Release(); - _renderTargetView = nullptr; - } - } - - ID3D11RenderTargetView* _renderTargetView = nullptr; + struct RenderTarget2D::PlatformImplementation { + comptr _renderTargetView = nullptr; D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{}; }; struct VertexBuffer::PlatformImplementation { - ~PlatformImplementation() { - if (dxBuffer) { - dxBuffer->Release(); - dxBuffer = nullptr; - } - } - - ID3D11Buffer* dxBuffer = nullptr; + comptr dxBuffer = nullptr; UINT size{ 0 }; }; struct VertexInputLayout::PlatformImplementation { - ~PlatformImplementation() { - if (_inputLayout) { - _inputLayout->Release(); - _inputLayout = nullptr; - } - } - - ID3D11InputLayout* _inputLayout{ nullptr }; + comptr _inputLayout{ nullptr }; std::vector _description{}; }; @@ -934,29 +825,12 @@ namespace xna { _depthStencilState = xna::DepthStencilState::Default(); _rasterizerState = xna::RasterizerState::CullCounterClockwise(); _samplerStates = snew(); - } - - ~PlatformImplementation() { - if (_device) { - _device->Release(); - _device = nullptr; - } - - if (_context) { - _context->Release(); - _device = nullptr; - } - - if (_factory) { - _factory->Release(); - _factory = nullptr; - } - } + } public: - ID3D11Device* _device = nullptr; - ID3D11DeviceContext* _context = nullptr; - IDXGIFactory1* _factory = nullptr; + comptr _device = nullptr; + comptr _context = nullptr; + comptr _factory = nullptr; PBlendState _blendState = nullptr; PRasterizerState _rasterizerState = nullptr; @@ -990,84 +864,29 @@ namespace xna { }; struct SoundEffect::PlatformImplementation { - ~PlatformImplementation() { - } - uptr _dxSoundEffect = nullptr; }; struct Effect::PlatformImplementation { - ~PlatformImplementation() { - if (dxEffect) { - dxEffect->Release(); - dxEffect = nullptr; - } - } - - ID3DX11Effect* dxEffect = nullptr; + comptr dxEffect = nullptr; }; struct EffectAnnotation::PlatformImplementation { - ~PlatformImplementation() { - if (dxVariable) { - dxVariable->Release(); - dxVariable = nullptr; - } - } - - ID3DX11EffectVariable* dxVariable = nullptr; + comptr dxVariable = nullptr; }; struct EffectPass::PlatformImplementation { - ~PlatformImplementation() { - if (dxPass) { - dxPass->Release(); - dxPass = nullptr; - } - - if (dxContext) { - dxContext->Release(); - dxContext = nullptr; - } - } - - ID3DX11EffectPass* dxPass = nullptr; - ID3D11DeviceContext* dxContext = nullptr; + comptr dxPass = nullptr; + comptr dxContext = nullptr; }; struct EffectTechnique::PlatformImplementation { - ~PlatformImplementation() { - if (dxTechnique) { - dxTechnique->Release(); - dxTechnique = nullptr; - } - - if (dxContext) { - dxContext->Release(); - dxContext = nullptr; - } - } - - ID3DX11EffectTechnique* dxTechnique = nullptr; - ID3D11DeviceContext* dxContext = nullptr; + comptr dxTechnique = nullptr; + comptr dxContext = nullptr; }; struct EffectParameter::PlatformImplementation { - PlatformImplementation(){} - - PlatformImplementation(ID3DX11EffectVariable* value) { - dxVariable = value; - dxVariable->AddRef(); - } - - ~PlatformImplementation() { - if (dxVariable) { - dxVariable->Release(); - dxVariable = nullptr; - } - } - - ID3DX11EffectVariable* dxVariable = nullptr; + comptr dxVariable = nullptr; }; template @@ -1076,7 +895,17 @@ namespace xna { Exception::Throw(ExMessage::InitializeComponent); } - const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &impl->dxBuffer); + if (impl->dxBuffer) { + impl->dxBuffer = nullptr; + } + + const auto hr = DirectX::CreateStaticBuffer( + m_device->impl->_device.Get(), + data.data(), + data.size(), + sizeof(T), + D3D11_BIND_INDEX_BUFFER, + impl->dxBuffer.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent); @@ -1091,7 +920,13 @@ namespace xna { Exception::Throw(ExMessage::InitializeComponent); } - const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &impl->dxBuffer); + const auto hr = DirectX::CreateStaticBuffer( + m_device->impl->_device.Get(), + data.data(), + data.size(), + sizeof(T), + D3D11_BIND_VERTEX_BUFFER, + impl->dxBuffer.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(ExMessage::CreateComponent);