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

Corrige Texture2D e RenderTarget2D

This commit is contained in:
Danilo 2024-05-23 14:38:16 -03:00
parent 7b8a37ab6d
commit 908539a995
13 changed files with 233 additions and 245 deletions

View File

@ -1,12 +1,13 @@
#include "common/color.hpp" #include "common/color.hpp"
#include "graphics/adapter.hpp" #include "graphics/adapter.hpp"
#include "graphics/blendstate.hpp" #include "graphics/blendstate.hpp"
#include "graphics/rendertarget.hpp"
#include "platform-dx/device-dx.hpp" #include "platform-dx/device-dx.hpp"
#include "platform-dx/gdeviceinfo-dx.hpp" #include "platform-dx/gdeviceinfo-dx.hpp"
#include "platform-dx/gdevicemanager-dx.hpp" #include "platform-dx/gdevicemanager-dx.hpp"
#include "platform-dx/implementations.hpp" #include "platform-dx/implementations.hpp"
#include "platform-dx/rendertarget-dx.hpp"
#include "platform-dx/window-dx.hpp" #include "platform-dx/window-dx.hpp"
#include "platform-dx/implementations.hpp"
namespace xna { namespace xna {
GraphicsDevice::GraphicsDevice() { GraphicsDevice::GraphicsDevice() {
@ -77,7 +78,7 @@ namespace xna {
bool GraphicsDevice::Present() { bool GraphicsDevice::Present() {
const auto result = _swapChain->Present(_usevsync); const auto result = _swapChain->Present(_usevsync);
_context->OMSetRenderTargets(1, &_renderTarget2D->_renderTargetView, nullptr); _context->OMSetRenderTargets(1, &_renderTarget2D->render_impl->_renderTargetView, nullptr);
return result; return result;
} }
@ -142,7 +143,7 @@ namespace xna {
} }
void GraphicsDevice::Clear() { void GraphicsDevice::Clear() {
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor); _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor);
} }
void GraphicsDevice::Clear(Color const& color) { void GraphicsDevice::Clear(Color const& color) {
@ -153,6 +154,6 @@ namespace xna {
_backgroundColor[2] = v4.Z; _backgroundColor[2] = v4.Z;
_backgroundColor[3] = v4.W; _backgroundColor[3] = v4.W;
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor); _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor);
} }
} }

View File

@ -1,9 +1,9 @@
#include "platform-dx/init-dx.hpp" #include "platform-dx/init-dx.hpp"
#include "csharp/type.hpp" #include "csharp/type.hpp"
#include "platform-dx/texture-dx.hpp"
#include "platform-dx/content-readers/texture2Dreader-dx.hpp" #include "platform-dx/content-readers/texture2Dreader-dx.hpp"
#include "content/typereadermanager.hpp" #include "content/typereadermanager.hpp"
#include "content/defaultreaders.hpp" #include "content/defaultreaders.hpp"
#include "platform-dx/implementations.hpp"
namespace xna { namespace xna {

View File

@ -1,26 +1,38 @@
#include "platform-dx/rendertarget-dx.hpp"
#include "graphics/device.hpp" #include "graphics/device.hpp"
#include "platform-dx/device-dx.hpp" #include "platform-dx/device-dx.hpp"
#include "platform-dx/implementations.hpp" #include "platform-dx/implementations.hpp"
#include "graphics/rendertarget.hpp"
namespace xna { namespace xna {
RenderTarget2D::RenderTarget2D() : Texture2D() {
render_impl = unew<PlatformImplementation>();
}
RenderTarget2D::RenderTarget2D(sptr<GraphicsDevice> const& device) : Texture2D(device) {
render_impl = unew<PlatformImplementation>();
}
RenderTarget2D::~RenderTarget2D() {
render_impl = nullptr;
}
bool RenderTarget2D::Initialize(xna_error_ptr_arg) { bool RenderTarget2D::Initialize(xna_error_ptr_arg) {
if (!m_device || !m_device->_device) { if (!impl || !m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false; return false;
} }
if (dxTexture2D) { if (impl->dxTexture2D) {
dxTexture2D->Release(); impl->dxTexture2D->Release();
dxTexture2D = nullptr; impl->dxTexture2D = nullptr;
} }
if (!m_device->_swapChain->impl->GetBackBuffer(dxTexture2D)) if (!m_device->_swapChain->impl->GetBackBuffer(impl->dxTexture2D))
return false; return false;
auto& dxdevice = m_device->_device; auto& dxdevice = m_device->_device;
const auto hr = dxdevice->CreateRenderTargetView(dxTexture2D, NULL, &_renderTargetView); const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D, NULL, &render_impl->_renderTargetView);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -37,7 +49,7 @@ namespace xna {
} }
auto& context = m_device->_context; auto& context = m_device->_context;
context->OMSetRenderTargets(1, &_renderTargetView, nullptr); context->OMSetRenderTargets(1, &render_impl->_renderTargetView, nullptr);
return true; return true;
} }
} }

