10 : m_device(device), m_width(0), m_height(0),
13 device->GetImmediateContext(m_deviceContext.GetAddressOf());
19 m_shaderResourceView.Reset();
21 m_samplerState.Reset();
31 D3D11_TEXTURE2D_DESC textureDesc = {};
32 textureDesc.Width =
width;
33 textureDesc.Height =
height;
34 textureDesc.MipLevels = 0;
35 textureDesc.ArraySize = 1;
36 textureDesc.Format = GetDXGIFormat(format);
37 textureDesc.SampleDesc.Count = 1;
38 textureDesc.SampleDesc.Quality = 0;
39 textureDesc.Usage = D3D11_USAGE_DEFAULT;
40 textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
41 textureDesc.CPUAccessFlags = 0;
42 textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
45 HRESULT hr = m_device->CreateTexture2D(&textureDesc,
nullptr, &m_texture);
47 SLEAK_ERROR(
"Failed to create texture! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
52 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
53 srvDesc.Format = textureDesc.Format;
54 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
55 srvDesc.Texture2D.MipLevels =
static_cast<UINT
>(-1);
56 srvDesc.Texture2D.MostDetailedMip = 0;
58 hr = m_device->CreateShaderResourceView(m_texture.Get(), &srvDesc, &m_shaderResourceView);
60 SLEAK_ERROR(
"Failed to create shader resource view! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
65 m_deviceContext->UpdateSubresource(m_texture.Get(), 0,
nullptr, data,
width * 4, 0);
66 m_deviceContext->GenerateMips(m_shaderResourceView.Get());
76 if (m_shaderResourceView && m_samplerState) {
77 m_deviceContext->PSSetShaderResources(slot, 1, m_shaderResourceView.GetAddressOf());
78 m_deviceContext->PSSetSamplers(slot, 1, m_samplerState.GetAddressOf());
84 Microsoft::WRL::ComPtr<IWICImagingFactory> wicFactory;
85 HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory,
nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicFactory));
87 SLEAK_ERROR(
"Failed to create WIC imaging factory! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
92 Microsoft::WRL::ComPtr<IWICBitmapDecoder> decoder;
93 hr = wicFactory->CreateDecoderFromFilename(
94 std::wstring(filePath.begin(), filePath.end()).c_str(),
97 WICDecodeMetadataCacheOnLoad,
100 SLEAK_ERROR(
"Failed to load texture file: {}", filePath);
105 Microsoft::WRL::ComPtr<IWICBitmapFrameDecode> frame;
106 hr = decoder->GetFrame(0, &frame);
108 SLEAK_ERROR(
"Failed to get frame from texture file: {}", filePath);
113 Microsoft::WRL::ComPtr<IWICFormatConverter> converter;
114 hr = wicFactory->CreateFormatConverter(&converter);
116 SLEAK_ERROR(
"Failed to create WIC format converter! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
120 hr = converter->Initialize(
122 GUID_WICPixelFormat32bppRGBA,
123 WICBitmapDitherTypeNone,
126 WICBitmapPaletteTypeCustom);
128 SLEAK_ERROR(
"Failed to initialize WIC format converter! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
136 SLEAK_ERROR(
"Failed to get texture dimensions! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
141 std::vector<uint8_t> imageData(
width *
height * 4);
144 hr = converter->CopyPixels(
147 static_cast<UINT
>(imageData.size()),
150 SLEAK_ERROR(
"Failed to copy texture pixels! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
159 ID3D11ShaderResourceView* nullSRV =
nullptr;
160 ID3D11SamplerState* nullSampler =
nullptr;
161 m_deviceContext->PSSetShaderResources(0, 1, &nullSRV);
162 m_deviceContext->PSSetSamplers(0, 1, &nullSampler);
167 CreateSamplerState();
171 m_wrapMode = wrapMode;
172 CreateSamplerState();
177 CreateSamplerState();
196void DirectX11Texture::CreateSamplerState() {
206 D3D11_SAMPLER_DESC samplerDesc = {};
207 samplerDesc.Filter = GetD3D11Filter(m_filter);
208 samplerDesc.AddressU = GetD3D11WrapMode(m_wrapMode);
209 samplerDesc.AddressV = GetD3D11WrapMode(m_wrapMode);
210 samplerDesc.AddressW = GetD3D11WrapMode(m_wrapMode);
211 samplerDesc.MipLODBias = m_lodBias;
212 samplerDesc.MaxAnisotropy = maxAniso;
213 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
214 samplerDesc.MinLOD = 0;
215 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
217 HRESULT hr = m_device->CreateSamplerState(&samplerDesc, &m_samplerState);
219 SLEAK_ERROR(
"Failed to create sampler state! HRESULT: 0x{:08X}",
static_cast<unsigned int>(hr));
223DXGI_FORMAT DirectX11Texture::GetDXGIFormat(
TextureFormat format)
const {
230 default:
return DXGI_FORMAT_UNKNOWN;
234D3D11_FILTER DirectX11Texture::GetD3D11Filter(
TextureFilter filter)
const {
243 default:
return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
247D3D11_TEXTURE_ADDRESS_MODE DirectX11Texture::GetD3D11WrapMode(
TextureWrapMode wrapMode)
const {
254 default:
return D3D11_TEXTURE_ADDRESS_WRAP;
void Unbind() const override
void SetWrapMode(TextureWrapMode wrapMode) override
TextureType GetType() const override
bool LoadFromMemory(const void *data, uint32_t width, uint32_t height, TextureFormat format) override
Uploads raw pixel data as the texture's contents, replacing any existing image.
void Bind(uint32_t slot=0) const override
void SetLodBias(float bias) override
uint32_t GetHeight() const override
DirectX11Texture(ID3D11Device *device)
uint32_t GetWidth() const override
void SetFilter(TextureFilter filter) override
bool LoadFromFile(const std::string &filePath) override
Loads and uploads an image file from disk.
TextureFormat GetFormat() const override
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.