Further directx cleaning.

This commit is contained in:
jmsgrogan 2023-01-05 12:06:17 +00:00
parent d99a36f24f
commit 7fcc8e43ae
23 changed files with 401 additions and 304 deletions

View file

@ -173,6 +173,23 @@ std::string StringUtils::convert(const std::wstring& input)
#endif #endif
} }
std::wstring StringUtils::convert(const std::string& input)
{
if (input.empty())
{
return std::wstring();
}
#ifdef _WIN32
const auto charsNeeded = ::MultiByteToWideChar(CP_UTF8, 0, input.data(), (int)input.size(), NULL, 0);
std::vector<wchar_t> buffer(charsNeeded);
const auto charsConverted = ::MultiByteToWideChar(CP_UTF8, 0, input.data(), (int)input.size(), &buffer[0], buffer.size());
return std::wstring(&buffer[0], charsConverted);
#else
throw std::logic_error("Not implemented");
#endif
}
std::string StringUtils::toPaddedString(unsigned numBytes, unsigned entry) std::string StringUtils::toPaddedString(unsigned numBytes, unsigned entry)
{ {
std::stringstream sstr; std::stringstream sstr;

View file

@ -20,6 +20,8 @@ public:
static std::string convert(const std::wstring& input); static std::string convert(const std::wstring& input);
static std::wstring convert(const std::string& input);
static bool isAlphaNumeric(char c); static bool isAlphaNumeric(char c);
static bool isAlphabetical(char c); static bool isAlphabetical(char c);

View file

@ -5,6 +5,15 @@ class DrawingContext;
class AbstractPainter class AbstractPainter
{ {
public: public:
AbstractPainter(DrawingContext* context)
: mDrawingContext(context)
{
}
virtual ~AbstractPainter() = default; virtual ~AbstractPainter() = default;
virtual void paint(DrawingContext* context) = 0; virtual void paint() = 0;
protected:
DrawingContext* mDrawingContext{ nullptr };
}; };

View file

@ -18,7 +18,9 @@ list(APPEND graphics_LIB_INCLUDES
list(APPEND graphics_HEADERS list(APPEND graphics_HEADERS
${platform_HEADERS} ${platform_HEADERS}
RasterPainter.h RasterPainter.h
DrawingContext.h
PainterFactory.h PainterFactory.h
AbstractPainter.h
) )
if(UNIX) if(UNIX)
set(OpenGL_GL_PREFERENCE "GLVND") set(OpenGL_GL_PREFERENCE "GLVND")
@ -44,12 +46,14 @@ if(UNIX)
endif() endif()
else() else()
list(APPEND graphics_LIB_INCLUDES list(APPEND graphics_LIB_INCLUDES
directx/DirectXMesh.cpp
directx/DirectXPainter.cpp directx/DirectXPainter.cpp
directx/DirectXTextPainter.cpp directx/DirectXTextPainter.cpp
directx/DirectXMeshPainter.cpp directx/DirectXMeshPainter.cpp
directx/DirectXShaderProgram.cpp directx/DirectXShaderProgram.cpp
) )
list(APPEND graphics_HEADERS list(APPEND graphics_HEADERS
directx/DirectXMesh.h
directx/DirectXPainter.h directx/DirectXPainter.h
directx/DirectXTextPainter.h directx/DirectXTextPainter.h
directx/DirectXMeshPainter.h directx/DirectXMeshPainter.h

View file

@ -14,7 +14,7 @@ DrawingContext::DrawingContext(DrawingSurface* surface, FontsManager* fontsManag
mDrawingMode(requestedDrawingMode), mDrawingMode(requestedDrawingMode),
mFontsManager(fontsManager) mFontsManager(fontsManager)
{ {
mPainter = PainterFactory::Create(mDrawingMode); mPainter = PainterFactory::Create(this, mDrawingMode);
} }
std::unique_ptr<DrawingContext> DrawingContext::Create(DrawingSurface* surface, FontsManager* fontsManager, DrawingMode requestedDrawingMode) std::unique_ptr<DrawingContext> DrawingContext::Create(DrawingSurface* surface, FontsManager* fontsManager, DrawingMode requestedDrawingMode)
@ -38,8 +38,7 @@ void DrawingContext::paint()
{ {
mSurface->getScene()->update(mFontsManager); mSurface->getScene()->update(mFontsManager);
} }
mPainter->paint();
mPainter->paint(this);
} }
AbstractPainter* DrawingContext::getPainter() const AbstractPainter* DrawingContext::getPainter() const

View file

@ -8,42 +8,41 @@
#include "OpenGlFontTexture.h" #include "OpenGlFontTexture.h"
#endif #endif
#ifdef _WIN32
#include "DirectXPainter.h"
//#include "DirectXMeshPainter.h"
//#include "DirectXTextPainter.h"
//#include "DirectXShaderProgram.h"
#endif
#include "Grid.h" #include "Grid.h"
#include "RasterPainter.h" #include "RasterPainter.h"
#include "DrawingContext.h" #include "DrawingContext.h"
std::unique_ptr<AbstractPainter> PainterFactory::Create(DrawingMode drawMode) #ifdef _WIN32
#include "DirectXPainter.h"
#include "DirectXMesh.h"
#include "DirectXMeshPainter.h"
#include "DirectXTextPainter.h"
#endif
std::unique_ptr<AbstractPainter> PainterFactory::Create(DrawingContext* context, DrawingMode drawMode)
{ {
#ifdef _WIN32 #ifdef _WIN32
if (drawMode == DrawingMode::GRAPH) if (drawMode == DrawingMode::GRAPH)
{ {
return std::make_unique<DirectXPainter>(); return std::make_unique<DirectXPainter>(context);
} }
else else
{ {
return std::make_unique<RasterPainter>(); return std::make_unique<RasterPainter>(context);
} }
#else #else
#ifdef HAS_OPENGL #ifdef HAS_OPENGL
if (drawMode == DrawingMode::GRAPH) if (drawMode == DrawingMode::GRAPH)
{ {
return std::make_unique<OpenGlPainter>(); return std::make_unique<OpenGlPainter>(context);
} }
else else
{ {
return std::make_unique<RasterPainter>(); return std::make_unique<RasterPainter>(context);
} }
#else #else
return std::make_unique<RasterPainter>(); return std::make_unique<RasterPainter>(context);
#endif #endif
#endif #endif
} }

View file

@ -5,9 +5,10 @@
#include <memory> #include <memory>
enum class DrawingMode; enum class DrawingMode;
class DrawingContext;
class PainterFactory class PainterFactory
{ {
public: public:
static std::unique_ptr<AbstractPainter> Create(DrawingMode drawMode); static std::unique_ptr<AbstractPainter> Create(DrawingContext* context, DrawingMode drawMode);
}; };

View file

@ -4,16 +4,17 @@
#include "DrawingContext.h" #include "DrawingContext.h"
#include "Grid.h" #include "Grid.h"
RasterPainter::RasterPainter() RasterPainter::RasterPainter(DrawingContext* context)
: mGrid(std::make_unique<Grid<unsigned char> >(Rectangle(Point(0, 0), Point(100, 100)))) : AbstractPainter(context),
mGrid(std::make_unique<Grid<unsigned char> >(Rectangle(Point(0, 0), Point(100, 100))))
{ {
} }
void RasterPainter::paint(DrawingContext* context) void RasterPainter::paint()
{ {
const auto width = context->getSurface()->getWidth(); const auto width = mDrawingContext->getSurface()->getWidth();
const auto height = context->getSurface()->getHeight(); const auto height = mDrawingContext->getSurface()->getHeight();
mGrid->resetBounds(Rectangle(Point(0, 0), Point(width, height))); mGrid->resetBounds(Rectangle(Point(0, 0), Point(width, height)));
/* /*

View file

@ -11,9 +11,9 @@ class Grid;
class RasterPainter : public AbstractPainter class RasterPainter : public AbstractPainter
{ {
public: public:
RasterPainter(); RasterPainter(DrawingContext* context);
void paint(DrawingContext* context) override; void paint() override;
private: private:
std::unique_ptr<Grid<unsigned char> > mGrid; std::unique_ptr<Grid<unsigned char> > mGrid;

View file

@ -0,0 +1,111 @@
#include "DirectXMesh.h"
#include "SceneModel.h"
#include "DrawingContext.h"
#include "DrawingSurface.h"
#include "TriMesh.h"
#include <directx/d3dx12.h>
DirectXMesh::DirectXMesh(SceneModel* model)
: mModel(model)
{
}
void DirectXMesh::update(DrawingContext* context, ID3D12Device* device)
{
mVertexBuffer.clear();
mIndexBuffer.clear();
const auto width = float(context->getSurface()->getWidth());
const auto height = float(context->getSurface()->getHeight());
auto model_color = mModel->getColor().getAsVectorDouble();
std::vector<float> color = { float(model_color[0]), float(model_color[1]), float(model_color[2]), float(model_color[3]) };
auto transform = mModel->getTransform();
for (const auto& node : mModel->getMesh()->getNodes())
{
auto x = node->getPoint().getX() * transform.getScaleX() + transform.getLocation().getX();
x = 2 * x / width - 1.0;
auto y = node->getPoint().getY() * transform.getScaleY() + transform.getLocation().getY();
y = 2 * y / height;
Vertex vert;
vert.position = DirectX::XMFLOAT3(x, y, 0.0);
vert.color = DirectX::XMFLOAT4(color[0], color[1], color[2], color[3]);
mVertexBuffer.push_back(vert);
}
mIndexBuffer = dynamic_cast<TriMesh*>(mModel->getMesh())->getFaceNodeIds();
createD3dVertexBuffer(device);
createD3dIndexBuffer(device);
mVertexBuffer.clear();
mIndexBuffer.clear();
}
void DirectXMesh::createD3dVertexBuffer(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);
const auto buffer_size = mVertexBuffer.size() * sizeof(Vertex);
auto desc = CD3DX12_RESOURCE_DESC::Buffer(buffer_size);
device->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&mD3dVertexBuffer));
UINT8* pDataBegin;
CD3DX12_RANGE readRange(0, 0);
mD3dVertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pDataBegin));
uploadVertexBuffer(pDataBegin);
mD3dVertexBuffer->Unmap(0, nullptr);
mVertexBufferView.BufferLocation = mD3dVertexBuffer->GetGPUVirtualAddress();
mVertexBufferView.StrideInBytes = sizeof(Vertex);
mVertexBufferView.SizeInBytes = buffer_size;
}
void DirectXMesh::uploadVertexBuffer(unsigned char* pBuffer) const
{
memcpy(pBuffer, mVertexBuffer.data(), mVertexBuffer.size() * sizeof(Vertex));
}
void DirectXMesh::createD3dIndexBuffer(ID3D12Device* device)
{
CD3DX12_HEAP_PROPERTIES heapProps(D3D12_HEAP_TYPE_UPLOAD);
const auto buffer_size = mIndexBuffer.size() * sizeof(unsigned);
auto desc = CD3DX12_RESOURCE_DESC::Buffer(buffer_size);
device->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&mD3dIndexBuffer));
UINT8* pDataBegin;
CD3DX12_RANGE readRange(0, 0);
mD3dIndexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pDataBegin));
uploadIndexBuffer(pDataBegin);
mD3dIndexBuffer->Unmap(0, nullptr);
mIndexBufferView.BufferLocation = mD3dIndexBuffer->GetGPUVirtualAddress();
mIndexBufferView.SizeInBytes = buffer_size;
}
void DirectXMesh::uploadIndexBuffer(unsigned char* pBuffer) const
{
memcpy(pBuffer, mIndexBuffer.data(), mIndexBuffer.size() * sizeof(unsigned));
}
const D3D12_VERTEX_BUFFER_VIEW& DirectXMesh::getVertBufferView() const
{
return mVertexBufferView;
}
const D3D12_INDEX_BUFFER_VIEW& DirectXMesh::getIndexBufferView() const
{
return mIndexBufferView;
}
SceneModel* DirectXMesh::getModel() const
{
return mModel;
}

View file

@ -0,0 +1,48 @@
#pragma once
#include <wrl.h>
#include <directxmath.h>
#include <d3d12.h>
#include <memory>
#include <vector>
class SceneModel;
class DrawingContext;
class DirectXMesh
{
public:
DirectXMesh(SceneModel* model);
const D3D12_VERTEX_BUFFER_VIEW& getVertBufferView() const;
const D3D12_INDEX_BUFFER_VIEW& getIndexBufferView() const;
SceneModel* getModel() const;
void update(DrawingContext* context, ID3D12Device* device);
private:
struct Vertex
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT4 color;
};
void createD3dVertexBuffer(ID3D12Device* device);
void uploadVertexBuffer(unsigned char* pBuffer) const;
void createD3dIndexBuffer(ID3D12Device* device);
void uploadIndexBuffer(unsigned char* pBuffer) const;
SceneModel* mModel{ nullptr };
std::vector<Vertex> mVertexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> mD3dVertexBuffer;
D3D12_VERTEX_BUFFER_VIEW mVertexBufferView{};
std::vector<unsigned> mIndexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> mD3dIndexBuffer;
D3D12_INDEX_BUFFER_VIEW mIndexBufferView{};
};

View file

@ -2,12 +2,14 @@
#include "DrawingContext.h" #include "DrawingContext.h"
#include "DrawingSurface.h" #include "DrawingSurface.h"
#include "Scene.h"
#include "SceneModel.h" #include "SceneModel.h"
#include "TriMesh.h" #include "TriMesh.h"
#include "LineMesh.h" #include "LineMesh.h"
#include "DirectXShaderProgram.h" #include "DirectXShaderProgram.h"
#include "DirectXMesh.h"
#include <windows.h> #include <windows.h>
#include <iostream> #include <iostream>
@ -17,14 +19,6 @@
DirectXMeshPainter::DirectXMeshPainter() 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"; auto shader_path = std::filesystem::path(__FILE__).parent_path() / "shaders.hlsl";
mShaderProgram = std::make_unique<DirectXShaderProgram>(shader_path, shader_path); mShaderProgram = std::make_unique<DirectXShaderProgram>(shader_path, shader_path);
} }
@ -34,172 +28,10 @@ void DirectXMeshPainter::initializeShader()
} }
DirectXShaderProgram* DirectXMeshPainter::getShaderProgram() const void DirectXMeshPainter::initializeD3d(ID3D12Device* device)
{ {
return mShaderProgram.get(); createRootSignature(device);
} createPipelineStateObject(device);
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) void DirectXMeshPainter::createRootSignature(ID3D12Device* device)
@ -240,3 +72,43 @@ void DirectXMeshPainter::createPipelineStateObject(ID3D12Device* device)
psoDesc.SampleDesc.Count = 1; psoDesc.SampleDesc.Count = 1;
device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&mPipelineState)); device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&mPipelineState));
} }
ID3D12PipelineState* DirectXMeshPainter::getPipelineState() const
{
return mPipelineState.Get();
}
ID3D12RootSignature* DirectXMeshPainter::getRootSignature() const
{
return mRootSignature.Get();
}
void DirectXMeshPainter::updateCommandList(ID3D12GraphicsCommandList* commandList)
{
commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
for (const auto& mesh : mDxMeshes)
{
commandList->IASetVertexBuffers(0, 1, &mesh->getVertBufferView());
commandList->IASetIndexBuffer(&mesh->getIndexBufferView());
commandList->DrawInstanced(3, 1, 0, 0);
}
}
void DirectXMeshPainter::updateBuffers(DrawingContext* context, ID3D12Device* device)
{
mDxMeshes.clear();
auto scene = context->getSurface()->getScene();
for (const auto item : scene->getItems())
{
if (item->getType() == SceneItem::Type::MODEL && item->isVisible())
{
auto model = dynamic_cast<SceneModel*>(item);
auto dx_mesh = std::make_unique<DirectXMesh>(model);
dx_mesh->update(context, device);
mDxMeshes.push_back(std::move(dx_mesh));
}
}
}

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <wrl.h> #include <wrl.h>
#include <directxmath.h>
#include <d3d12.h> #include <d3d12.h>
#include <memory> #include <memory>
@ -9,49 +8,26 @@
class DrawingContext; class DrawingContext;
class DirectXShaderProgram; class DirectXShaderProgram;
class SceneModel; class DirectXMesh;
struct CD3DX12_CPU_DESCRIPTOR_HANDLE;
class DirectXMeshPainter class DirectXMeshPainter
{ {
public: public:
DirectXMeshPainter(); 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; ID3D12PipelineState* getPipelineState() const;
ID3D12RootSignature* getRootSignature() const; ID3D12RootSignature* getRootSignature() const;
void updateCommandList(const CD3DX12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList); void initializeD3d(ID3D12Device* device);
void updateBuffers(DrawingContext* context, ID3D12Device* device);
void updateCommandList(ID3D12GraphicsCommandList* commandList);
private: private:
struct Vertex
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT4 color;
};
void initializeShader(); void initializeShader();
void initializeBuffers(); void createRootSignature(ID3D12Device* device);
void createPipelineStateObject(ID3D12Device* device);
unsigned int getVertexBufferSize() const; std::vector<std::unique_ptr<DirectXMesh> > mDxMeshes;
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<ID3D12RootSignature> mRootSignature;
Microsoft::WRL::ComPtr<ID3D12PipelineState> mPipelineState; Microsoft::WRL::ComPtr<ID3D12PipelineState> mPipelineState;

View file

@ -16,15 +16,18 @@
#include "DirectXShaderProgram.h" #include "DirectXShaderProgram.h"
#include "DirectXMeshPainter.h" #include "DirectXMeshPainter.h"
#include "DirectXTextPainter.h" #include "DirectXTextPainter.h"
#include "DirectXMesh.h"
#include "File.h" #include "File.h"
#include <windows.h> #include <windows.h>
#include <directx/d3dx12.h>
#include <iostream> #include <iostream>
DirectXPainter::DirectXPainter() DirectXPainter::DirectXPainter(DrawingContext* context)
: mMeshPainter(std::make_unique<DirectXMeshPainter>()), : AbstractPainter(context),
mMeshPainter(std::make_unique<DirectXMeshPainter>()),
mTextPainter(std::make_unique<DirectXTextPainter>()) mTextPainter(std::make_unique<DirectXTextPainter>())
{ {
@ -40,34 +43,48 @@ DirectXTextPainter* DirectXPainter::getTextPainter() const
return mTextPainter.get(); return mTextPainter.get();
} }
void DirectXPainter::paint(DrawingContext* context) void DirectXPainter::initializeMesh(ID3D12Device* device)
{ {
auto surface = context->getSurface(); mMeshPainter->initializeD3d(device);
const auto width = double(surface->getWidth()); }
const auto height = double(surface->getHeight());
/* void DirectXPainter::initializeText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
glViewport(0, 0, width, height); {
glOrtho(0, width, 0, height, -1.0, 1.0); mTextPainter->initialize(d2dContext, directWriteFactory);
}
glClearColor(0.5, 0.5, 1.0, 0.0); void DirectXPainter::updateMesh(ID3D12Device* device)
glClear(GL_COLOR_BUFFER_BIT); {
*/ mMeshPainter->updateBuffers(mDrawingContext, device);
}
auto scene = context->getSurface()->getScene(); void DirectXPainter::paint()
{
}
void DirectXPainter::paintBackground(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);
}
void DirectXPainter::paintMesh(ID3D12GraphicsCommandList* commandList)
{
mMeshPainter->updateCommandList(commandList);
}
void DirectXPainter::paintText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
{
auto scene = mDrawingContext->getSurface()->getScene();
auto width = mDrawingContext->getSurface()->getWidth();
auto height = mDrawingContext->getSurface()->getHeight();
for (const auto item : scene->getItems()) for (const auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::MODEL) if (item->getType() == SceneItem::Type::TEXT && item->isVisible())
{ {
mMeshPainter->paint(dynamic_cast<SceneModel*>(item), context); auto text = dynamic_cast<SceneText*>(item);
} mTextPainter->paint(text, mDrawingContext, d2dContext, width, height);
else if (item->getType() == SceneItem::Type::TEXT)
{
mTextPainter->paint(dynamic_cast<SceneText*>(item), context);
} }
} }
/*
glFlush();
*/
} }

View file

@ -1,21 +1,41 @@
#pragma once #pragma once
#include "AbstractPainter.h" #include "AbstractPainter.h"
#include <dwrite.h>
#include <d2d1_3.h>
#include <d2d1_1.h>
#include <d3d12.h>
#include <memory> #include <memory>
class DrawingContext; class DrawingContext;
class DirectXMeshPainter; class DirectXMeshPainter;
class DirectXTextPainter; class DirectXTextPainter;
struct CD3DX12_CPU_DESCRIPTOR_HANDLE;
class DirectXPainter : public AbstractPainter class DirectXPainter : public AbstractPainter
{ {
public: public:
DirectXPainter(); DirectXPainter(DrawingContext* context);
void paint(DrawingContext* context) override; void paint() override;
DirectXMeshPainter* getMeshPainter() const; DirectXMeshPainter* getMeshPainter() const;
DirectXTextPainter* getTextPainter() const; DirectXTextPainter* getTextPainter() const;
void initializeMesh(ID3D12Device* device);
void initializeText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory);
void paintBackground(const CD3DX12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList);
void paintMesh(ID3D12GraphicsCommandList* commandList);
void paintText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory);
void updateMesh(ID3D12Device* device);
private: private:
std::unique_ptr<DirectXMeshPainter> mMeshPainter; std::unique_ptr<DirectXMeshPainter> mMeshPainter;
std::unique_ptr<DirectXTextPainter> mTextPainter; std::unique_ptr<DirectXTextPainter> mTextPainter;

View file

@ -11,6 +11,7 @@
#include "SceneText.h" #include "SceneText.h"
#include "StringUtils.h"
#include "File.h" #include "File.h"
#include <windows.h> #include <windows.h>
@ -24,26 +25,15 @@ DirectXTextPainter::DirectXTextPainter()
} }
void DirectXTextPainter::initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
void DirectXTextPainter::paint(SceneText* text, DrawingContext* context)
{ {
initializeBrush(d2dContext);
} initializeTextFormat(directWriteFactory);
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) void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext)
{ {
d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush); d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
} }
void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory) void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory)
@ -62,3 +52,17 @@ void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory
mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
} }
void DirectXTextPainter::paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, float width, float height)
{
D2D1_RECT_F textRect = D2D1::RectF(0, 0, width, height);
auto content = StringUtils::convert(text->getTextData().mContent);
d2dContext->BeginDraw();
d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
d2dContext->DrawText(content.c_str(), static_cast<UINT32>(content.size()) - 1, mTextFormat.Get(), &textRect, mTextBrush.Get());
d2dContext->EndDraw();
}