View File

@ -1,7 +1,6 @@
#include "platform-dx/device-dx.hpp" #include "platform-dx/device-dx.hpp"
#include "graphics/rasterizerstate.hpp" #include "graphics/rasterizerstate.hpp"
#include "graphics/samplerstate.hpp" #include "graphics/samplerstate.hpp"
#include "platform-dx/texture-dx.hpp"
#include "common/color.hpp" #include "common/color.hpp"
#include "common/numerics.hpp" #include "common/numerics.hpp"
#include "graphics/sprite.hpp" #include "graphics/sprite.hpp"
@ -98,7 +97,7 @@ namespace xna {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -106,7 +105,7 @@ namespace xna {
XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
implementation->_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.impl->dxShaderResource,
_position, _position,
_color _color
); );
@ -116,7 +115,7 @@ namespace xna {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -133,7 +132,7 @@ namespace xna {
}; };
implementation->_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.impl->dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color); _color);
@ -143,7 +142,7 @@ namespace xna {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -163,7 +162,7 @@ namespace xna {
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
implementation->_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.impl->dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,
@ -178,7 +177,7 @@ namespace xna {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -199,7 +198,7 @@ namespace xna {
const XMFLOAT2 _scale = { scale.X, scale.Y }; const XMFLOAT2 _scale = { scale.X, scale.Y };
implementation->_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.impl->dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,
@ -214,7 +213,7 @@ namespace xna {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
RECT _destinationRect{}; RECT _destinationRect{};
@ -226,14 +225,14 @@ 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->_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, _color); implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, _color);
} }
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) { void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
RECT _destinationRect{}; RECT _destinationRect{};
@ -254,14 +253,14 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
implementation->_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
} }
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
if (!implementation->_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.impl->dxShaderResource)
return; return;
RECT _destinationRect{}; RECT _destinationRect{};
@ -286,7 +285,7 @@ namespace xna {
const auto _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
implementation->_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.impl->dxShaderResource,
_destinationRect, _destinationRect,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
_color, _color,

View File

@ -83,7 +83,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.dxTexture2D)); const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.impl->dxTexture2D));
return !FAILED(hr); return !FAILED(hr);
} }

View File

