Start aligning Dx and OpenGl approaches.

This commit is contained in:
jmsgrogan 2023-01-05 08:46:01 +00:00
parent d1ec8b4f68
commit d99a36f24f
22 changed files with 899 additions and 366 deletions

View file

@ -44,9 +44,20 @@ if(UNIX)
endif()
else()
list(APPEND graphics_LIB_INCLUDES
directx/DirectXPainter.cpp)
directx/DirectXPainter.cpp
directx/DirectXTextPainter.cpp
directx/DirectXMeshPainter.cpp
directx/DirectXShaderProgram.cpp
)
list(APPEND graphics_HEADERS
directx/DirectXPainter.h)
directx/DirectXPainter.h
directx/DirectXTextPainter.h
directx/DirectXMeshPainter.h
directx/DirectXShaderProgram.h
)
find_package(DirectX-Headers REQUIRED)
list(APPEND platform_LIBS D3D12.lib D3DCompiler.lib Dwrite.lib D2d1.lib Microsoft::DirectX-Headers)
endif()
add_library(${MODULE_NAME} SHARED
@ -57,6 +68,7 @@ target_include_directories(${MODULE_NAME} PUBLIC
${platform_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/opengl
${CMAKE_CURRENT_SOURCE_DIR}/directx
)
target_compile_definitions(${MODULE_NAME} PRIVATE ${DEFINES})

View file

@ -41,3 +41,8 @@ void DrawingContext::paint()
mPainter->paint(this);
}
AbstractPainter* DrawingContext::getPainter() const
{
return mPainter.get();
}

View file

@ -26,6 +26,8 @@ public:
void paint();
AbstractPainter* getPainter() const;
private:
DrawingMode mDrawingMode;
FontsManager* mFontsManager{nullptr};

View file

@ -8,6 +8,13 @@
#include "OpenGlFontTexture.h"
#endif
#ifdef _WIN32
#include "DirectXPainter.h"
//#include "DirectXMeshPainter.h"
//#include "DirectXTextPainter.h"
//#include "DirectXShaderProgram.h"
#endif
#include "Grid.h"
#include "RasterPainter.h"
@ -16,17 +23,28 @@
std::unique_ptr<AbstractPainter> PainterFactory::Create(DrawingMode drawMode)
{
#ifdef HAS_OPENGL
#ifdef _WIN32
if (drawMode == DrawingMode::GRAPH)
{
return std::make_unique<OpenGlPainter>();
return std::make_unique<DirectXPainter>();
}
else
{
return std::make_unique<RasterPainter>();
}
#else
return std::make_unique<RasterPainter>();
#ifdef HAS_OPENGL
if (drawMode == DrawingMode::GRAPH)
{
return std::make_unique<OpenGlPainter>();
}
else
{
return std::make_unique<RasterPainter>();
}
#else
return std::make_unique<RasterPainter>();
#endif
#endif
}

View file

@ -0,0 +1,242 @@
#include "DirectXMeshPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "SceneModel.h"
#include "TriMesh.h"
#include "LineMesh.h"
#include "DirectXShaderProgram.h"
#include <windows.h>
#include <iostream>
#include <filesystem>
#include <directx/d3dx12.h>
DirectXMeshPainter::DirectXMeshPainter()
{
const float aspectRatio = 1.0;
mVertexBuffer =
{
{ { 0.0f, 0.25f * aspectRatio, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
{ { 0.25f, -0.25f * aspectRatio, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { -0.25f, -0.25f * aspectRatio, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }
};
auto shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders.hlsl";
mShaderProgram = std::make_unique<DirectXShaderProgram>(shader_path, shader_path);
}
void DirectXMeshPainter::initializeShader()
{
}
DirectXShaderProgram* DirectXMeshPainter::getShaderProgram() const
{
return mShaderProgram.get();
}
unsigned int DirectXMeshPainter::getVertexBufferSize() const
{
return mVertexBuffer.size()*sizeof(Vertex);
}
unsigned int DirectXMeshPainter::getVertexSize() const
{
return sizeof(Vertex);
}
ID3D12PipelineState* DirectXMeshPainter::getPipelineState() const
{
return mPipelineState.Get();
}
ID3D12RootSignature* DirectXMeshPainter::getRootSignature() const
{
return mRootSignature.Get();
}
void DirectXMeshPainter::updateVertexBuffer(unsigned char* pBuffer) const
{
memcpy(pBuffer, mVertexBuffer.data(), getVertexBufferSize());
}
void DirectXMeshPainter::updateCommandList(const CD3DX12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList)
{
const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
commandList->IASetVertexBuffers(0, 1, &mVertexBufferView);
commandList->DrawInstanced(3, 1, 0, 0);
}
void DirectXMeshPainter::initializeBuffers()
{
/*
glGenBuffers(1, &mVertexBuffer);
glGenBuffers(1, &mElementBuffer);
glGenVertexArrays(1, &mVertexArray);
*/
}
void DirectXMeshPainter::paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color, bool lines)
{
/*
glBindVertexArray(mVertexArray);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(float), verts.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned), elements.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//glm::mat4 projection = glm::ortho(0.0f, width, 0.0f, height);
//glUniformMatrix4fv(glGetUniformLocation(mShaderProgram->getHandle(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(mShaderProgram->getHandle());
glBindVertexArray(mVertexArray);
int vertexColorLocation = glGetUniformLocation(mShaderProgram->getHandle(), "ourColor");
glUniform4f(vertexColorLocation, float(color[0]), float(color[1]), float(color[2]), float(color[3]));
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (lines)
{
glDrawElements(GL_LINES, elements.size(), GL_UNSIGNED_INT, 0);
}
else
{
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
*/
}
void DirectXMeshPainter::paint(SceneModel* model, DrawingContext* context)
{
return;
/*
if (!mShaderProgram)
{
initializeShader();
}
if (mVertexArray == 0)
{
initializeBuffers();
}
auto surface = context->getSurface();
const auto width = float(surface->getWidth());
const auto height = float(surface->getHeight());
auto transform = model->getTransform();
auto vertices = model->getMesh()->getVerticesFlat<float>();
for (std::size_t idx = 0; idx < vertices.size(); idx++)
{
if (idx % 3 == 0)
{
auto x = vertices[idx] * transform.getScaleX() + transform.getLocation().getX();
vertices[idx] = 2 * x / width - 1.0;
}
else if (idx % 3 == 1)
{
auto y = vertices[idx] * transform.getScaleY() + transform.getLocation().getY();
vertices[idx] = 1.0 - 2 * y / height;
}
}
std::vector<unsigned> indices;
const bool line_mesh = model->getMesh()->getType() == AbstractMesh::MeshType::LINE;
if (line_mesh)
{
indices = dynamic_cast<LineMesh*>(model->getMesh())->getEdgeNodeIds();
}
else
{
indices = dynamic_cast<TriMesh*>(model->getMesh())->getFaceNodeIds();
}
auto model_color = model->getColor().getAsVectorDouble();
std::vector<float> color = { float(model_color[0]), float(model_color[1]), float(model_color[2]), float(model_color[3]) };
paint(vertices, indices, color, line_mesh);
if (model->getShowOutline())
{
auto edge_indices = dynamic_cast<TriMesh*>(model->getMesh())->getEdgeNodeIds();
paint(vertices, edge_indices, { 0, 0, 0, 1 }, true);
}
*/
}
void DirectXMeshPainter::createVertexBuffer(ID3D12Device* device)
{
// Note: using upload heaps to transfer static data like vert buffers is not
// recommended. Every time the GPU needs it, the upload heap will be marshalled
// over. Please read up on Default Heap usage. An upload heap is used here for
// code simplicity and because there are very few verts to actually transfer.
CD3DX12_HEAP_PROPERTIES heapProps(D3D12_HEAP_TYPE_UPLOAD);
auto desc = CD3DX12_RESOURCE_DESC::Buffer(getVertexBufferSize());
device->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&mD3dVertexBuffer));
// Copy the mesh data to the vertex buffer.
UINT8* pVertexDataBegin;
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
mD3dVertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin));
updateVertexBuffer(pVertexDataBegin);
mD3dVertexBuffer->Unmap(0, nullptr);
// Initialize the vertex buffer view.
mVertexBufferView.BufferLocation = mD3dVertexBuffer->GetGPUVirtualAddress();
mVertexBufferView.StrideInBytes = getVertexSize();
mVertexBufferView.SizeInBytes = getVertexBufferSize();
}
void DirectXMeshPainter::createRootSignature(ID3D12Device* device)
{
CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc;
rootSignatureDesc.Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
Microsoft::WRL::ComPtr<ID3DBlob> signature;
Microsoft::WRL::ComPtr<ID3DBlob> error;
D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error);
device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature));
}
void DirectXMeshPainter::createPipelineStateObject(ID3D12Device* device)
{
auto vert_shader = mShaderProgram->getVertexShader();
auto pixel_shader = mShaderProgram->getPixelShader();
D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
};
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
psoDesc.pRootSignature = mRootSignature.Get();
psoDesc.VS = CD3DX12_SHADER_BYTECODE(vert_shader);
psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixel_shader);
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoDesc.DepthStencilState.DepthEnable = FALSE;
psoDesc.DepthStencilState.StencilEnable = FALSE;
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
psoDesc.SampleDesc.Count = 1;
device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&mPipelineState));
}

