Эх сурвалжийг харах

Updated to work with new shell lib layout, does not properly handle window resizes yet

David Wimsey 11 жил өмнө
parent
commit
820ec165b7

+ 15 - 23
Samples/basic/directx10/src/RenderInterfaceDirectx10.cpp

@@ -44,32 +44,24 @@ const D3D10_INPUT_ELEMENT_DESC layout[] =
 };
 
 //The constructor of the render
-RenderInterfaceDirectX10::RenderInterfaceDirectX10(ID3D10Device * pD3D10Device,float screenWidth,float screenHeight)
+RenderInterfaceDirectX10::RenderInterfaceDirectX10(void)
 {
+	m_rocket_context = NULL;
+	m_pD3D10Device = NULL;
+
+	m_pEffect = NULL;
+	m_pTechnique = NULL;
+	m_pVertexLayout = NULL;
+
+	m_pSwapChain = NULL;
+	m_pRenderTargetView = NULL;
+
+	m_pProjectionMatrixVariable = NULL;
+	m_pWorldMatrixVariable = NULL;
+	m_pDiffuseTextureVariable = NULL;
+
 	m_pScissorTestDisable = NULL;
 	m_pScissorTestEnable = NULL;
-	m_pD3D10Device=pD3D10Device;
-	setupEffect();
-	//Create our view and projection matrix
-	D3DXMatrixOrthoOffCenterLH(&m_matProjection, 0, screenWidth, screenHeight, 0, -1, 1);
-	m_pProjectionMatrixVariable->SetMatrix((float*)m_matProjection);
-
-	//Create scissor raster states
-	D3D10_RASTERIZER_DESC rasterDesc;
-	rasterDesc.FillMode=D3D10_FILL_SOLID;
-	rasterDesc.CullMode=D3D10_CULL_NONE;
-	rasterDesc.ScissorEnable=TRUE;
-	rasterDesc.FrontCounterClockwise=TRUE;
-	if (FAILED(m_pD3D10Device->CreateRasterizerState(&rasterDesc, &m_pScissorTestEnable)))
-	{
-		Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Can't create Raster State - ScissorEnable");
-	}
-
-	rasterDesc.ScissorEnable=FALSE;
-	if (FAILED(m_pD3D10Device->CreateRasterizerState(&rasterDesc, &m_pScissorTestDisable)))
-	{
-		Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Can't create Raster State - ScissorDisable");
-	}
 }
 
 //Loads the effect from memory and retrieves initial variables from the effect

+ 22 - 4
Samples/basic/directx10/src/RenderInterfaceDirectx10.h

@@ -2,6 +2,7 @@
 #define RENDERINTERFACEDIRECTX_H
 
 #include <Rocket/Core/RenderInterface.h>
+#include "../../../shell/include/ShellRenderInterfaceExtensions.h"
 #include <d3d10.h>
 #include <d3dx10.h>
 
@@ -14,11 +15,11 @@
 	@author Brian McDonald
  */
 
-class RenderInterfaceDirectX10 : public Rocket::Core::RenderInterface
+class RenderInterfaceDirectX10 : public Rocket::Core::RenderInterface, public ShellRenderInterfaceExtensions
 {
 public:
-	RenderInterfaceDirectX10(ID3D10Device * pD3D10Device,float screenWidth,float screenHeight);
-	virtual ~RenderInterfaceDirectX10();
+	RenderInterfaceDirectX10();
+	virtual ~RenderInterfaceDirectX10(void);
 
 	/// Called by Rocket when it wants to render geometry that it does not wish to optimise.
 	virtual void RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation);
@@ -51,7 +52,19 @@ public:
 	//loads the effect from memory
 	void setupEffect();
 
+	// ShellRenderInterfaceExtensions
+	virtual void SetViewport(int width, int height);
+	virtual void SetContext(void *context);
+	virtual bool AttachToNative(void *nativeWindow);
+	virtual void DetachFromNative(void);
+	virtual void PrepareRenderBuffer(void);
+	virtual void PresentRenderBuffer(void);
+
 private:
+	// Rocket Context, needed for when the shell window is resized so we can update the Rocket::Core::Context
+	// dimensions
+	void *m_rocket_context;
+
 	//The D3D 10 Device
 	ID3D10Device * m_pD3D10Device;
 	//The Effect we are using to render GUI
