1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em GraphicsDevice

This commit is contained in:
Danilo 2024-06-24 15:11:07 -03:00
parent 5d46b4246f
commit e7deb038d5
9 changed files with 139 additions and 27 deletions

View File

@ -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

View File

@ -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<PlatformImplementation>();
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<xna::BlendState> GraphicsDevice::BlendState() const {
return impl->_blendState;
}
void GraphicsDevice::BlendState(sptr<xna::BlendState> const& value) {
impl->_blendState = value;
}
sptr<xna::DepthStencilState> GraphicsDevice::DepthStencilState() const {
return impl->_depthStencilState;
}
void GraphicsDevice::DepthStencilState(sptr<xna::DepthStencilState> const& value) {
impl->_depthStencilState = value;
}
sptr<xna::RasterizerState> GraphicsDevice::RasterizerState() const {
return impl->_rasterizerState;
}
void GraphicsDevice::RasterizerState(sptr<xna::RasterizerState> const& value) {
impl->_rasterizerState = value;
}
sptr<SamplerStateCollection> GraphicsDevice::SamplerStates() const {
return impl->_samplerStates;
}
Int GraphicsDevice::MultiSampleMask() const {
return impl->_multiSampleMask;
}
void GraphicsDevice::MultiSampleMask(Int value) {
impl->_multiSampleMask = value;
}
}

View File

@ -74,6 +74,7 @@ namespace xna {
class TextureCube;
class RasterizerState;
class SamplerState;
class SamplerStateCollection;
class Shader;
class SpriteBatch;
class SpriteFont;

View File

@ -76,6 +76,8 @@ namespace xna {
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
using PBlendState = sptr<BlendState>;
}
#endif

View File

@ -92,6 +92,8 @@ namespace xna {
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
using PDepthStencilState = sptr<DepthStencilState>;
}
#endif

View File

@ -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<GraphicsDevice> {
public:
GraphicsDevice();
GraphicsDevice(GraphicsDeviceInformation const& info);
~GraphicsDevice();
//Gets the graphics adapter.
sptr<GraphicsAdapter> 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<xna::BlendState> 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<xna::BlendState> const& value);
//Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default.
sptr<xna::DepthStencilState> DepthStencilState() const;
//Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default.
void DepthStencilState(sptr<xna::DepthStencilState> const& value);
//Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise.
sptr<xna::RasterizerState> RasterizerState() const;
//Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise.
void RasterizerState(sptr<xna::RasterizerState> const& value);
//Retrieves a collection of SamplerState objects for the current GraphicsDevice.
sptr<SamplerStateCollection> 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<GraphicsAdapter> Adapter() const;
xna::Viewport Viewport() const;
void Viewport(xna::Viewport const& viewport);
void UseVSync(bool use);

View File

@ -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<GraphicsDevice> const& device);
@ -58,6 +58,8 @@ namespace xna {
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
using PRasterizerState = sptr<RasterizerState>;
}
#endif

View File

@ -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<GraphicsDevice> const& device);
@ -70,6 +70,32 @@ namespace xna {
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
using PSamplerState = sptr<SamplerState>;
//Collection of SamplerState objects.
class SamplerStateCollection {
public:
SamplerStateCollection(){}
SamplerStateCollection(size_t size)
: samplers(size){}
SamplerStateCollection(std::vector<PSamplerState> const& samplers)
: samplers(samplers) {}
PSamplerState operator[](size_t index) {
if (index >= samplers.size())
return nullptr;
return samplers[index];
}
public:
std::vector<PSamplerState> samplers;
};
using PSamplerStateCollection = sptr<SamplerStateCollection>;
}
#endif

View File

@ -929,6 +929,13 @@ namespace xna {
};
struct GraphicsDevice::PlatformImplementation {
PlatformImplementation() {
_blendState = xna::BlendState::Opaque();
_depthStencilState = xna::DepthStencilState::Default();
_rasterizerState = xna::RasterizerState::CullCounterClockwise();
_samplerStates = snew<SamplerStateCollection>();
}
~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> _swapChain{ nullptr };
sptr<GraphicsAdapter> _adapter{ nullptr };
sptr<RenderTarget2D> _renderTarget2D{ nullptr };
sptr<BlendState> _blendState{ nullptr };
PBlendState _blendState = nullptr;
PRasterizerState _rasterizerState = nullptr;
PDepthStencilState _depthStencilState = nullptr;
PSamplerStateCollection _samplerStates = nullptr;
Int _multiSampleMask = 0xffffffff;
sptr<SwapChain> _swapChain = nullptr;
sptr<GraphicsAdapter> _adapter = nullptr;
sptr<RenderTarget2D> _renderTarget2D = nullptr;
sptr<GameWindow> _gameWindow = nullptr;
xna::Viewport _viewport{};
sptr<xna::PresentationParameters> _presentationParameters;