View file

@ -0,0 +1,60 @@
#pragma once
#include <wrl.h>
#include <directxmath.h>
#include <d3d12.h>
#include <memory>
#include <vector>
class DrawingContext;
class DirectXShaderProgram;
class SceneModel;
struct CD3DX12_CPU_DESCRIPTOR_HANDLE;
class DirectXMeshPainter
{
public:
DirectXMeshPainter();
void paint(SceneModel* model, DrawingContext* context);
DirectXShaderProgram* getShaderProgram() const;
void createVertexBuffer(ID3D12Device* device);
void createRootSignature(ID3D12Device* device);
void createPipelineStateObject(ID3D12Device* device);
ID3D12PipelineState* getPipelineState() const;
ID3D12RootSignature* getRootSignature() const;
void updateCommandList(const CD3DX12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList);
private:
struct Vertex
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT4 color;
};
void initializeShader();
void initializeBuffers();
unsigned int getVertexBufferSize() const;
unsigned int getVertexSize() const;
void updateVertexBuffer(unsigned char* pBuffer) const;
void paint(const std::vector<float>& verts, const std::vector<unsigned>& elements, const std::vector<float>& color, bool lines = false);
unsigned int mVertexBufferSize{ 0 };
std::vector<Vertex> mVertexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> mD3dVertexBuffer;
D3D12_VERTEX_BUFFER_VIEW mVertexBufferView{};
Microsoft::WRL::ComPtr<ID3D12RootSignature> mRootSignature;
Microsoft::WRL::ComPtr<ID3D12PipelineState> mPipelineState;
std::unique_ptr<DirectXShaderProgram> mShaderProgram;
};

