diff --git a/framework/content/lzx/decoder.cpp b/framework/content/lzx/decoder.cpp index c545867..81ad46a 100644 --- a/framework/content/lzx/decoder.cpp +++ b/framework/content/lzx/decoder.cpp @@ -11,8 +11,8 @@ namespace xna { } int LzxDecoder::Decompress(Stream* inData, int inLen, Stream* outData, int outLen) { - auto input = reinterpret_cast(inData->Data()); - auto output = reinterpret_cast(outData->Data()); + mspack_file* input = nullptr; + mspack_file* output = nullptr; auto lzxstream = lzxd_init( //struct mspack_system* system, diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 1e297d8..5f440ff 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -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); } diff --git a/framework/platform-dx/rendertarget.cpp b/framework/platform-dx/rendertarget.cpp index d89678b..ddd6b4a 100644 --- a/framework/platform-dx/rendertarget.cpp +++ b/framework/platform-dx/rendertarget.cpp @@ -1,49 +1,49 @@ #include "xna/xna-dx.hpp" namespace xna { - RenderTarget2D::RenderTarget2D() : Texture2D() { - render_impl = unew(); - } - RenderTarget2D::RenderTarget2D(sptr const& device) : Texture2D(device) { - render_impl = unew(); - } + impl2 = unew(); + } - 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); } } \ No newline at end of file diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 2d74881..38219e1 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -1,41 +1,8 @@ #include "xna/xna-dx.hpp" namespace xna { - Texture2D::~Texture2D() { - impl = nullptr; - } - - sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName) - { - auto _this = device.shared_from_this(); - auto texture2d = snew(_this); - comptr 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(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(impl->dxDescription.Width); + height = static_cast(impl->dxDescription.Height); + } Texture2D::Texture2D() : Texture(nullptr) { impl = unew(); - setDefaultDesc(*impl); + setDefaultTexture2DDesc(*impl); } Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device) { impl = unew(); - setDefaultDesc(*impl); + setDefaultTexture2DDesc(*impl); impl->dxDescription.Width = static_cast(width); impl->dxDescription.Height = static_cast(height); } Texture2D::Texture2D(sptr const& device) : Texture(device) { impl = unew(); - setDefaultDesc(*impl); + setDefaultTexture2DDesc(*impl); } Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device) { impl = unew(); - setDefaultDesc(*impl); + setDefaultTexture2DDesc(*impl); impl->dxDescription.Width = static_cast(width); impl->dxDescription.Height = static_cast(height); impl->dxDescription.MipLevels = static_cast(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 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 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 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 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::FromMemory(GraphicsDevice& device, std::vector const& data) + P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream) + { + std::vector data; + const auto lenght = stream->Length(); + stream->Read(data, 0, lenght - 1); + + return FromStream(device, data); + } + + sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName) + { + auto _this = device.shared_from_this(); + auto texture2d = snew(_this); + comptr 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::FromStream(GraphicsDevice& device, std::vector const& data) { auto _this = device.shared_from_this(); auto texture2d = snew(_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(impl->dxDescription.Width); + if (FAILED(hr)) { + Exception::Throw(Exception::FAILED_TO_CREATE); + } + } + + comptr 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(impl->dxDescription.Height); - } - - Rectangle Texture2D::Bounds() const { - if (!impl) return {}; - - return Rectangle( - 0, 0, - static_cast(impl->dxDescription.Width), - static_cast(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; } } \ No newline at end of file diff --git a/inc/xna/csharp/stream.hpp b/inc/xna/csharp/stream.hpp index 103f881..58c0304 100644 --- a/inc/xna/csharp/stream.hpp +++ b/inc/xna/csharp/stream.hpp @@ -38,9 +38,7 @@ namespace xna { virtual void Write(std::vector 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 const& buffer, Int offset, Int count) override; virtual void WriteByte(Byte value) override; - - virtual void* Data() override { - return _buffer.data(); - } public: std::vector _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 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; diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index 9d2c563..b6a4602 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -202,6 +202,9 @@ namespace xna { using P_RasterizerState = sptr; using P_PresentationParameters = sptr; using P_SamplerStateCollection = sptr; + using P_Stream = sptr; + using P_MemoryStream = sptr; + using P_FileStream = sptr; using P_Texture = sptr; using P_Texture2D = sptr; } diff --git a/inc/xna/graphics/rendertarget.hpp b/inc/xna/graphics/rendertarget.hpp index 5c4adc7..ae49df5 100644 --- a/inc/xna/graphics/rendertarget.hpp +++ b/inc/xna/graphics/rendertarget.hpp @@ -8,15 +8,25 @@ namespace xna { class RenderTarget2D : public Texture2D { public: RenderTarget2D(); - RenderTarget2D(sptr const& device); + RenderTarget2D(sptr 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 render_impl = nullptr; + uptr impl2 = nullptr; }; } diff --git a/inc/xna/graphics/texture.hpp b/inc/xna/graphics/texture.hpp index 6d6a00c..a26059e 100644 --- a/inc/xna/graphics/texture.hpp +++ b/inc/xna/graphics/texture.hpp @@ -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 const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(Int level, Rectangle* rect, std::vector 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 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 const& data); + + void Initialize(); + + protected: + SurfaceFormat surfaceFormat{ SurfaceFormat::Color }; + Int levelCount{ 0 }; + int width{ 0 }; + int height{ 0 }; public: struct PlatformImplementation;