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 {
|
2024-06-24 11:09:31 -03:00
|
|
|
SamplerState::SamplerState() : SamplerState(nullptr){}
|
2024-05-22 11:55:13 -03:00
|
|
|
|
|
|
|
SamplerState::SamplerState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation = unew<SamplerStateImplementation>();
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool SamplerState::Initialize()
|
2024-05-22 11:55:13 -03:00
|
|
|
{
|
2024-11-15 16:45:48 -03:00
|
|
|
if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-11-15 16:45:48 -03:00
|
|
|
if (Implementation->SamplerState) {
|
|
|
|
Implementation->SamplerState = nullptr;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-11-14 21:44:46 -03:00
|
|
|
const auto hr = BaseGraphicsDevice->Implementation->Device->CreateSamplerState(
|
2024-11-15 16:45:48 -03:00
|
|
|
&Implementation->Description,
|
|
|
|
Implementation->SamplerState.GetAddressOf());
|
2024-05-22 11:55:13 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool SamplerState::Apply()
|
2024-04-26 11:35:59 -03:00
|
|
|
{
|
2024-11-15 16:45:48 -03:00
|
|
|
if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-26 11:35:59 -03:00
|
|
|
}
|
|
|
|
|
2024-11-15 16:45:48 -03:00
|
|
|
if (!Implementation->SamplerState) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-26 11:35:59 -03:00
|
|
|
}
|
|
|
|
|
2024-11-15 16:45:48 -03:00
|
|
|
BaseGraphicsDevice->Implementation->Context->PSSetSamplers(0, 1, Implementation->SamplerState.GetAddressOf());
|
2024-04-26 11:35:59 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-06-24 20:49:06 -03:00
|
|
|
|
|
|
|
void SamplerStateCollection::Apply(GraphicsDevice const& device) {
|
|
|
|
if (samplers.empty())
|
|
|
|
return;
|
|
|
|
|
2024-11-14 15:25:11 -03:00
|
|
|
if (!device.Implementation || !device.Implementation->Device || !device.Implementation->Context) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-06-24 20:49:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ID3D11SamplerState*> states(samplers.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < samplers.size(); ++i) {
|
|
|
|
const auto& current = samplers[0];
|
|
|
|
|
2024-11-15 16:45:48 -03:00
|
|
|
if (!current || !current->Implementation || !current->Implementation->SamplerState)
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-06-24 20:49:06 -03:00
|
|
|
|
2024-11-15 16:45:48 -03:00
|
|
|
states[i] = current->Implementation->SamplerState.Get();
|
2024-06-24 20:49:06 -03:00
|
|
|
states[i]->AddRef();
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:25:11 -03:00
|
|
|
device.Implementation->Context->PSSetSamplers(
|
2024-07-16 09:50:53 -03:00
|
|
|
0,
|
|
|
|
static_cast<UINT>(states.size()),
|
|
|
|
states.data());
|
2024-06-24 20:49:06 -03:00
|
|
|
|
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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>();
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
2024-05-22 11:55:13 -03:00
|
|
|
break;
|
|
|
|
case xna::TextureFilter::Point:
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_MIP_POINT;
|
2024-05-22 11:55:13 -03:00
|
|
|
break;
|
|
|
|
case xna::TextureFilter::Anisotropic:
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.Filter = D3D11_FILTER::D3D11_FILTER_ANISOTROPIC;
|
2024-05-22 11:55:13 -03:00
|
|
|
break;
|
|
|
|
case xna::TextureFilter::LinearMipPoint:
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
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:
|
2024-11-15 16:45:48 -03:00
|
|
|
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) {
|
2024-11-15 16:45:48 -03:00
|
|
|
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) {
|
2024-11-15 16:45:48 -03:00
|
|
|
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) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.AddressW = DxHelpers::TextureAddresModeToDx(value);
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SamplerState::Comparison(ComparisonFunction value) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.ComparisonFunc = static_cast<D3D11_COMPARISON_FUNC>(static_cast<int>(value) + 1);
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
void SamplerState::MipMapLevelOfDetailBias(float value) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.MipLODBias = value;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
void SamplerState::MinMipLevel(float value) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.MinLOD = value;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
void SamplerState::MaxMipLevel (float value) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.MaxLOD = value;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SamplerState::MaxAnisotropy(Uint value) {
|
2024-11-15 16:45:48 -03:00
|
|
|
Implementation->Description.MaxAnisotropy = static_cast<UINT>(value);
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureFilter SamplerState::Filter() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
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 {
|
2024-11-15 16:45:48 -03:00
|
|
|
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 {
|
2024-11-15 16:45:48 -03:00
|
|
|
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 {
|
2024-11-15 16:45:48 -03:00
|
|
|
return DxHelpers::TextureAddresModeToXna(Implementation->Description.AddressW);
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
ComparisonFunction SamplerState::Comparison() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
return static_cast<ComparisonFunction>(Implementation->Description.ComparisonFunc - 1);
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
float SamplerState::MipMapLevelOfDetailBias() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
return Implementation->Description.MipLODBias;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
float SamplerState::MinMipLevel() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
return Implementation->Description.MinLOD;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:09:31 -03:00
|
|
|
float SamplerState::MaxMipLevel() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
return Implementation->Description.MaxLOD;
|
2024-05-22 11:55:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Uint SamplerState::MaxAnisotropy() const {
|
2024-11-15 16:45:48 -03:00
|
|
|
return Implementation->Description.MaxAnisotropy;
|
2024-04-13 11:49:03 -03:00
|
|
|
}
|
2024-04-13 11:45:45 -03:00
|
|
|
}
|