View file

@ -0,0 +1,73 @@
#include "DirectXPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "Scene.h"
#include "SceneItem.h"
#include "SceneModel.h"
#include "SceneText.h"
#include "TriMesh.h"
#include "FontsManager.h"
#include "FontGlyph.h"
#include "TextData.h"
#include "DirectXShaderProgram.h"
#include "DirectXMeshPainter.h"
#include "DirectXTextPainter.h"
#include "File.h"
#include <windows.h>
#include <iostream>
DirectXPainter::DirectXPainter()
: mMeshPainter(std::make_unique<DirectXMeshPainter>()),
mTextPainter(std::make_unique<DirectXTextPainter>())
{
}
DirectXMeshPainter* DirectXPainter::getMeshPainter() const
{
return mMeshPainter.get();
}
DirectXTextPainter* DirectXPainter::getTextPainter() const
{
return mTextPainter.get();
}
void DirectXPainter::paint(DrawingContext* context)
{
auto surface = context->getSurface();
const auto width = double(surface->getWidth());
const auto height = double(surface->getHeight());
/*
glViewport(0, 0, width, height);
glOrtho(0, width, 0, height, -1.0, 1.0);
glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
*/
auto scene = context->getSurface()->getScene();
for (const auto item : scene->getItems())
{
if (item->getType() == SceneItem::Type::MODEL)
{
mMeshPainter->paint(dynamic_cast<SceneModel*>(item), context);
}
else if (item->getType() == SceneItem::Type::TEXT)
{
mTextPainter->paint(dynamic_cast<SceneText*>(item), context);
}
}
/*
glFlush();
*/
}

View file

