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

Merge pull request #16 from borgesdan/develop

Implementações em Texture
This commit is contained in:
Danilo Borges 2024-08-03 22:45:52 -03:00 committed by GitHub
commit c5ebd88adf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 175 additions and 166 deletions

View File

@ -11,8 +11,8 @@ namespace xna {
}
int LzxDecoder::Decompress(Stream* inData, int inLen, Stream* outData, int outLen) {
auto input = reinterpret_cast<mspack_file*>(inData->Data());
auto output = reinterpret_cast<mspack_file*>(outData->Data());
mspack_file* input = nullptr;
mspack_file* output = nullptr;
auto lzxstream = lzxd_init(
//struct mspack_system* system,

View File

@ -87,7 +87,7 @@ namespace xna {
impl->_context->OMSetRenderTargets(
1,
impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(),
impl->_renderTarget2D->impl2->_renderTargetView.GetAddressOf(),
nullptr);
return result;
@ -104,7 +104,7 @@ namespace xna {
impl->_backgroundColor[3] = v4.W;
impl->_context->ClearRenderTargetView(
impl->_renderTarget2D->render_impl->_renderTargetView.Get(),
impl->_renderTarget2D->impl2->_renderTargetView.Get(),
impl->_backgroundColor);
}

View File

@ -1,49 +1,49 @@
#include "xna/xna-dx.hpp"
namespace xna {
RenderTarget2D::RenderTarget2D() : Texture2D() {
render_impl = unew<PlatformImplementation>();
}
RenderTarget2D::RenderTarget2D(sptr<GraphicsDevice> const& device) : Texture2D(device) {
render_impl = unew<PlatformImplementation>();
}
impl2 = unew<PlatformImplementation>();
}
RenderTarget2D::~RenderTarget2D() {
render_impl = nullptr;
}
bool RenderTarget2D::Initialize() {
void RenderTarget2D::Initialize() {
if (!impl || !m_device || !m_device->impl->_device) {
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
}
if (!m_device->impl->_swapChain->impl->GetBackBuffer(impl->dxTexture2D))
return false;
auto& swapChain = m_device->impl->_swapChain;
if (!swapChain->impl->GetBackBuffer(impl->dxTexture2D))
{
Exception::Throw(Exception::FAILED_TO_CREATE);
}
impl->dxTexture2D->GetDesc(&impl->dxDescription);
auto& dxdevice = m_device->impl->_device;
const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, render_impl->_renderTargetView.ReleaseAndGetAddressOf());
const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, impl2->_renderTargetView.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE);
}
return true;
impl2->_renderTargetView->GetDesc(&impl2->_renderTargetDesc);
//depthStencilFormat = DepthFormat::None;
multiSampleCount = impl->dxDescription.SampleDesc.Count;
//targetUsage = RenderTargetUsage::DiscardContent;
}
bool RenderTarget2D::Apply() {
void RenderTarget2D::Apply() {
if (!m_device || !m_device->impl->_context) {
Exception::Throw(Exception::FAILED_TO_APPLY);
}
if (!render_impl->_renderTargetView)
if (!impl2->_renderTargetView)
{
Initialize();
}
auto& context = m_device->impl->_context;
context->OMSetRenderTargets(1, render_impl->_renderTargetView.GetAddressOf(), nullptr);
return true;
context->OMSetRenderTargets(1, impl2->_renderTargetView.GetAddressOf(), nullptr);
}
}

View File

@ -1,41 +1,8 @@
#include "xna/xna-dx.hpp"
namespace xna {
Texture2D::~Texture2D() {
impl = nullptr;
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
comptr<ID3D11Resource> resource = nullptr;
auto wstr = XnaHelper::ToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile(
device.impl->_device.Get(),
device.impl->_context.Get(),
wstr.c_str(),
resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(),
0U);
if (FAILED(result)) {
return nullptr;
}
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(result)) {
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
return texture2d;
}
static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl);
static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data);
void Texture2D::Initialize()
{
@ -64,80 +31,36 @@ namespace xna {
surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format);
levelCount = static_cast<Int>(impl->dxShaderDescription.Texture2D.MipLevels);
}
void setDefaultDesc(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;
}
width = static_cast<Int>(impl->dxDescription.Width);
height = static_cast<Int>(impl->dxDescription.Height);
}
Texture2D::Texture2D() : Texture(nullptr) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
setDefaultTexture2DDesc(*impl);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
setDefaultTexture2DDesc(*impl);
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
setDefaultTexture2DDesc(*impl);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device)
{
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
setDefaultTexture2DDesc(*impl);
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
impl->dxDescription.MipLevels = static_cast<UINT>(mipMap);
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format);
}
HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
{
if (!impl.dxTexture2D) {
auto hr = device.impl->_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.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
hr = device.impl->_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 Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount)
{
@ -145,7 +68,7 @@ namespace xna {
Exception::Throw(Exception::INVALID_OPERATION);
}
internalSetData(*impl, *m_device, data.data());
internalTexture2DSetData(*impl, *m_device, data.data());
}
void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
@ -166,8 +89,8 @@ namespace xna {
++fIndex;
}
internalSetData(*impl, *m_device, finalData.data());
}
internalTexture2DSetData(*impl, *m_device, finalData.data());
}
void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
{
@ -241,10 +164,51 @@ namespace xna {
++finalDataIndex;
}
internalSetData(*impl, *m_device, finalData.data());
internalTexture2DSetData(*impl, *m_device, finalData.data());
}
sptr<Texture2D> Texture2D::FromMemory(GraphicsDevice& device, std::vector<Byte> const& data)
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream)
{
std::vector<Byte> data;
const auto lenght = stream->Length();
stream->Read(data, 0, lenght - 1);
return FromStream(device, data);
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
comptr<ID3D11Resource> resource = nullptr;
auto wstr = XnaHelper::ToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile(
device.impl->_device.Get(),
device.impl->_context.Get(),
wstr.c_str(),
resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(),
0U);
if (FAILED(result)) {
return nullptr;
}
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(result)) {
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
return texture2d;
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, std::vector<Byte> const& data)
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
@ -274,27 +238,51 @@ namespace xna {
texture2d->impl->dxDescription = desc;
return texture2d;
}
}
Int Texture2D::Width() const {
if (!impl) return 0;
HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
{
if (!impl.dxTexture2D) {
auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf());
return static_cast<Int>(impl->dxDescription.Width);
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.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
hr = device.impl->_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;
}
Int Texture2D::Height() const {
if (!impl) return 0;
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;
return static_cast<Int>(impl->dxDescription.Height);
}
Rectangle Texture2D::Bounds() const {
if (!impl) return {};
return Rectangle(
0, 0,
static_cast<Int>(impl->dxDescription.Width),
static_cast<Int>(impl->dxDescription.Height)
);
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;
}
}

