From f9f94bf6552c9b83edad25ca0b0a056ab9c2c86f Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Sun, 18 Nov 2012 23:29:52 -0500 Subject: [PATCH] WinRT: started work on renderer by getting Direct3D 11.1 to display a single, fullscreen rectangle --- VisualC/SDL/SDL_VS2012_WinRT.vcxproj | 19 ++- src/video/windowsrt/SDL_WinRTApp.cpp | 18 ++- src/video/windowsrt/SDL_WinRTApp.h | 5 +- src/video/windowsrt/SDL_winrtframebuffer.cpp | 6 + ...CubeRenderer.cpp => SDL_winrtrenderer.cpp} | 110 +++--------------- .../{CubeRenderer.h => SDL_winrtrenderer.h} | 9 +- 6 files changed, 58 insertions(+), 109 deletions(-) rename src/video/windowsrt/{CubeRenderer.cpp => SDL_winrtrenderer.cpp} (54%) rename src/video/windowsrt/{CubeRenderer.h => SDL_winrtrenderer.h} (78%) diff --git a/VisualC/SDL/SDL_VS2012_WinRT.vcxproj b/VisualC/SDL/SDL_VS2012_WinRT.vcxproj index fd95bea51..479d3c71b 100644 --- a/VisualC/SDL/SDL_VS2012_WinRT.vcxproj +++ b/VisualC/SDL/SDL_VS2012_WinRT.vcxproj @@ -101,7 +101,7 @@ - + true true true @@ -109,7 +109,7 @@ true true - + true true true @@ -117,7 +117,7 @@ true true - + true true true @@ -125,15 +125,22 @@ true true - + + true + true true true + true + true + + true true + true + true true true - true true @@ -245,13 +252,13 @@ - + diff --git a/src/video/windowsrt/SDL_WinRTApp.cpp b/src/video/windowsrt/SDL_WinRTApp.cpp index 38304436c..dcf0b2472 100644 --- a/src/video/windowsrt/SDL_WinRTApp.cpp +++ b/src/video/windowsrt/SDL_WinRTApp.cpp @@ -11,6 +11,9 @@ extern "C" { #include "SDL_log.h" } +// TODO, WinRT: Remove reference(s) to BasicTimer.h +#include "BasicTimer.h" + // HACK, DLudwig: The C-style main() will get loaded via the app's // WinRT-styled main(), which is part of SDLmain_for_WinRT.cpp. // This seems wrong on some level, but does seem to work. @@ -54,7 +57,7 @@ void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView) CoreApplication::Resuming += ref new EventHandler(this, &SDL_WinRTApp::OnResuming); - m_renderer = ref new CubeRenderer(); + m_renderer = ref new SDL_winrtrenderer(); } void SDL_WinRTApp::SetWindow(CoreWindow^ window) @@ -112,9 +115,6 @@ void SDL_WinRTApp::PumpEvents() if (m_windowVisible) { CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); - m_renderer->Update(0.0f, 0.0f); - m_renderer->Render(); - m_renderer->Present(); // This call is synchronized to the display frame rate. } else { @@ -123,6 +123,16 @@ void SDL_WinRTApp::PumpEvents() } } +void SDL_WinRTApp::UpdateWindowFramebuffer(SDL_Surface * surface, SDL_Rect * rects, int numrects) +{ + if (!m_windowClosed && m_windowVisible) + { + m_renderer->Update(0.0f, 0.0f); + m_renderer->Render(); + m_renderer->Present(); // This call is synchronized to the display frame rate. + } +} + void SDL_WinRTApp::Uninitialize() { } diff --git a/src/video/windowsrt/SDL_WinRTApp.h b/src/video/windowsrt/SDL_WinRTApp.h index 09cf520c5..333f734ba 100644 --- a/src/video/windowsrt/SDL_WinRTApp.h +++ b/src/video/windowsrt/SDL_WinRTApp.h @@ -2,7 +2,7 @@ #include "SDLmain_WinRT_common.h" #include "SDL_winrtvideo.h" -#include "CubeRenderer.h" +#include "SDL_winrtrenderer.h" #include using namespace Windows::UI::Core; @@ -26,6 +26,7 @@ ref class SDL_WinRTApp sealed : public Windows::ApplicationModel::Core::IFramewo const SDL_WindowData * GetSDLWindowData() const; bool HasSDLWindowData() const; void SetSDLWindowData(const SDL_WindowData * windowData); + void UpdateWindowFramebuffer(SDL_Surface * surface, SDL_Rect * rects, int numrects); protected: // Event Handlers. @@ -43,7 +44,7 @@ ref class SDL_WinRTApp sealed : public Windows::ApplicationModel::Core::IFramewo void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); private: - CubeRenderer^ m_renderer; + SDL_winrtrenderer^ m_renderer; bool m_windowClosed; bool m_windowVisible; const SDL_WindowData* m_sdlWindowData; diff --git a/src/video/windowsrt/SDL_winrtframebuffer.cpp b/src/video/windowsrt/SDL_winrtframebuffer.cpp index 2426eac0f..555a195d9 100644 --- a/src/video/windowsrt/SDL_winrtframebuffer.cpp +++ b/src/video/windowsrt/SDL_winrtframebuffer.cpp @@ -24,6 +24,9 @@ #include "../SDL_sysvideo.h" #include "SDL_winrtframebuffer_c.h" +#include "SDL_WinRTApp.h" + +extern SDL_WinRTApp ^ SDL_WinRTGlobalApp; #define WINRT_SURFACE "_SDL_WinRTSurface" @@ -76,6 +79,9 @@ int SDL_WINRT_UpdateWindowFramebuffer(_THIS, SDL_Window * window, SDL_Rect * rec SDL_GetWindowID(window), ++frame_number); SDL_SaveBMP(surface, file); } + + SDL_WinRTGlobalApp->UpdateWindowFramebuffer(surface, rects, numrects); + return 0; } diff --git a/src/video/windowsrt/CubeRenderer.cpp b/src/video/windowsrt/SDL_winrtrenderer.cpp similarity index 54% rename from src/video/windowsrt/CubeRenderer.cpp rename to src/video/windowsrt/SDL_winrtrenderer.cpp index 52e858607..fae779ce4 100644 --- a/src/video/windowsrt/CubeRenderer.cpp +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp @@ -1,18 +1,18 @@ #include "SDLmain_WinRT_common.h" -#include "CubeRenderer.h" +#include "SDL_winrtrenderer.h" using namespace DirectX; using namespace Microsoft::WRL; using namespace Windows::Foundation; using namespace Windows::UI::Core; -CubeRenderer::CubeRenderer() : +SDL_winrtrenderer::SDL_winrtrenderer() : m_loadingComplete(false), - m_indexCount(0) + m_vertexCount(0) { } -void CubeRenderer::CreateDeviceResources() +void SDL_winrtrenderer::CreateDeviceResources() { Direct3DBase::CreateDeviceResources(); @@ -69,16 +69,14 @@ void CubeRenderer::CreateDeviceResources() auto createCubeTask = (createPSTask && createVSTask).then([this] () { VertexPositionColor cubeVertices[] = { - {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)}, - {XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, - {XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, - {XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)}, - {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, - {XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)}, - {XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)}, - {XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)}, + {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)}, + {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)}, + {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)}, + {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)}, }; + m_vertexCount = ARRAYSIZE(cubeVertices); + D3D11_SUBRESOURCE_DATA vertexBufferData = {0}; vertexBufferData.pSysMem = cubeVertices; vertexBufferData.SysMemPitch = 0; @@ -91,42 +89,6 @@ void CubeRenderer::CreateDeviceResources() &m_vertexBuffer ) ); - - unsigned short cubeIndices[] = - { - 0,2,1, // -x - 1,2,3, - - 4,5,6, // +x - 5,7,6, - - 0,1,5, // -y - 0,5,4, - - 2,6,7, // +y - 2,7,3, - - 0,4,6, // -z - 0,6,2, - - 1,3,7, // +z - 1,7,5, - }; - - m_indexCount = ARRAYSIZE(cubeIndices); - - D3D11_SUBRESOURCE_DATA indexBufferData = {0}; - indexBufferData.pSysMem = cubeIndices; - indexBufferData.SysMemPitch = 0; - indexBufferData.SysMemSlicePitch = 0; - CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER); - DX::ThrowIfFailed( - m_d3dDevice->CreateBuffer( - &indexBufferDesc, - &indexBufferData, - &m_indexBuffer - ) - ); }); createCubeTask.then([this] () { @@ -134,35 +96,7 @@ void CubeRenderer::CreateDeviceResources() }); } -void CubeRenderer::CreateWindowSizeDependentResources() -{ - Direct3DBase::CreateWindowSizeDependentResources(); - - float aspectRatio = m_windowBounds.Width / m_windowBounds.Height; - float fovAngleY = 70.0f * XM_PI / 180.0f; - - // Note that the m_orientationTransform3D matrix is post-multiplied here - // in order to correctly orient the scene to match the display orientation. - // This post-multiplication step is required for any draw calls that are - // made to the swap chain render target. For draw calls to other targets, - // this transform should not be applied. - XMStoreFloat4x4( - &m_constantBufferData.projection, - XMMatrixTranspose( - XMMatrixMultiply( - XMMatrixPerspectiveFovRH( - fovAngleY, - aspectRatio, - 0.01f, - 100.0f - ), - XMLoadFloat4x4(&m_orientationTransform3D) - ) - ) - ); -} - -void CubeRenderer::Update(float timeTotal, float timeDelta) +void SDL_winrtrenderer::Update(float timeTotal, float timeDelta) { (void) timeDelta; // Unused parameter. @@ -170,11 +104,11 @@ void CubeRenderer::Update(float timeTotal, float timeDelta) XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); - XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up))); - XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4))); + XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixIdentity()); + XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixIdentity()); } -void CubeRenderer::Render() +void SDL_winrtrenderer::Render() { const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f }; m_d3dContext->ClearRenderTargetView( @@ -195,6 +129,8 @@ void CubeRenderer::Render() return; } + m_d3dContext->RSSetState(m_rasterState.Get()); + m_d3dContext->OMSetRenderTargets( 1, m_renderTargetView.GetAddressOf(), @@ -220,13 +156,7 @@ void CubeRenderer::Render() &offset ); - m_d3dContext->IASetIndexBuffer( - m_indexBuffer.Get(), - DXGI_FORMAT_R16_UINT, - 0 - ); - - m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); m_d3dContext->IASetInputLayout(m_inputLayout.Get()); @@ -248,9 +178,5 @@ void CubeRenderer::Render() 0 ); - m_d3dContext->DrawIndexed( - m_indexCount, - 0, - 0 - ); + m_d3dContext->Draw(4, 0); } diff --git a/src/video/windowsrt/CubeRenderer.h b/src/video/windowsrt/SDL_winrtrenderer.h similarity index 78% rename from src/video/windowsrt/CubeRenderer.h rename to src/video/windowsrt/SDL_winrtrenderer.h index b3ce23001..3a51d0a94 100644 --- a/src/video/windowsrt/CubeRenderer.h +++ b/src/video/windowsrt/SDL_winrtrenderer.h @@ -16,14 +16,13 @@ struct VertexPositionColor }; // This class renders a simple spinning cube. -ref class CubeRenderer sealed : public Direct3DBase +ref class SDL_winrtrenderer sealed : public Direct3DBase { public: - CubeRenderer(); + SDL_winrtrenderer(); // Direct3DBase methods. virtual void CreateDeviceResources() override; - virtual void CreateWindowSizeDependentResources() override; virtual void Render() override; // Method for updating time-dependent objects. @@ -34,11 +33,11 @@ ref class CubeRenderer sealed : public Direct3DBase Microsoft::WRL::ComPtr m_inputLayout; Microsoft::WRL::ComPtr m_vertexBuffer; - Microsoft::WRL::ComPtr m_indexBuffer; Microsoft::WRL::ComPtr m_vertexShader; Microsoft::WRL::ComPtr m_pixelShader; Microsoft::WRL::ComPtr m_constantBuffer; + Microsoft::WRL::ComPtr m_rasterState; - uint32 m_indexCount; + uint32 m_vertexCount; ModelViewProjectionConstantBuffer m_constantBufferData; };