@ -0,0 +1,22 @@
#pragma once
#include "AbstractPainter.h"
#include <memory>
class DrawingContext;
class DirectXMeshPainter;
class DirectXTextPainter;
class DirectXPainter : public AbstractPainter
{
public:
DirectXPainter();
void paint(DrawingContext* context) override;
DirectXMeshPainter* getMeshPainter() const;
DirectXTextPainter* getTextPainter() const;
private:
std::unique_ptr<DirectXMeshPainter> mMeshPainter;
std::unique_ptr<DirectXTextPainter> mTextPainter;
};

View file

@ -0,0 +1,31 @@
#include "DirectXShaderProgram.h"
#include "File.h"
#include "FileLogger.h"
#include <d3d12.h>
#include <d3dcompiler.h>
DirectXShaderProgram::DirectXShaderProgram(const Path& vertShaderPath, const Path& fragShaderPath)
{
#if defined(_DEBUG)
// Enable better shader debugging with the graphics debugging tools.
// UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
UINT compileFlags = 0;
#else
UINT compileFlags = 0;
#endif
D3DCompileFromFile(vertShaderPath.c_str(), nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &mVertexShader, nullptr);
D3DCompileFromFile(fragShaderPath.c_str(), nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &mPixelShader, nullptr);
}
ID3DBlob* DirectXShaderProgram::getVertexShader() const
{
return mVertexShader.Get();
}
ID3DBlob* DirectXShaderProgram::getPixelShader() const
{
return mPixelShader.Get();
}

View file

@ -0,0 +1,20 @@
#pragma once
#include <wrl.h>
#include <D3Dcommon.h>
#include <filesystem>
using Path = std::filesystem::path;
class DirectXShaderProgram
{
public:
DirectXShaderProgram(const Path& vertShaderPath, const Path& pixelShaderPath);
ID3DBlob* getVertexShader() const;
ID3DBlob* getPixelShader() const;
private:
Microsoft::WRL::ComPtr<ID3DBlob> mVertexShader;
Microsoft::WRL::ComPtr<ID3DBlob> mPixelShader;
};

View file

@ -0,0 +1,64 @@
#include "DirectXTextPainter.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "FontsManager.h"
#include "FontGlyph.h"
#include "DirectXShaderProgram.h"
#include "TextData.h"
#include "SceneText.h"
#include "File.h"
#include <windows.h>
#include <d2d1_3.h>
#include <d2d1_1.h>
#include <dwrite.h>
DirectXTextPainter::DirectXTextPainter()
{
}
void DirectXTextPainter::paint(SceneText* text, DrawingContext* context)
{
}
void DirectXTextPainter::paint(ID2D1DeviceContext2* d2dContext, float width, float height)
{
D2D1_RECT_F textRect = D2D1::RectF(0, 0, width, height);
static const WCHAR text[] = L"11On12";
d2dContext->BeginDraw();
d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
d2dContext->DrawText(text, _countof(text) - 1, mTextFormat.Get(), &textRect, mTextBrush.Get());
d2dContext->EndDraw();
}
void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext)
{
d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
}
void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory)
{
directWriteFactory->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
50,
L"en-us",
&mTextFormat
);
mTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}

View file

@ -0,0 +1,37 @@
#pragma once
#include <wrl.h>
#include <dwrite.h>
#include <d2d1_3.h>
#include <d2d1_1.h>
#include <unordered_map>
#include <memory>
class DrawingContext;
class DirectXShaderProgram;
class TextData;
class SceneText;
struct ID2D1DeviceContext2;
struct ID2D1SolidColorBrush;
struct IDWriteFactory;
class DirectXTextPainter
{
public:
DirectXTextPainter();
void paint(SceneText* text, DrawingContext* context);
void paint(ID2D1DeviceContext2* d2dContext, float width, float height);
void initializeBrush(ID2D1DeviceContext2* d2dContext);
void initializeTextFormat(IDWriteFactory* directWriteFactory);
private:
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush;
Microsoft::WRL::ComPtr<IDWriteTextFormat> mTextFormat;
};

View file

@ -0,0 +1,20 @@
struct PSInput
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
PSInput VSMain(float4 position : POSITION, float4 color : COLOR)
{
PSInput result;
result.position = position;
result.color = color;
return result;
}
float4 PSMain(PSInput input) : SV_TARGET
{
return input.color;
}