2024-06-03 21:55:09 -03:00
|
|
|
#include "xna/graphics/depthstencilstate.hpp"
|
2024-09-06 22:23:32 -03:00
|
|
|
#include "xna-dx/framework.hpp"
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
namespace xna {
|
2024-05-20 10:29:20 -03:00
|
|
|
static D3D11_DEPTH_STENCIL_DESC defaultDesc() {
|
|
|
|
D3D11_DEPTH_STENCIL_DESC _description{};
|
|
|
|
_description.DepthEnable = true;
|
|
|
|
_description.StencilEnable = true;
|
|
|
|
_description.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
|
|
|
|
|
|
|
|
_description.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
|
|
|
|
_description.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
_description.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
_description.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
|
|
|
|
_description.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
|
|
|
|
_description.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
_description.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
_description.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
|
|
|
|
|
2024-07-14 13:28:47 -03:00
|
|
|
_description.StencilReadMask = static_cast<UINT8>(IntMaxValue);
|
|
|
|
_description.StencilWriteMask = static_cast<UINT8>(IntMaxValue);
|
2024-06-23 23:14:06 -03:00
|
|
|
_description.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
2024-05-20 10:29:20 -03:00
|
|
|
|
|
|
|
return _description;
|
|
|
|
}
|
|
|
|
|
|
|
|
DepthStencilState::DepthStencilState() : GraphicsResource(nullptr) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation = unew<DepthStencilStateImplementation>();
|
|
|
|
Implementation->Description = defaultDesc();
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
DepthStencilState::DepthStencilState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation = unew<DepthStencilStateImplementation>();
|
|
|
|
Implementation->Description = defaultDesc();
|
2024-06-23 23:14:06 -03:00
|
|
|
}
|
2024-05-20 10:29:20 -03:00
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool DepthStencilState::Initialize()
|
2024-04-15 09:48:16 -03:00
|
|
|
{
|
2024-11-14 21:44:46 -03:00
|
|
|
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|
|
|
|
|
2024-11-14 09:14:22 -03:00
|
|
|
if (Implementation->DepthStencil) {
|
|
|
|
Implementation->DepthStencil = nullptr;
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|
|
|
|
|
2024-11-14 21:44:46 -03:00
|
|
|
const auto hr = BaseGraphicsDevice->Implementation->Device->CreateDepthStencilState(
|
2024-11-14 09:14:22 -03:00
|
|
|
&Implementation->Description,
|
|
|
|
Implementation->DepthStencil.GetAddressOf());
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool DepthStencilState::Apply()
|
2024-04-15 09:48:16 -03:00
|
|
|
{
|
2024-11-14 21:44:46 -03:00
|
|
|
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|
|
|
|
|
2024-11-14 09:14:22 -03:00
|
|
|
if (!Implementation->DepthStencil) {
|
2024-08-02 16:40:06 -03:00
|
|
|
Initialize();
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|
|
|
|
|
2024-11-14 21:44:46 -03:00
|
|
|
BaseGraphicsDevice->Implementation->Context->OMSetDepthStencilState(Implementation->DepthStencil.Get(), 0);
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-25 14:51:33 -03:00
|
|
|
uptr<DepthStencilState> DepthStencilState::None() {
|
2024-06-22 11:52:21 -03:00
|
|
|
auto stencil = unew<DepthStencilState>();
|
2024-11-14 09:14:22 -03:00
|
|
|
stencil->Implementation->Description.DepthEnable = false;
|
|
|
|
stencil->Implementation->Description.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
return stencil;
|
|
|
|
}
|
|
|
|
|
2024-04-25 14:51:33 -03:00
|
|
|
uptr<DepthStencilState> DepthStencilState::Default() {
|
2024-06-22 11:52:21 -03:00
|
|
|
auto stencil = unew<DepthStencilState>();
|
2024-11-14 09:14:22 -03:00
|
|
|
stencil->Implementation->Description.DepthEnable = true;
|
|
|
|
stencil->Implementation->Description.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
return stencil;
|
|
|
|
}
|
|
|
|
|
2024-04-25 14:51:33 -03:00
|
|
|
uptr<DepthStencilState> DepthStencilState::DepthRead() {
|
2024-06-22 11:52:21 -03:00
|
|
|
auto stencil = unew<DepthStencilState>();
|
2024-11-14 09:14:22 -03:00
|
|
|
stencil->Implementation->Description.DepthEnable = true;
|
|
|
|
stencil->Implementation->Description.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
2024-04-15 09:48:16 -03:00
|
|
|
|
|
|
|
return stencil;
|
|
|
|
}
|
2024-05-20 10:29:20 -03:00
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::DepthBufferEnable(bool value) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.DepthEnable = value;
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::DepthBufferWriteEnable(bool value) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.DepthWriteMask = static_cast<D3D11_DEPTH_WRITE_MASK>(value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::DepthBufferFunction(ComparisonFunction value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.DepthFunc = static_cast<D3D11_COMPARISON_FUNC>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilEnable(bool value) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.StencilEnable = value;
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilMask(int value) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.StencilReadMask = static_cast<UINT8>(value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DepthStencilState::StencilWriteMask(Int value) {
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.StencilWriteMask = static_cast<UINT8>(value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilPass(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.FrontFace.StencilPassOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilFail(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.FrontFace.StencilFailOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilDepthBufferFail(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.FrontFace.StencilDepthFailOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::StencilFunction(ComparisonFunction value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.FrontFace.StencilFunc = static_cast<D3D11_COMPARISON_FUNC>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::CounterClockwiseStencilPass(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.BackFace.StencilPassOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::CounterClockwiseStencilFail(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.BackFace.StencilFailOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::CounterClockwiseStencilDepthBufferFail(StencilOperation value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.BackFace.StencilDepthFailOp = static_cast<D3D11_STENCIL_OP>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
void DepthStencilState::CounterClockwiseStencilFunction(ComparisonFunction value) {
|
2024-05-20 10:29:20 -03:00
|
|
|
const auto _value = static_cast<int>(value) + 1;
|
2024-11-14 09:14:22 -03:00
|
|
|
Implementation->Description.BackFace.StencilFunc = static_cast<D3D11_COMPARISON_FUNC>(_value);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
bool DepthStencilState::DepthBufferEnable() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
return Implementation->Description.DepthEnable;
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
bool DepthStencilState::DepthBufferWriteEnable() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
return static_cast<bool>(Implementation->Description.DepthWriteMask);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
ComparisonFunction DepthStencilState::DepthBufferFunction() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.DepthFunc) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<ComparisonFunction>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
bool DepthStencilState::StencilEnable() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
return Implementation->Description.StencilEnable;
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
Int DepthStencilState::StencilMask() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
return static_cast<int>(Implementation->Description.StencilReadMask);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Int DepthStencilState::StencilWriteMask() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
return static_cast<int>(Implementation->Description.StencilWriteMask);
|
2024-05-20 10:29:20 -03:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::StencilPass() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.FrontFace.StencilPassOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::StencilFail() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.FrontFace.StencilFailOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::StencilDepthBufferFail() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.FrontFace.StencilDepthFailOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
ComparisonFunction DepthStencilState::StencilFunction() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.FrontFace.StencilFunc) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<ComparisonFunction>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::CounterClockwiseStencilPass() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.BackFace.StencilPassOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::CounterClockwiseStencilFail() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.BackFace.StencilFailOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
StencilOperation DepthStencilState::CounterClockwiseStencilDepthBufferFail() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.BackFace.StencilDepthFailOp) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<StencilOperation>(_value);
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:14:06 -03:00
|
|
|
ComparisonFunction DepthStencilState::CounterClockwiseStencilFunction() const {
|
2024-11-14 09:14:22 -03:00
|
|
|
const auto _value = static_cast<int>(Implementation->Description.BackFace.StencilFunc) - 1;
|
2024-05-20 10:29:20 -03:00
|
|
|
return static_cast<ComparisonFunction>(_value);
|
|
|
|
}
|
2024-04-15 09:48:16 -03:00
|
|
|
}
|