@ -1,10 +1,13 @@
#include "platform-dx/texture-dx.hpp"
#include "platform-dx/device-dx.hpp" #include "platform-dx/device-dx.hpp"
#include "graphics/adapter.hpp" #include "graphics/adapter.hpp"
#include "platform-dx/implementations.hpp" #include "platform-dx/implementations.hpp"
#include "platform-dx/dxhelpers.hpp" #include "platform-dx/dxhelpers.hpp"
namespace xna { namespace xna {
Texture2D::~Texture2D() {
impl = nullptr;
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg) sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg)
{ {
auto _this = device.shared_from_this(); auto _this = device.shared_from_this();
@ -17,7 +20,7 @@ namespace xna {
device._context, device._context,
wstr.c_str(), wstr.c_str(),
&resource, &resource,
&texture2d->dxShaderResource, &texture2d->impl->dxShaderResource,
0U); 0U);
if (FAILED(result)) if (FAILED(result))
@ -32,7 +35,7 @@ namespace xna {
return nullptr; return nullptr;
} }
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D); result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
if (FAILED(result)) { if (FAILED(result)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -46,8 +49,8 @@ namespace xna {
} }
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
texture2d->dxTexture2D->GetDesc(&desc); texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->dxDescription = desc; texture2d->impl->dxDescription = desc;
resource->Release(); resource->Release();
resource = nullptr; resource = nullptr;
@ -57,7 +60,7 @@ namespace xna {
bool Texture2D::Initialize(xna_error_ptr_arg) bool Texture2D::Initialize(xna_error_ptr_arg)
{ {
if (dxTexture2D) { if (impl->dxTexture2D) {
xna_error_apply(err, XnaErrorCode::WARNING_INITIALIZED_RESOURCE); xna_error_apply(err, XnaErrorCode::WARNING_INITIALIZED_RESOURCE);
return false; return false;
} }
@ -67,7 +70,7 @@ namespace xna {
return false; return false;
} }
auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D); auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -75,14 +78,14 @@ namespace xna {
} }
ID3D11Resource* resource = nullptr; ID3D11Resource* resource = nullptr;
hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false; return false;
} }
hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource); hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
if (resource) { if (resource) {
resource->Release(); resource->Release();
@ -97,33 +100,100 @@ namespace xna {
return true; return true;
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : GraphicsResource(device) { void setDefaultDesc(Texture2D::PlatformImplementation& impl) {
setDefaultDesc(); impl.dxDescription.MipLevels = 1;
dxDescription.Width = static_cast<UINT>(width); impl.dxDescription.ArraySize = 1;
dxDescription.Height = static_cast<UINT>(height); 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;
}
Texture2D::Texture2D() : GraphicsResource(nullptr) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : GraphicsResource(device) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : GraphicsResource(device) { Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
setDefaultDesc(); impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
} }
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device) Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device)
{ {
setDefaultDesc(); impl = unew<PlatformImplementation>();
dxDescription.Width = static_cast<UINT>(width); setDefaultDesc(*impl);
dxDescription.Height = static_cast<UINT>(height); impl->dxDescription.Width = static_cast<UINT>(width);
dxDescription.MipLevels = static_cast<UINT>(mipMap); impl->dxDescription.Height = static_cast<UINT>(height);
dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format); impl->dxDescription.MipLevels = static_cast<UINT>(mipMap);
impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format);
}
HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data, xna_error_ptr_arg)
{
if (!impl.dxTexture2D) {
auto hr = device._device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return hr;
}
}
ID3D11Resource* resource = nullptr;
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return hr;
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
device._context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (impl.dxShaderResource) {
impl.dxShaderResource->Release();
impl.dxShaderResource = nullptr;
}
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
hr = device._device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource);
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return hr;
}
impl.dxTexture2D->GetDesc(&impl.dxDescription);
return NO_ERROR;
} }
void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg)
{ {
if (!m_device || !m_device->_device || !m_device->_context) { if (!impl || !m_device || !m_device->_device || !m_device->_context) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return; return;
} }
internalSetData(data.data(), err); internalSetData(*impl, *m_device, data.data(), err);
} }
void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg)
@ -145,7 +215,7 @@ namespace xna {
++fIndex; ++fIndex;
} }
internalSetData(finalData.data(), err); internalSetData(*impl, *m_device, finalData.data(), err);
} }
void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg)
@ -167,8 +237,8 @@ namespace xna {
++fIndex; ++fIndex;
} }
if (!dxTexture2D) { if (!impl->dxTexture2D) {
auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D); auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -177,7 +247,7 @@ namespace xna {
} }
ID3D11Resource* resource = nullptr; ID3D11Resource* resource = nullptr;
auto hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource); auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -195,15 +265,16 @@ namespace xna {
box.front = 0; box.front = 0;
} }
m_device->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); constexpr int R8G8B8A8U_BYTE_SIZE = 4;
m_device->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (dxShaderResource) { if (impl->dxShaderResource) {
dxShaderResource->Release(); impl->dxShaderResource->Release();
dxShaderResource = nullptr; impl->dxShaderResource = nullptr;
} }
dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels; impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels;
hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource); hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
if (resource) { if (resource) {
resource->Release(); resource->Release();
@ -215,7 +286,7 @@ namespace xna {
return; return;
} }
dxTexture2D->GetDesc(&dxDescription); impl->dxTexture2D->GetDesc(&impl->dxDescription);
} }
void Texture2D::SetData(std::vector<Color> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) void Texture2D::SetData(std::vector<Color> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg)
@ -233,7 +304,7 @@ namespace xna {
++finalDataIndex; ++finalDataIndex;
} }
internalSetData(finalData.data(), err); internalSetData(*impl, *m_device, finalData.data(), err);
} }
sptr<Texture2D> Texture2D::FromMemory(GraphicsDevice& device, std::vector<Byte> const& data, xna_error_ptr_arg) sptr<Texture2D> Texture2D::FromMemory(GraphicsDevice& device, std::vector<Byte> const& data, xna_error_ptr_arg)
@ -248,7 +319,7 @@ namespace xna {
data.data(), data.data(),
data.size(), data.size(),
&resource, &resource,
&texture2d->dxShaderResource); &texture2d->impl->dxShaderResource);
if (FAILED(hr)) if (FAILED(hr))
{ {
@ -262,7 +333,7 @@ namespace xna {
return nullptr; return nullptr;
} }
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D); hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
if (FAILED(hr)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -276,54 +347,12 @@ namespace xna {
} }
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
texture2d->dxTexture2D->GetDesc(&desc); texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->dxDescription = desc; texture2d->impl->dxDescription = desc;
resource->Release(); resource->Release();
resource = nullptr; resource = nullptr;
return texture2d; return texture2d;
} }
void Texture2D::internalSetData(UINT const* data, xna_error_ptr_arg)
{
if (!dxTexture2D) {
auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
}
ID3D11Resource* resource = nullptr;
auto hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
m_device->_context->UpdateSubresource(resource, 0, nullptr, data, dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (dxShaderResource) {
dxShaderResource->Release();
dxShaderResource = nullptr;
}
dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels;
hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDescription, &dxShaderResource);
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
dxTexture2D->GetDesc(&dxDescription);
}
} }

