diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index 187d3bd..71e26f9 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -7,6 +7,7 @@ #include "xna/graphics/blendstate.hpp" #include "xna/graphics/depthstencilstate.hpp" #include "xna/platform-dx/dx.hpp" +#include using DxSpriteBatch = DirectX::SpriteBatch; using DxSpriteSortMode = DirectX::SpriteSortMode; @@ -127,18 +128,18 @@ namespace xna { if (!device->impl->_context) return; - implementation = unew(); - implementation->_dxspriteBatch = snew( + impl = unew(); + impl->_dxspriteBatch = snew( //ID3D11DeviceContext* deviceContext device->impl->_context.Get() ); Viewport(device->Viewport()); - } + } void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Effect* effect, Matrix const& transformMatrix) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; DxSpriteSortMode sort = DxHelpers::SpriteSortToDx(sortMode); @@ -148,28 +149,49 @@ namespace xna { t.M11, t.M12, t.M13, t.M14, t.M21, t.M22, t.M23, t.M24, t.M31, t.M32, t.M33, t.M34, - t.M41, t.M42, t.M43, t.M44); + t.M41, t.M42, t.M43, t.M44); - implementation->_dxspriteBatch->Begin( + if (effect && !impl->dxInputLayout) { + void const* shaderByteCode; + size_t byteCodeLength; + + effect->impl->dxEffect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength); + + m_device->impl->_device->CreateInputLayout( + DirectX::VertexPositionColorTexture::InputElements, + DirectX::VertexPositionColorTexture::InputElementCount, + shaderByteCode, byteCodeLength, + impl->dxInputLayout.GetAddressOf()); + } + + auto& context = m_device->impl->_context; + + std::function funcEffect = [=] { + effect->impl->dxEffect->Apply(context.Get()); + context->IASetInputLayout(impl->dxInputLayout.Get()); + }; + std::function funcVoid = [=] {}; + + impl->_dxspriteBatch->Begin( sort, 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, + effect ? funcEffect : funcVoid, matrix ); } void SpriteBatch::End() { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; - implementation->_dxspriteBatch->End(); + impl->_dxspriteBatch->End(); } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Color const& color) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -179,7 +201,7 @@ namespace xna { const auto v4 = color.ToVector4(); XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - implementation->_dxspriteBatch->Draw( + impl->_dxspriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, _color @@ -187,7 +209,7 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -206,7 +228,7 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - implementation->_dxspriteBatch->Draw( + impl->_dxspriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -214,7 +236,7 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -236,7 +258,7 @@ namespace xna { const DxSpriteEffects _effects = static_cast(effects); - implementation->_dxspriteBatch->Draw( + impl->_dxspriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -249,7 +271,7 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -272,7 +294,7 @@ namespace xna { const auto _effects = static_cast(effects); const XMFLOAT2 _scale = { scale.X, scale.Y }; - implementation->_dxspriteBatch->Draw( + impl->_dxspriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -285,7 +307,7 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -300,11 +322,11 @@ namespace xna { const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); + impl->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -328,11 +350,11 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); + impl->_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) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; if (!texture.impl->dxShaderResource) @@ -359,7 +381,7 @@ namespace xna { auto _origin = XMFLOAT2(origin.X, origin.Y); const auto _effects = static_cast(effects); - implementation->_dxspriteBatch->Draw( + impl->_dxspriteBatch->Draw( texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, @@ -371,7 +393,7 @@ namespace xna { } void SpriteBatch::Viewport(xna::Viewport const& value) { - if (!implementation->_dxspriteBatch) + if (!impl->_dxspriteBatch) return; D3D11_VIEWPORT _view{}; @@ -382,11 +404,11 @@ namespace xna { _view.MinDepth = value.MinDetph; _view.MaxDepth = value.MaxDepth; - implementation->_dxspriteBatch->SetViewport(_view); + impl->_dxspriteBatch->SetViewport(_view); } void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) { - if (!implementation->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) + if (!impl->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -394,7 +416,7 @@ namespace xna { const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; spriteFont.impl->_dxSpriteFont->DrawString( - implementation->_dxspriteBatch.get(), + impl->_dxspriteBatch.get(), text.c_str(), _position, _color @@ -403,7 +425,7 @@ namespace xna { void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { - if (!implementation->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) + if (!impl->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -413,7 +435,7 @@ namespace xna { const auto _effects = static_cast(effects); spriteFont.impl->_dxSpriteFont->DrawString( - implementation->_dxspriteBatch.get(), + impl->_dxspriteBatch.get(), text.c_str(), _position, _color, diff --git a/inc/xna/graphics/sprite.hpp b/inc/xna/graphics/sprite.hpp index 9701c65..b21c456 100644 --- a/inc/xna/graphics/sprite.hpp +++ b/inc/xna/graphics/sprite.hpp @@ -87,7 +87,7 @@ namespace xna { public: struct PlatformImplementation; - uptr implementation = nullptr; + uptr impl = nullptr; }; //Represents a font texture. diff --git a/inc/xna/platform-dx/dx.hpp b/inc/xna/platform-dx/dx.hpp index eefb0aa..321acc8 100644 --- a/inc/xna/platform-dx/dx.hpp +++ b/inc/xna/platform-dx/dx.hpp @@ -572,6 +572,7 @@ namespace xna { struct SpriteBatch::PlatformImplementation { sptr _dxspriteBatch = nullptr; + comptr dxInputLayout = nullptr; }; struct GraphicsAdapter::PlatformImplementation {