Successful DirectX context through GLFW, praise Gaben!

This commit is contained in:
2015-10-12 14:31:48 +02:00
parent 16ecdb76d3
commit a782e6b40a
16 changed files with 564 additions and 6 deletions
+2 -2
View File
@@ -24,9 +24,9 @@ endif()
set(BUILD_SHARED_LIBS FALSE)
include_directories(${PROJECT_SOURCE_DIR}/include)
# Compiling for OpenGL target
include_directories(${PROJECT_SOURCE_DIR}/include/Rendering/OpenGL)
#include_directories(${PROJECT_SOURCE_DIR}/include/Rendering/OpenGL)
# Compiling for DirectX target
#include_directories(${PROJECT_SOURCE_DIR}/include/Rendering/DirectX)
include_directories(${PROJECT_SOURCE_DIR}/include/Rendering/DirectX)
#set(GLEW_INCLUDE_DIR ${daydream_SOURCE_DIR}/libs/glew-1.11.0/include)
#set(GLEW_LIBRARY ${daydream_SOURCE_DIR}/libs/glew-1.11.0/lib/Release/glew32.lib)
+34
View File
@@ -0,0 +1,34 @@
#ifndef DD_STATICSYSTEM_H__
#define DD_STATICSYSTEM_H__
#include <unordered_map>
namespace dd
{
class StaticSystem
{
private:
StaticSystem();
public:
template <typename T>
static T* Get()
{
return static_cast<T*>(Instances.at(typeid(T).hash_code()));
}
template <typename T>
static void Set(T* ptr)
{
Instances[typeid(T).hash_code()] = static_cast<void*>(ptr);
}
private:
static std::unordered_map<size_t, void*> Instances;
};
}
#endif
+2 -2
View File
@@ -29,7 +29,7 @@ public:
job.Scissor = (m_ScissorEnabled) ? m_Parent->AbsoluteRectangle() : Rectangle();
job.Viewport = Rectangle(Left(), Top(), Width, Height);
job.TextureID = m_FadeTexture->ResourceID;
job.DiffuseTexture = *m_FadeTexture;
job.DiffuseTexture = m_FadeTexture;
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a);
job.Name = Name();
rq.GUI.Add(job);
@@ -41,7 +41,7 @@ public:
job.Scissor = (m_ScissorEnabled) ? m_Parent->AbsoluteRectangle() : Rectangle();
job.Viewport = Rectangle(Left(), Top(), Width, Height);
job.TextureID = m_Texture->ResourceID;
job.DiffuseTexture = *m_Texture;
job.DiffuseTexture = m_Texture;
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a * m_CurrentFade);
job.Name = Name();
rq.GUI.Add(job);
+4
View File
@@ -27,7 +27,11 @@
// OpenGL
#include <GL/glew.h>
#define GLFW_INCLUDE_GLU
#define NOMINMAX
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#include <GLFW/glfw3native.h>
#include <glext.h>
#include "Core/Util/GLError.h"
@@ -0,0 +1,30 @@
#ifndef Model_h__
#define Model_h__
#include <string>
#include <d3d11.h>
#include "Rendering/RawModel.h"
#include "Rendering/Renderer.h"
#include "Core/StaticSystem.h"
namespace dd
{
class Model : public RawModel
{
friend class ResourceManager;
private:
Model(std::string fileName);
public:
~Model();
ID3D11Buffer* VertexBuffer;
unsigned int NumVertices;
};
}
#endif // Model_h__
@@ -0,0 +1,71 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DD_RENDERER_H__
#define DD_RENDERER_H__
#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
#include <DirectXTK/SimpleMath.h>
using namespace DirectX::SimpleMath;
//#pragma comment (lib, "DirectXTK.lib")
#include "Rendering/BaseRenderer.h"
#include "Core/StaticSystem.h"
namespace dd
{
class Model;
class ShaderProgram;
class Renderer : public BaseRenderer
{
public:
~Renderer();
void Initialize() override;
void Draw(RenderQueueCollection& rq) override;
IDXGISwapChain* m_SwapChain = nullptr;
ID3D11Device* m_Device = nullptr;
ID3D11DeviceContext* m_DeviceContext = nullptr;
ID3D11RenderTargetView* m_BackBuffer = nullptr;
ID3D11DepthStencilView* m_DepthBuffer = nullptr;
private:
struct ConstantBufferStruct
{
Matrix MVP;
Matrix ModelMatrix;
Matrix ViewMatrix;
Matrix ProjectionMatrix;
Matrix InverseTransposeViewModel;
Vector3 LightPosition;
} constantBufferStruct;
ShaderProgram* shaderProgram = nullptr;
const Model* testModel = nullptr;
ID3D11Buffer* constantBuffer = nullptr;
DirectX::SimpleMath::Matrix GLMtoDX(glm::mat4 m);
};
}
#endif // Renderer_h__
@@ -0,0 +1,41 @@
#ifndef ShaderProgram_h__
#define ShaderProgram_h__
#include <string>
#include <d3d11.h>
#include <d3dcompiler.h>
#pragma comment (lib, "D3DCompiler.lib")
#include "Core/StaticSystem.h"
#include "Rendering/Renderer.h"
namespace dd
{
class ShaderProgram
{
public:
ShaderProgram();
~ShaderProgram();
void CompileVertexShader(std::wstring filename);
void CompilePixelShader(std::wstring filename);
HRESULT CreateInputLayout(D3D11_INPUT_ELEMENT_DESC* ied, UINT numElements);
ID3D11InputLayout* GetInputLayout() const { return m_InputLayout; }
void Bind();
void Unbind();
private:
ID3D10Blob* m_VertexShaderBlob;
ID3D11VertexShader* m_VertexShader;
ID3D10Blob* m_PixelShaderBlob;
ID3D11PixelShader* m_PixelShader;
ID3D11InputLayout* m_InputLayout;
};
}
#endif // ShaderProgram_h__
@@ -0,0 +1,49 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Texture_h__
#define Texture_h__
#include <string>
#include <unordered_map>
#include <cstdio>
#include "Core/ResourceManager.h"
#include "Rendering/PNG.h"
namespace dd
{
class Texture : public Resource
{
friend class ResourceManager;
private:
Texture(std::string path);
public:
~Texture();
unsigned int Width = 0;
unsigned int Height = 0;
};
}
#endif // Texture_h__
+1 -1
View File
@@ -96,4 +96,4 @@ private:
}
#endif // Model_h__
#endif
+2
View File
@@ -29,6 +29,8 @@
namespace dd
{
class Model;
class Texture;
class RenderQueue;
struct RenderJob
+3 -1
View File
@@ -104,7 +104,7 @@ set(SOURCE_FILES
${SOURCE_FILES_Core_Util}
${SOURCE_FILES_Input}
${SOURCE_FILES_Rendering}
${SOURCE_FILES_Rendering_OpenGL}
#${SOURCE_FILES_Rendering_OpenGL}
${SOURCE_FILES_Rendering_DirectX}
${SOURCE_FILES_Transform}
${SOURCE_FILES_Physics}
@@ -131,6 +131,7 @@ target_link_libraries(game
${X11_LIBRARIES}
${OPENAL_LIBRARY}
${liquidfun_LIBRARIES}
${CMAKE_LIBRARY_PATH}/DirectXTK.lib
)
add_executable(breakout
@@ -147,5 +148,6 @@ target_link_libraries(breakout
${X11_LIBRARIES}
${OPENAL_LIBRARY}
${liquidfun_LIBRARIES}
${CMAKE_LIBRARY_PATH}/DirectXTK.lib
)
+22
View File
@@ -0,0 +1,22 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "Core/StaticSystem.h"
std::unordered_map<size_t, void*> dd::StaticSystem::Instances;
+13
View File
@@ -0,0 +1,13 @@
#include "PrecompiledHeader.h"
#include "Rendering/Model.h"
dd::Model::Model(std::string fileName)
: dd::RawModel(fileName)
{
D3D11_BUFFER_DESC vbd = { 0 };
vbd.ByteWidth = sizeof(RawModel::Vertex) * m_Vertices.size();
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA vsrd = { m_Vertices.data(), 0, 0 };
StaticSystem::Get<Renderer>()->m_Device->CreateBuffer(&vbd, &vsrd, &VertexBuffer);
}
+196
View File
@@ -0,0 +1,196 @@
#include "PrecompiledHeader.h"
#include "Rendering/Renderer.h"
#include "Rendering/ShaderProgram.h"
void dd::Renderer::Initialize()
{
StaticSystem::Set(this);
// Initialize GLFW
if (!glfwInit()) {
LOG_ERROR("GLFW: Initialization failed");
exit(EXIT_FAILURE);
}
// Create a window
GLFWmonitor* monitor = nullptr;
if (m_Fullscreen) {
monitor = glfwGetPrimaryMonitor();
}
glfwWindowHint(GLFW_SAMPLES, 8);
m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "daydream", monitor, nullptr);
if (!m_Window) {
LOG_ERROR("GLFW: Failed to create window");
exit(EXIT_FAILURE);
}
//glfwMakeContextCurrent(m_Window);
DXGI_SWAP_CHAIN_DESC scd = { 0 };
scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = glfwGetWin32Window(m_Window);
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&m_SwapChain,
&m_Device,
NULL,
&m_DeviceContext);
// Set the back buffer render target
ID3D11Texture2D* pBackBuffer;
m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
m_Device->CreateRenderTargetView(pBackBuffer, NULL, &m_BackBuffer);
pBackBuffer->Release();
// Depth buffer
D3D11_TEXTURE2D_DESC texd = { };
texd.Width = m_Resolution.Width;
texd.Height = m_Resolution.Height;
texd.ArraySize = 1;
texd.MipLevels = 1;
texd.SampleDesc.Count = 4;
texd.Format = DXGI_FORMAT_D32_FLOAT;
texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
ID3D11Texture2D* pDepthBuffer;
m_Device->CreateTexture2D(&texd, NULL, &pDepthBuffer);
D3D11_DEPTH_STENCIL_VIEW_DESC dsvd = { };
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
m_Device->CreateDepthStencilView(pDepthBuffer, &dsvd, &m_DepthBuffer);
pDepthBuffer->Release();
// Set back and depth buffer
m_DeviceContext->OMSetRenderTargets(1, &m_BackBuffer, m_DepthBuffer);
// Rasterizer settings
D3D11_RASTERIZER_DESC rd;
memset(&rd, 0, sizeof(D3D11_RASTERIZER_DESC));
rd.AntialiasedLineEnable = false;
rd.CullMode = D3D11_CULL_BACK;
rd.DepthBias = 0;
rd.DepthBiasClamp = 0.0f;
rd.DepthClipEnable = true;
rd.FillMode = D3D11_FILL_SOLID;
rd.FrontCounterClockwise = true;
rd.MultisampleEnable = false;
rd.ScissorEnable = false;
rd.SlopeScaledDepthBias = 0.0f;
ID3D11RasterizerState* rasterState;
m_Device->CreateRasterizerState(&rd, &rasterState);
m_DeviceContext->RSSetState(rasterState);
// Set the viewport
D3D11_VIEWPORT viewport = { 0 };
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = m_Resolution.Width;
viewport.Height = m_Resolution.Height;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
m_DeviceContext->RSSetViewports(1, &viewport);
testModel = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
shaderProgram = new ShaderProgram();
shaderProgram->CompileVertexShader(L"vertex.hlsl");
shaderProgram->CompilePixelShader(L"pixel.hlsl");
D3D11_INPUT_ELEMENT_DESC VertexIED[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
shaderProgram->CreateInputLayout(VertexIED, sizeof(VertexIED) / sizeof(VertexIED[0]));
shaderProgram->Bind();
// Create constant buffer
// TODO: Put this in shader
D3D11_BUFFER_DESC bd = { };
bd.ByteWidth = 64*6;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
m_Device->CreateBuffer(&bd, NULL, &constantBuffer);
m_DeviceContext->VSSetConstantBuffers(0, 1, &constantBuffer);
m_DeviceContext->PSSetConstantBuffers(0, 1, &constantBuffer);
}
void dd::Renderer::Draw(RenderQueueCollection& rq)
{
float color[] = { 0.f, 0.f, 0.f, 1.f };
m_DeviceContext->ClearRenderTargetView(m_BackBuffer, color);
m_DeviceContext->ClearDepthStencilView(m_DepthBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
constantBufferStruct.MVP = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ModelMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ViewMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.ProjectionMatrix = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.InverseTransposeViewModel = GLMtoDX(glm::mat4(1.f));
constantBufferStruct.LightPosition = DirectX::SimpleMath::Vector3(0.f, 0.f, 100.f);
m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Model::Vertex);
UINT offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &testModel->VertexBuffer, &stride, &offset);
for (auto matGroup : testModel->TextureGroups) {
//m_DeviceContext->PSSetShaderResources(0, 1, &matGroup.Texture);
m_DeviceContext->Draw(matGroup.EndIndex - matGroup.StartIndex, matGroup.StartIndex);
}
m_SwapChain->Present(0, 0);
}
//void dd::Renderer::Resize(int width, int height)
//{
// m_DeviceContext->OMSetRenderTargets(0, 0, 0);
// m_BackBuffer->Release();
//
// HRESULT hr; // TODO: Error handling
// // Preserve the existing buffer count and format.
// hr = m_SwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
//
// ID3D11Texture2D* pBuffer;
// hr = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBuffer);
// hr = m_Device->CreateRenderTargetView(pBuffer, NULL, &m_BackBuffer);
// pBuffer->Release();
// m_DeviceContext->OMSetRenderTargets(1, &BackBuffer, NULL);
//
// // Set the viewport
// D3D11_VIEWPORT viewport = { 0 };
// viewport.TopLeftX = 0;
// viewport.TopLeftY = 0;
// viewport.Width = width;
// viewport.Height = height;
// m_DeviceContext->RSSetViewports(1, &viewport);
//}
dd::Renderer::~Renderer()
{
m_SwapChain->Release();
m_BackBuffer->Release();
m_Device->Release();
m_DeviceContext->Release();
}
DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDX(glm::mat4 gm)
{
gm = glm::transpose(gm);
DirectX::SimpleMath::Matrix dm(
gm[0][0], gm[0][1], gm[0][2], gm[0][3],
gm[1][0], gm[1][1], gm[1][2], gm[1][3],
gm[2][0], gm[2][1], gm[2][2], gm[2][3],
gm[3][0], gm[3][1], gm[3][2], gm[3][3]
);
return dm;
}
@@ -0,0 +1,66 @@
#include "PrecompiledHeader.h"
#include "Rendering/ShaderProgram.h"
dd::ShaderProgram::ShaderProgram()
{
m_VertexShaderBlob = nullptr;
m_VertexShader = nullptr;
m_PixelShaderBlob = nullptr;
m_PixelShader = nullptr;
m_InputLayout = nullptr;
}
dd::ShaderProgram::~ShaderProgram()
{
if (m_VertexShader != nullptr)
m_VertexShader->Release();
if (m_VertexShaderBlob != nullptr)
m_VertexShaderBlob->Release();
if (m_PixelShader != nullptr)
m_PixelShader->Release();
if (m_PixelShaderBlob != nullptr)
m_PixelShaderBlob->Release();
if (m_InputLayout != nullptr)
m_InputLayout->Release();
}
void dd::ShaderProgram::CompileVertexShader(std::wstring filename)
{
//D3DX11CompileFromFile(filename.c_str(), 0, 0, "main", "vs_5_0", 0, 0, 0, &m_VertexShaderBlob, 0, 0);
D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "vs_5_0", 0, 0, &m_VertexShaderBlob, nullptr);
StaticSystem::Get<Renderer>()->m_Device->CreateVertexShader(m_VertexShaderBlob->GetBufferPointer(), m_VertexShaderBlob->GetBufferSize(), NULL, &m_VertexShader);
}
void dd::ShaderProgram::CompilePixelShader(std::wstring filename)
{
//D3DX11CompileFromFile(filename.c_str(), 0, 0, "main", "ps_5_0", 0, 0, 0, &m_PixelShaderBlob, 0, 0);
D3DCompileFromFile(filename.c_str(), nullptr, nullptr, "main", "ps_5_0", 0, 0, &m_PixelShaderBlob, nullptr);
StaticSystem::Get<Renderer>()->m_Device->CreatePixelShader(m_PixelShaderBlob->GetBufferPointer(), m_PixelShaderBlob->GetBufferSize(), NULL, &m_PixelShader);
}
void dd::ShaderProgram::Bind()
{
if (m_VertexShader != nullptr)
StaticSystem::Get<Renderer>()->m_DeviceContext->VSSetShader(m_VertexShader, 0, 0);
if (m_PixelShader != nullptr)
StaticSystem::Get<Renderer>()->m_DeviceContext->PSSetShader(m_PixelShader, 0, 0);
if (m_InputLayout != nullptr)
StaticSystem::Get<Renderer>()->m_DeviceContext->IASetInputLayout(m_InputLayout);
}
void dd::ShaderProgram::Unbind()
{
if (m_VertexShader != nullptr) {
StaticSystem::Get<Renderer>()->m_DeviceContext->VSSetShader(NULL, 0, 0);
}
if (m_PixelShader != nullptr) {
StaticSystem::Get<Renderer>()->m_DeviceContext->PSSetShader(NULL, 0, 0);
}
//if (m_InputLayout != nullptr)
// dd::RENDERER->DeviceContext->IASetInputLayout(NULL);
}
HRESULT dd::ShaderProgram::CreateInputLayout(D3D11_INPUT_ELEMENT_DESC* ied, UINT numElements)
{
return StaticSystem::Get<Renderer>()->m_Device->CreateInputLayout(ied, numElements, m_VertexShaderBlob->GetBufferPointer(), m_VertexShaderBlob->GetBufferSize(), &m_InputLayout);
}
+28
View File
@@ -0,0 +1,28 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "Rendering/Texture.h"
dd::Texture::Texture(std::string path)
{
}
dd::Texture::~Texture()
{
}