View File

@ -2,15 +2,21 @@
#define XNA_GRAPHICS_RENDERTARGET_HPP #define XNA_GRAPHICS_RENDERTARGET_HPP
#include "../default.hpp" #include "../default.hpp"
#include "texture.hpp"
namespace xna { namespace xna {
class IRenderTarget2D { class RenderTarget2D : public Texture2D {
public: public:
virtual ~IRenderTarget2D(){} RenderTarget2D();
RenderTarget2D(sptr<GraphicsDevice> const& device);
virtual bool Initialize(xna_error_nullarg) = 0; ~RenderTarget2D() override;
virtual bool Apply(xna_error_nullarg) = 0; bool Initialize(xna_error_nullarg);
bool Apply(xna_error_nullarg);
public:
struct PlatformImplementation;
uptr<PlatformImplementation> render_impl = nullptr;
}; };
} }

View File

@ -2,18 +2,35 @@
#define XNA_GRAPHICS_TEXTURE_HPP #define XNA_GRAPHICS_TEXTURE_HPP
#include "../default.hpp" #include "../default.hpp"
#include "gresource.hpp"
namespace xna { namespace xna {
class Texture { class Texture {
public:
~Texture() {}
}; };
class ITexture2D : public Texture { class Texture2D : public Texture, public GraphicsResource {
public: public:
virtual ~ITexture2D(){} Texture2D();
virtual Int Width() const = 0; Texture2D(sptr<GraphicsDevice> const& device);
virtual Int Height() const = 0; Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height);
virtual Rectangle Bounds() const = 0; Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format);
virtual bool Initialize(xna_error_nullarg) = 0; ~Texture2D() override;
Int Width() const;
Int Height() const;
Rectangle Bounds() const;
bool Initialize(xna_error_nullarg);
void SetData(std::vector<Color> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(std::vector<Uint> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(std::vector<Byte> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_nullarg);
static sptr<Texture2D> FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg);
static sptr<Texture2D> FromMemory(GraphicsDevice& device, std::vector<Byte> const& data, xna_error_nullarg);
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
}; };
} }

View File

