From 61da1aac46f22d407ba697c5909261aec1199fa5 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 16 Nov 2024 12:15:40 -0300 Subject: [PATCH] Adiciona PlatformImplementation em Texture2D --- includes/xna-dx/implementations.hpp | 38 +++-- includes/xna/content/readers/graphics.hpp | 2 + includes/xna/graphics/rendertarget.hpp | 2 + includes/xna/graphics/texture.hpp | 49 +++--- includes/xna/graphics/vertexposition.hpp | 1 - sources/framework-dx/effect.cpp | 4 +- sources/framework-dx/rendertarget.cpp | 14 +- sources/framework-dx/sprite.cpp | 18 +-- sources/framework-dx/swapchain.cpp | 2 +- sources/framework-dx/texture.cpp | 177 ++++++++++------------ 10 files changed, 156 insertions(+), 151 deletions(-) diff --git a/includes/xna-dx/implementations.hpp b/includes/xna-dx/implementations.hpp index 4440962..5d1a5fe 100644 --- a/includes/xna-dx/implementations.hpp +++ b/includes/xna-dx/implementations.hpp @@ -61,19 +61,43 @@ namespace xna { struct SamplerStateImplementation { comptr SamplerState; - D3D11_SAMPLER_DESC Description; + D3D11_SAMPLER_DESC Description{}; }; struct SpriteBatchImplementation { std::shared_ptr SpriteBatch; - comptr InputLayout; std::shared_ptr EffectBuffer; + comptr InputLayout; }; struct SpriteFontImplementation { std::unique_ptr SpriteFont; }; + struct Texture2DImplementation { + Texture2DImplementation() { + Description.MipLevels = 1; + Description.ArraySize = 1; + Description.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + Description.SampleDesc.Count = 1; + Description.Usage = D3D11_USAGE_DEFAULT; + Description.BindFlags = D3D11_BIND_SHADER_RESOURCE; + + ShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + ShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + ShaderDescription.Texture2D.MipLevels = Description.MipLevels; + ShaderDescription.Texture2D.MostDetailedMip = 0; + } + + comptr Texture2D; + comptr ShaderResource; + D3D11_SUBRESOURCE_DATA SubResource{}; + D3D11_TEXTURE2D_DESC Description{}; + D3D11_SHADER_RESOURCE_VIEW_DESC ShaderDescription{}; + + HRESULT SetData(GraphicsDevice& device, UINT const* data); + }; + struct GamePad::PlatformImplementation { uptr _dxGamePad = unew(); @@ -127,15 +151,7 @@ namespace xna { return !FAILED(hr); } - }; - - struct Texture2D::PlatformImplementation { - comptr dxTexture2D{ nullptr }; - comptr dxShaderResource{ nullptr }; - D3D11_SUBRESOURCE_DATA dxSubResource{}; - D3D11_TEXTURE2D_DESC dxDescription{}; - D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{}; - }; + }; struct RenderTarget2D::PlatformImplementation { comptr _renderTargetView = nullptr; diff --git a/includes/xna/content/readers/graphics.hpp b/includes/xna/content/readers/graphics.hpp index 3cec14e..b9946c9 100644 --- a/includes/xna/content/readers/graphics.hpp +++ b/includes/xna/content/readers/graphics.hpp @@ -11,6 +11,8 @@ #include "../../graphics/shared.hpp" namespace xna { + using PTexture2D = std::shared_ptr; + class Texture2DReader : public ContentTypeReaderT { public: Texture2DReader() : ContentTypeReaderT(typeof()) { diff --git a/includes/xna/graphics/rendertarget.hpp b/includes/xna/graphics/rendertarget.hpp index 168fd6c..8e7979a 100644 --- a/includes/xna/graphics/rendertarget.hpp +++ b/includes/xna/graphics/rendertarget.hpp @@ -5,6 +5,8 @@ #include "texture.hpp" namespace xna { + struct RenderTarget2DImplementation; + //Contains a 2D texture that can be used as a render target. class RenderTarget2D : public Texture2D { public: diff --git a/includes/xna/graphics/texture.hpp b/includes/xna/graphics/texture.hpp index 7a7dd53..09c5fb6 100644 --- a/includes/xna/graphics/texture.hpp +++ b/includes/xna/graphics/texture.hpp @@ -1,72 +1,71 @@ #ifndef XNA_GRAPHICS_TEXTURE_HPP #define XNA_GRAPHICS_TEXTURE_HPP -#include "../default.hpp" +#include "../csharp/stream.hpp" +#include "../platform.hpp" #include "gresource.hpp" #include "shared.hpp" +#include +#include +#include namespace xna { //Represents a texture resource. class Texture : public GraphicsResource { public: - Texture(P_GraphicsDevice const& graphicsDevice) : GraphicsResource(graphicsDevice) {} + Texture(std::shared_ptr const& graphicsDevice) : GraphicsResource(graphicsDevice) {} //Gets the format of the texture data. virtual SurfaceFormat Format() const = 0; //Gets the number of texture levels in a multilevel texture. - virtual Int LevelCount() const = 0; + virtual int32_t LevelCount() const = 0; }; + struct Texture2DImplementation; + //Represents a 2D grid of texels. - class Texture2D : public Texture { + class Texture2D : public Texture, public PlatformImplementation { public: Texture2D(); - Texture2D(P_GraphicsDevice const& device); - Texture2D(P_GraphicsDevice const& device, size_t width, size_t height); - Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); + Texture2D(std::shared_ptr const& device); + Texture2D(std::shared_ptr const& device, size_t width, size_t height); + Texture2D(std::shared_ptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); //Gets the width of this texture resource, in pixels. - constexpr Int Width() const { return width; } + constexpr int32_t Width() const { return width; } //Gets the height of this texture resource, in pixels. - constexpr Int Height() const { return height; } + constexpr int32_t Height() const { return height; } //Gets the size of this resource. constexpr Rectangle Bounds() const { return { 0, 0, width, height }; } //Gets the format of the texture data. constexpr SurfaceFormat Format() const override { return surfaceFormat; } //Gets the number of texture levels in a multilevel texture. - constexpr Int LevelCount() const { return levelCount; } + constexpr int32_t LevelCount() const { return levelCount; } //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); //Sets data to the texture. - void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); //Sets data to the texture. - void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); //Sets data to the texture. - void SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount); + void SetData(int32_t level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount); //Loads texture data from a stream. - static P_Texture2D FromStream(GraphicsDevice& device, P_Stream const& stream); + static P_Texture2D FromStream(GraphicsDevice& device, std::shared_ptr const& stream); //Loads texture data from a file. - static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName); + static P_Texture2D FromStream(GraphicsDevice& device, std::string const& fileName); //Loads texture data from a data. - static P_Texture2D FromStream(GraphicsDevice& device, std::vector const& data); + static P_Texture2D FromStream(GraphicsDevice& device, std::vector const& data); void Initialize(); protected: SurfaceFormat surfaceFormat{ SurfaceFormat::Color }; - Int levelCount{ 0 }; + int32_t levelCount{ 0 }; int width{ 0 }; - int height{ 0 }; - - public: - struct PlatformImplementation; - uptr impl = nullptr; + int height{ 0 }; }; - - - using PTexture2D = P_Texture2D; } #endif \ No newline at end of file diff --git a/includes/xna/graphics/vertexposition.hpp b/includes/xna/graphics/vertexposition.hpp index e3048f9..fba723c 100644 --- a/includes/xna/graphics/vertexposition.hpp +++ b/includes/xna/graphics/vertexposition.hpp @@ -3,7 +3,6 @@ #include "../common/numerics.hpp" #include "../common/color.hpp" -#include "../default.hpp" namespace xna { //Describes a custom vertex format structure that contains position and color information. diff --git a/sources/framework-dx/effect.cpp b/sources/framework-dx/effect.cpp index c702c68..4657aa2 100644 --- a/sources/framework-dx/effect.cpp +++ b/sources/framework-dx/effect.cpp @@ -99,10 +99,10 @@ namespace xna { } void BasicEffect::Texture(sptr const& value) { - if (!value || !value->impl || !value->impl->dxShaderResource) + if (!value || !value->Implementation || !value->Implementation->ShaderResource) Exception::Throw(Exception::ARGUMENT_IS_NULL); - impl->dxBasicEffect->SetTexture(value->impl->dxShaderResource.Get()); + impl->dxBasicEffect->SetTexture(value->Implementation->ShaderResource.Get()); } void BasicEffect::TextureEnabled(bool value) { diff --git a/sources/framework-dx/rendertarget.cpp b/sources/framework-dx/rendertarget.cpp index 1bc3a9d..e9db058 100644 --- a/sources/framework-dx/rendertarget.cpp +++ b/sources/framework-dx/rendertarget.cpp @@ -12,10 +12,10 @@ namespace xna { P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) { auto& swapChain = device->Implementation->SwapChain; auto rt = snew(device); - auto& implementation = rt->impl; + auto& implementation = rt->Implementation; auto& implementation2 = rt->impl2; - if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D)) + if (!swapChain->impl->GetBackBuffer(implementation->Texture2D)) { Exception::Throw(Exception::FAILED_TO_CREATE); } @@ -26,23 +26,23 @@ namespace xna { } void RenderTarget2D::Initialize() { - if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) { + if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); } if (impl2->_renderTargetView) return; - impl->dxDescription.Width = width; - impl->dxDescription.Height = height; - impl->dxDescription.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET; + Implementation->Description.Width = width; + Implementation->Description.Height = height; + Implementation->Description.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET; Texture2D::Initialize(); auto& dxdevice = BaseGraphicsDevice->Implementation->Device; const auto hr = dxdevice->CreateRenderTargetView( - impl->dxTexture2D.Get(), + Implementation->Texture2D.Get(), NULL, impl2->_renderTargetView.ReleaseAndGetAddressOf()); diff --git a/sources/framework-dx/sprite.cpp b/sources/framework-dx/sprite.cpp index 9ec1aa3..1a0ac1d 100644 --- a/sources/framework-dx/sprite.cpp +++ b/sources/framework-dx/sprite.cpp @@ -23,7 +23,7 @@ namespace xna { std::optional const& defaultCharacter) { Exception::ThrowIfNull(texture, nameof(texture)); - Exception::ThrowIfNull(texture->impl->dxShaderResource.Get(), nameof(texture->impl->dxShaderResource)); + Exception::ThrowIfNull(texture->Implementation->ShaderResource.Get(), nameof(texture->Implementation->ShaderResource)); if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size())) Exception::Throw("Cropping, charmap and kerning (if not empty) must all be the same size."); @@ -55,7 +55,7 @@ namespace xna { Implementation = unew(); Implementation->SpriteFont = unew( //ID3D11ShaderResourceView* texture - texture->impl->dxShaderResource.Get(), + texture->Implementation->ShaderResource.Get(), //Glyph const* glyphs dxGlyps.data(), //size_t glyphCount @@ -177,7 +177,7 @@ namespace xna { const auto _color = DxHelpers::VectorToDx(v4); Implementation->SpriteBatch->Draw( - texture.impl->dxShaderResource.Get(), + texture.Implementation->ShaderResource.Get(), _position, _color ); @@ -195,7 +195,7 @@ namespace xna { }; Implementation->SpriteBatch->Draw( - texture.impl->dxShaderResource.Get(), + texture.Implementation->ShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color); @@ -216,7 +216,7 @@ namespace xna { const DxSpriteEffects _effects = static_cast(effects); Implementation->SpriteBatch->Draw( - texture.impl->dxShaderResource.Get(), + texture.Implementation->ShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -243,7 +243,7 @@ namespace xna { const XMFLOAT2 _scale = { scale.X, scale.Y }; Implementation->SpriteBatch->Draw( - texture.impl->dxShaderResource.Get(), + texture.Implementation->ShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, _color, @@ -260,7 +260,7 @@ namespace xna { const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - Implementation->SpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); + Implementation->SpriteBatch->Draw(texture.Implementation->ShaderResource.Get(), _destinationRect, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { @@ -278,7 +278,7 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - Implementation->SpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); + Implementation->SpriteBatch->Draw(texture.Implementation->ShaderResource.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) { @@ -297,7 +297,7 @@ namespace xna { const auto _effects = static_cast(effects); Implementation->SpriteBatch->Draw( - texture.impl->dxShaderResource.Get(), + texture.Implementation->ShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color, diff --git a/sources/framework-dx/swapchain.cpp b/sources/framework-dx/swapchain.cpp index 5ae5700..e3d366a 100644 --- a/sources/framework-dx/swapchain.cpp +++ b/sources/framework-dx/swapchain.cpp @@ -74,7 +74,7 @@ namespace xna { if (!impl || !impl->dxSwapChain) return false; - const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.impl->dxTexture2D)); + const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.Implementation->Texture2D)); return !FAILED(hr); } diff --git a/sources/framework-dx/texture.cpp b/sources/framework-dx/texture.cpp index 3322e87..e771964 100644 --- a/sources/framework-dx/texture.cpp +++ b/sources/framework-dx/texture.cpp @@ -1,34 +1,58 @@ #include "xna-dx/framework.hpp" namespace xna { - static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl); - static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data); + HRESULT Texture2DImplementation::SetData(GraphicsDevice& device, UINT const* data) { + if (!Texture2D) { + auto hr = device.Implementation->Device->CreateTexture2D(&Description, nullptr, Texture2D.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + return hr; + } + } + + comptr resource = nullptr; + auto hr = Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); + + if (FAILED(hr)) { + return hr; + } + + constexpr int R8G8B8A8U_BYTE_SIZE = 4; + device.Implementation->Context->UpdateSubresource(resource.Get(), 0, nullptr, data, Description.Width * R8G8B8A8U_BYTE_SIZE, 0); + + ShaderDescription.Texture2D.MipLevels = Description.MipLevels; + hr = device.Implementation->Device->CreateShaderResourceView(resource.Get(), &ShaderDescription, ShaderResource.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + return hr; + } + + Texture2D->GetDesc(&Description); + + return NO_ERROR; + } Texture2D::Texture2D() : Texture(nullptr) { - impl = unew(); - setDefaultTexture2DDesc(*impl); + Implementation = unew(); } 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); + Implementation = unew(); + Implementation->Description.Width = static_cast(this->width); + Implementation->Description.Height = static_cast(this->height); } Texture2D::Texture2D(sptr const& device) : Texture(device) { - impl = unew(); - setDefaultTexture2DDesc(*impl); + Implementation = unew(); } 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); + Implementation = unew(); + Implementation->Description.Width = static_cast(this->width); + Implementation->Description.Height = static_cast(this->height); + Implementation->Description.MipLevels = static_cast(this->levelCount); + Implementation->Description.Format = DxHelpers::SurfaceFormatToDx(format); } void Texture2D::Initialize() @@ -41,51 +65,54 @@ namespace xna { HRESULT hr = 0; - if (!impl->dxTexture2D) { + if (!Implementation->Texture2D) { hr = deviceImpl->Device->CreateTexture2D( - &impl->dxDescription, + &Implementation->Description, nullptr, - impl->dxTexture2D.ReleaseAndGetAddressOf()); + Implementation->Texture2D.ReleaseAndGetAddressOf()); if FAILED(hr) Exception::Throw(Exception::FAILED_TO_CREATE); } else { //Updates description if texture is not null - impl->dxTexture2D->GetDesc(&impl->dxDescription); + Implementation->Texture2D->GetDesc(&Implementation->Description); } comptr resource = nullptr; - hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); + hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); if FAILED(hr) Exception::Throw(Exception::INVALID_OPERATION); //Only initializes if it is a ShaderResource - if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) { + if (Implementation->Description.BindFlags & D3D11_BIND_SHADER_RESOURCE) { hr = deviceImpl->Device->CreateShaderResourceView( resource.Get(), - &impl->dxShaderDescription, - impl->dxShaderResource.ReleaseAndGetAddressOf()); + &Implementation->ShaderDescription, + Implementation->ShaderResource.ReleaseAndGetAddressOf()); 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); + surfaceFormat = DxHelpers::SurfaceFormatToXna(Implementation->Description.Format); + levelCount = static_cast(Implementation->ShaderDescription.Texture2D.MipLevels); + width = static_cast(Implementation->Description.Width); + height = static_cast(Implementation->Description.Height); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) { - if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) { + if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) { Exception::Throw(Exception::INVALID_OPERATION); } - internalTexture2DSetData(*impl, *BaseGraphicsDevice, data.data()); + auto hr = Implementation->SetData(*BaseGraphicsDevice, data.data()); + + if (FAILED(hr)) + Exception::Throw(Exception::FAILED_TO_APPLY); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) @@ -106,7 +133,10 @@ namespace xna { ++fIndex; } - internalTexture2DSetData(*impl, *BaseGraphicsDevice, finalData.data()); + auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data()); + + if (FAILED(hr)) + Exception::Throw(Exception::FAILED_TO_APPLY); } void Texture2D::SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount) @@ -127,8 +157,8 @@ namespace xna { ++fIndex; } - if (!impl->dxTexture2D) { - auto hr = BaseGraphicsDevice->Implementation->Device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.GetAddressOf()); + if (!Implementation->Texture2D) { + auto hr = BaseGraphicsDevice->Implementation->Device->CreateTexture2D(&Implementation->Description, nullptr, Implementation->Texture2D.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); @@ -136,7 +166,7 @@ namespace xna { } comptr resource = nullptr; - auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); + auto hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::INVALID_OPERATION); @@ -154,17 +184,17 @@ namespace xna { } constexpr int R8G8B8A8U_BYTE_SIZE = 4; - BaseGraphicsDevice->Implementation->Context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + BaseGraphicsDevice->Implementation->Context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), Implementation->Description.Width * R8G8B8A8U_BYTE_SIZE, 0); - impl->dxShaderDescription.Format = impl->dxDescription.Format; - impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels; - hr = BaseGraphicsDevice->Implementation->Device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf()); + Implementation->ShaderDescription.Format = Implementation->Description.Format; + Implementation->ShaderDescription.Texture2D.MipLevels = Implementation->Description.MipLevels; + hr = BaseGraphicsDevice->Implementation->Device->CreateShaderResourceView(resource.Get(), &Implementation->ShaderDescription, Implementation->ShaderResource.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); } - impl->dxTexture2D->GetDesc(&impl->dxDescription); + Implementation->Texture2D->GetDesc(&Implementation->Description); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) @@ -181,7 +211,10 @@ namespace xna { ++finalDataIndex; } - internalTexture2DSetData(*impl, *BaseGraphicsDevice, finalData.data()); + auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data()); + + if (FAILED(hr)) + Exception::Throw(Exception::FAILED_TO_APPLY); } P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream) @@ -205,22 +238,22 @@ namespace xna { device.Implementation->Context.Get(), wstr.c_str(), resource.GetAddressOf(), - texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(), + texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf(), 0U); if (FAILED(result)) { return nullptr; } - result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); + result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf()); if (FAILED(result)) { return nullptr; } D3D11_TEXTURE2D_DESC desc; - texture2d->impl->dxTexture2D->GetDesc(&desc); - texture2d->impl->dxDescription = desc; + texture2d->Implementation->Texture2D->GetDesc(&desc); + texture2d->Implementation->Description = desc; return texture2d; } @@ -237,69 +270,23 @@ namespace xna { data.data(), data.size(), resource.GetAddressOf(), - texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf()); + texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf()); if (FAILED(hr)) { return nullptr; } - hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); + hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf()); if (FAILED(hr)) { return nullptr; } D3D11_TEXTURE2D_DESC desc; - texture2d->impl->dxTexture2D->GetDesc(&desc); - texture2d->impl->dxDescription = desc; + texture2d->Implementation->Texture2D->GetDesc(&desc); + texture2d->Implementation->Description = desc; return texture2d; - } - - HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data) - { - if (!impl.dxTexture2D) { - auto hr = device.Implementation->Device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - } - - comptr resource = nullptr; - auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::INVALID_OPERATION); - } - - constexpr int R8G8B8A8U_BYTE_SIZE = 4; - device.Implementation->Context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - - impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; - hr = device.Implementation->Device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - - impl.dxTexture2D->GetDesc(&impl.dxDescription); - - return NO_ERROR; - } - - void setDefaultTexture2DDesc(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; - } + } } \ No newline at end of file