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

Implementações em BlendState

This commit is contained in:
Danilo 2024-06-22 22:05:35 -03:00
parent dd43ea7dbc
commit 19ccd27dea
9 changed files with 221 additions and 57 deletions

View File

@ -11,10 +11,6 @@ namespace xna {
impl = unew<PlatformImplementation>();
}
GraphicsAdapter::~GraphicsAdapter() {
impl = nullptr;
}
uptr<GraphicsAdapter> GraphicsAdapter::DefaultAdapter() {
IDXGIFactory1* pFactory = nullptr;
@ -38,28 +34,6 @@ namespace xna {
return nullptr;
}
void GraphicsAdapter::Adapters(std::vector<sptr<GraphicsAdapter>>& adapters){
IDXGIFactory1* pFactory = nullptr;
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory))
Exception::Throw(ExMessage::CreateComponent);
IDXGIAdapter1* pAdapter = nullptr;
UINT count = 0;
for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) {
auto adp = snew<GraphicsAdapter>();
adp->impl->_index = count;
adp->impl->dxadapter = pAdapter;
adapters.push_back(adp);
}
pFactory->Release();
pFactory = nullptr;
}
void GraphicsAdapter::Adapters(std::vector<uptr<GraphicsAdapter>>& adapters) {
IDXGIFactory1* pFactory = nullptr;
@ -180,7 +154,7 @@ namespace xna {
if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) {
const auto currentSurface = static_cast<SurfaceFormat>(f);
DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface);
DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface);
UINT numModes = 0;
pOutput->GetDisplayModeList(format, 0, &numModes, nullptr);
@ -211,7 +185,7 @@ namespace xna {
UINT bufferOffset = 0;
if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(surfaceFormat);
DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(surfaceFormat);
UINT numModes = 0;
@ -273,7 +247,7 @@ namespace xna {
if (adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) {
const auto currentSurface = static_cast<SurfaceFormat>(f);
DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface);
DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface);
UINT num = 0;
pOutput->GetDisplayModeList(format, 0, &num, nullptr);

View File

@ -3,16 +3,96 @@
#include "xna/platform-dx/dx.hpp"
namespace xna {
BlendState::BlendState() : GraphicsResource(nullptr) {
impl = unew<PlatformImplementation>();
}
BlendState::BlendState() : BlendState(nullptr) {}
BlendState::BlendState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = unew<PlatformImplementation>();
impl->dxDescription.AlphaToCoverageEnable = false;
impl->dxDescription.IndependentBlendEnable = false;
impl->dxDescription.RenderTarget[0].BlendEnable = true;
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::ConvertBlend(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::ConvertBlend(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::ConvertOperation(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::ConvertBlend(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::ConvertBlend(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::ConvertOperation(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ConvertColorWrite(ColorWriteChannels::All);
}
BlendState::~BlendState() {
impl = nullptr;
BlendFunction BlendState::AlphaBlendFunction() const {
return DxHelpers::ConvertOperationDx(impl->dxDescription.RenderTarget[0].BlendOpAlpha);
}
void BlendState::AlphaBlendFunction(BlendFunction value) {
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::ConvertOperation(value);
}
Blend BlendState::AlphaDestinationBlend() const {
return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].DestBlendAlpha);
}
void BlendState::AlphaDestinationBlend(Blend value) {
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::ConvertBlend(value);
}
Blend BlendState::AlphaSourceBlend() const {
return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].SrcBlendAlpha);
}
void BlendState::AlphaSourceBlend(Blend value) {
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::ConvertBlend(value);
}
BlendFunction BlendState::ColorBlendFunction() const {
return DxHelpers::ConvertOperationDx(impl->dxDescription.RenderTarget[0].BlendOp);
}
void BlendState::ColorBlendFunction(BlendFunction value) {
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::ConvertOperation(value);
}
Blend BlendState::ColorDestinationBlend() const {
return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].DestBlend);
}
void BlendState::ColorDestinationBlend(Blend value) {
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::ConvertBlend(value);
}
Blend BlendState::ColorSourceBlend() const {
return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].SrcBlend);
}
void BlendState::ColorSourceBlend(Blend value) {
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::ConvertBlend(value);
}
Color BlendState::BlendFactor() const {
auto color = Color(
impl->blendFactor[0],
impl->blendFactor[1],
impl->blendFactor[2],
impl->blendFactor[3]
);
return color;
}
void BlendState::BlendFactor(Color const& value) {
auto v4 = value.ToVector4();
impl->blendFactor[0] = v4.X;
impl->blendFactor[1] = v4.Y;
impl->blendFactor[2] = v4.Z;
impl->blendFactor[3] = v4.W;
}
Int BlendState::MultiSampleMask() const {
return static_cast<Int>(impl->sampleMask);
}
void BlendState::MultiSampleMast(Int value) {
impl->sampleMask = static_cast<UINT>(value);
}
bool BlendState::Initialize()
@ -45,13 +125,13 @@ namespace xna {
if (!impl->dxBlendState) {
Exception::Throw(ExMessage::UnintializedComponent);
}
m_device->impl->_context->OMSetBlendState(
impl->dxBlendState,
impl->blendFactor,
m_device->impl->_context->OMSetBlendState(
impl->dxBlendState,
impl->blendFactor,
impl->sampleMask);
return true;
return true;
}
void BlendState::AlphaToCoverageEnable(bool value) {
@ -78,15 +158,15 @@ namespace xna {
uptr<BlendState> BlendState::Opaque() {
auto blendState = unew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
return blendState;
}
uptr<BlendState> BlendState::AlphaBlend() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
auto blendState = std::unique_ptr<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
@ -96,7 +176,7 @@ namespace xna {
}
uptr<BlendState> BlendState::Additive() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
auto blendState = std::unique_ptr<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
@ -106,7 +186,7 @@ namespace xna {
}
uptr<BlendState> BlendState::NonPremultiplied() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
auto blendState = std::unique_ptr<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;

View File

@ -142,7 +142,7 @@ namespace xna {
return;
DxSpriteSortMode sort;
DxHelpers::ConvertSpriteSort(sortMode, sort);
DxHelpers::SpriteSortToDx(sortMode, sort);
const auto& t = transformMatrix;
DxMatrix matrix = DxMatrix(

View File

@ -59,7 +59,7 @@ namespace xna {
impl->dxDescription.Width = static_cast<UINT>(parameters->BackBufferWidth);
impl->dxDescription.Height = static_cast<UINT>(parameters->BackBufferHeight);
impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(parameters->BackBufferFormat);
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(parameters->BackBufferFormat);
impl->dxDescription.SampleDesc.Count = 1;
impl->dxDescription.SampleDesc.Quality = 0;
impl->dxDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

View File

@ -122,7 +122,7 @@ namespace xna {
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
impl->dxDescription.MipLevels = static_cast<UINT>(mipMap);
impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format);
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format);
}
HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)

View File

@ -119,7 +119,8 @@ namespace xna {
Green,
Blue,
Alpha,
All
All,
None
};
enum class ContainmentType {

View File

@ -4,26 +4,41 @@
#include "../default.hpp"
namespace xna {
//Provides methods to retrieve and manipulate graphics adapters.
class GraphicsAdapter {
public:
GraphicsAdapter();
~GraphicsAdapter();
//Retrieves a string used for presentation to the user.
String Description() const;
//Retrieves a value that is used to help identify a particular chip set.
Uint DeviceId() const;
//Retrieves a string that contains the device name.
String DeviceName() const;
//Determines if this instance of GraphicsAdapter is the default adapter.
bool IsDefaultAdapter() const;
//Retrieves the handle of the monitor
intptr_t MonitorHandle() const;
//Retrieves a value used to help identify the revision level of a particular chip set.
Uint Revision() const;
//Retrieves a value used to identify the subsystem.
Uint SubSystemId() const;
//Retrieves a value used to identify the manufacturer.
Uint VendorId() const;
uptr<DisplayModeCollection> SupportedDisplayModes() const;
uptr<DisplayModeCollection> SupportedDisplayModes(SurfaceFormat surfaceFormat) const;
//Gets the current display mode.
sptr<DisplayMode> CurrentDisplayMode();
//Gets the current display mode.
void CurrentDisplayMode(SurfaceFormat surfaceFormat, Uint width, Uint height);
//Gets the default adapter.
static uptr<GraphicsAdapter> DefaultAdapter();
static void Adapters(std::vector<sptr<GraphicsAdapter>>& adapters);
//Collection of available adapters on the system.
static void Adapters(std::vector<uptr<GraphicsAdapter>>& adapters);
public:

View File

@ -3,24 +3,73 @@
#include "../default.hpp"
#include "gresource.hpp"
#include "../common/color.hpp"
namespace xna {
struct BlendRenderTarget;
//Contains blend state for the device.
class BlendState : public GraphicsResource {
public:
BlendState();
BlendState(sptr<GraphicsDevice> const& device);
~BlendState() override;
bool Initialize() ;
void AlphaToCoverageEnable(bool value) ;
void IndependentBlendEnable(bool value) ;
//Gets or sets the arithmetic operation when blending alpha values. The default is BlendFunction.Add.
BlendFunction AlphaBlendFunction() const;
//Gets or sets the arithmetic operation when blending alpha values. The default is BlendFunction.Add.
void AlphaBlendFunction(BlendFunction value);
//Gets or sets the blend factor for the destination alpha, which is the percentage of the destination alpha included in the blended result. The default is Blend.One.
Blend AlphaDestinationBlend() const;
//Gets or sets the blend factor for the destination alpha, which is the percentage of the destination alpha included in the blended result. The default is Blend.One.
void AlphaDestinationBlend(Blend value);
//Gets or sets the alpha blend factor. The default is Blend.One.
Blend AlphaSourceBlend() const;
//Gets or sets the alpha blend factor. The default is Blend.One.
void AlphaSourceBlend(Blend value);
//Gets or sets the arithmetic operation when blending color values. The default is BlendFunction.Add.
BlendFunction ColorBlendFunction() const;
//Gets or sets the arithmetic operation when blending color values. The default is BlendFunction.Add.
void ColorBlendFunction(BlendFunction value);
//Gets or sets the blend factor for the destination color. The default is Blend.One.
Blend ColorDestinationBlend() const;
//Gets or sets the blend factor for the destination color. The default is Blend.One.
void ColorDestinationBlend(Blend value);
//Gets or sets the blend factor for the source color. The default is Blend.One.
Blend ColorSourceBlend() const;
//Gets or sets the blend factor for the source color. The default is Blend.One.
void ColorSourceBlend(Blend value);
//Gets or sets the four-component (RGBA) blend factor for alpha blending.
Color BlendFactor() const;
//Gets or sets the four-component (RGBA) blend factor for alpha blending.
void BlendFactor(Color const& value);
//Gets or sets a bitmask which defines which samples can be written during multisampling. The default is 0xffffffff.
Int MultiSampleMask() const;
//Gets or sets a bitmask which defines which samples can be written during multisampling. The default is 0xffffffff.
void MultiSampleMast(Int value);
//Specifies whether to use alpha-to-coverage as a multisampling technique when setting a pixel to a render target.
void AlphaToCoverageEnable(bool value);
//Specifies whether to enable independent blending in simultaneous render targets
void IndependentBlendEnable(bool value);
void RenderTargets(std::vector<BlendRenderTarget> const& value);
bool Initialize();
bool Apply();
//A built-in state object with settings for opaque blend,
//that is overwriting the source with the destination data.
static uptr<BlendState> Opaque();
//A built-in state object with settings for alpha blend,
//that is blending the source and destination data using alpha.
static uptr<BlendState> AlphaBlend();
//A built-in state object with settings for additive blend,
//that is adding the destination data to the source data without using alpha.
static uptr<BlendState> Additive();
//A built-in state object with settings for blending with non-premultipled alpha,
//that is blending source and destination data using alpha while assuming the color data contains no alpha information.
static uptr<BlendState> NonPremultiplied();
public:

View File

@ -101,11 +101,11 @@ namespace xna {
//==============================================//
struct DxHelpers {
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
static constexpr void SpriteSortToDx(SpriteSortMode value, DirectX::SpriteSortMode& target) {
target = static_cast<DirectX::SpriteSortMode>(static_cast<int>(value));
}
static constexpr DXGI_FORMAT ConvertSurfaceToDXGIFORMAT(SurfaceFormat format)
static constexpr DXGI_FORMAT SurfaceFormatToDx(SurfaceFormat format)
{
switch (format)
{
@ -199,6 +199,47 @@ namespace xna {
}
}
static constexpr Blend ConvertBlendDx(D3D11_BLEND blend) {
switch (blend) {
case D3D11_BLEND_ZERO:
return Blend::Zero;
case D3D11_BLEND_ONE:
return Blend::One;
case D3D11_BLEND_SRC_COLOR:
return Blend::SourceColor;
case D3D11_BLEND_INV_SRC_COLOR:
return Blend::InverseSourceColor;
case D3D11_BLEND_SRC_ALPHA:
return Blend::SourceAlpha;
case D3D11_BLEND_INV_SRC_ALPHA:
return Blend::InverseSourceAlpha;
case D3D11_BLEND_DEST_ALPHA:
return Blend::DestinationAlpha;
case D3D11_BLEND_INV_DEST_ALPHA:
return Blend::InverseDestinationAlpha;
case D3D11_BLEND_DEST_COLOR:
return Blend::DestinationColor;
case D3D11_BLEND_INV_DEST_COLOR:
return Blend::InverseDestinationColor;
case D3D11_BLEND_SRC_ALPHA_SAT:
return Blend::SourceAlphaSaturation;
case D3D11_BLEND_BLEND_FACTOR:
return Blend::BlendFactor;
case D3D11_BLEND_INV_BLEND_FACTOR:
return Blend::InverseBlendFactor;
case D3D11_BLEND_SRC1_COLOR:
return Blend::Source1Color;
case D3D11_BLEND_INV_SRC1_COLOR:
return Blend::InverseSource1Color;
case D3D11_BLEND_SRC1_ALPHA:
return Blend::Source1Alpha;
case D3D11_BLEND_INV_SRC1_ALPHA:
return Blend::InverseSource1Alpha;
default:
return Blend::Zero;
}
}
static constexpr D3D11_BLEND ConvertBlend(Blend blend) {
switch (blend)
{
@ -245,6 +286,10 @@ namespace xna {
return static_cast<D3D11_BLEND_OP>(static_cast<int>(op) + 1);
}
static constexpr BlendOperation ConvertOperationDx(D3D11_BLEND_OP op) {
return static_cast<BlendOperation>(static_cast<int>(op) - 1);
}
static constexpr D3D11_COLOR_WRITE_ENABLE ConvertColorWrite(ColorWriteChannels colorWrite) {
switch (colorWrite)
{
@ -257,7 +302,7 @@ namespace xna {
case xna::ColorWriteChannels::Alpha:
return D3D11_COLOR_WRITE_ENABLE_ALPHA;
case xna::ColorWriteChannels::All:
return D3D11_COLOR_WRITE_ENABLE_ALL;
return D3D11_COLOR_WRITE_ENABLE_ALL;
default:
return D3D11_COLOR_WRITE_ENABLE_ALL;
}