From e7deb038d5f2b177a16e8f9cb14790df53a16b05 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 24 Jun 2024 15:11:07 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GraphicsDevice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- framework/platform-dx/device.cpp | 58 +++++++++++++++++++------- inc/xna/forward.hpp | 1 + inc/xna/graphics/blendstate.hpp | 2 + inc/xna/graphics/depthstencilstate.hpp | 2 + inc/xna/graphics/device.hpp | 28 ++++++++++++- inc/xna/graphics/rasterizerstate.hpp | 4 +- inc/xna/graphics/samplerstate.hpp | 28 ++++++++++++- inc/xna/platform-dx/dx.hpp | 41 +++++++++++++++--- 9 files changed, 139 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 87797e4..7ea68a5 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The project is still under development and the next updates will focus on the following tasks: - [x] Finish basic classes -- [x] Code refactoring and cleaning +- [ ] Code refactoring and cleaning - [ ] 3D support - [ ] Implementation of missing classes and functions - [ ] Content Pipeline diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 2af9b6b..a0ac169 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -18,10 +18,6 @@ namespace xna { impl._factory->Release(); impl._factory = nullptr; } - - impl._blendState = nullptr; - impl._swapChain = nullptr; - impl._renderTarget2D = nullptr; } static void createDevice(GraphicsDevice::PlatformImplementation& impl) { @@ -80,19 +76,15 @@ namespace xna { impl->_presentationParameters->BackBufferFormat, impl->_presentationParameters->BackBufferWidth, impl->_presentationParameters->BackBufferHeight); - } - - GraphicsDevice::~GraphicsDevice() { - impl = nullptr; - } + } bool GraphicsDevice::Initialize() { + auto _this = shared_from_this(); + if (!impl) impl = uptr(); reset(*impl); - - auto _this = shared_from_this(); createDevice(*impl); @@ -139,10 +131,7 @@ namespace xna { impl->_context->RSSetViewports(1, &view); - impl->_blendState = BlendState::NonPremultiplied(); - impl->_blendState->Bind(_this); - impl->_blendState->Initialize(); - impl->_blendState->Apply(); + impl->InitializeAndApplyStates(_this); return true; } @@ -199,5 +188,42 @@ namespace xna { if (!impl) return; impl->_usevsync = use; - } + } + + + sptr GraphicsDevice::BlendState() const { + return impl->_blendState; + } + + void GraphicsDevice::BlendState(sptr const& value) { + impl->_blendState = value; + } + + sptr GraphicsDevice::DepthStencilState() const { + return impl->_depthStencilState; + } + + void GraphicsDevice::DepthStencilState(sptr const& value) { + impl->_depthStencilState = value; + } + + sptr GraphicsDevice::RasterizerState() const { + return impl->_rasterizerState; + } + + void GraphicsDevice::RasterizerState(sptr const& value) { + impl->_rasterizerState = value; + } + + sptr GraphicsDevice::SamplerStates() const { + return impl->_samplerStates; + } + + Int GraphicsDevice::MultiSampleMask() const { + return impl->_multiSampleMask; + } + + void GraphicsDevice::MultiSampleMask(Int value) { + impl->_multiSampleMask = value; + } } \ No newline at end of file diff --git a/inc/xna/forward.hpp b/inc/xna/forward.hpp index 2313c0e..081be41 100644 --- a/inc/xna/forward.hpp +++ b/inc/xna/forward.hpp @@ -74,6 +74,7 @@ namespace xna { class TextureCube; class RasterizerState; class SamplerState; + class SamplerStateCollection; class Shader; class SpriteBatch; class SpriteFont; diff --git a/inc/xna/graphics/blendstate.hpp b/inc/xna/graphics/blendstate.hpp index 9d2d006..ba65f71 100644 --- a/inc/xna/graphics/blendstate.hpp +++ b/inc/xna/graphics/blendstate.hpp @@ -76,6 +76,8 @@ namespace xna { struct PlatformImplementation; uptr impl = nullptr; }; + + using PBlendState = sptr; } #endif \ No newline at end of file diff --git a/inc/xna/graphics/depthstencilstate.hpp b/inc/xna/graphics/depthstencilstate.hpp index 71b0812..0664143 100644 --- a/inc/xna/graphics/depthstencilstate.hpp +++ b/inc/xna/graphics/depthstencilstate.hpp @@ -92,6 +92,8 @@ namespace xna { struct PlatformImplementation; uptr impl = nullptr; }; + + using PDepthStencilState = sptr; } #endif \ No newline at end of file diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 1ca5b04..2c7e63b 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -4,16 +4,40 @@ #include "../default.hpp" namespace xna { + //Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. class GraphicsDevice : public std::enable_shared_from_this { public: GraphicsDevice(); GraphicsDevice(GraphicsDeviceInformation const& info); - ~GraphicsDevice(); + + //Gets the graphics adapter. + sptr Adapter() const; + + //Gets or sets a system-defined instance of a blend state object initialized for alpha blending. The default value is BlendState.Opaque. + sptr BlendState() const; + //Gets or sets a system-defined instance of a blend state object initialized for alpha blending. The default value is BlendState.Opaque. + void BlendState(sptr const& value); + //Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default. + sptr DepthStencilState() const; + //Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default. + void DepthStencilState(sptr const& value); + //Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise. + sptr RasterizerState() const; + //Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise. + void RasterizerState(sptr const& value); + //Retrieves a collection of SamplerState objects for the current GraphicsDevice. + sptr SamplerStates() const; + + //Gets or sets a bitmask controlling modification of the samples in a multisample render target. The default value is -1 (0xffffffff). + Int MultiSampleMask() const; + //Gets or sets a bitmask controlling modification of the samples in a multisample render target. The default value is -1 (0xffffffff). + void MultiSampleMask(Int value); + void Clear(); void Clear(Color const& color); bool Initialize(); bool Present(); - sptr Adapter() const; + xna::Viewport Viewport() const; void Viewport(xna::Viewport const& viewport); void UseVSync(bool use); diff --git a/inc/xna/graphics/rasterizerstate.hpp b/inc/xna/graphics/rasterizerstate.hpp index bbf79e4..a04f55e 100644 --- a/inc/xna/graphics/rasterizerstate.hpp +++ b/inc/xna/graphics/rasterizerstate.hpp @@ -6,7 +6,7 @@ namespace xna { //Contains rasterizer state, which determines how to convert vector data (shapes) into raster data (pixels). - class RasterizerState : GraphicsResource { + class RasterizerState : public GraphicsResource { public: RasterizerState(); RasterizerState(sptr const& device); @@ -58,6 +58,8 @@ namespace xna { struct PlatformImplementation; uptr impl = nullptr; }; + + using PRasterizerState = sptr; } #endif \ No newline at end of file diff --git a/inc/xna/graphics/samplerstate.hpp b/inc/xna/graphics/samplerstate.hpp index 75f1303..7a4aed6 100644 --- a/inc/xna/graphics/samplerstate.hpp +++ b/inc/xna/graphics/samplerstate.hpp @@ -6,7 +6,7 @@ namespace xna { //Contains sampler state, which determines how to sample texture data. - class SamplerState : GraphicsResource { + class SamplerState : public GraphicsResource { public: SamplerState(); SamplerState(sptr const& device); @@ -70,6 +70,32 @@ namespace xna { struct PlatformImplementation; uptr impl = nullptr; }; + + using PSamplerState = sptr; + + //Collection of SamplerState objects. + class SamplerStateCollection { + public: + SamplerStateCollection(){} + + SamplerStateCollection(size_t size) + : samplers(size){} + + SamplerStateCollection(std::vector const& samplers) + : samplers(samplers) {} + + PSamplerState operator[](size_t index) { + if (index >= samplers.size()) + return nullptr; + + return samplers[index]; + } + + public: + std::vector samplers; + }; + + using PSamplerStateCollection = sptr; } #endif \ No newline at end of file diff --git a/inc/xna/platform-dx/dx.hpp b/inc/xna/platform-dx/dx.hpp index 450039b..ce86e07 100644 --- a/inc/xna/platform-dx/dx.hpp +++ b/inc/xna/platform-dx/dx.hpp @@ -929,6 +929,13 @@ namespace xna { }; struct GraphicsDevice::PlatformImplementation { + PlatformImplementation() { + _blendState = xna::BlendState::Opaque(); + _depthStencilState = xna::DepthStencilState::Default(); + _rasterizerState = xna::RasterizerState::CullCounterClockwise(); + _samplerStates = snew(); + } + ~PlatformImplementation() { if (_device) { _device->Release(); @@ -944,15 +951,37 @@ namespace xna { _factory->Release(); _factory = nullptr; } + } + + private: + void InitializeAndApplyStates(PGraphicsDevice const& device) { + _blendState->Bind(device); + _blendState->Initialize(); + _blendState->Apply(); + + _rasterizerState->Bind(device); + _rasterizerState->Initialize(); + _rasterizerState->Apply(); + + _depthStencilState->Bind(device); + _depthStencilState->Initialize(); + _depthStencilState->Apply(); } - ID3D11Device* _device{ nullptr }; - ID3D11DeviceContext* _context{ nullptr }; + public: + ID3D11Device* _device = nullptr; + ID3D11DeviceContext* _context = nullptr; IDXGIFactory1* _factory = nullptr; - sptr _swapChain{ nullptr }; - sptr _adapter{ nullptr }; - sptr _renderTarget2D{ nullptr }; - sptr _blendState{ nullptr }; + + PBlendState _blendState = nullptr; + PRasterizerState _rasterizerState = nullptr; + PDepthStencilState _depthStencilState = nullptr; + PSamplerStateCollection _samplerStates = nullptr; + Int _multiSampleMask = 0xffffffff; + + sptr _swapChain = nullptr; + sptr _adapter = nullptr; + sptr _renderTarget2D = nullptr; sptr _gameWindow = nullptr; xna::Viewport _viewport{}; sptr _presentationParameters;