Commit 31e225d7 authored by Administrator's avatar Administrator
Browse files

add drawing triangle

parent 87608150
......@@ -3,3 +3,6 @@
# Exclude x64 folder
x64/
# Exclude cso files
*.cso
......@@ -25,5 +25,6 @@ void App::DoFrame()
{
const float c = sin(timer.Peek()) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer(c, c, 1.0f);
wnd.Gfx().DrawTestTriangle();
wnd.Gfx().EndFrame();
}
\ No newline at end of file
#include "Graphics.h"
#include "dxerr.h"
#include <sstream>
#include <d3dcompiler.h>
namespace wrl = Microsoft::WRL;
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"D3DCompiler.lib")
// graphics exception checking/throwing macros (some with dxgi infos)
#define GFX_EXCEPT_NOINFO(hr) Graphics::HrException( __LINE__,__FILE__,(hr) )
......@@ -14,10 +16,12 @@ namespace wrl = Microsoft::WRL;
#define GFX_EXCEPT(hr) Graphics::HrException( __LINE__,__FILE__,(hr),infoManager.GetMessages() )
#define GFX_THROW_INFO(hrcall) infoManager.Set(); if( FAILED( hr = (hrcall) ) ) throw GFX_EXCEPT(hr)
#define GFX_DEVICE_REMOVED_EXCEPT(hr) Graphics::DeviceRemovedException( __LINE__,__FILE__,(hr),infoManager.GetMessages() )
#define GFX_THROW_INFO_ONLY(call) infoManager.Set(); (call); {auto v = infoManager.GetMessages(); if(!v.empty()) {throw Graphics::InfoException( __LINE__,__FILE__,v);}}
#else
#define GFX_EXCEPT(hr) Graphics::HrException( __LINE__,__FILE__,(hr) )
#define GFX_THROW_INFO(hrcall) GFX_THROW_NOINFO(hrcall)
#define GFX_DEVICE_REMOVED_EXCEPT(hr) Graphics::DeviceRemovedException( __LINE__,__FILE__,(hr) )
#define GFX_THROW_INFO_ONLY(call) (call)
#endif
Graphics::Graphics( HWND hWnd )
......@@ -98,6 +102,95 @@ void Graphics::ClearBuffer(float red, float green, float blue) noexcept
pContext->ClearRenderTargetView(pTarget.Get(), color);
}
void Graphics::DrawTestTriangle()
{
namespace wrl = Microsoft::WRL;
HRESULT hr;
struct Vertex
{
float x;
float y;
};
// create vertex buffer (1 2d triangle at center of screen)
const Vertex vertices[] =
{
{ 0.0f,0.5f },
{ 0.5f,-0.5f },
{ -0.5f,-0.5f },
};
wrl::ComPtr<ID3D11Buffer> pVertexBuffer;
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = sizeof(vertices);
bd.StructureByteStride = sizeof(Vertex);
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices;
GFX_THROW_INFO(pDevice->CreateBuffer(&bd, &sd, &pVertexBuffer));
// Bind vertex buffer to pipeline
const UINT stride = sizeof(Vertex);
const UINT offset = 0u;
pContext->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset);
// create pixel shader
wrl::ComPtr<ID3D11PixelShader> pPixelShader;
wrl::ComPtr<ID3DBlob> pBlob;
GFX_THROW_INFO(D3DReadFileToBlob(L"PixelShader.cso", &pBlob));
GFX_THROW_INFO(pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pPixelShader));
// bind pixel shader
pContext->PSSetShader(pPixelShader.Get(), nullptr, 0u);
// create vertex shader
wrl::ComPtr<ID3D11VertexShader> pVertexShader;
GFX_THROW_INFO(D3DReadFileToBlob(L"VertexShader.cso", &pBlob));
GFX_THROW_INFO(pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pVertexShader));
// bind vertex shader
pContext->VSSetShader(pVertexShader.Get(), nullptr, 0u);
// create pixel shader
// input (vertex) layout (2d position only)
wrl::ComPtr<ID3D11InputLayout> pInputLayout;
const D3D11_INPUT_ELEMENT_DESC ied[] =
{
{ "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
GFX_THROW_INFO(pDevice->CreateInputLayout(
ied, (UINT)std::size(ied),
pBlob->GetBufferPointer(),
pBlob->GetBufferSize(),
&pInputLayout
));
// bind vertex layout
pContext->IASetInputLayout(pInputLayout.Get());
// bind render target
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), nullptr);
// Set primitive topology to triangle list (groups of 3 vertices)
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// configure viewport
D3D11_VIEWPORT vp;
vp.Width = 800;
vp.Height = 600;
vp.MinDepth = 0;
vp.MaxDepth = 1;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
pContext->RSSetViewports(1u, &vp);
GFX_THROW_INFO_ONLY(pContext->Draw((UINT)std::size(vertices), 0u));
}
// Graphics exception stuff
Graphics::HrException::HrException(int line, const char* file, HRESULT hr, std::vector<std::string> infoMsgs) noexcept
:
......@@ -165,3 +258,41 @@ const char* Graphics::DeviceRemovedException::GetType() const noexcept
{
return "Chili Graphics Exception [Device Removed] (DXGI_ERROR_DEVICE_REMOVED)";
}
Graphics::InfoException::InfoException(int line, const char* file, std::vector<std::string> infoMsgs) noexcept
:
Exception(line, file)
{
// join all info messages with newlines into single string
for (const auto& m : infoMsgs)
{
info += m;
info.push_back('\n');
}
// remove final newline if exists
if (!info.empty())
{
info.pop_back();
}
}
const char* Graphics::InfoException::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
<< "\n[Error Info]\n" << GetErrorInfo() << std::endl << std::endl;
oss << GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Graphics::InfoException::GetType() const noexcept
{
return "Chili Graphics Info Exception";
}
std::string Graphics::InfoException::GetErrorInfo() const noexcept
{
return info;
}
\ No newline at end of file
......@@ -27,6 +27,16 @@ public:
HRESULT hr;
std::string info;
};
class InfoException : public Exception
{
public:
InfoException(int line, const char* file, std::vector<std::string> infoMsgs) noexcept;
const char* what() const noexcept override;
const char* GetType() const noexcept override;
std::string GetErrorInfo() const noexcept;
private:
std::string info;
};
class DeviceRemovedException : public HrException
{
using HrException::HrException;
......@@ -43,6 +53,7 @@ public:
void EndFrame();
// 刷新RGB
void ClearBuffer(float red, float green, float blue) noexcept;
void DrawTestTriangle();
private:
#ifndef NDEBUG
DxgiInfoManager infoManager;
......
float4 main() : SV_Target
{
return float4(1.0f,1.0f,1.0f,1.0f);
}
\ No newline at end of file
......@@ -174,6 +174,20 @@
<None Include="DXGetErrorString.inl" />
<None Include="DXTrace.inl" />
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="VertexShader.hlsl">
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
</FxCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
......
......@@ -16,6 +16,9 @@
<Filter Include="Inline FIles">
<UniqueIdentifier>{4ea6ad36-f6fa-4bd0-a3a7-f0e7c18bdeac}</UniqueIdentifier>
</Filter>
<Filter Include="Shader">
<UniqueIdentifier>{a93d4d3c-0eaa-43f3-b67b-4145e2090bfc}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="WinMain.cpp">
......@@ -111,4 +114,12 @@
<Filter>Inline FIles</Filter>
</None>
</ItemGroup>
<ItemGroup>
<FxCompile Include="VertexShader.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="PixelShader.hlsl">
<Filter>Shader</Filter>
</FxCompile>
</ItemGroup>
</Project>
\ No newline at end of file
float4 main( float2 pos : Position ) : SV_Position
{
return float4(pos.x,pos.y,0.0f,1.0f);
}
\ No newline at end of file
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