View File

@ -38,9 +38,7 @@ namespace xna {
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) = 0;
//Writes a byte to the current position in the stream and advances the position within the stream by one byte.
virtual void WriteByte(Byte value) = 0;
virtual void* Data() = 0;
virtual void WriteByte(Byte value) = 0;
};
//A simplified port of the System.IO.MemoryStream.
@ -84,10 +82,6 @@ namespace xna {
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
virtual void WriteByte(Byte value) override;
virtual void* Data() override {
return _buffer.data();
}
public:
std::vector<Byte> _buffer;
@ -125,11 +119,7 @@ namespace xna {
virtual Int ReadByte() override;
virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override;
virtual void Write(std::vector<Byte> const& buffer, Int offset, Int count) override;
virtual void WriteByte(Byte value) override;
virtual void* Data() override {
return _fstream.rdbuf();
}
virtual void WriteByte(Byte value) override;
public:
std::fstream _fstream;

View File

@ -202,6 +202,9 @@ namespace xna {
using P_RasterizerState = sptr<RasterizerState>;
using P_PresentationParameters = sptr<PresentationParameters>;
using P_SamplerStateCollection = sptr<SamplerStateCollection>;
using P_Stream = sptr<Stream>;
using P_MemoryStream = sptr<MemoryStream>;
using P_FileStream = sptr<FileStream>;
using P_Texture = sptr<Texture>;
using P_Texture2D = sptr<Texture2D>;
}

View File

@ -8,15 +8,25 @@ namespace xna {
class RenderTarget2D : public Texture2D {
public:
RenderTarget2D();
RenderTarget2D(sptr<GraphicsDevice> const& device);
RenderTarget2D(sptr<GraphicsDevice> const& device);
//Gets the data format for the depth stencil data.
constexpr DepthFormat DepthStencilFormat() const { return depthStencilFormat; }
//Gets the number of sample locations during multisampling.
constexpr Int MultiSampleCount() const { return multiSampleCount; }
//Gets or sets the render target usage.
constexpr RenderTargetUsage TargetUsage() const { return targetUsage; }
~RenderTarget2D() override;
bool Initialize();
bool Apply();
void Initialize();
void Apply();
private:
DepthFormat depthStencilFormat{ DepthFormat::None };
Int multiSampleCount{ 0 };
RenderTargetUsage targetUsage{ RenderTargetUsage::DiscardContents };
public:
struct PlatformImplementation;
uptr<PlatformImplementation> render_impl = nullptr;
uptr<PlatformImplementation> impl2 = nullptr;
};
}

View File

@ -8,38 +8,56 @@ namespace xna {
//Represents a texture resource.
class Texture : public GraphicsResource {
public:
Texture(P_GraphicsDevice const& graphicsDevice) : GraphicsResource(graphicsDevice) {}
virtual ~Texture() {}
Texture(P_GraphicsDevice const& graphicsDevice) : GraphicsResource(graphicsDevice) {}
//Gets the format of the texture data.
constexpr SurfaceFormat Format() const { return surfaceFormat; }
virtual SurfaceFormat Format() const = 0;
//Gets the number of texture levels in a multilevel texture.
constexpr Int LevelCount() const { return levelCount; }
protected:
SurfaceFormat surfaceFormat{SurfaceFormat::Color};
Int levelCount{ 0 };
virtual Int LevelCount() const = 0;
};
//Represents a 2D grid of texels.
class Texture2D : public Texture {
public:
Texture2D();
Texture2D(P_GraphicsDevice const& device);
Texture2D(P_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() override;
Int Width() const;
Int Height() const;
Rectangle Bounds() const;
Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format);
//Gets the width of this texture resource, in pixels.
constexpr Int Width() const { return width; }
//Gets the height of this texture resource, in pixels.
constexpr Int Height() const { return height; }
//Gets the size of this resource.
constexpr Rectangle Bounds() const { return { 0, 0, width, height }; }
//Gets the format of the texture data.
constexpr SurfaceFormat Format() const override { return surfaceFormat; }
//Gets the number of texture levels in a multilevel texture.
constexpr Int LevelCount() const { return levelCount; }
//Sets data to the texture.
void SetData(std::vector<Color> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture.
void SetData(std::vector<Uint> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture.
void SetData(std::vector<Byte> const& data, size_t startIndex = 0, size_t elementCount = 0);
//Sets data to the texture.
void SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount);
static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName);
static P_Texture2D FromMemory(GraphicsDevice& device, std::vector<Byte> const& data);
void Initialize();
//Loads texture data from a stream.
static P_Texture2D FromStream(GraphicsDevice& device, P_Stream const& stream);
//Loads texture data from a file.
static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName);
//Loads texture data from a data.
static P_Texture2D FromStream(GraphicsDevice& device, std::vector<Byte> const& data);
void Initialize();
protected:
SurfaceFormat surfaceFormat{ SurfaceFormat::Color };
Int levelCount{ 0 };
int width{ 0 };
int height{ 0 };
public:
struct PlatformImplementation;