@@ -61,7 +74,12 @@ private:
 	//The Vertex Layout
 	ID3D10InputLayout*      m_pVertexLayout;
 
-	//Effect varibales, used to send variables to the effect
+	//Swap Chain
+	IDXGISwapChain* m_pSwapChain;
+	//Render Target
+	ID3D10RenderTargetView* m_pRenderTargetView;
+
+	//Effect variables, used to send variables to the effect
 	ID3D10EffectMatrixVariable * m_pProjectionMatrixVariable;
 	ID3D10EffectMatrixVariable * m_pWorldMatrixVariable;
 	ID3D10EffectShaderResourceVariable *m_pDiffuseTextureVariable;

+ 199 - 0
Samples/basic/directx10/src/ShellRenderInterfaceExtensionsDirectX10_Win32.cpp

@@ -0,0 +1,199 @@
+/*
+ * This source file is part of libRocket, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://www.librocket.com
+ *
+ * Copyright (c) 2014 David Wimsey
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "RenderInterfaceDirectX10.h"
+#include <Rocket/Core.h>
+#include <d3d10.h>
+#include <d3dx10.h>
+
+// For _T unicode/mbcs macro
+#include <tchar.h>
+
+void RenderInterfaceDirectX10::SetContext(void *context)
+{
+	m_rocket_context = context;
+}
+
+void RenderInterfaceDirectX10::SetViewport(int width, int height)
+{
+	if(this->m_pD3D10Device != NULL)
+	{
+		// @TODO Deal with the resize here
+	}
+
+	if(m_rocket_context != NULL)
+	{
+		((Rocket::Core::Context*)m_rocket_context)->SetDimensions(Rocket::Core::Vector2i(width, height));
+	}
+}
+
+bool RenderInterfaceDirectX10::AttachToNative(void *nativeWindow)
+{
+	RECT clientRect;
+	if(!GetClientRect((HWND) nativeWindow, &clientRect))
+	{
+		// if we can't lookup the client rect, abort, something is seriously wrong
+		return false;
+	}
+	int width = clientRect.right - clientRect.left;
+	int height = clientRect.bottom - clientRect.top;
+
+	//put the device into debug if we are in a debug build
+	UINT createDeviceFlags=0;
+#ifdef _DEBUG
+	createDeviceFlags|=D3D10_CREATE_DEVICE_DEBUG;
+#endif
+
+	//Setup swap chain
+	DXGI_SWAP_CHAIN_DESC sd;
+	ZeroMemory( &sd, sizeof( sd ) );
+	sd.BufferCount=1;
+	sd.OutputWindow = (HWND) nativeWindow;
+	sd.Windowed = TRUE;
+	sd.SampleDesc.Count = 1;
+	sd.SampleDesc.Quality = 0;
+	sd.BufferDesc.Width = width;
+	sd.BufferDesc.Height = height;
+	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+	sd.BufferDesc.RefreshRate.Numerator = 60;
+	sd.BufferDesc.RefreshRate.Denominator = 1;
+	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+
+	//Create device and swapchain
+	if (FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags,	D3D10_SDK_VERSION, &sd, &this->m_pSwapChain, &this->m_pD3D10Device)))
+	{
+		if(MessageBox(NULL, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_HARDWARE.\r\n\r\nWould you like to try the reference renderer, this will be very slow!"), _T("Could not intialized DirectX 10"), MB_OKCANCEL|MB_ICONERROR) == IDOK)
+		{
+			if (FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, createDeviceFlags,	D3D10_SDK_VERSION, &sd, &this->m_pSwapChain, &this->m_pD3D10Device)))
+			{
+				MessageBox(NULL, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_REFERENCE, giving up."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
+				return false;
+			}
+		}
+		else
+		{
+			return false;
+		}
+	}
+	
+	//Create Render Target
+	ID3D10Texture2D *pBackBuffer;
+	if ( FAILED (this->m_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D),(void**)&pBackBuffer)))
+	{
+		MessageBox(NULL, _T("SwapChain->GetBuffer failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
+		return false;
+	}
+	if (FAILED(this->m_pD3D10Device->CreateRenderTargetView( pBackBuffer, NULL, &this->m_pRenderTargetView )))
+	{
+			pBackBuffer->Release();
+			MessageBox(NULL, _T("D3D10Device->CreateRenderTargetView failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
+			return false;
+	}
+	pBackBuffer->Release();
+	
+	this->m_pD3D10Device->OMSetRenderTargets(1,&this->m_pRenderTargetView,NULL);
+
+	D3D10_VIEWPORT vp;
+	vp.Width = width;
+	vp.Height = height;
+	vp.MinDepth = 0.0f;
+	vp.MaxDepth = 1.0f;
+	vp.TopLeftX = 0;
+	vp.TopLeftY = 0;
+	this->m_pD3D10Device->RSSetViewports( 1, &vp );
+
+	setupEffect();
+
+	//Create our view and projection matrix
+	D3DXMatrixOrthoOffCenterLH(&this->m_matProjection, 0, width, height, 0, -1, 1);
+	m_pProjectionMatrixVariable->SetMatrix((float*)this->m_matProjection);
+
+	//Create scissor raster states
+	D3D10_RASTERIZER_DESC rasterDesc;
+	rasterDesc.FillMode=D3D10_FILL_SOLID;
+	rasterDesc.CullMode=D3D10_CULL_NONE;
+	rasterDesc.ScissorEnable=TRUE;
+	rasterDesc.FrontCounterClockwise=TRUE;
+	if (FAILED(this->m_pD3D10Device->CreateRasterizerState(&rasterDesc, &this->m_pScissorTestEnable)))
+	{
+		Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Can't create Raster State - ScissorEnable");
+	}
+
+	rasterDesc.ScissorEnable=FALSE;
+	if (FAILED(this->m_pD3D10Device->CreateRasterizerState(&rasterDesc, &this->m_pScissorTestDisable)))
+	{
+		Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Can't create Raster State - ScissorDisable");
+	}
+
+	return true;
+}
+
+void RenderInterfaceDirectX10::DetachFromNative()
+{
+	if (this->m_pD3D10Device != NULL)
+	{
+		this->m_pD3D10Device->ClearState();
+		this->m_pD3D10Device = NULL;
+	}
+	if (this->m_pRenderTargetView != NULL)
+	{
+		this->m_pRenderTargetView->Release();
+		this->m_pRenderTargetView = NULL;
+	}
+	if (this->m_pSwapChain != NULL)
+	{
+		this->m_pSwapChain->Release();
+		this->m_pSwapChain = NULL;
+	}
+	if (this->m_pD3D10Device != NULL)
+	{
+		this->m_pD3D10Device->Release();
+		this->m_pD3D10Device = NULL;
+	}
+
+}
+
+void RenderInterfaceDirectX10::PrepareRenderBuffer()
+{
+	if(this->m_pD3D10Device == NULL)
+	{
+		return;
+	}
+
+	float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
+	this->m_pD3D10Device->ClearRenderTargetView(this->m_pRenderTargetView, ClearColor);
+}
+
+void RenderInterfaceDirectX10::PresentRenderBuffer()
+{
+	if(this->m_pSwapChain == NULL)
+	{
+		return;
+	}
+
+	this->m_pSwapChain->Present(0, 0);
+}

+ 11 - 101
Samples/basic/directx10/src/main.cpp

@@ -10,101 +10,17 @@
 // For _T unicode/mbcs macro
 #include <tchar.h>
 
-//Our device for this sample
-static ID3D10Device * pD3D10Device=NULL;
-//Swap Chain
-static IDXGISwapChain * pSwapChain=NULL;
-//Render Target
-static ID3D10RenderTargetView * pRenderTargetView=NULL;
-
 static Rocket::Core::Context* context = NULL;
 
-bool InitialiseDirectX()
-{
-	//get the size of the window
-	RECT windowRect;
-	GetClientRect((HWND) Shell::GetWindowHandle(),&windowRect);
-	UINT width=windowRect.right-windowRect.left;
-	UINT height=windowRect.bottom-windowRect.top;
-
-	//put the device into debug if we are in a debug build
-	UINT createDeviceFlags=0;
-#ifdef _DEBUG
-	createDeviceFlags|=D3D10_CREATE_DEVICE_DEBUG;
-#endif
-
-	//Setup swap chain
-	DXGI_SWAP_CHAIN_DESC sd;
-	ZeroMemory( &sd, sizeof( sd ) );
-	sd.BufferCount=1;
-	sd.OutputWindow = (HWND) Shell::GetWindowHandle();
-	sd.Windowed = TRUE;
-	sd.SampleDesc.Count = 1;
-	sd.SampleDesc.Quality = 0;
-	sd.BufferDesc.Width = width;
-	sd.BufferDesc.Height = height;
-	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
-	sd.BufferDesc.RefreshRate.Numerator = 60;
-	sd.BufferDesc.RefreshRate.Denominator = 1;
-	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
-
-	//Create device and swapchain
-	if (FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags,	D3D10_SDK_VERSION, &sd, &pSwapChain, &pD3D10Device)))
-	{
-		MessageBox(NULL, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_HARDWARE."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
-		return false;
-	}
-	
-	//Create Render Target
-	ID3D10Texture2D *pBackBuffer;
-	if ( FAILED (pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D),(void**)&pBackBuffer)))
-	{
-		MessageBox(NULL, _T("SwapChain->GetBuffer failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
-		return false;
-	}
-	if (FAILED(pD3D10Device->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView )))
-	{
-			pBackBuffer->Release();
-			MessageBox(NULL, _T("D3D10Device->CreateRenderTargetView failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
-			return false;
-	}
-	pBackBuffer->Release();
-	
-	pD3D10Device->OMSetRenderTargets(1,&pRenderTargetView,NULL);
-
-	D3D10_VIEWPORT vp;
-	vp.Width = width;
-	vp.Height = height;
-	vp.MinDepth = 0.0f;
-	vp.MaxDepth = 1.0f;
-	vp.TopLeftX = 0;
-	vp.TopLeftY = 0;
-	pD3D10Device->RSSetViewports( 1, &vp );
-
-	return true;
-}
-
-void ShutdownDirectX()
-{	
-	if (pD3D10Device)
-		pD3D10Device->ClearState();
-	if (pRenderTargetView)
-		pRenderTargetView->Release();
-	if (pSwapChain)
-		pSwapChain->Release();
-	if (pD3D10Device)
-		pD3D10Device->Release();
-}
+ShellRenderInterfaceExtensions *shell_renderer;
 
 void GameLoop()
 {
-	float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
-	pD3D10Device->ClearRenderTargetView( pRenderTargetView,ClearColor );
-
 	context->Update();
-	context->Render();
 
-	pSwapChain->Present( 0, 0 );
+	shell_renderer->PrepareRenderBuffer();
+	context->Render();
+	shell_renderer->PresentRenderBuffer();
 }
 
 int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
@@ -114,25 +30,21 @@ int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTAN
 	ROCKET_UNUSED(command_line);
 	ROCKET_UNUSED(command_show);
 
+	int window_width = 1024;
+	int window_height = 768;
+
+	RenderInterfaceDirectX10 directx_renderer;
+	shell_renderer = &directx_renderer;
+
 	// Generic OS initialisation, creates a window and does not attach OpenGL.
 	if (!Shell::Initialise("../Samples/basic/directx/") ||
-		!Shell::OpenWindow("DirectX 10 Sample", false))
+		!Shell::OpenWindow("DirectX 10 Sample", shell_renderer, window_width, window_height, true))
 	{
 		Shell::Shutdown();
 		return -1;
 	}
 
-	// DirectX initialisation.
-	if (!InitialiseDirectX())
-	{
-		Shell::CloseWindow();
-		Shell::Shutdown();
-
-		return -1;
-	}
-
 	// Install our DirectX render interface into Rocket.
-	RenderInterfaceDirectX10 directx_renderer(pD3D10Device,1024,768);
 	Rocket::Core::SetRenderInterface(&directx_renderer);
 
 	ShellSystemInterface system_interface;
@@ -168,8 +80,6 @@ int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTAN
 	context->RemoveReference();
 	Rocket::Core::Shutdown();
 
-	ShutdownDirectX();
-
 	Shell::CloseWindow();
 	Shell::Shutdown();