@ -4,7 +4,8 @@
#include "../../content/manager.hpp" #include "../../content/manager.hpp"
#include "../../content/reader.hpp" #include "../../content/reader.hpp"
#include "../../csharp/type.hpp" #include "../../csharp/type.hpp"
#include "../texture-dx.hpp" #include "graphics/texture.hpp"
#include "platform-dx/implementations.hpp"
namespace xna { namespace xna {
class Texture2DReader : public ContentTypeReaderT<PTexture2D> { class Texture2DReader : public ContentTypeReaderT<PTexture2D> {

View File

@ -1,3 +1,6 @@
#ifndef XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP
#define XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP
#include "dxheaders.hpp" #include "dxheaders.hpp"
#include "graphics/adapter.hpp" #include "graphics/adapter.hpp"
#include "graphics/blendstate.hpp" #include "graphics/blendstate.hpp"
@ -12,9 +15,11 @@
#include "input/mouse.hpp" #include "input/mouse.hpp"
#include "graphics/rasterizerstate.hpp" #include "graphics/rasterizerstate.hpp"
#include "graphics/presentparams.hpp" #include "graphics/presentparams.hpp"
#include "platform-dx/rendertarget-dx.hpp"
#include "graphics/shader.hpp" #include "graphics/shader.hpp"
#include "graphics/swapchain.hpp" #include "graphics/swapchain.hpp"
#include "graphics/texture.hpp"
#include "graphics/rendertarget.hpp"
#include "device-dx.hpp"
namespace xna { namespace xna {
struct SpriteFont::PlatformImplementation { struct SpriteFont::PlatformImplementation {
@ -26,7 +31,7 @@ namespace xna {
}; };
struct GraphicsAdapter::PlatformImplementation { struct GraphicsAdapter::PlatformImplementation {
~PlatformImplementation(){ ~PlatformImplementation() {
if (dxadapter) { if (dxadapter) {
dxadapter->Release(); dxadapter->Release();
dxadapter = nullptr; dxadapter = nullptr;
@ -40,7 +45,7 @@ namespace xna {
sptr<DisplayMode> _currentDisplayMode = nullptr; sptr<DisplayMode> _currentDisplayMode = nullptr;
public: public:
bool GetOutput(UINT slot, IDXGIOutput*& output); bool GetOutput(UINT slot, IDXGIOutput*& output);
}; };
struct BlendRenderTarget { struct BlendRenderTarget {
@ -56,7 +61,7 @@ namespace xna {
constexpr BlendRenderTarget() = default; constexpr BlendRenderTarget() = default;
}; };
struct BlendState::PlatformImplementation { struct BlendState::PlatformImplementation {
~PlatformImplementation() { ~PlatformImplementation() {
if (dxBlendState) { if (dxBlendState) {
dxBlendState->Release(); dxBlendState->Release();
@ -67,8 +72,8 @@ namespace xna {
ID3D11BlendState* dxBlendState = nullptr; ID3D11BlendState* dxBlendState = nullptr;
D3D11_BLEND_DESC dxDescription{}; D3D11_BLEND_DESC dxDescription{};
float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F };
UINT sampleMask{ 0xffffffff }; UINT sampleMask{ 0xffffffff };
}; };
struct ConstantBuffer::PlatformImplementation { struct ConstantBuffer::PlatformImplementation {
~PlatformImplementation() { ~PlatformImplementation() {
@ -109,7 +114,7 @@ namespace xna {
ID3D11DepthStencilState* dxDepthStencil = nullptr; ID3D11DepthStencilState* dxDepthStencil = nullptr;
D3D11_DEPTH_STENCIL_DESC dxDescription{}; D3D11_DEPTH_STENCIL_DESC dxDescription{};
}; };
struct DisplayModeRefreshRate { struct DisplayModeRefreshRate {
constexpr DisplayModeRefreshRate() = default; constexpr DisplayModeRefreshRate() = default;
@ -265,4 +270,38 @@ namespace xna {
return !FAILED(hr); return !FAILED(hr);
} }
}; };
}
struct Texture2D::PlatformImplementation {
~PlatformImplementation() {
if (dxTexture2D) {
dxTexture2D->Release();
dxTexture2D = nullptr;
}
if (dxShaderResource) {
dxShaderResource->Release();
dxShaderResource = nullptr;
}
}
ID3D11Texture2D* dxTexture2D{ nullptr };
ID3D11ShaderResourceView* dxShaderResource{ nullptr };
D3D11_SUBRESOURCE_DATA dxSubResource{};
D3D11_TEXTURE2D_DESC dxDescription{};
D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{};
};
struct RenderTarget2D::PlatformImplementation {
~PlatformImplementation() {
if (_renderTargetView) {
_renderTargetView->Release();
_renderTargetView = nullptr;
}
}
ID3D11RenderTargetView* _renderTargetView = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{};
};
}
#endif

View File

@ -1,30 +0,0 @@
#ifndef XNA_PLATFORM_RENDERTARGET_DX_HPP
#define XNA_PLATFORM_RENDERTARGET_DX_HPP
#include "../graphics/rendertarget.hpp"
#include "texture-dx.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class RenderTarget2D : public IRenderTarget2D, public Texture2D {
public:
RenderTarget2D(sptr<GraphicsDevice> const& device) : Texture2D(device){}
virtual ~RenderTarget2D() override {
if (_renderTargetView) {
_renderTargetView->Release();
_renderTargetView = nullptr;
}
}
virtual bool Initialize(xna_error_nullarg) override;
virtual bool Apply(xna_error_nullarg) override;
public:
ID3D11RenderTargetView* _renderTargetView = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{};
};
}
#endif

View File

@ -1,84 +0,0 @@
#ifndef XNA_PLATFORM_TEXTURE_DX_HPP
#define XNA_PLATFORM_TEXTURE_DX_HPP
#include "../common/numerics.hpp"
#include "../graphics/gresource.hpp"
#include "../graphics/texture.hpp"
#include "dxheaders.hpp"
#include "device-dx.hpp"
namespace xna {
class Texture2D : public ITexture2D, public GraphicsResource {
public:
Texture2D() : GraphicsResource(nullptr){
setDefaultDesc();
}
Texture2D(sptr<GraphicsDevice> const& device);
Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height);
Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format);
virtual ~Texture2D() override {
if (dxTexture2D) {
dxTexture2D->Release();
dxTexture2D = nullptr;
}
if (dxShaderResource) {
dxShaderResource->Release();
dxShaderResource = nullptr;
}
}
virtual constexpr Int Width() const override {
return dxDescription.Width;
}
virtual constexpr Int Height() const override {
return dxDescription.Height;
}
constexpr Rectangle Bounds() const override {
return { 0, 0, static_cast<Int>(dxDescription.Width), static_cast<Int>(dxDescription.Height) };
}
bool Initialize(xna_error_nullarg) override;
void SetData(std::vector<Color> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(std::vector<Uint> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(std::vector<Byte> const& data, size_t startIndex = 0, size_t elementCount = 0, xna_error_nullarg);
void SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount, xna_error_nullarg);
static sptr<Texture2D> FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg);
static sptr<Texture2D> FromMemory(GraphicsDevice& device, std::vector<Byte> const& data, xna_error_nullarg);
public:
ID3D11Texture2D* dxTexture2D{ nullptr };
ID3D11ShaderResourceView* dxShaderResource{ nullptr };
D3D11_SUBRESOURCE_DATA dxSubResource{};
D3D11_TEXTURE2D_DESC dxDescription{};
D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{};
private:
static constexpr int R8G8B8A8U_BYTE_SIZE = 4;
void setDefaultDesc() {
dxDescription.MipLevels = 1;
dxDescription.ArraySize = 1;
dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dxDescription.SampleDesc.Count = 1;
dxDescription.Usage = D3D11_USAGE_DEFAULT;
dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dxShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dxShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dxShaderDescription.Texture2D.MipLevels = dxDescription.MipLevels;
dxShaderDescription.Texture2D.MostDetailedMip = 0;
}
void internalSetData(UINT const* data, xna_error_nullarg);
};
}
#endif

View File

@ -7,9 +7,7 @@
#include "gdeviceinfo-dx.hpp" #include "gdeviceinfo-dx.hpp"
#include "gdevicemanager-dx.hpp" #include "gdevicemanager-dx.hpp"
#include "init-dx.hpp" #include "init-dx.hpp"
#include "rendertarget-dx.hpp"
#include "soundeffect-dx.hpp" #include "soundeffect-dx.hpp"
#include "texture-dx.hpp"
#include "vertexbuffer-dx.hpp" #include "vertexbuffer-dx.hpp"
#include "vertexinput-dx.hpp" #include "vertexinput-dx.hpp"
#include "window-dx.hpp" #include "window-dx.hpp"