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

Adiciona PlatformImplementation em Texture2D

This commit is contained in:
Danilo 2024-11-16 12:15:40 -03:00
parent cdc628d3c1
commit 61da1aac46
10 changed files with 156 additions and 151 deletions

View File

@ -61,19 +61,43 @@ namespace xna {
struct SamplerStateImplementation { struct SamplerStateImplementation {
comptr<ID3D11SamplerState> SamplerState; comptr<ID3D11SamplerState> SamplerState;
D3D11_SAMPLER_DESC Description; D3D11_SAMPLER_DESC Description{};
}; };
struct SpriteBatchImplementation { struct SpriteBatchImplementation {
std::shared_ptr<DirectX::SpriteBatch> SpriteBatch; std::shared_ptr<DirectX::SpriteBatch> SpriteBatch;
comptr<ID3D11InputLayout> InputLayout;
std::shared_ptr<DirectX::DX11::IEffect> EffectBuffer; std::shared_ptr<DirectX::DX11::IEffect> EffectBuffer;
comptr<ID3D11InputLayout> InputLayout;
}; };
struct SpriteFontImplementation { struct SpriteFontImplementation {
std::unique_ptr<DirectX::SpriteFont> SpriteFont; std::unique_ptr<DirectX::SpriteFont> SpriteFont;
}; };
struct Texture2DImplementation {
Texture2DImplementation() {
Description.MipLevels = 1;
Description.ArraySize = 1;
Description.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
Description.SampleDesc.Count = 1;
Description.Usage = D3D11_USAGE_DEFAULT;
Description.BindFlags = D3D11_BIND_SHADER_RESOURCE;
ShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
ShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
ShaderDescription.Texture2D.MipLevels = Description.MipLevels;
ShaderDescription.Texture2D.MostDetailedMip = 0;
}
comptr<ID3D11Texture2D> Texture2D;
comptr<ID3D11ShaderResourceView> ShaderResource;
D3D11_SUBRESOURCE_DATA SubResource{};
D3D11_TEXTURE2D_DESC Description{};
D3D11_SHADER_RESOURCE_VIEW_DESC ShaderDescription{};
HRESULT SetData(GraphicsDevice& device, UINT const* data);
};
struct GamePad::PlatformImplementation { struct GamePad::PlatformImplementation {
uptr<DirectX::GamePad> _dxGamePad = unew<DirectX::GamePad>(); uptr<DirectX::GamePad> _dxGamePad = unew<DirectX::GamePad>();
@ -127,15 +151,7 @@ namespace xna {
return !FAILED(hr); return !FAILED(hr);
} }
}; };
struct Texture2D::PlatformImplementation {
comptr<ID3D11Texture2D> dxTexture2D{ nullptr };
comptr<ID3D11ShaderResourceView> dxShaderResource{ nullptr };
D3D11_SUBRESOURCE_DATA dxSubResource{};
D3D11_TEXTURE2D_DESC dxDescription{};
D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{};
};
struct RenderTarget2D::PlatformImplementation { struct RenderTarget2D::PlatformImplementation {
comptr<ID3D11RenderTargetView> _renderTargetView = nullptr; comptr<ID3D11RenderTargetView> _renderTargetView = nullptr;

View File

@ -11,6 +11,8 @@
#include "../../graphics/shared.hpp" #include "../../graphics/shared.hpp"
namespace xna { namespace xna {
using PTexture2D = std::shared_ptr<Texture2D>;
class Texture2DReader : public ContentTypeReaderT<PTexture2D> { class Texture2DReader : public ContentTypeReaderT<PTexture2D> {
public: public:
Texture2DReader() : ContentTypeReaderT(typeof<PTexture2D>()) { Texture2DReader() : ContentTypeReaderT(typeof<PTexture2D>()) {

View File

@ -5,6 +5,8 @@
#include "texture.hpp" #include "texture.hpp"
namespace xna { namespace xna {
struct RenderTarget2DImplementation;
//Contains a 2D texture that can be used as a render target. //Contains a 2D texture that can be used as a render target.
class RenderTarget2D : public Texture2D { class RenderTarget2D : public Texture2D {
public: public:

View File

@ -1,72 +1,71 @@
#ifndef XNA_GRAPHICS_TEXTURE_HPP #ifndef XNA_GRAPHICS_TEXTURE_HPP
#define XNA_GRAPHICS_TEXTURE_HPP #define XNA_GRAPHICS_TEXTURE_HPP
#include "../default.hpp" #include "../csharp/stream.hpp"
#include "../platform.hpp"
#include "gresource.hpp" #include "gresource.hpp"
#include "shared.hpp" #include "shared.hpp"
#include <cstdint>
#include <string>
#include <vector>
namespace xna { namespace xna {
//Represents a texture resource. //Represents a texture resource.
class Texture : public GraphicsResource { class Texture : public GraphicsResource {
public: public:
Texture(P_GraphicsDevice const& graphicsDevice) : GraphicsResource(graphicsDevice) {} Texture(std::shared_ptr<GraphicsDevice> const& graphicsDevice) : GraphicsResource(graphicsDevice) {}
//Gets the format of the texture data. //Gets the format of the texture data.
virtual SurfaceFormat Format() const = 0; virtual SurfaceFormat Format() const = 0;
//Gets the number of texture levels in a multilevel texture. //Gets the number of texture levels in a multilevel texture.
virtual Int LevelCount() const = 0; virtual int32_t LevelCount() const = 0;
}; };
struct Texture2DImplementation;
//Represents a 2D grid of texels. //Represents a 2D grid of texels.
class Texture2D : public Texture { class Texture2D : public Texture, public PlatformImplementation<Texture2DImplementation> {
public: public:
Texture2D(); Texture2D();
Texture2D(P_GraphicsDevice const& device); Texture2D(std::shared_ptr<GraphicsDevice> const& device);
Texture2D(P_GraphicsDevice const& device, size_t width, size_t height); Texture2D(std::shared_ptr<GraphicsDevice> const& device, size_t width, size_t height);
Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); Texture2D(std::shared_ptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format);
//Gets the width of this texture resource, in pixels. //Gets the width of this texture resource, in pixels.
constexpr Int Width() const { return width; } constexpr int32_t Width() const { return width; }
//Gets the height of this texture resource, in pixels. //Gets the height of this texture resource, in pixels.
constexpr Int Height() const { return height; } constexpr int32_t Height() const { return height; }
//Gets the size of this resource. //Gets the size of this resource.
constexpr Rectangle Bounds() const { return { 0, 0, width, height }; } constexpr Rectangle Bounds() const { return { 0, 0, width, height }; }
//Gets the format of the texture data. //Gets the format of the texture data.
constexpr SurfaceFormat Format() const override { return surfaceFormat; } constexpr SurfaceFormat Format() const override { return surfaceFormat; }
//Gets the number of texture levels in a multilevel texture. //Gets the number of texture levels in a multilevel texture.
constexpr Int LevelCount() const { return levelCount; } constexpr int32_t LevelCount() const { return levelCount; }
//Sets data to the texture. //Sets data to the texture.
void SetData(std::vector<Color> const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(std::vector<Color> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture. //Sets data to the texture.
void SetData(std::vector<Uint> const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(std::vector<uint32_t> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture. //Sets data to the texture.
void SetData(std::vector<Byte> const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(std::vector<uint8_t> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture. //Sets data to the texture.
void SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount); void SetData(int32_t level, Rectangle* rect, std::vector<uint8_t> const& data, size_t startIndex, size_t elementCount);
//Loads texture data from a stream. //Loads texture data from a stream.
static P_Texture2D FromStream(GraphicsDevice& device, P_Stream const& stream); static P_Texture2D FromStream(GraphicsDevice& device, std::shared_ptr<Stream> const& stream);
//Loads texture data from a file. //Loads texture data from a file.
static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName); static P_Texture2D FromStream(GraphicsDevice& device, std::string const& fileName);
//Loads texture data from a data. //Loads texture data from a data.
static P_Texture2D FromStream(GraphicsDevice& device, std::vector<Byte> const& data); static P_Texture2D FromStream(GraphicsDevice& device, std::vector<uint8_t> const& data);
void Initialize(); void Initialize();
protected: protected:
SurfaceFormat surfaceFormat{ SurfaceFormat::Color }; SurfaceFormat surfaceFormat{ SurfaceFormat::Color };
Int levelCount{ 0 }; int32_t levelCount{ 0 };
int width{ 0 }; int width{ 0 };
int height{ 0 }; int height{ 0 };
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
}; };
using PTexture2D = P_Texture2D;
} }
#endif #endif

View File

@ -3,7 +3,6 @@
#include "../common/numerics.hpp" #include "../common/numerics.hpp"
#include "../common/color.hpp" #include "../common/color.hpp"
#include "../default.hpp"
namespace xna { namespace xna {
//Describes a custom vertex format structure that contains position and color information. //Describes a custom vertex format structure that contains position and color information.

View File

@ -99,10 +99,10 @@ namespace xna {
} }
void BasicEffect::Texture(sptr<xna::Texture2D> const& value) { void BasicEffect::Texture(sptr<xna::Texture2D> const& value) {
if (!value || !value->impl || !value->impl->dxShaderResource) if (!value || !value->Implementation || !value->Implementation->ShaderResource)
Exception::Throw(Exception::ARGUMENT_IS_NULL); Exception::Throw(Exception::ARGUMENT_IS_NULL);
impl->dxBasicEffect->SetTexture(value->impl->dxShaderResource.Get()); impl->dxBasicEffect->SetTexture(value->Implementation->ShaderResource.Get());
} }
void BasicEffect::TextureEnabled(bool value) { void BasicEffect::TextureEnabled(bool value) {

View File

@ -12,10 +12,10 @@ namespace xna {
P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) { P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) {
auto& swapChain = device->Implementation->SwapChain; auto& swapChain = device->Implementation->SwapChain;
auto rt = snew<RenderTarget2D>(device); auto rt = snew<RenderTarget2D>(device);
auto& implementation = rt->impl; auto& implementation = rt->Implementation;
auto& implementation2 = rt->impl2; auto& implementation2 = rt->impl2;
if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D)) if (!swapChain->impl->GetBackBuffer(implementation->Texture2D))
{ {
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
} }
@ -26,23 +26,23 @@ namespace xna {
} }
void RenderTarget2D::Initialize() { void RenderTarget2D::Initialize() {
if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) { if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
Exception::Throw(Exception::UNABLE_TO_INITIALIZE); Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
} }
if (impl2->_renderTargetView) if (impl2->_renderTargetView)
return; return;
impl->dxDescription.Width = width; Implementation->Description.Width = width;
impl->dxDescription.Height = height; Implementation->Description.Height = height;
impl->dxDescription.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET; Implementation->Description.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET;
Texture2D::Initialize(); Texture2D::Initialize();
auto& dxdevice = BaseGraphicsDevice->Implementation->Device; auto& dxdevice = BaseGraphicsDevice->Implementation->Device;
const auto hr = dxdevice->CreateRenderTargetView( const auto hr = dxdevice->CreateRenderTargetView(
impl->dxTexture2D.Get(), Implementation->Texture2D.Get(),
NULL, NULL,
impl2->_renderTargetView.ReleaseAndGetAddressOf()); impl2->_renderTargetView.ReleaseAndGetAddressOf());

View File

@ -23,7 +23,7 @@ namespace xna {
std::optional<Char> const& defaultCharacter) std::optional<Char> const& defaultCharacter)
{ {
Exception::ThrowIfNull(texture, nameof(texture)); Exception::ThrowIfNull(texture, nameof(texture));
Exception::ThrowIfNull(texture->impl->dxShaderResource.Get(), nameof(texture->impl->dxShaderResource)); Exception::ThrowIfNull(texture->Implementation->ShaderResource.Get(), nameof(texture->Implementation->ShaderResource));
if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size())) if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size()))
Exception::Throw("Cropping, charmap and kerning (if not empty) must all be the same size."); Exception::Throw("Cropping, charmap and kerning (if not empty) must all be the same size.");
@ -55,7 +55,7 @@ namespace xna {
Implementation = unew<SpriteFontImplementation>(); Implementation = unew<SpriteFontImplementation>();
Implementation->SpriteFont = unew<DxSpriteFont>( Implementation->SpriteFont = unew<DxSpriteFont>(
//ID3D11ShaderResourceView* texture //ID3D11ShaderResourceView* texture
texture->impl->dxShaderResource.Get(), texture->Implementation->ShaderResource.Get(),
//Glyph const* glyphs //Glyph const* glyphs
dxGlyps.data(), dxGlyps.data(),
//size_t glyphCount //size_t glyphCount
@ -177,7 +177,7 @@ namespace xna {
const auto _color = DxHelpers::VectorToDx(v4); const auto _color = DxHelpers::VectorToDx(v4);
Implementation->SpriteBatch->Draw( Implementation->SpriteBatch->Draw(
texture.impl->dxShaderResource.Get(), texture.Implementation->ShaderResource.Get(),
_position, _position,
_color _color
); );
@ -195,7 +195,7 @@ namespace xna {
}; };
Implementation->SpriteBatch->Draw( Implementation->SpriteBatch->Draw(
texture.impl->dxShaderResource.Get(), texture.Implementation->ShaderResource.Get(),
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color); _color);
@ -216,7 +216,7 @@ namespace xna {
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
Implementation->SpriteBatch->Draw( Implementation->SpriteBatch->Draw(
texture.impl->dxShaderResource.Get(), texture.Implementation->ShaderResource.Get(),
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,
@ -243,7 +243,7 @@ namespace xna {
const XMFLOAT2 _scale = { scale.X, scale.Y }; const XMFLOAT2 _scale = { scale.X, scale.Y };
Implementation->SpriteBatch->Draw( Implementation->SpriteBatch->Draw(
texture.impl->dxShaderResource.Get(), texture.Implementation->ShaderResource.Get(),
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,
@ -260,7 +260,7 @@ namespace xna {
const auto v4 = color.ToVector4(); const auto v4 = color.ToVector4();
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
Implementation->SpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); Implementation->SpriteBatch->Draw(texture.Implementation->ShaderResource.Get(), _destinationRect, _color);
} }
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) { void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
@ -278,7 +278,7 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
Implementation->SpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); Implementation->SpriteBatch->Draw(texture.Implementation->ShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
} }
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
@ -297,7 +297,7 @@ namespace xna {
const auto _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
Implementation->SpriteBatch->Draw( Implementation->SpriteBatch->Draw(
texture.impl->dxShaderResource.Get(), texture.Implementation->ShaderResource.Get(),
_destinationRect, _destinationRect,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,

View File

@ -74,7 +74,7 @@ namespace xna {
if (!impl || !impl->dxSwapChain) if (!impl || !impl->dxSwapChain)
return false; return false;
const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.impl->dxTexture2D)); const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.Implementation->Texture2D));
return !FAILED(hr); return !FAILED(hr);
} }

View File

@ -1,34 +1,58 @@
#include "xna-dx/framework.hpp" #include "xna-dx/framework.hpp"
namespace xna { namespace xna {
static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl); HRESULT Texture2DImplementation::SetData(GraphicsDevice& device, UINT const* data) {
static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data); if (!Texture2D) {
auto hr = device.Implementation->Device->CreateTexture2D(&Description, nullptr, Texture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
return hr;
}
}
comptr<ID3D11Resource> resource = nullptr;
auto hr = Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
return hr;
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
device.Implementation->Context->UpdateSubresource(resource.Get(), 0, nullptr, data, Description.Width * R8G8B8A8U_BYTE_SIZE, 0);
ShaderDescription.Texture2D.MipLevels = Description.MipLevels;
hr = device.Implementation->Device->CreateShaderResourceView(resource.Get(), &ShaderDescription, ShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
return hr;
}
Texture2D->GetDesc(&Description);
return NO_ERROR;
}
Texture2D::Texture2D() : Texture(nullptr) { Texture2D::Texture2D() : Texture(nullptr) {
impl = unew<PlatformImplementation>(); Implementation = unew<Texture2DImplementation>();
setDefaultTexture2DDesc(*impl);
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device), width(width), height(height) { Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device), width(width), height(height) {
impl = unew<PlatformImplementation>(); Implementation = unew<Texture2DImplementation>();
setDefaultTexture2DDesc(*impl); Implementation->Description.Width = static_cast<UINT>(this->width);
impl->dxDescription.Width = static_cast<UINT>(this->width); Implementation->Description.Height = static_cast<UINT>(this->height);
impl->dxDescription.Height = static_cast<UINT>(this->height);
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) { Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) {
impl = unew<PlatformImplementation>(); Implementation = unew<Texture2DImplementation>();
setDefaultTexture2DDesc(*impl);
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format)
: Texture(device), width(width), height(height), levelCount(mipMap) { : Texture(device), width(width), height(height), levelCount(mipMap) {
impl = unew<PlatformImplementation>(); Implementation = unew<Texture2DImplementation>();
setDefaultTexture2DDesc(*impl); Implementation->Description.Width = static_cast<UINT>(this->width);
impl->dxDescription.Width = static_cast<UINT>(this->width); Implementation->Description.Height = static_cast<UINT>(this->height);
impl->dxDescription.Height = static_cast<UINT>(this->height); Implementation->Description.MipLevels = static_cast<UINT>(this->levelCount);
impl->dxDescription.MipLevels = static_cast<UINT>(this->levelCount); Implementation->Description.Format = DxHelpers::SurfaceFormatToDx(format);
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format);
} }
void Texture2D::Initialize() void Texture2D::Initialize()
@ -41,51 +65,54 @@ namespace xna {
HRESULT hr = 0; HRESULT hr = 0;
if (!impl->dxTexture2D) { if (!Implementation->Texture2D) {
hr = deviceImpl->Device->CreateTexture2D( hr = deviceImpl->Device->CreateTexture2D(
&impl->dxDescription, &Implementation->Description,
nullptr, nullptr,
impl->dxTexture2D.ReleaseAndGetAddressOf()); Implementation->Texture2D.ReleaseAndGetAddressOf());
if FAILED(hr) if FAILED(hr)
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
} }
else { else {
//Updates description if texture is not null //Updates description if texture is not null
impl->dxTexture2D->GetDesc(&impl->dxDescription); Implementation->Texture2D->GetDesc(&Implementation->Description);
} }
comptr<ID3D11Resource> resource = nullptr; comptr<ID3D11Resource> resource = nullptr;
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if FAILED(hr) if FAILED(hr)
Exception::Throw(Exception::INVALID_OPERATION); Exception::Throw(Exception::INVALID_OPERATION);
//Only initializes if it is a ShaderResource //Only initializes if it is a ShaderResource
if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) { if (Implementation->Description.BindFlags & D3D11_BIND_SHADER_RESOURCE) {
hr = deviceImpl->Device->CreateShaderResourceView( hr = deviceImpl->Device->CreateShaderResourceView(
resource.Get(), resource.Get(),
&impl->dxShaderDescription, &Implementation->ShaderDescription,
impl->dxShaderResource.ReleaseAndGetAddressOf()); Implementation->ShaderResource.ReleaseAndGetAddressOf());
if FAILED(hr) if FAILED(hr)
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
} }
surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format); surfaceFormat = DxHelpers::SurfaceFormatToXna(Implementation->Description.Format);
levelCount = static_cast<Int>(impl->dxShaderDescription.Texture2D.MipLevels); levelCount = static_cast<Int>(Implementation->ShaderDescription.Texture2D.MipLevels);
width = static_cast<Int>(impl->dxDescription.Width); width = static_cast<Int>(Implementation->Description.Width);
height = static_cast<Int>(impl->dxDescription.Height); height = static_cast<Int>(Implementation->Description.Height);
} }
void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount) void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount)
{ {
if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) { if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) {
Exception::Throw(Exception::INVALID_OPERATION); Exception::Throw(Exception::INVALID_OPERATION);
} }
internalTexture2DSetData(*impl, *BaseGraphicsDevice, data.data()); auto hr = Implementation->SetData(*BaseGraphicsDevice, data.data());
if (FAILED(hr))
Exception::Throw(Exception::FAILED_TO_APPLY);
} }
void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount) void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
@ -106,7 +133,10 @@ namespace xna {
++fIndex; ++fIndex;
} }
internalTexture2DSetData(*impl, *BaseGraphicsDevice, finalData.data()); auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data());
if (FAILED(hr))
Exception::Throw(Exception::FAILED_TO_APPLY);
} }
void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount) void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
@ -127,8 +157,8 @@ namespace xna {
++fIndex; ++fIndex;
} }
if (!impl->dxTexture2D) { if (!Implementation->Texture2D) {
auto hr = BaseGraphicsDevice->Implementation->Device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.GetAddressOf()); auto hr = BaseGraphicsDevice->Implementation->Device->CreateTexture2D(&Implementation->Description, nullptr, Implementation->Texture2D.GetAddressOf());
if (FAILED(hr)) { if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
@ -136,7 +166,7 @@ namespace xna {
} }
comptr<ID3D11Resource> resource = nullptr; comptr<ID3D11Resource> resource = nullptr;
auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); auto hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) { if (FAILED(hr)) {
Exception::Throw(Exception::INVALID_OPERATION); Exception::Throw(Exception::INVALID_OPERATION);
@ -154,17 +184,17 @@ namespace xna {
} }
constexpr int R8G8B8A8U_BYTE_SIZE = 4; constexpr int R8G8B8A8U_BYTE_SIZE = 4;
BaseGraphicsDevice->Implementation->Context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); BaseGraphicsDevice->Implementation->Context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), Implementation->Description.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl->dxShaderDescription.Format = impl->dxDescription.Format; Implementation->ShaderDescription.Format = Implementation->Description.Format;
impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels; Implementation->ShaderDescription.Texture2D.MipLevels = Implementation->Description.MipLevels;
hr = BaseGraphicsDevice->Implementation->Device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf()); hr = BaseGraphicsDevice->Implementation->Device->CreateShaderResourceView(resource.Get(), &Implementation->ShaderDescription, Implementation->ShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) { if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
} }
impl->dxTexture2D->GetDesc(&impl->dxDescription); Implementation->Texture2D->GetDesc(&Implementation->Description);
} }
void Texture2D::SetData(std::vector<Color> const& data, size_t startIndex, size_t elementCount) void Texture2D::SetData(std::vector<Color> const& data, size_t startIndex, size_t elementCount)
@ -181,7 +211,10 @@ namespace xna {
++finalDataIndex; ++finalDataIndex;
} }
internalTexture2DSetData(*impl, *BaseGraphicsDevice, finalData.data()); auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data());
if (FAILED(hr))
Exception::Throw(Exception::FAILED_TO_APPLY);
} }
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream) P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream)
@ -205,22 +238,22 @@ namespace xna {
device.Implementation->Context.Get(), device.Implementation->Context.Get(),
wstr.c_str(), wstr.c_str(),
resource.GetAddressOf(), resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(), texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf(),
0U); 0U);
if (FAILED(result)) { if (FAILED(result)) {
return nullptr; return nullptr;
} }
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf());
if (FAILED(result)) { if (FAILED(result)) {
return nullptr; return nullptr;
} }
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
texture2d->impl->dxTexture2D->GetDesc(&desc); texture2d->Implementation->Texture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc; texture2d->Implementation->Description = desc;
return texture2d; return texture2d;
} }
@ -237,69 +270,23 @@ namespace xna {
data.data(), data.data(),
data.size(), data.size(),
resource.GetAddressOf(), resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf()); texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
return nullptr; return nullptr;
} }
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) { if (FAILED(hr)) {
return nullptr; return nullptr;
} }
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
texture2d->impl->dxTexture2D->GetDesc(&desc); texture2d->Implementation->Texture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc; texture2d->Implementation->Description = desc;
return texture2d; return texture2d;
} }
HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
{
if (!impl.dxTexture2D) {
auto hr = device.Implementation->Device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE);
}
}
comptr<ID3D11Resource> resource = nullptr;
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(Exception::INVALID_OPERATION);
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
device.Implementation->Context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
hr = device.Implementation->Device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE);
}
impl.dxTexture2D->GetDesc(&impl.dxDescription);
return NO_ERROR;
}
void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl) {
impl.dxDescription.MipLevels = 1;
impl.dxDescription.ArraySize = 1;
impl.dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
impl.dxDescription.SampleDesc.Count = 1;
impl.dxDescription.Usage = D3D11_USAGE_DEFAULT;
impl.dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE;
impl.dxShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
impl.dxShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
impl.dxShaderDescription.Texture2D.MostDetailedMip = 0;
}
} }