Commit 2942ed94 authored by Administrator's avatar Administrator
Browse files

Fixed all compile issue. To debug graph not display issue.

IndexBuffer and VertexBuffer: 指定的格式与缓冲区大小不匹配
parent cef53f68
#pragma once
#include "Bindable.h"
template<typename C>
class ConstantBuffer : public Bindable
{
public:
void Update(Graphics& gfx, const C& consts)
{
D3D11_MAPPED_SUBRESOURCE msr;
GetContext(gfx)->Map(
pConstantBuffer.Get(), 0u,
D3D11_MAP_WRITE_DISCARD, 0u,
&msr
);
memcpy(msr.pData, &consts, sizeof(consts));
GetContext(gfx)->Unmap(pConstantBuffer.Get(), 0u);
}
ConstantBuffer(Graphics& gfx, const C& consts)
{
D3D11_BUFFER_DESC cbd;
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0u;
cbd.ByteWidth = sizeof(consts);
cbd.StructureByteStride = 0u;
D3D11_SUBRESOURCE_DATA csd = {};
csd.pSysMem = &consts;
GetDevice(gfx)->CreateBuffer(&cbd, &csd, &pConstantBuffer);
}
ConstantBuffer(Graphics& gfx)
{
D3D11_BUFFER_DESC cbd;
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0u;
cbd.ByteWidth = sizeof(C);
cbd.StructureByteStride = 0u;
GetDevice(gfx)->CreateBuffer(&cbd, nullptr, &pConstantBuffer);
}
protected:
Microsoft::WRL::ComPtr<ID3D11Buffer> pConstantBuffer;
};
template<typename C>
class VertexConstantBuffer : public ConstantBuffer<C>
{
using ConstantBuffer<C>::pConstantBuffer;
using Bindable::GetContext;
public:
using ConstantBuffer<C>::ConstantBuffer;
void Bind(Graphics& gfx) noexcept override
{
GetContext(gfx)->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
}
};
template<typename C>
class PixelConstantBuffer : public ConstantBuffer<C>
{
using ConstantBuffer<C>::pConstantBuffer;
using Bindable::GetContext;
public:
using ConstantBuffer<C>::ConstantBuffer;
void Bind(Graphics& gfx) noexcept override
{
GetContext(gfx)->PSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
}
};
\ No newline at end of file
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
#include <cassert> #include <cassert>
#include <typeinfo> #include <typeinfo>
/*void Drawable::Draw(Graphics& gfx) const noexcept() void Drawable::Draw(Graphics& gfx) const noexcept
{ {
for (auto& b : binds) for (auto& b : binds)
{ {
b->Bind(gfx); b->Bind(gfx);
} }
gfx.DrawIndexed(pIndexBuffer->GetCount()); gfx.DrawIndexed(pIndexBuffer->GetCount());
}*/ }
void Drawable::AddBind(std::unique_ptr<Bindable> bind) noexcept void Drawable::AddBind(std::unique_ptr<Bindable> bind) noexcept
{ {
......
...@@ -13,8 +13,8 @@ class Drawable ...@@ -13,8 +13,8 @@ class Drawable
public: public:
Drawable() = default; Drawable() = default;
Drawable(const Drawable&) = delete; Drawable(const Drawable&) = delete;
//virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0; virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0;
//void Draw(Graphics& gfx) const noexcept(); void Draw(Graphics& gfx) const noexcept;
//virtual void Update(float dt) noexcept = 0; //virtual void Update(float dt) noexcept = 0;
void AddBind(std::unique_ptr<Bindable> bind) noexcept; void AddBind(std::unique_ptr<Bindable> bind) noexcept;
......
...@@ -132,12 +132,41 @@ void Graphics::RendorFrame(void) ...@@ -132,12 +132,41 @@ void Graphics::RendorFrame(void)
************************************/ ************************************/
// do 3D rendering on the back buffer here // do 3D rendering on the back buffer here
//drawObjects(); //drawObjects();
}
/************************************
* 3 切换显示
************************************/
void Graphics::EndFrame(void)
{
// 指定View Port
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
// 控制View port的位置
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
pContext->RSSetViewports(1, &viewport);
/************************************
* 3 DrawPrimitive
************************************/
// switch the back buffer and the front buffer // switch the back buffer and the front buffer
// 切换显示,将back buffer的内容交换到front buffer
pSwapChain->Present(0, 0); pSwapChain->Present(0, 0);
}
void Graphics::SetProjection(DirectX::FXMMATRIX proj) noexcept
{
projection = proj;
}
DirectX::XMMATRIX Graphics::GetProjection() const noexcept
{
return projection;
}
void Graphics::DrawIndexed(UINT count) noexcept
{
pContext->DrawIndexed(count, 0u, 0u);
} }
\ No newline at end of file
#pragma once #pragma once
#include <d3d11.h> #include <d3d11.h>
#include <wrl.h> #include <wrl.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
namespace wrl = Microsoft::WRL; namespace wrl = Microsoft::WRL;
...@@ -19,10 +21,15 @@ public: ...@@ -19,10 +21,15 @@ public:
// 初始化3大件,Device/SwapChain/deviceContext // 初始化3大件,Device/SwapChain/deviceContext
void InitD3D(HWND hWnd); void InitD3D(HWND hWnd);
void RendorFrame(void); void RendorFrame(void);
void EndFrame(void);
//void drawObjects(); //void DrawTestTriangle(float angle, float x, float y);
void DrawIndexed(UINT count) noexcept;
void SetProjection(DirectX::FXMMATRIX proj) noexcept;
DirectX::XMMATRIX GetProjection() const noexcept;
private: private:
DirectX::XMMATRIX projection;
// 指向Device的指针 // 指向Device的指针
wrl::ComPtr<ID3D11Device> pDevice; wrl::ComPtr<ID3D11Device> pDevice;
// 指向交换链的指针 // 指向交换链的指针
......
#include "InputLayout.h"
InputLayout::InputLayout(Graphics& gfx,
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
ID3DBlob* pVertexShaderBytecode)
{
GetDevice(gfx)->CreateInputLayout(
layout.data(), (UINT)layout.size(),
pVertexShaderBytecode->GetBufferPointer(),
pVertexShaderBytecode->GetBufferSize(),
&pInputLayout
);
}
void InputLayout::Bind(Graphics& gfx) noexcept
{
GetContext(gfx)->IASetInputLayout(pInputLayout.Get());
}
#pragma once
#include "Bindable.h"
#include <vector>
class InputLayout : public Bindable
{
public:
InputLayout(Graphics& gfx,
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
ID3DBlob* pVertexShaderBytecode);
void Bind(Graphics& gfx) noexcept override;
protected:
Microsoft::WRL::ComPtr<ID3D11InputLayout> pInputLayout;
};
\ No newline at end of file
#pragma once
#include "PixelShader.h"
#include <d3d11.h>
#include <d3dcompiler.h>
PixelShader::PixelShader(Graphics& gfx, const std::wstring& path)
{
Microsoft::WRL::ComPtr<ID3DBlob> pBlob;
D3DReadFileToBlob(path.c_str(), &pBlob);
GetDevice(gfx)->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pPixelShader);
}
void PixelShader::Bind(Graphics& gfx) noexcept
{
GetContext(gfx)->PSSetShader(pPixelShader.Get(), nullptr, 0u);
}
#pragma once
#include "Bindable.h"
#include <string>
class PixelShader : public Bindable
{
public:
PixelShader(Graphics& gfx, const std::wstring& path);
void Bind(Graphics& gfx) noexcept override;
protected:
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;
};
\ No newline at end of file
...@@ -128,12 +128,18 @@ ...@@ -128,12 +128,18 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Bindable.cpp" /> <ClCompile Include="Bindable.cpp" />
<ClCompile Include="ConstantBuffer.h" />
<ClCompile Include="Drawable.cpp" /> <ClCompile Include="Drawable.cpp" />
<ClCompile Include="Graphics.cpp" /> <ClCompile Include="Graphics.cpp" />
<ClCompile Include="IndexBuffer.cpp" /> <ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="InputLayout.cpp" />
<ClCompile Include="PixelShader.cpp" />
<ClCompile Include="Timer.cpp" /> <ClCompile Include="Timer.cpp" />
<ClCompile Include="Topology.cpp" />
<ClCompile Include="TransformCBuf.cpp" />
<ClCompile Include="Triangle.cpp" /> <ClCompile Include="Triangle.cpp" />
<ClCompile Include="VertexBuffer.cpp" /> <ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="VertexShader.cpp" />
<ClCompile Include="WinMain.cpp" /> <ClCompile Include="WinMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
...@@ -161,10 +167,15 @@ ...@@ -161,10 +167,15 @@
<ClInclude Include="Drawable.h" /> <ClInclude Include="Drawable.h" />
<ClInclude Include="Graphics.h" /> <ClInclude Include="Graphics.h" />
<ClInclude Include="IndexBuffer.h" /> <ClInclude Include="IndexBuffer.h" />
<ClInclude Include="InputLayout.h" />
<ClInclude Include="PixelShader.h" />
<ClInclude Include="Structs.h" /> <ClInclude Include="Structs.h" />
<ClInclude Include="Timer.h" /> <ClInclude Include="Timer.h" />
<ClInclude Include="Topology.h" />
<ClInclude Include="TransformCBuf.h" />
<ClInclude Include="Triangle.h" /> <ClInclude Include="Triangle.h" />
<ClInclude Include="VertexBuffer.h" /> <ClInclude Include="VertexBuffer.h" />
<ClInclude Include="VertexShader.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
......
...@@ -9,4 +9,15 @@ struct StructVertex ...@@ -9,4 +9,15 @@ struct StructVertex
float y; float y;
float z; float z;
} pos; } pos;
};
struct StructPixelConstBuf
{
struct
{
float r;
float g;
float b;
float a;
} face_colors[6];
}; };
\ No newline at end of file
#include "Topology.h"
Topology::Topology(Graphics& gfx, D3D11_PRIMITIVE_TOPOLOGY type)
:
type(type)
{}
void Topology::Bind(Graphics& gfx) noexcept
{
GetContext(gfx)->IASetPrimitiveTopology(type);
}
#pragma once
#include "Bindable.h"
class Topology : public Bindable
{
public:
Topology(Graphics& gfx, D3D11_PRIMITIVE_TOPOLOGY type);
void Bind(Graphics& gfx) noexcept override;
protected:
D3D11_PRIMITIVE_TOPOLOGY type;
};
\ No newline at end of file
#include "TransformCbuf.h"
TransformCbuf::TransformCbuf(Graphics& gfx, const Drawable& parent)
:
vcbuf(gfx),
parent(parent)
{}
void TransformCbuf::Bind(Graphics& gfx) noexcept
{
vcbuf.Update(gfx,
DirectX::XMMatrixTranspose(
parent.GetTransformXM() * gfx.GetProjection()
)
);
vcbuf.Bind(gfx);
}
#pragma once
#include "ConstantBuffer.h"
#include "Drawable.h"
#include <DirectXMath.h>
class TransformCbuf : public Bindable
{
public:
TransformCbuf(Graphics& gfx, const Drawable& parent);
void Bind(Graphics& gfx) noexcept override;
private:
VertexConstantBuffer<DirectX::XMMATRIX> vcbuf;
const Drawable& parent;
};
\ No newline at end of file
...@@ -3,14 +3,32 @@ ...@@ -3,14 +3,32 @@
#include "Bindable.h" #include "Bindable.h"
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "VertexShader.h"
#include "PixelShader.h"
#include "ConstantBuffer.h"
#include "InputLayout.h"
#include "Topology.h"
#include "TransformCBuf.h"
#include "Graphics.h"
Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b) Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b)
: :
r(r), r(r),
g(g), g(g),
b(b), b(b)
position(position)
{ {
// Assign position values
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
this->position[i][j] = position[i][j];
}
}
/************************************
* 2.3 DefineVertexBuffer
************************************/
const std::vector<StructVertex> vertices = const std::vector<StructVertex> vertices =
{ {
{ position[0][0], position[0][1], position[0][2] }, { position[0][0], position[0][1], position[0][2] },
...@@ -20,16 +38,22 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b) ...@@ -20,16 +38,22 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b)
AddBind(std::make_unique<VertexBuffer>(gfx, vertices)); AddBind(std::make_unique<VertexBuffer>(gfx, vertices));
/* /************************************
* 2.7 DefineVertexShader
auto pvs = std::make_unique<VertexShader>(gfx, L"VertexShader.cso"); ************************************/
auto pvsbc = pvs->GetBytecode(); auto pVertexShader = std::make_unique<VertexShader>(gfx, L"VertexShader.cso");
AddBind(std::move(pvs)); auto pvsbc = pVertexShader->GetBytecode();
AddBind(std::move(pVertexShader));
/************************************
* 2.8 DefinePixelShader
************************************/
AddBind(std::make_unique<PixelShader>(gfx, L"PixelShader.cso")); AddBind(std::make_unique<PixelShader>(gfx, L"PixelShader.cso"));
*/
const std::vector<unsigned short> indices = /************************************
* 2.4 DefineIndexBuffer
************************************/
/*const std::vector<unsigned short> indices =
{ {
0,2,1, 2,3,1, 0,2,1, 2,3,1,
1,3,5, 3,7,5, 1,3,5, 3,7,5,
...@@ -37,21 +61,28 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b) ...@@ -37,21 +61,28 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b)
4,5,7, 4,7,6, 4,5,7, 4,7,6,
0,4,2, 2,4,6, 0,4,2, 2,4,6,
0,1,4, 1,5,4 0,1,4, 1,5,4
};*/
const std::vector<unsigned short> indices =
{
0,1,2
}; };
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices)); AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices));
/* /************************************
struct ConstantBuffer2 * 2.6 DefineColorConstBuffer(PixelConstBuf)
************************************/
/*const StructPixelConstBuf cb2 =
{ {
struct
{ {
float r; { 1.0f,0.0f,1.0f },
float g; { 1.0f,0.0f,0.0f },
float b; { 0.0f,1.0f,0.0f },
float a; { 0.0f,0.0f,1.0f },
} face_colors[6]; { 1.0f,1.0f,0.0f },
}; { 0.0f,1.0f,1.0f },
const ConstantBuffer2 cb2 = }
};*/
const StructPixelConstBuf cb2 =
{ {
{ {
{ 1.0f,0.0f,1.0f }, { 1.0f,0.0f,1.0f },
...@@ -62,19 +93,28 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b) ...@@ -62,19 +93,28 @@ Triangle::Triangle(Graphics& gfx, float** position, float r, float g, float b)
{ 0.0f,1.0f,1.0f }, { 0.0f,1.0f,1.0f },
} }
}; };
AddBind(std::make_unique<PixelConstantBuffer<ConstantBuffer2>>(gfx, cb2));
AddBind(std::make_unique<PixelConstantBuffer<StructPixelConstBuf>>(gfx, cb2));
/************************************
* 2.9 DefineInputLayout
************************************/
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied = const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{ {
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
AddBind(std::make_unique<InputLayout>(gfx, ied, pvsbc)); AddBind(std::make_unique<InputLayout>(gfx, ied, pvsbc));
/************************************
* 2.10 DefinePrimitiveTopology
************************************/
AddBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)); AddBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
/************************************
* 2.5 DefineTransConstBuffer
************************************/
AddBind(std::make_unique<TransformCbuf>(gfx, *this)); AddBind(std::make_unique<TransformCbuf>(gfx, *this));
*/
} }
/* /*
...@@ -87,12 +127,14 @@ void Box::Update(float dt) noexcept ...@@ -87,12 +127,14 @@ void Box::Update(float dt) noexcept
phi += dphi * dt; phi += dphi * dt;
chi += dchi * dt; chi += dchi * dt;
} }
*/
DirectX::XMMATRIX Box::GetTransformXM() const noexcept DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept
{ {
return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) * /*return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) *
DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) * DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) *
DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) * DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) *
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/
return
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f); DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);
} }
*/ \ No newline at end of file
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
class Triangle : public Drawable class Triangle : public Drawable
{ {
public: public:
Triangle(Graphics& gfx, float** position, float r, float g, float b); Triangle(Graphics& gfx, float position[][3], float r, float g, float b);
//DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM() const noexcept override;
private: private:
// positional // positional
float** position; float position[3][3];
// color // color
float r; float r;
float g; float g;
......
#include "VertexShader.h"
#include <d3d11.h>
#include <d3dcompiler.h>
VertexShader::VertexShader(Graphics& gfx, const std::wstring& path)
{
D3DReadFileToBlob(path.c_str(), &pBytecodeBlob);
GetDevice(gfx)->CreateVertexShader(
pBytecodeBlob->GetBufferPointer(),
pBytecodeBlob->GetBufferSize(),
nullptr,
&pVertexShader
);
}
void VertexShader::Bind(Graphics& gfx) noexcept
{
GetContext(gfx)->VSSetShader(pVertexShader.Get(), nullptr, 0u);
}
ID3DBlob* VertexShader::GetBytecode() const noexcept
{
return pBytecodeBlob.Get();
}
#pragma once
#include "Bindable.h"
#include <string>
class VertexShader : public Bindable
{
public:
VertexShader(Graphics& gfx, const std::wstring& path);
void Bind(Graphics& gfx) noexcept override;
ID3DBlob* GetBytecode() const noexcept;
protected:
Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob;
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
};
\ No newline at end of file
...@@ -20,6 +20,8 @@ Timer timer; ...@@ -20,6 +20,8 @@ Timer timer;
#include "Graphics.h" #include "Graphics.h"
#include <memory> #include <memory>
#include "Triangle.h"
/************************************ /************************************
* 1.1 CreateDeviceAndSwapChain * 1.1 CreateDeviceAndSwapChain
************************************/ ************************************/
...@@ -671,7 +673,15 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -671,7 +673,15 @@ int WINAPI WinMain(HINSTANCE hInstance,
// set up and initialize Direct3D // set up and initialize Direct3D
//InitD3D(hWnd); //InitD3D(hWnd);
//std::unique_ptr<Graphics> pGfx = std::make_unique<Graphics>(hWnd);
Graphics gfx(hWnd); Graphics gfx(hWnd);
float pos[3][3] =
{
{0.0f, 1.0f, 0.0f},
{1.0f, -1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f}
};
Triangle tri(gfx, pos, 1.0f, 1.0f, 1.0f);
// enter the main loop: // enter the main loop:
...@@ -699,6 +709,8 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -699,6 +709,8 @@ int WINAPI WinMain(HINSTANCE hInstance,
{ {
//RenderFrame(); //RenderFrame();
gfx.RendorFrame(); gfx.RendorFrame();
tri.Draw(gfx);
gfx.EndFrame();
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment