diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 5f440ff..018dca0 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -22,47 +22,61 @@ namespace xna { } void GraphicsDevice::Initialize() { - auto _this = shared_from_this(); - - reset(*impl); - createDevice(*impl, *adapter); - - auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory); + reset(*impl); - if FAILED(hr) - Exception::Throw(Exception::FAILED_TO_CREATE); - - viewport = xna::Viewport(0.0F, 0.0F, - presentationParameters->BackBufferWidth, - presentationParameters->BackBufferHeight, - 0.0F, 1.F); + auto _this = shared_from_this(); + + createDevice(*impl, *adapter); + // + // Background + // + const auto backColor = Colors::CornflowerBlue; const auto backColorV3 = backColor.ToVector3(); impl->_backgroundColor[0] = backColorV3.X; impl->_backgroundColor[1] = backColorV3.Y; impl->_backgroundColor[2] = backColorV3.Z; - impl->_backgroundColor[3] = 1.0f; + impl->_backgroundColor[3] = 1.0f; - impl->_swapChain = snew(_this); - impl->_swapChain->Initialize(); + // + // Window Association + // + + auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory); + + if FAILED(hr) + Exception::Throw(Exception::FAILED_TO_CREATE); auto hwnd = reinterpret_cast(presentationParameters->DeviceWindowHandle); hr = impl->_factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION); - - impl->_renderTarget2D = snew(_this); - impl->_renderTarget2D->Initialize(); - impl->_renderTarget2D->Apply(); + + // + // Viewport + // + + viewport = xna::Viewport(0.0F, 0.0F, + presentationParameters->BackBufferWidth, + presentationParameters->BackBufferHeight, + 0.0F, 1.F); D3D11_VIEWPORT view = DxHelpers::ViewportToDx(viewport); impl->_context->RSSetViewports(1, &view); + // + // States + // + initAndApplyState(blendState, rasterizerState, depthStencilState, samplerStateCollection, _this); + // + // Presentation + // + const auto currentPresenInterval = presentationParameters->PresentationInterval; switch (currentPresenInterval) @@ -79,10 +93,27 @@ namespace xna { impl->vSyncValue = 1; break; } + + impl->_swapChain = snew(_this); + impl->_swapChain->Initialize(); + + // + //Render Target + // + + if (renderTarget) { + renderTarget->Initialize(); + impl->_renderTarget2D = renderTarget; + } + else { + impl->_renderTarget2D = RenderTarget2D::FromBackBuffer(_this); + } + + const auto& renderView = impl->_renderTarget2D->impl2->_renderTargetView; + impl->_context->OMSetRenderTargets(1, renderView.GetAddressOf(), nullptr); } bool GraphicsDevice::Present() const { - const auto currentPresenInterval = presentationParameters->PresentationInterval; bool result = impl->_swapChain->Present(impl->vSyncValue != 0); impl->_context->OMSetRenderTargets( diff --git a/framework/platform-dx/rendertarget.cpp b/framework/platform-dx/rendertarget.cpp index ddd6b4a..08a83ca 100644 --- a/framework/platform-dx/rendertarget.cpp +++ b/framework/platform-dx/rendertarget.cpp @@ -1,49 +1,55 @@ #include "xna/xna-dx.hpp" namespace xna { + RenderTarget2D::RenderTarget2D() : Texture2D(nullptr) { + impl2 = unew(); + } + RenderTarget2D::RenderTarget2D(sptr const& device) : Texture2D(device) { impl2 = unew(); } - void RenderTarget2D::Initialize() { - if (!impl || !m_device || !m_device->impl->_device) { - Exception::Throw(Exception::UNABLE_TO_INITIALIZE); - } + P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) { + auto& swapChain = device->impl->_swapChain; + auto rt = snew(device); + auto& implementation = rt->impl; + auto& implementation2 = rt->impl2; - auto& swapChain = m_device->impl->_swapChain; - - if (!swapChain->impl->GetBackBuffer(impl->dxTexture2D)) + if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D)) { Exception::Throw(Exception::FAILED_TO_CREATE); } - impl->dxTexture2D->GetDesc(&impl->dxDescription); + rt->Initialize(); + + return rt; + } + + void RenderTarget2D::Initialize() { + if (!impl || !m_device || !m_device->impl->_device) { + Exception::Throw(Exception::UNABLE_TO_INITIALIZE); + } + + if (impl2->_renderTargetView) + return; + + impl->dxDescription.Width = width; + impl->dxDescription.Height = height; + impl->dxDescription.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET; + + Texture2D::Initialize(); + auto& dxdevice = m_device->impl->_device; - const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, impl2->_renderTargetView.ReleaseAndGetAddressOf()); + const auto hr = dxdevice->CreateRenderTargetView( + impl->dxTexture2D.Get(), + NULL, + impl2->_renderTargetView.ReleaseAndGetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); } impl2->_renderTargetView->GetDesc(&impl2->_renderTargetDesc); - - //depthStencilFormat = DepthFormat::None; - multiSampleCount = impl->dxDescription.SampleDesc.Count; - //targetUsage = RenderTargetUsage::DiscardContent; - } - - void RenderTarget2D::Apply() { - if (!m_device || !m_device->impl->_context) { - Exception::Throw(Exception::FAILED_TO_APPLY); - } - - if (!impl2->_renderTargetView) - { - Initialize(); - } - - auto& context = m_device->impl->_context; - 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 38219e1..a4955d1 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -4,47 +4,16 @@ namespace xna { static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl); static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data); - void Texture2D::Initialize() - { - if (!m_device || !m_device->impl->_device) { - Exception::Throw(Exception::UNABLE_TO_INITIALIZE); - } - - auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - - comptr resource = nullptr; - hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::INVALID_OPERATION); - } - - hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, &impl->dxShaderResource); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - - surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format); - levelCount = static_cast(impl->dxShaderDescription.Texture2D.MipLevels); - width = static_cast(impl->dxDescription.Width); - height = static_cast(impl->dxDescription.Height); - } - Texture2D::Texture2D() : Texture(nullptr) { impl = unew(); setDefaultTexture2DDesc(*impl); } - Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device) { + Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device), width(width), height(height) { impl = unew(); setDefaultTexture2DDesc(*impl); - impl->dxDescription.Width = static_cast(width); - impl->dxDescription.Height = static_cast(height); + impl->dxDescription.Width = static_cast(this->width); + impl->dxDescription.Height = static_cast(this->height); } Texture2D::Texture2D(sptr const& device) : Texture(device) { @@ -52,15 +21,63 @@ namespace xna { setDefaultTexture2DDesc(*impl); } - Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device) - { + Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) + : Texture(device), width(width), height(height), levelCount(mipMap) { impl = unew(); setDefaultTexture2DDesc(*impl); - impl->dxDescription.Width = static_cast(width); - impl->dxDescription.Height = static_cast(height); - impl->dxDescription.MipLevels = static_cast(mipMap); + impl->dxDescription.Width = static_cast(this->width); + impl->dxDescription.Height = static_cast(this->height); + impl->dxDescription.MipLevels = static_cast(this->levelCount); impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format); - } + } + + void Texture2D::Initialize() + { + if (!m_device || !m_device->impl->_device) { + Exception::Throw(Exception::UNABLE_TO_INITIALIZE); + } + + auto& deviceImpl = m_device->impl; + + HRESULT hr = 0; + + if (!impl->dxTexture2D) { + hr = deviceImpl->_device->CreateTexture2D( + &impl->dxDescription, + nullptr, + impl->dxTexture2D.ReleaseAndGetAddressOf()); + + if FAILED(hr) + Exception::Throw(Exception::FAILED_TO_CREATE); + } + else { + //Updates description if texture is not null + impl->dxTexture2D->GetDesc(&impl->dxDescription); + } + + comptr resource = nullptr; + hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); + + if FAILED(hr) + Exception::Throw(Exception::INVALID_OPERATION); + + //Only initializes if it is a ShaderResource + if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) { + hr = deviceImpl->_device->CreateShaderResourceView( + resource.Get(), + &impl->dxShaderDescription, + impl->dxShaderResource.ReleaseAndGetAddressOf()); + + if FAILED(hr) + Exception::Throw(Exception::FAILED_TO_CREATE); + + } + + surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format); + levelCount = static_cast(impl->dxShaderDescription.Texture2D.MipLevels); + width = static_cast(impl->dxDescription.Width); + height = static_cast(impl->dxDescription.Height); + } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) { diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index b6a4602..a1ed6e5 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -207,6 +207,7 @@ namespace xna { using P_FileStream = sptr; using P_Texture = sptr; using P_Texture2D = sptr; + using P_RenderTarget2D = sptr; } diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 08f259e..0cb6f4c 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -45,7 +45,9 @@ namespace xna { constexpr xna::Viewport Viewport() const { return viewport; } //Gets or sets a viewport identifying the portion of the render target to receive draw calls. void Viewport(xna::Viewport const& viewport); - + //Sets a new render target for this GraphicsDevice. + void SetRenderTarget(P_RenderTarget2D const& renderTarget) { this->renderTarget = renderTarget; } + void Initialize(); private: @@ -55,6 +57,7 @@ namespace xna { P_RasterizerState rasterizerState{ nullptr }; P_SamplerStateCollection samplerStateCollection{ nullptr }; P_PresentationParameters presentationParameters{ nullptr }; + P_RenderTarget2D renderTarget{ nullptr }; GraphicsProfile graphicsProfile{ GraphicsProfile::HiDef }; xna::Viewport viewport{}; diff --git a/inc/xna/graphics/presentparams.hpp b/inc/xna/graphics/presentparams.hpp index 2306f12..982d9d3 100644 --- a/inc/xna/graphics/presentparams.hpp +++ b/inc/xna/graphics/presentparams.hpp @@ -4,17 +4,24 @@ #include "../default.hpp" namespace xna { + //Contains presentation parameters. struct PresentationParameters { - constexpr PresentationParameters() = default; - + //Gets or sets a value indicating the width of the new swap chain's back buffer. Int BackBufferWidth{ 0 }; + //Gets or sets a value indicating the height of the new swap chain's back buffer. Int BackBufferHeight{ 0 }; + //Gets or sets the format of the back buffer. SurfaceFormat BackBufferFormat{ SurfaceFormat::Color }; SwapEffect PresentationSwapEffect{ SwapEffect::FlipDiscard }; + //Gets or sets the handle to the device window. intptr_t DeviceWindowHandle{ 0 }; + //Gets or sets a value indicating whether the application is in full screen mode. bool IsFullscreen{ false }; + //Gets or sets a value indicating the number of sample locations during multisampling. Int MultiSampleCount{ 0 }; + //Gets or sets the maximum rate at which the swap chain's back buffers can be presented to the front buffer. PresentInterval PresentationInterval{ PresentInterval::Default }; + //Gets or sets the depth stencil data format. DepthFormat DepthStencilFormat{ DepthFormat::None }; }; } diff --git a/inc/xna/graphics/rendertarget.hpp b/inc/xna/graphics/rendertarget.hpp index ae49df5..168fd6c 100644 --- a/inc/xna/graphics/rendertarget.hpp +++ b/inc/xna/graphics/rendertarget.hpp @@ -5,25 +5,15 @@ #include "texture.hpp" namespace xna { + //Contains a 2D texture that can be used as a render target. class RenderTarget2D : public Texture2D { public: RenderTarget2D(); - 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(sptr const& device); + + static P_RenderTarget2D FromBackBuffer(P_GraphicsDevice const& device); void Initialize(); - void Apply(); - private: - DepthFormat depthStencilFormat{ DepthFormat::None }; - Int multiSampleCount{ 0 }; - RenderTargetUsage targetUsage{ RenderTargetUsage::DiscardContents }; - public: struct PlatformImplementation; uptr impl2 = nullptr; diff --git a/inc/xna/graphics/vertexposition.hpp b/inc/xna/graphics/vertexposition.hpp index f8ef31f..e3048f9 100644 --- a/inc/xna/graphics/vertexposition.hpp +++ b/inc/xna/graphics/vertexposition.hpp @@ -6,70 +6,74 @@ #include "../default.hpp" namespace xna { + //Describes a custom vertex format structure that contains position and color information. struct VertexPositionColor { - Vector3 position{}; - Color color{}; + Vector3 Position{}; + xna::Color Color{}; constexpr VertexPositionColor() = default; - constexpr VertexPositionColor(Vector3 const& position, Color const& color): - position(position), color(color){} + constexpr VertexPositionColor(Vector3 const& position, xna::Color const& color): + Position(position), Color(color){} constexpr bool operator==(const VertexPositionColor& other) const { - return position == other.position && color == other.color; + return Position == other.Position && Color == other.Color; } }; + //Describes a custom vertex format structure that contains position and one set of texture coordinates. struct VertexPositionTexture { - Vector3 position{}; - Vector2 textureCoordinate{}; + Vector3 Position{}; + Vector2 TextureCoordinate{}; constexpr VertexPositionTexture() = default; constexpr bool operator==(const VertexPositionTexture& other) const { - return position == other.position - && textureCoordinate == other.textureCoordinate; + return Position == other.Position + && TextureCoordinate == other.TextureCoordinate; } constexpr VertexPositionTexture(const Vector3& position, const Vector2& textureCoordinate) - : position(position), textureCoordinate(textureCoordinate) + : Position(position), TextureCoordinate(textureCoordinate) { } }; + //Describes a custom vertex format structure that contains position, color, and one set of texture coordinates. struct VertexPositionColorTexture { - Vector3 position{}; - Vector2 textureCoodinate{}; - Color color{}; + Vector3 Position{}; + Vector2 TextureCoodinate{}; + xna::Color Color{}; constexpr VertexPositionColorTexture() = default; constexpr bool operator==(const VertexPositionColorTexture& other) const { - return position == other.position - && textureCoodinate == other.textureCoodinate - && color == other.color; + return Position == other.Position + && TextureCoodinate == other.TextureCoodinate + && Color == other.Color; } - constexpr VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const Color& color) - : position(position), textureCoodinate(textureCoodinate), color(color) + constexpr VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const xna::Color& color) + : Position(position), TextureCoodinate(textureCoodinate), Color(color) { } }; + //Describes a custom vertex format structure that contains position, normal data, and one set of texture coordinates. struct VertexPositionNormalTexture { - Vector3 position{}; - Vector3 normal{}; - Vector2 textureCoodinate{}; + Vector3 Position{}; + Vector3 Normal{}; + Vector2 TextureCoodinate{}; constexpr VertexPositionNormalTexture() = default; bool operator==(const VertexPositionNormalTexture& other) const { - return position == other.position - && normal == other.normal - && textureCoodinate == other.textureCoodinate; + return Position == other.Position + && Normal == other.Normal + && TextureCoodinate == other.TextureCoodinate; } constexpr VertexPositionNormalTexture(const Vector3& position, const Vector3& normal, const Vector2& textureCoodinate) - : position(position), normal(normal), textureCoodinate(textureCoodinate) + : Position(position), Normal(normal), TextureCoodinate(textureCoodinate) { } }; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 24ef843..45257e9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -#add_subdirectory ("02_PlatfformerStarterKit") +add_subdirectory ("02_PlatfformerStarterKit")