View file

@ -4,6 +4,7 @@
#include <dwrite.h> #include <dwrite.h>
#include <d2d1_3.h> #include <d2d1_3.h>
#include <d2d1_1.h> #include <d2d1_1.h>
#include <d3d12.h>
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
@ -23,15 +24,14 @@ class DirectXTextPainter
public: public:
DirectXTextPainter(); DirectXTextPainter();
void paint(SceneText* text, DrawingContext* context); void initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory);
void paint(ID2D1DeviceContext2* d2dContext, float width, float height); void paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, float width, float height);
void initializeBrush(ID2D1DeviceContext2* d2dContext);
void initializeTextFormat(IDWriteFactory* directWriteFactory);
private: private:
void initializeBrush(ID2D1DeviceContext2* d2dContext);
void initializeTextFormat(IDWriteFactory* directWriteFactory);
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush; Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush;
Microsoft::WRL::ComPtr<IDWriteTextFormat> mTextFormat; Microsoft::WRL::ComPtr<IDWriteTextFormat> mTextFormat;
}; };

View file

@ -20,10 +20,6 @@
#include "File.h" #include "File.h"
#ifdef _WIN32
#include <windows.h>
#endif
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
@ -35,14 +31,15 @@
#include <iostream> #include <iostream>
OpenGlPainter::OpenGlPainter() OpenGlPainter::OpenGlPainter(DrawingContext* context)
: mMeshPainter(std::make_unique<OpenGlMeshPainter>()), : AbstractPainter(context)
mTextPainter(std::make_unique<OpenGlTextPainter>()) mMeshPainter(std::make_unique<OpenGlMeshPainter>()),
mTextPainter(std::make_unique<OpenGlTextPainter>())
{ {
} }
void OpenGlPainter::paint(DrawingContext* context) void OpenGlPainter::paint()
{ {
auto surface = context->getSurface(); auto surface = context->getSurface();
const auto width = double(surface->getWidth()); const auto width = double(surface->getWidth());
@ -54,16 +51,16 @@ void OpenGlPainter::paint(DrawingContext* context)
glClearColor(0.5, 0.5, 1.0, 0.0); glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
auto scene = context->getSurface()->getScene(); auto scene = mDrawingContext->getSurface()->getScene();
for (const auto item : scene->getItems()) for (const auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::MODEL) if (item->getType() == SceneItem::Type::MODEL)
{ {
mMeshPainter->paint(dynamic_cast<SceneModel*>(item), context); mMeshPainter->paint(dynamic_cast<SceneModel*>(item), mDrawingContext);
} }
else if (item->getType() == SceneItem::Type::TEXT) else if (item->getType() == SceneItem::Type::TEXT)
{ {
mTextPainter->paint(dynamic_cast<SceneText*>(item), context); mTextPainter->paint(dynamic_cast<SceneText*>(item), mDrawingContext);
} }
} }

