From 1955962ae871550836c22df5ad13d35b9bafcd02 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 15 Nov 2024 16:45:48 -0300 Subject: [PATCH] Adiciona PlatformImplementation em SamplerState --- includes/xna-dx/implementations.hpp | 14 +-- includes/xna/enumerations.hpp | 23 +---- includes/xna/graphics/samplerstate.hpp | 87 ++++++++++++------ sources/framework-dx/samplerstate.cpp | 122 ++++++++++++------------- sources/framework-dx/sprite.cpp | 2 +- 5 files changed, 131 insertions(+), 117 deletions(-) diff --git a/includes/xna-dx/implementations.hpp b/includes/xna-dx/implementations.hpp index 6070bab..2f0491b 100644 --- a/includes/xna-dx/implementations.hpp +++ b/includes/xna-dx/implementations.hpp @@ -54,6 +54,11 @@ namespace xna { UINT vSyncValue = 1; }; + struct SamplerStateImplementation { + comptr SamplerState; + D3D11_SAMPLER_DESC Description; + }; + struct SpriteBatchImplementation { std::shared_ptr SpriteBatch; comptr InputLayout; @@ -62,7 +67,7 @@ namespace xna { struct SpriteFontImplementation { std::unique_ptr SpriteFont; - }; + }; struct GamePad::PlatformImplementation { uptr _dxGamePad = unew(); @@ -107,12 +112,7 @@ namespace xna { struct RasterizerState::PlatformImplementation { comptr dxRasterizerState = nullptr; D3D11_RASTERIZER_DESC dxDescription{}; - }; - - struct SamplerState::PlatformImplementation { - comptr _samplerState = nullptr; - D3D11_SAMPLER_DESC _description{}; - }; + }; struct SwapChain::PlatformImplementation { comptr dxSwapChain{ nullptr }; diff --git a/includes/xna/enumerations.hpp b/includes/xna/enumerations.hpp index 3cf975a..e03dea3 100644 --- a/includes/xna/enumerations.hpp +++ b/includes/xna/enumerations.hpp @@ -142,28 +142,7 @@ namespace xna { FlipHorizontally = 1, FlipVertically = 2, Both = FlipHorizontally | FlipVertically - }; - - - enum class TextureAddressMode { - Wrap, - Mirror, - Clamp, - Border, - MirrorOnce - }; - - enum class TextureFilter { - Linear, - Point, - Anisotropic, - LinearMipPoint, - PointMipLinear, - MinLinearMagPointMipLinear, - MinLinearMagPointMipPoint, - MinPointMagLinearMipLinear, - MinPointMagLinearMipPoint, - }; + }; constexpr int SURFACE_FORMAT_COUNT = 19; } diff --git a/includes/xna/graphics/samplerstate.hpp b/includes/xna/graphics/samplerstate.hpp index 105b24a..bfc5324 100644 --- a/includes/xna/graphics/samplerstate.hpp +++ b/includes/xna/graphics/samplerstate.hpp @@ -1,21 +1,64 @@ #ifndef XNA_GRAPHICS_SAMPLERSTATE_HPP #define XNA_GRAPHICS_SAMPLERSTATE_HPP -#include "../default.hpp" -#include "shared.hpp" +#include "../platform.hpp" #include "gresource.hpp" +#include "shared.hpp" +#include +#include namespace xna { + //Defines modes for addressing texels using texture coordinates that are outside of the typical range of 0.0 to 1.0. + enum class TextureAddressMode { + //Tile the texture at every integer junction. + //For example, for u values between 0 and 3, the texture is repeated three times; no mirroring is performed. + Wrap, + //Similar to Wrap, except that the texture is flipped at every integer junction. + //For u values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is flipped (mirrored); between 2 and 3, the texture is normal again, and so on. + Mirror, + //Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively. + Clamp, + //Texture coordinates outside the range [0.0, 1.0] are set to the border color specified. + Border, + //Similar to Mirror and Clamp. + //Takes the absolute value of the texture coordinate (thus, mirroring around 0), and then clamps to the maximum value. + MirrorOnce + }; + + //Defines filtering types during texture sampling. + enum class TextureFilter { + //Use linear filtering. + Linear, + //Use point filtering. + Point, + //Use anisotropic filtering. + Anisotropic, + //Use linear filtering to shrink or expand, and point filtering between mipmap levels (mip). + LinearMipPoint, + //Use point filtering to shrink (minify) or expand (magnify), and linear filtering between mipmap levels. + PointMipLinear, + //Use linear filtering to shrink, point filtering to expand, and linear filtering between mipmap levels. + MinLinearMagPointMipLinear, + //Use linear filtering to shrink, point filtering to expand, and point filtering between mipmap levels. + MinLinearMagPointMipPoint, + //Use point filtering to shrink, linear filtering to expand, and linear filtering between mipmap levels. + MinPointMagLinearMipLinear, + //Use point filtering to shrink, linear filtering to expand, and point filtering between mipmap levels. + MinPointMagLinearMipPoint, + }; + + struct SamplerStateImplementation; + //Contains sampler state, which determines how to sample texture data. - class SamplerState : public GraphicsResource { + class SamplerState : public GraphicsResource, public PlatformImplementation { public: SamplerState(); - SamplerState(sptr const& device); + SamplerState(std::shared_ptr const& device); //Gets or sets the maximum anisotropy. The default value is 0. - void MaxAnisotropy(Uint value); + void MaxAnisotropy(uint32_t value); //Gets or sets the maximum anisotropy. The default value is 0. - Uint MaxAnisotropy() const; + uint32_t MaxAnisotropy() const; //Gets or sets the type of filtering during sampling. void Filter(TextureFilter value); //Gets or sets the type of filtering during sampling. @@ -49,30 +92,24 @@ namespace xna { float MinMipLevel() const; //Contains default state for point filtering and texture coordinate wrapping. - static uptr PoinWrap(); + static std::unique_ptr PoinWrap(); //Contains default state for point filtering and texture coordinate clamping. - static uptr PointClamp(); + static std::unique_ptr PointClamp(); //Contains default state for linear filtering and texture coordinate wrapping. - static uptr LinearWrap(); + static std::unique_ptr LinearWrap(); //Contains default state for linear filtering and texture coordinate clamping. - static uptr LinearClamp(); + static std::unique_ptr LinearClamp(); //Contains default state for anisotropic filtering and texture coordinate wrapping. - static uptr AnisotropicWrap(); + static std::unique_ptr AnisotropicWrap(); //Contains default state for anisotropic filtering and texture coordinate clamping. - static uptr AnisotropicClamp(); + static std::unique_ptr AnisotropicClamp(); ComparisonFunction Comparison() const; void Comparison(ComparisonFunction value); bool Initialize(); - bool Apply(); - - public: - struct PlatformImplementation; - uptr impl = nullptr; - }; - - using PSamplerState = sptr; + bool Apply(); + }; //Collection of SamplerState objects. class SamplerStateCollection { @@ -82,10 +119,10 @@ namespace xna { SamplerStateCollection(size_t size) : samplers(size){} - SamplerStateCollection(std::vector const& samplers) + SamplerStateCollection(std::vector> const& samplers) : samplers(samplers) {} - PSamplerState operator[](size_t index) { + std::shared_ptr operator[](size_t index) { if (index >= samplers.size()) return nullptr; @@ -99,10 +136,8 @@ namespace xna { void Apply(GraphicsDevice const& device); public: - std::vector samplers; - }; - - using PSamplerStateCollection = sptr; + std::vector> samplers; + }; } #endif \ No newline at end of file diff --git a/sources/framework-dx/samplerstate.cpp b/sources/framework-dx/samplerstate.cpp index e91d9c2..c90fbd3 100644 --- a/sources/framework-dx/samplerstate.cpp +++ b/sources/framework-dx/samplerstate.cpp @@ -6,22 +6,22 @@ namespace xna { SamplerState::SamplerState() : SamplerState(nullptr){} SamplerState::SamplerState(sptr const& device) : GraphicsResource(device) { - impl = unew(); + Implementation = unew(); } bool SamplerState::Initialize() { - if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) { + if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); } - if (impl->_samplerState) { - impl->_samplerState = nullptr; + if (Implementation->SamplerState) { + Implementation->SamplerState = nullptr; } const auto hr = BaseGraphicsDevice->Implementation->Device->CreateSamplerState( - &impl->_description, - impl->_samplerState.GetAddressOf()); + &Implementation->Description, + Implementation->SamplerState.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); @@ -32,15 +32,15 @@ namespace xna { bool SamplerState::Apply() { - if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) { + if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) { Exception::Throw(Exception::INVALID_OPERATION); } - if (!impl->_samplerState) { + if (!Implementation->SamplerState) { Exception::Throw(Exception::INVALID_OPERATION); } - BaseGraphicsDevice->Implementation->Context->PSSetSamplers(0, 1, impl->_samplerState.GetAddressOf()); + BaseGraphicsDevice->Implementation->Context->PSSetSamplers(0, 1, Implementation->SamplerState.GetAddressOf()); return true; } @@ -58,10 +58,10 @@ namespace xna { for (size_t i = 0; i < samplers.size(); ++i) { const auto& current = samplers[0]; - if (!current || !current->impl || !current->impl->_samplerState) + if (!current || !current->Implementation || !current->Implementation->SamplerState) Exception::Throw(Exception::INVALID_OPERATION); - states[i] = current->impl->_samplerState.Get(); + states[i] = current->Implementation->SamplerState.Get(); states[i]->AddRef(); } @@ -78,55 +78,55 @@ namespace xna { uptr SamplerState::PoinWrap() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; return state; } uptr SamplerState::PointClamp() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; return state; } uptr SamplerState::LinearWrap() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; return state; } uptr SamplerState::LinearClamp() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; return state; } uptr SamplerState::AnisotropicWrap() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_ANISOTROPIC; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.Filter = D3D11_FILTER_ANISOTROPIC; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; return state; } uptr SamplerState::AnisotropicClamp() { auto state = unew(); - state->impl->_description.Filter = D3D11_FILTER_ANISOTROPIC; - state->impl->_description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; - state->impl->_description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.Filter = D3D11_FILTER_ANISOTROPIC; + state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; return state; } @@ -134,31 +134,31 @@ namespace xna { switch (value) { case xna::TextureFilter::Linear: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR; break; case xna::TextureFilter::Point: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_POINT; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_POINT; break; case xna::TextureFilter::Anisotropic: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_ANISOTROPIC; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_ANISOTROPIC; break; case xna::TextureFilter::LinearMipPoint: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; break; case xna::TextureFilter::PointMipLinear: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR; break; case xna::TextureFilter::MinLinearMagPointMipLinear: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR; break; case xna::TextureFilter::MinLinearMagPointMipPoint: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; break; case xna::TextureFilter::MinPointMagLinearMipLinear: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR; break; case xna::TextureFilter::MinPointMagLinearMipPoint: - impl->_description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT; + Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT; break; default: break; @@ -166,39 +166,39 @@ namespace xna { } void SamplerState::AddressU(TextureAddressMode value) { - impl->_description.AddressU = DxHelpers::TextureAddresModeToDx(value); + Implementation->Description.AddressU = DxHelpers::TextureAddresModeToDx(value); } void SamplerState::AddressV(TextureAddressMode value) { - impl->_description.AddressV = DxHelpers::TextureAddresModeToDx(value); + Implementation->Description.AddressV = DxHelpers::TextureAddresModeToDx(value); } void SamplerState::AddressW(TextureAddressMode value) { - impl->_description.AddressW = DxHelpers::TextureAddresModeToDx(value); + Implementation->Description.AddressW = DxHelpers::TextureAddresModeToDx(value); } void SamplerState::Comparison(ComparisonFunction value) { - impl->_description.ComparisonFunc = static_cast(static_cast(value) + 1); + Implementation->Description.ComparisonFunc = static_cast(static_cast(value) + 1); } void SamplerState::MipMapLevelOfDetailBias(float value) { - impl->_description.MipLODBias = value; + Implementation->Description.MipLODBias = value; } void SamplerState::MinMipLevel(float value) { - impl->_description.MinLOD = value; + Implementation->Description.MinLOD = value; } void SamplerState::MaxMipLevel (float value) { - impl->_description.MaxLOD = value; + Implementation->Description.MaxLOD = value; } void SamplerState::MaxAnisotropy(Uint value) { - impl->_description.MaxAnisotropy = static_cast(value); + Implementation->Description.MaxAnisotropy = static_cast(value); } TextureFilter SamplerState::Filter() const { - switch (impl->_description.Filter) + switch (Implementation->Description.Filter) { case D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR: return xna::TextureFilter::Linear; @@ -224,34 +224,34 @@ namespace xna { } TextureAddressMode SamplerState::AddressU() const { - return DxHelpers::TextureAddresModeToXna(impl->_description.AddressU); + return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressU); } TextureAddressMode SamplerState::AddressV() const { - return DxHelpers::TextureAddresModeToXna(impl->_description.AddressV); + return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressV); } TextureAddressMode SamplerState::AddressW() const { - return DxHelpers::TextureAddresModeToXna(impl->_description.AddressW); + return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressW); } ComparisonFunction SamplerState::Comparison() const { - return static_cast(impl->_description.ComparisonFunc - 1); + return static_cast(Implementation->Description.ComparisonFunc - 1); } float SamplerState::MipMapLevelOfDetailBias() const { - return impl->_description.MipLODBias; + return Implementation->Description.MipLODBias; } float SamplerState::MinMipLevel() const { - return impl->_description.MinLOD; + return Implementation->Description.MinLOD; } float SamplerState::MaxMipLevel() const { - return impl->_description.MaxLOD; + return Implementation->Description.MaxLOD; } Uint SamplerState::MaxAnisotropy() const { - return impl->_description.MaxAnisotropy; + return Implementation->Description.MaxAnisotropy; } } \ No newline at end of file diff --git a/sources/framework-dx/sprite.cpp b/sources/framework-dx/sprite.cpp index 859c60f..9744c56 100644 --- a/sources/framework-dx/sprite.cpp +++ b/sources/framework-dx/sprite.cpp @@ -159,7 +159,7 @@ namespace xna { Implementation->SpriteBatch->Begin( _sortMode, blendState ? blendState->Implementation->BlendState.Get() : nullptr, - samplerState ? samplerState->impl->_samplerState.Get() : nullptr, + samplerState ? samplerState->Implementation->SamplerState.Get() : nullptr, depthStencil ? depthStencil->Implementation->DepthStencil.Get() : nullptr, rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr, effectFunc,