diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 5f440ff..ffff87b 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -54,8 +54,7 @@ namespace xna { if (FAILED(hr)) Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION); - impl->_renderTarget2D = snew(_this); - impl->_renderTarget2D->Initialize(); + impl->_renderTarget2D = RenderTarget2D::FromBackBuffer(_this); impl->_renderTarget2D->Apply(); D3D11_VIEWPORT view = DxHelpers::ViewportToDx(viewport); diff --git a/framework/platform-dx/rendertarget.cpp b/framework/platform-dx/rendertarget.cpp index ddd6b4a..6bc26cb 100644 --- a/framework/platform-dx/rendertarget.cpp +++ b/framework/platform-dx/rendertarget.cpp @@ -1,36 +1,53 @@ #include "xna/xna-dx.hpp" namespace xna { + RenderTarget2D::RenderTarget2D() : Texture2D(nullptr) { + impl2 = unew(); + } + RenderTarget2D::RenderTarget2D(sptr const& device) : Texture2D(device) { impl2 = unew(); } - void RenderTarget2D::Initialize() { - if (!impl || !m_device || !m_device->impl->_device) { - Exception::Throw(Exception::UNABLE_TO_INITIALIZE); - } + P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) { + auto& swapChain = device->impl->_swapChain; + auto rt = snew(device); + auto& implementation = rt->impl; + auto& implementation2 = rt->impl2; - auto& swapChain = m_device->impl->_swapChain; - - if (!swapChain->impl->GetBackBuffer(impl->dxTexture2D)) + if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D)) { Exception::Throw(Exception::FAILED_TO_CREATE); } - impl->dxTexture2D->GetDesc(&impl->dxDescription); + rt->Initialize(); + + return rt; + } + + void RenderTarget2D::Initialize() { + if (!impl || !m_device || !m_device->impl->_device) { + Exception::Throw(Exception::UNABLE_TO_INITIALIZE); + } + + impl->dxDescription.Width = width; + impl->dxDescription.Height = height; + impl->dxDescription.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET; + + Texture2D::Initialize(); + auto& dxdevice = m_device->impl->_device; - const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, impl2->_renderTargetView.ReleaseAndGetAddressOf()); + const auto hr = dxdevice->CreateRenderTargetView( + impl->dxTexture2D.Get(), + NULL, + impl2->_renderTargetView.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); } impl2->_renderTargetView->GetDesc(&impl2->_renderTargetDesc); - - //depthStencilFormat = DepthFormat::None; - multiSampleCount = impl->dxDescription.SampleDesc.Count; - //targetUsage = RenderTargetUsage::DiscardContent; } void RenderTarget2D::Apply() { diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 38219e1..1dde906 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -4,16 +4,50 @@ namespace xna { static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl); static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data); + Texture2D::Texture2D() : Texture(nullptr) { + impl = unew(); + setDefaultTexture2DDesc(*impl); + } + + Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device), width(width), height(height) { + impl = unew(); + setDefaultTexture2DDesc(*impl); + impl->dxDescription.Width = static_cast(this->width); + impl->dxDescription.Height = static_cast(this->height); + } + + Texture2D::Texture2D(sptr const& device) : Texture(device) { + impl = unew(); + setDefaultTexture2DDesc(*impl); + } + + Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) + : Texture(device), width(width), height(height), levelCount(mipMap) { + impl = unew(); + setDefaultTexture2DDesc(*impl); + impl->dxDescription.Width = static_cast(this->width); + impl->dxDescription.Height = static_cast(this->height); + impl->dxDescription.MipLevels = static_cast(this->levelCount); + impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format); + } + void Texture2D::Initialize() { if (!m_device || !m_device->impl->_device) { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); } - auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf()); + HRESULT hr = 0; - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); + if (!impl->dxTexture2D) { + hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + Exception::Throw(Exception::FAILED_TO_CREATE); + } + } + else { + impl->dxTexture2D->GetDesc(&impl->dxDescription); } comptr resource = nullptr; @@ -22,45 +56,20 @@ namespace xna { if (FAILED(hr)) { Exception::Throw(Exception::INVALID_OPERATION); } + + if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) { + hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf()); - hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, &impl->dxShaderResource); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } + if (FAILED(hr)) { + Exception::Throw(Exception::FAILED_TO_CREATE); + } + } surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format); levelCount = static_cast(impl->dxShaderDescription.Texture2D.MipLevels); width = static_cast(impl->dxDescription.Width); height = static_cast(impl->dxDescription.Height); - } - - Texture2D::Texture2D() : Texture(nullptr) { - impl = unew(); - setDefaultTexture2DDesc(*impl); - } - - Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device) { - impl = unew(); - setDefaultTexture2DDesc(*impl); - impl->dxDescription.Width = static_cast(width); - impl->dxDescription.Height = static_cast(height); - } - - Texture2D::Texture2D(sptr const& device) : Texture(device) { - impl = unew(); - setDefaultTexture2DDesc(*impl); - } - - Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device) - { - impl = unew(); - setDefaultTexture2DDesc(*impl); - impl->dxDescription.Width = static_cast(width); - impl->dxDescription.Height = static_cast(height); - impl->dxDescription.MipLevels = static_cast(mipMap); - impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format); - } + } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) { diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index b6a4602..a1ed6e5 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -207,6 +207,7 @@ namespace xna { using P_FileStream = sptr; using P_Texture = sptr; using P_Texture2D = sptr; + using P_RenderTarget2D = sptr; } diff --git a/inc/xna/graphics/rendertarget.hpp b/inc/xna/graphics/rendertarget.hpp index ae49df5..ee91904 100644 --- a/inc/xna/graphics/rendertarget.hpp +++ b/inc/xna/graphics/rendertarget.hpp @@ -8,22 +8,13 @@ namespace xna { class RenderTarget2D : public Texture2D { public: RenderTarget2D(); - RenderTarget2D(sptr const& device); + RenderTarget2D(sptr const& device); - //Gets the data format for the depth stencil data. - constexpr DepthFormat DepthStencilFormat() const { return depthStencilFormat; } - //Gets the number of sample locations during multisampling. - constexpr Int MultiSampleCount() const { return multiSampleCount; } - //Gets or sets the render target usage. - constexpr RenderTargetUsage TargetUsage() const { return targetUsage; } + + static P_RenderTarget2D FromBackBuffer(P_GraphicsDevice const& device); void Initialize(); void Apply(); - private: - DepthFormat depthStencilFormat{ DepthFormat::None }; - Int multiSampleCount{ 0 }; - RenderTargetUsage targetUsage{ RenderTargetUsage::DiscardContents }; - public: struct PlatformImplementation; uptr impl2 = nullptr; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 24ef843..45257e9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -#add_subdirectory ("02_PlatfformerStarterKit") +add_subdirectory ("02_PlatfformerStarterKit")