View file

@ -14,8 +14,8 @@ class TriMesh;
class OpenGlPainter : public AbstractPainter class OpenGlPainter : public AbstractPainter
{ {
public: public:
OpenGlPainter(); OpenGlPainter(DrawingContext* context);
void paint(DrawingContext* context) override; void paint() override;
private: private:
std::unique_ptr<OpenGlMeshPainter> mMeshPainter; std::unique_ptr<OpenGlMeshPainter> mMeshPainter;

View file

@ -21,6 +21,16 @@ bool AbstractMesh::hasVectorAttribute(const std::string& tag) const
return mVectorAttributes.find(tag) != mVectorAttributes.end(); return mVectorAttributes.find(tag) != mVectorAttributes.end();
} }
unsigned AbstractMesh::getNumNodes() const
{
return unsigned(mNodes.size());
}
const VecNodes& AbstractMesh::getNodes() const
{
return mNodes;
}
std::vector<double> AbstractMesh::getVectorAttribute(const std::string& tag) const std::vector<double> AbstractMesh::getVectorAttribute(const std::string& tag) const
{ {
auto iter = mVectorAttributes.find(tag); auto iter = mVectorAttributes.find(tag);

View file

@ -58,10 +58,9 @@ public:
void transform(const Transform& transform); void transform(const Transform& transform);
unsigned getNumNodes() const unsigned getNumNodes() const;
{
return unsigned(mNodes.size()); const VecNodes& getNodes() const;
}
protected: protected:
std::unordered_map<std::string, std::vector<double> > mVectorAttributes; std::unordered_map<std::string, std::vector<double> > mVectorAttributes;

View file

@ -92,7 +92,8 @@ void Win32DxWindowInterface::populateCommandList()
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(mRtvHeap->GetCPUDescriptorHandleForHeapStart(), static_cast<INT>(mFrameIndex), mRtvDescriptorSize); CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(mRtvHeap->GetCPUDescriptorHandleForHeapStart(), static_cast<INT>(mFrameIndex), mRtvDescriptorSize);
mCommandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr); mCommandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
painter->getMeshPainter()->updateCommandList(rtvHandle, mCommandList.Get()); painter->paintBackground(rtvHandle, mCommandList.Get());
painter->paintMesh(mCommandList.Get());
// Indicate that the back buffer will now be used to present. // Indicate that the back buffer will now be used to present.
//barrier = CD3DX12_RESOURCE_BARRIER::Transition(mRenderTargets[mFrameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT); //barrier = CD3DX12_RESOURCE_BARRIER::Transition(mRenderTargets[mFrameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
@ -110,8 +111,7 @@ void Win32DxWindowInterface::renderD2d()
mDxInterface->getD2dContext()->SetTarget(mD2dRenderTargets[mFrameIndex].Get()); mDxInterface->getD2dContext()->SetTarget(mD2dRenderTargets[mFrameIndex].Get());
auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter()); auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter());
D2D1_SIZE_F rtSize = mD2dRenderTargets[mFrameIndex]->GetSize(); painter->paintText(mDxInterface->getD2dContext(), mDxInterface->getDirectWriteFactory());
painter->getTextPainter()->paint(mDxInterface->getD2dContext(), rtSize.width, rtSize.height);
// Release our wrapped render target resource. Releasing // Release our wrapped render target resource. Releasing
// transitions the back buffer resource to the state specified // transitions the back buffer resource to the state specified
@ -205,6 +205,19 @@ void Win32DxWindowInterface::setupDescriptorHeaps()
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
mDxInterface->getDevice()->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&mRtvHeap)); mDxInterface->getDevice()->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&mRtvHeap));
D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = {};
srvHeapDesc.NumDescriptors = 1;
srvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
mDxInterface->getDevice()->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&mSrvHeap));
D3D12_DESCRIPTOR_HEAP_DESC cbvHeapDesc = {};
cbvHeapDesc.NumDescriptors = 1;
cbvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
cbvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
mDxInterface->getDevice()->CreateDescriptorHeap(&cbvHeapDesc, IID_PPV_ARGS(&mCbvHeap));
mRtvDescriptorSize = mDxInterface->getDevice()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); mRtvDescriptorSize = mDxInterface->getDevice()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
} }
@ -253,25 +266,22 @@ bool Win32DxWindowInterface::loadAssets()
{ {
auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter()); auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter());
painter->getMeshPainter()->createRootSignature(mDxInterface->getDevice()); painter->initializeMesh(mDxInterface->getDevice());
painter->getMeshPainter()->createPipelineStateObject(mDxInterface->getDevice());
// Create the command list. // Create the command list.
mDxInterface->getDevice()->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocator.Get(), mDxInterface->getDevice()->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocator.Get(),
painter->getMeshPainter()->getPipelineState(), IID_PPV_ARGS(&mCommandList)); painter->getMeshPainter()->getPipelineState(), IID_PPV_ARGS(&mCommandList));
// Create D2D/DWrite objects for rendering text. // Create D2D/DWrite objects for rendering text.
painter->getTextPainter()->initializeBrush(mDxInterface->getD2dContext()); painter->initializeText(mDxInterface->getD2dContext(), mDxInterface->getDirectWriteFactory());
painter->getTextPainter()->initializeTextFormat(mDxInterface->getDirectWriteFactory());
// Command lists are created in the recording state, but there is nothing // Command lists are created in the recording state, but there is nothing
// to record yet. The main loop expects it to be closed, so close it now. // to record yet. The main loop expects it to be closed, so close it now.
mCommandList->Close(); mCommandList->Close();
painter->getMeshPainter()->createVertexBuffer(mDxInterface->getDevice()); painter->updateMesh(mDxInterface->getDevice());
createSyncObjects(); createSyncObjects();
return true; return true;
} }
@ -306,6 +316,5 @@ void Win32DxWindowInterface::waitForPreviousFrame()
mFence->SetEventOnCompletion(fence, mFenceEvent); mFence->SetEventOnCompletion(fence, mFenceEvent);
::WaitForSingleObject(mFenceEvent, INFINITE); ::WaitForSingleObject(mFenceEvent, INFINITE);
} }
mFrameIndex = mSwapChain->GetCurrentBackBufferIndex(); mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
} }

View file

@ -72,6 +72,8 @@ private:
Microsoft::WRL::ComPtr<IDXGISwapChain4> mSwapChain; Microsoft::WRL::ComPtr<IDXGISwapChain4> mSwapChain;
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mRtvHeap; Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mRtvHeap;
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mSrvHeap;
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> mCbvHeap;
UINT mRtvDescriptorSize{ 0 }; UINT mRtvDescriptorSize{ 0 };
Microsoft::WRL::ComPtr<ID3D12Resource> mRenderTargets[FrameCount]; Microsoft::WRL::ComPtr<ID3D12Resource> mRenderTargets[FrameCount];