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

257 lines
8.8 KiB
C++
Raw Permalink Normal View History

2024-06-03 21:55:09 -03:00
#include "xna/graphics/samplerstate.hpp"
#include "xna/graphics/samplerstate.hpp"
2024-09-06 22:23:32 -03:00
#include "xna-dx/framework.hpp"
2024-04-13 11:45:45 -03:00
namespace xna {
SamplerState::SamplerState() : SamplerState(nullptr){}
2024-05-22 11:55:13 -03:00
SamplerState::SamplerState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
Implementation = unew<SamplerStateImplementation>();
2024-05-22 11:55:13 -03:00
}
bool SamplerState::Initialize()
2024-05-22 11:55:13 -03:00
{
if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
throw csharp::InvalidOperationException();
2024-05-22 11:55:13 -03:00
}
if (Implementation->SamplerState) {
Implementation->SamplerState = nullptr;
2024-05-22 11:55:13 -03:00
}
const auto hr = BaseGraphicsDevice->Implementation->Device->CreateSamplerState(
&Implementation->Description,
Implementation->SamplerState.GetAddressOf());
2024-05-22 11:55:13 -03:00
if (FAILED(hr)) {
throw csharp::InvalidOperationException();
2024-05-22 11:55:13 -03:00
}
return true;
}
bool SamplerState::Apply()
2024-04-26 11:35:59 -03:00
{
if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-04-26 11:35:59 -03:00
}
if (!Implementation->SamplerState) {
throw csharp::InvalidOperationException();
2024-04-26 11:35:59 -03:00
}
BaseGraphicsDevice->Implementation->Context->PSSetSamplers(0, 1, Implementation->SamplerState.GetAddressOf());
2024-04-26 11:35:59 -03:00
return true;
}
void SamplerStateCollection::Apply(GraphicsDevice const& device) {
if (samplers.empty())
return;
if (!device.Implementation || !device.Implementation->Device || !device.Implementation->Context) {
throw csharp::InvalidOperationException();
}
std::vector<ID3D11SamplerState*> states(samplers.size());
for (size_t i = 0; i < samplers.size(); ++i) {
const auto& current = samplers[0];
if (!current || !current->Implementation || !current->Implementation->SamplerState)
throw csharp::InvalidOperationException();
states[i] = current->Implementation->SamplerState.Get();
states[i]->AddRef();
}
device.Implementation->Context->PSSetSamplers(
0,
static_cast<UINT>(states.size()),
states.data());
for (size_t i = 0; i < samplers.size(); ++i) {
states[i]->Release();
states[i] = nullptr;
}
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::PoinWrap() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
2024-04-13 11:45:45 -03:00
return state;
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::PointClamp() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2024-04-13 11:45:45 -03:00
return state;
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::LinearWrap() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
2024-04-13 11:45:45 -03:00
return state;
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::LinearClamp() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2024-04-13 11:45:45 -03:00
return state;
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::AnisotropicWrap() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_ANISOTROPIC;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
2024-04-13 11:45:45 -03:00
return state;
}
2024-04-26 11:35:59 -03:00
uptr<SamplerState> SamplerState::AnisotropicClamp() {
2024-05-22 11:55:13 -03:00
auto state = unew<SamplerState>();
state->Implementation->Description.Filter = D3D11_FILTER_ANISOTROPIC;
state->Implementation->Description.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
state->Implementation->Description.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2024-04-13 11:45:45 -03:00
return state;
2024-05-22 11:55:13 -03:00
}
2024-04-13 11:49:03 -03:00
2024-05-22 11:55:13 -03:00
void SamplerState::Filter(TextureFilter value) {
switch (value)
{
case xna::TextureFilter::Linear:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::Point:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_POINT;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::Anisotropic:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_ANISOTROPIC;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::LinearMipPoint:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::PointMipLinear:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::MinLinearMagPointMipLinear:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::MinLinearMagPointMipPoint:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::MinPointMagLinearMipLinear:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
2024-05-22 11:55:13 -03:00
break;
case xna::TextureFilter::MinPointMagLinearMipPoint:
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
2024-05-22 11:55:13 -03:00
break;
default:
break;
2024-04-13 11:49:03 -03:00
}
2024-05-22 11:55:13 -03:00
}
2024-04-13 11:49:03 -03:00
2024-05-22 11:55:13 -03:00
void SamplerState::AddressU(TextureAddressMode value) {
Implementation->Description.AddressU = DxHelpers::TextureAddresModeToDx(value);
2024-05-22 11:55:13 -03:00
}
2024-04-13 11:49:03 -03:00
2024-05-22 11:55:13 -03:00
void SamplerState::AddressV(TextureAddressMode value) {
Implementation->Description.AddressV = DxHelpers::TextureAddresModeToDx(value);
2024-05-22 11:55:13 -03:00
}
2024-04-13 11:49:03 -03:00
2024-05-22 11:55:13 -03:00
void SamplerState::AddressW(TextureAddressMode value) {
Implementation->Description.AddressW = DxHelpers::TextureAddresModeToDx(value);
2024-05-22 11:55:13 -03:00
}
void SamplerState::Comparison(ComparisonFunction value) {
Implementation->Description.ComparisonFunc = static_cast<D3D11_COMPARISON_FUNC>(static_cast<int>(value) + 1);
2024-05-22 11:55:13 -03:00
}
void SamplerState::MipMapLevelOfDetailBias(float value) {
Implementation->Description.MipLODBias = value;
2024-05-22 11:55:13 -03:00
}
void SamplerState::MinMipLevel(float value) {
Implementation->Description.MinLOD = value;
2024-05-22 11:55:13 -03:00
}
void SamplerState::MaxMipLevel (float value) {
Implementation->Description.MaxLOD = value;
2024-05-22 11:55:13 -03:00
}
void SamplerState::MaxAnisotropy(Uint value) {
Implementation->Description.MaxAnisotropy = static_cast<UINT>(value);
2024-05-22 11:55:13 -03:00
}
TextureFilter SamplerState::Filter() const {
switch (Implementation->Description.Filter)
2024-05-22 11:55:13 -03:00
{
case D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR:
return xna::TextureFilter::Linear;
case D3D11_FILTER_MIN_MAG_MIP_POINT:
return xna::TextureFilter::Point;
case D3D11_FILTER_ANISOTROPIC:
return xna::TextureFilter::Anisotropic;
case D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT:
return xna::TextureFilter::LinearMipPoint;
case D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR:
return xna::TextureFilter::PointMipLinear;
case D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
return xna::TextureFilter::MinLinearMagPointMipLinear;
case D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT:
return xna::TextureFilter::MinLinearMagPointMipPoint;
case D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR:
return xna::TextureFilter::MinPointMagLinearMipLinear;
case D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
return xna::TextureFilter::MinPointMagLinearMipPoint;
default:
return xna::TextureFilter::Linear;
2024-04-13 11:49:03 -03:00
}
2024-05-22 11:55:13 -03:00
}
2024-04-13 11:49:03 -03:00
2024-05-22 11:55:13 -03:00
TextureAddressMode SamplerState::AddressU() const {
return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressU);
2024-05-22 11:55:13 -03:00
}
2024-06-25 22:04:09 -03:00
TextureAddressMode SamplerState::AddressV() const {
return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressV);
2024-05-22 11:55:13 -03:00
}
2024-06-25 22:04:09 -03:00
TextureAddressMode SamplerState::AddressW() const {
return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressW);
2024-05-22 11:55:13 -03:00
}
ComparisonFunction SamplerState::Comparison() const {
return static_cast<ComparisonFunction>(Implementation->Description.ComparisonFunc - 1);
2024-05-22 11:55:13 -03:00
}
float SamplerState::MipMapLevelOfDetailBias() const {
return Implementation->Description.MipLODBias;
2024-05-22 11:55:13 -03:00
}
float SamplerState::MinMipLevel() const {
return Implementation->Description.MinLOD;
2024-05-22 11:55:13 -03:00
}
float SamplerState::MaxMipLevel() const {
return Implementation->Description.MaxLOD;
2024-05-22 11:55:13 -03:00
}
Uint SamplerState::MaxAnisotropy() const {
return Implementation->Description.MaxAnisotropy;
2024-04-13 11:49:03 -03:00
}
2024-04-13 11:45:45 -03:00
}