diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index e9321fb..251b131 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -1,12 +1,13 @@ #include "common/color.hpp" #include "graphics/adapter.hpp" #include "graphics/blendstate.hpp" +#include "graphics/rendertarget.hpp" #include "platform-dx/device-dx.hpp" #include "platform-dx/gdeviceinfo-dx.hpp" #include "platform-dx/gdevicemanager-dx.hpp" #include "platform-dx/implementations.hpp" -#include "platform-dx/rendertarget-dx.hpp" #include "platform-dx/window-dx.hpp" +#include "platform-dx/implementations.hpp" namespace xna { GraphicsDevice::GraphicsDevice() { @@ -77,7 +78,7 @@ namespace xna { bool GraphicsDevice::Present() { const auto result = _swapChain->Present(_usevsync); - _context->OMSetRenderTargets(1, &_renderTarget2D->_renderTargetView, nullptr); + _context->OMSetRenderTargets(1, &_renderTarget2D->render_impl->_renderTargetView, nullptr); return result; } @@ -142,7 +143,7 @@ namespace xna { } void GraphicsDevice::Clear() { - _context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor); + _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor); } void GraphicsDevice::Clear(Color const& color) { @@ -153,6 +154,6 @@ namespace xna { _backgroundColor[2] = v4.Z; _backgroundColor[3] = v4.W; - _context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor); + _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor); } } \ No newline at end of file diff --git a/framework/platform/init-dx.cpp b/framework/platform/init-dx.cpp index a41a5fc..b030e0b 100644 --- a/framework/platform/init-dx.cpp +++ b/framework/platform/init-dx.cpp @@ -1,9 +1,9 @@ #include "platform-dx/init-dx.hpp" #include "csharp/type.hpp" -#include "platform-dx/texture-dx.hpp" #include "platform-dx/content-readers/texture2Dreader-dx.hpp" #include "content/typereadermanager.hpp" #include "content/defaultreaders.hpp" +#include "platform-dx/implementations.hpp" namespace xna { diff --git a/framework/platform/rendertarget-dx.cpp b/framework/platform/rendertarget-dx.cpp index 1ca4400..16ebe1f 100644 --- a/framework/platform/rendertarget-dx.cpp +++ b/framework/platform/rendertarget-dx.cpp @@ -1,26 +1,38 @@ -#include "platform-dx/rendertarget-dx.hpp" #include "graphics/device.hpp" #include "platform-dx/device-dx.hpp" #include "platform-dx/implementations.hpp" +#include "graphics/rendertarget.hpp" namespace xna { + RenderTarget2D::RenderTarget2D() : Texture2D() { + render_impl = unew(); + } + + RenderTarget2D::RenderTarget2D(sptr const& device) : Texture2D(device) { + render_impl = unew(); + } + + RenderTarget2D::~RenderTarget2D() { + render_impl = nullptr; + } + bool RenderTarget2D::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - if (dxTexture2D) { - dxTexture2D->Release(); - dxTexture2D = nullptr; + if (impl->dxTexture2D) { + impl->dxTexture2D->Release(); + impl->dxTexture2D = nullptr; } - if (!m_device->_swapChain->impl->GetBackBuffer(dxTexture2D)) + if (!m_device->_swapChain->impl->GetBackBuffer(impl->dxTexture2D)) return false; auto& dxdevice = m_device->_device; - const auto hr = dxdevice->CreateRenderTargetView(dxTexture2D, NULL, &_renderTargetView); + const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D, NULL, &render_impl->_renderTargetView); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -37,7 +49,7 @@ namespace xna { } auto& context = m_device->_context; - context->OMSetRenderTargets(1, &_renderTargetView, nullptr); + context->OMSetRenderTargets(1, &render_impl->_renderTargetView, nullptr); return true; } } \ No newline at end of file diff --git a/framework/platform/sprite-dx.cpp b/framework/platform/sprite-dx.cpp index d61badd..c3ea32c 100644 --- a/framework/platform/sprite-dx.cpp +++ b/framework/platform/sprite-dx.cpp @@ -1,7 +1,6 @@ #include "platform-dx/device-dx.hpp" #include "graphics/rasterizerstate.hpp" #include "graphics/samplerstate.hpp" -#include "platform-dx/texture-dx.hpp" #include "common/color.hpp" #include "common/numerics.hpp" #include "graphics/sprite.hpp" @@ -98,7 +97,7 @@ namespace xna { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -106,7 +105,7 @@ namespace xna { XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; implementation->_dxspriteBatch->Draw( - texture.dxShaderResource, + texture.impl->dxShaderResource, _position, _color ); @@ -116,7 +115,7 @@ namespace xna { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -133,7 +132,7 @@ namespace xna { }; implementation->_dxspriteBatch->Draw( - texture.dxShaderResource, + texture.impl->dxShaderResource, _position, sourceRectangle ? &_sourceRect : nullptr, _color); @@ -143,7 +142,7 @@ namespace xna { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -163,7 +162,7 @@ namespace xna { const DxSpriteEffects _effects = static_cast(effects); implementation->_dxspriteBatch->Draw( - texture.dxShaderResource, + texture.impl->dxShaderResource, _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -178,7 +177,7 @@ namespace xna { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -199,7 +198,7 @@ namespace xna { const XMFLOAT2 _scale = { scale.X, scale.Y }; implementation->_dxspriteBatch->Draw( - texture.dxShaderResource, + texture.impl->dxShaderResource, _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -214,7 +213,7 @@ namespace xna { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; RECT _destinationRect{}; @@ -226,14 +225,14 @@ namespace xna { const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - implementation->_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, _color); + implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; RECT _destinationRect{}; @@ -254,14 +253,14 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - implementation->_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); + implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { if (!implementation->_dxspriteBatch) return; - if (!texture.dxShaderResource) + if (!texture.impl->dxShaderResource) return; RECT _destinationRect{}; @@ -286,7 +285,7 @@ namespace xna { const auto _effects = static_cast(effects); implementation->_dxspriteBatch->Draw( - texture.dxShaderResource, + texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color, diff --git a/framework/platform/swapchain-dx.cpp b/framework/platform/swapchain-dx.cpp index 009f8b5..6205414 100644 --- a/framework/platform/swapchain-dx.cpp +++ b/framework/platform/swapchain-dx.cpp @@ -83,7 +83,7 @@ namespace xna { if (!impl || !impl->dxSwapChain) return false; - const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.dxTexture2D)); + const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.impl->dxTexture2D)); return !FAILED(hr); } diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index 15c516c..3ea8104 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -1,10 +1,13 @@ -#include "platform-dx/texture-dx.hpp" #include "platform-dx/device-dx.hpp" #include "graphics/adapter.hpp" #include "platform-dx/implementations.hpp" #include "platform-dx/dxhelpers.hpp" namespace xna { + Texture2D::~Texture2D() { + impl = nullptr; + } + sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg) { auto _this = device.shared_from_this(); @@ -17,7 +20,7 @@ namespace xna { device._context, wstr.c_str(), &resource, - &texture2d->dxShaderResource, + &texture2d->impl->dxShaderResource, 0U); if (FAILED(result)) @@ -32,7 +35,7 @@ namespace xna { return nullptr; } - result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D); + result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D); if (FAILED(result)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -46,8 +49,8 @@ namespace xna { } D3D11_TEXTURE2D_DESC desc; - texture2d->dxTexture2D->GetDesc(&desc); - texture2d->dxDescription = desc; + texture2d->impl->dxTexture2D->GetDesc(&desc); + texture2d->impl->dxDescription = desc; resource->Release(); resource = nullptr; @@ -57,7 +60,7 @@ namespace xna { bool Texture2D::Initialize(xna_error_ptr_arg) { - if (dxTexture2D) { + if (impl->dxTexture2D) { xna_error_apply(err, XnaErrorCode::WARNING_INITIALIZED_RESOURCE); return false; } @@ -67,7 +70,7 @@ namespace xna { return false; } - auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D); + auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -75,14 +78,14 @@ namespace xna { } ID3D11Resource* resource = nullptr; - hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); return false; } - hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource); + hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); if (resource) { resource->Release(); @@ -97,33 +100,100 @@ namespace xna { return true; } - Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : GraphicsResource(device) { - setDefaultDesc(); - dxDescription.Width = static_cast(width); - dxDescription.Height = static_cast(height); + void setDefaultDesc(Texture2D::PlatformImplementation& impl) { + impl.dxDescription.MipLevels = 1; + impl.dxDescription.ArraySize = 1; + impl.dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + impl.dxDescription.SampleDesc.Count = 1; + impl.dxDescription.Usage = D3D11_USAGE_DEFAULT; + impl.dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE; + + impl.dxShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + impl.dxShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; + impl.dxShaderDescription.Texture2D.MostDetailedMip = 0; + } + + Texture2D::Texture2D() : GraphicsResource(nullptr) { + impl = unew(); + setDefaultDesc(*impl); + } + + Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : GraphicsResource(device) { + impl = unew(); + setDefaultDesc(*impl); + impl->dxDescription.Width = static_cast(width); + impl->dxDescription.Height = static_cast(height); } Texture2D::Texture2D(sptr const& device) : GraphicsResource(device) { - setDefaultDesc(); + impl = unew(); + setDefaultDesc(*impl); } Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device) { - setDefaultDesc(); - dxDescription.Width = static_cast(width); - dxDescription.Height = static_cast(height); - dxDescription.MipLevels = static_cast(mipMap); - dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format); + impl = unew(); + setDefaultDesc(*impl); + impl->dxDescription.Width = static_cast(width); + impl->dxDescription.Height = static_cast(height); + impl->dxDescription.MipLevels = static_cast(mipMap); + impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format); + } + + HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data, xna_error_ptr_arg) + { + if (!impl.dxTexture2D) { + auto hr = device._device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return hr; + } + } + + ID3D11Resource* resource = nullptr; + auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return hr; + } + + constexpr int R8G8B8A8U_BYTE_SIZE = 4; + device._context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + + if (impl.dxShaderResource) { + impl.dxShaderResource->Release(); + impl.dxShaderResource = nullptr; + } + + impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; + hr = device._device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource); + + if (resource) { + resource->Release(); + resource = nullptr; + } + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return hr; + } + + impl.dxTexture2D->GetDesc(&impl.dxDescription); + + return NO_ERROR; } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !m_device->_context) { + if (!impl || !m_device || !m_device->_device || !m_device->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return; } - internalSetData(data.data(), err); + internalSetData(*impl, *m_device, data.data(), err); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) @@ -145,7 +215,7 @@ namespace xna { ++fIndex; } - internalSetData(finalData.data(), err); + internalSetData(*impl, *m_device, finalData.data(), err); } void Texture2D::SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) @@ -167,8 +237,8 @@ namespace xna { ++fIndex; } - if (!dxTexture2D) { - auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D); + if (!impl->dxTexture2D) { + auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -177,7 +247,7 @@ namespace xna { } ID3D11Resource* resource = nullptr; - auto hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); + auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -195,15 +265,16 @@ namespace xna { box.front = 0; } - m_device->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + constexpr int R8G8B8A8U_BYTE_SIZE = 4; + m_device->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - if (dxShaderResource) { - dxShaderResource->Release(); - dxShaderResource = nullptr; + if (impl->dxShaderResource) { + impl->dxShaderResource->Release(); + impl->dxShaderResource = nullptr; } - dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels; - hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource); + impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels; + hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); if (resource) { resource->Release(); @@ -215,7 +286,7 @@ namespace xna { return; } - dxTexture2D->GetDesc(&dxDescription); + impl->dxTexture2D->GetDesc(&impl->dxDescription); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) @@ -233,7 +304,7 @@ namespace xna { ++finalDataIndex; } - internalSetData(finalData.data(), err); + internalSetData(*impl, *m_device, finalData.data(), err); } sptr Texture2D::FromMemory(GraphicsDevice& device, std::vector const& data, xna_error_ptr_arg) @@ -248,7 +319,7 @@ namespace xna { data.data(), data.size(), &resource, - &texture2d->dxShaderResource); + &texture2d->impl->dxShaderResource); if (FAILED(hr)) { @@ -262,7 +333,7 @@ namespace xna { return nullptr; } - hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D); + hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -276,54 +347,12 @@ namespace xna { } D3D11_TEXTURE2D_DESC desc; - texture2d->dxTexture2D->GetDesc(&desc); - texture2d->dxDescription = desc; + texture2d->impl->dxTexture2D->GetDesc(&desc); + texture2d->impl->dxDescription = desc; resource->Release(); resource = nullptr; return texture2d; - } - - void Texture2D::internalSetData(UINT const* data, xna_error_ptr_arg) - { - if (!dxTexture2D) { - auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return; - } - } - - ID3D11Resource* resource = nullptr; - auto hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return; - } - - m_device->_context->UpdateSubresource(resource, 0, nullptr, data, dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - - if (dxShaderResource) { - dxShaderResource->Release(); - dxShaderResource = nullptr; - } - - dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels; - hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource); - - if (resource) { - resource->Release(); - resource = nullptr; - } - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return; - } - - dxTexture2D->GetDesc(&dxDescription); - } + } } \ No newline at end of file diff --git a/inc/graphics/rendertarget.hpp b/inc/graphics/rendertarget.hpp index 90361db..1a41f3d 100644 --- a/inc/graphics/rendertarget.hpp +++ b/inc/graphics/rendertarget.hpp @@ -2,15 +2,21 @@ #define XNA_GRAPHICS_RENDERTARGET_HPP #include "../default.hpp" - +#include "texture.hpp" namespace xna { - class IRenderTarget2D { + class RenderTarget2D : public Texture2D { public: - virtual ~IRenderTarget2D(){} + RenderTarget2D(); + RenderTarget2D(sptr const& device); - virtual bool Initialize(xna_error_nullarg) = 0; - virtual bool Apply(xna_error_nullarg) = 0; + ~RenderTarget2D() override; + bool Initialize(xna_error_nullarg); + bool Apply(xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr render_impl = nullptr; }; } diff --git a/inc/graphics/texture.hpp b/inc/graphics/texture.hpp index ec7dbf4..26ea477 100644 --- a/inc/graphics/texture.hpp +++ b/inc/graphics/texture.hpp @@ -2,18 +2,35 @@ #define XNA_GRAPHICS_TEXTURE_HPP #include "../default.hpp" +#include "gresource.hpp" namespace xna { class Texture { + public: + ~Texture() {} }; - class ITexture2D : public Texture { + class Texture2D : public Texture, public GraphicsResource { public: - virtual ~ITexture2D(){} - virtual Int Width() const = 0; - virtual Int Height() const = 0; - virtual Rectangle Bounds() const = 0; - virtual bool Initialize(xna_error_nullarg) = 0; + Texture2D(); + Texture2D(sptr const& device); + Texture2D(sptr const& device, size_t width, size_t height); + Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); + ~Texture2D() override; + Int Width() const; + Int Height() const; + Rectangle Bounds() const; + bool Initialize(xna_error_nullarg); + void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); + void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); + void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); + void SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount, xna_error_nullarg); + static sptr FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg); + static sptr FromMemory(GraphicsDevice& device, std::vector const& data, xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr impl = nullptr; }; } diff --git a/inc/platform-dx/content-readers/texture2Dreader-dx.hpp b/inc/platform-dx/content-readers/texture2Dreader-dx.hpp index 7bff5bc..4bd1c7b 100644 --- a/inc/platform-dx/content-readers/texture2Dreader-dx.hpp +++ b/inc/platform-dx/content-readers/texture2Dreader-dx.hpp @@ -4,7 +4,8 @@ #include "../../content/manager.hpp" #include "../../content/reader.hpp" #include "../../csharp/type.hpp" -#include "../texture-dx.hpp" +#include "graphics/texture.hpp" +#include "platform-dx/implementations.hpp" namespace xna { class Texture2DReader : public ContentTypeReaderT { diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 6c978ac..7631278 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -1,3 +1,6 @@ +#ifndef XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP +#define XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP + #include "dxheaders.hpp" #include "graphics/adapter.hpp" #include "graphics/blendstate.hpp" @@ -12,9 +15,11 @@ #include "input/mouse.hpp" #include "graphics/rasterizerstate.hpp" #include "graphics/presentparams.hpp" -#include "platform-dx/rendertarget-dx.hpp" #include "graphics/shader.hpp" #include "graphics/swapchain.hpp" +#include "graphics/texture.hpp" +#include "graphics/rendertarget.hpp" +#include "device-dx.hpp" namespace xna { struct SpriteFont::PlatformImplementation { @@ -26,7 +31,7 @@ namespace xna { }; struct GraphicsAdapter::PlatformImplementation { - ~PlatformImplementation(){ + ~PlatformImplementation() { if (dxadapter) { dxadapter->Release(); dxadapter = nullptr; @@ -40,7 +45,7 @@ namespace xna { sptr _currentDisplayMode = nullptr; public: - bool GetOutput(UINT slot, IDXGIOutput*& output); + bool GetOutput(UINT slot, IDXGIOutput*& output); }; struct BlendRenderTarget { @@ -56,7 +61,7 @@ namespace xna { constexpr BlendRenderTarget() = default; }; - struct BlendState::PlatformImplementation { + struct BlendState::PlatformImplementation { ~PlatformImplementation() { if (dxBlendState) { dxBlendState->Release(); @@ -67,8 +72,8 @@ namespace xna { ID3D11BlendState* dxBlendState = nullptr; D3D11_BLEND_DESC dxDescription{}; float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; - UINT sampleMask{ 0xffffffff }; - }; + UINT sampleMask{ 0xffffffff }; + }; struct ConstantBuffer::PlatformImplementation { ~PlatformImplementation() { @@ -109,7 +114,7 @@ namespace xna { ID3D11DepthStencilState* dxDepthStencil = nullptr; D3D11_DEPTH_STENCIL_DESC dxDescription{}; - }; + }; struct DisplayModeRefreshRate { constexpr DisplayModeRefreshRate() = default; @@ -265,4 +270,38 @@ namespace xna { return !FAILED(hr); } }; -} \ No newline at end of file + + struct Texture2D::PlatformImplementation { + ~PlatformImplementation() { + if (dxTexture2D) { + dxTexture2D->Release(); + dxTexture2D = nullptr; + } + + if (dxShaderResource) { + dxShaderResource->Release(); + dxShaderResource = nullptr; + } + } + + ID3D11Texture2D* dxTexture2D{ nullptr }; + ID3D11ShaderResourceView* 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; + D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{}; + }; +} + +#endif \ No newline at end of file diff --git a/inc/platform-dx/rendertarget-dx.hpp b/inc/platform-dx/rendertarget-dx.hpp deleted file mode 100644 index 0ed0667..0000000 --- a/inc/platform-dx/rendertarget-dx.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef XNA_PLATFORM_RENDERTARGET_DX_HPP -#define XNA_PLATFORM_RENDERTARGET_DX_HPP - -#include "../graphics/rendertarget.hpp" -#include "texture-dx.hpp" -#include "dxgi.h" -#include "d3d11.h" - -namespace xna { - class RenderTarget2D : public IRenderTarget2D, public Texture2D { - public: - RenderTarget2D(sptr const& device) : Texture2D(device){} - - virtual ~RenderTarget2D() override { - if (_renderTargetView) { - _renderTargetView->Release(); - _renderTargetView = nullptr; - } - } - - virtual bool Initialize(xna_error_nullarg) override; - virtual bool Apply(xna_error_nullarg) override; - - public: - ID3D11RenderTargetView* _renderTargetView = nullptr; - D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{}; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/texture-dx.hpp b/inc/platform-dx/texture-dx.hpp deleted file mode 100644 index b0d5309..0000000 --- a/inc/platform-dx/texture-dx.hpp +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef XNA_PLATFORM_TEXTURE_DX_HPP -#define XNA_PLATFORM_TEXTURE_DX_HPP - -#include "../common/numerics.hpp" -#include "../graphics/gresource.hpp" -#include "../graphics/texture.hpp" -#include "dxheaders.hpp" -#include "device-dx.hpp" - -namespace xna { - class Texture2D : public ITexture2D, public GraphicsResource { - public: - Texture2D() : GraphicsResource(nullptr){ - setDefaultDesc(); - } - - Texture2D(sptr const& device); - Texture2D(sptr const& device, size_t width, size_t height); - Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); - - virtual ~Texture2D() override { - if (dxTexture2D) { - dxTexture2D->Release(); - dxTexture2D = nullptr; - } - - if (dxShaderResource) { - dxShaderResource->Release(); - dxShaderResource = nullptr; - } - } - - virtual constexpr Int Width() const override { - return dxDescription.Width; - } - - virtual constexpr Int Height() const override { - return dxDescription.Height; - } - - constexpr Rectangle Bounds() const override { - return { 0, 0, static_cast(dxDescription.Width), static_cast(dxDescription.Height) }; - } - - bool Initialize(xna_error_nullarg) override; - - void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); - void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); - void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg); - - void SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount, xna_error_nullarg); - - static sptr FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg); - static sptr FromMemory(GraphicsDevice& device, std::vector const& data, xna_error_nullarg); - - public: - ID3D11Texture2D* dxTexture2D{ nullptr }; - ID3D11ShaderResourceView* dxShaderResource{ nullptr }; - D3D11_SUBRESOURCE_DATA dxSubResource{}; - D3D11_TEXTURE2D_DESC dxDescription{}; - D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{}; - - private: - static constexpr int R8G8B8A8U_BYTE_SIZE = 4; - - void setDefaultDesc() { - dxDescription.MipLevels = 1; - dxDescription.ArraySize = 1; - dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - dxDescription.SampleDesc.Count = 1; - dxDescription.Usage = D3D11_USAGE_DEFAULT; - dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE; - - dxShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - dxShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels; - dxShaderDescription.Texture2D.MostDetailedMip = 0; - } - - void internalSetData(UINT const* data, xna_error_nullarg); - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index b99939c..b355340 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -7,9 +7,7 @@ #include "gdeviceinfo-dx.hpp" #include "gdevicemanager-dx.hpp" #include "init-dx.hpp" -#include "rendertarget-dx.hpp" #include "soundeffect-dx.hpp" -#include "texture-dx.hpp" #include "vertexbuffer-dx.hpp" #include "vertexinput-dx.hpp" #include "window-dx.hpp"