#include"mGraphics.h"
#include<xutility> //std::size
#include<DirectXMath.h> //Matrix Vetor
#include<d3dcompiler.h>
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dcompiler.lib")
mGraphics::mGraphics(HWND outputWindowHandle)
:outputWindowHandle(outputWindowHandle)
{
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 0;
sd.BufferDesc.RefreshRate.Denominator = 0;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = outputWindowHandle;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0;
D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&sd,
&pSwap,
&pDevice,
nullptr,
&pContext
);
//创建 目标渲染视图
Microsoft::WRL::ComPtr<ID3D11Resource> pBackBuffer;
pSwap->GetBuffer(0, __uuidof(ID3D11Resource), &pBackBuffer); //从交换链获取buffer
pDevice->CreateRenderTargetView(pBackBuffer.Get(), nullptr, &pTarget);
//深度缓冲状态
D3D11_DEPTH_STENCIL_DESC dsDesc; //深度和模板测试的结构体
dsDesc.DepthEnable = TRUE; //开启深度测试
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; //是否写入深度缓冲"MASK_ALL"或"MASK_ZERO"
dsDesc.DepthFunc = D3D11_COMPARISON_LESS; //比较方式,枚举8种
Microsoft::WRL::ComPtr<ID3D11DepthStencilState> pDSState;
pDevice->CreateDepthStencilState(&dsDesc, &pDSState); //创建深度模板状态对象,封装合并输出阶段的深度模板测试所需信息
pContext->OMSetDepthStencilState(pDSState.Get(), 1u); //参数:深度-模板测试接口指针,模板引用
//创建 2D纹理资源,用于写入深度
D3D11_TEXTURE2D_DESC texDesc{};
texDesc.Width = 800; //宽度纹素数量
texDesc.Height = 600; //高度
texDesc.MipLevels = 1u; //mipmap数量,1u表示一个多采样纹理,0u表示生成完整一套子纹理
texDesc.ArraySize = 1u; //纹理数组中的纹理数
texDesc.Format = DXGI_FORMAT_D32_FLOAT; //存储格式
texDesc.SampleDesc.Count = 1u; //采样描述//每个像素多采样数,1表示无抗锯齿,
texDesc.SampleDesc.Quality = 0u; //质量0~1
texDesc.Usage = D3D11_USAGE_DEFAULT; //访问属性,GPU读写
texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; //资源绑定到渲染管线的位置,(使用位运算"|"多选)
Microsoft::WRL::ComPtr<ID3D11Texture2D> pDepthStencil;
pDevice->CreateTexture2D(&texDesc, nullptr, &pDepthStencil);//创建纹理 不需要第二个参数进行 初识化纹理
//创建深度缓冲
D3D11_DEPTH_STENCIL_VIEW_DESC DSVDesc{};
DSVDesc.Format = DXGI_FORMAT_D32_FLOAT; //格式
DSVDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; //使用2D纹理作为深度缓冲区
DSVDesc.Texture2D.MipSlice = 0u; //union 此处指定使用Texture2D结构体,使用的第一个mipmap索引
pDevice->CreateDepthStencilView(pDepthStencil.Get(), &DSVDesc, &pDSV);//使用纹理创建深度模板缓冲
//在输出 使用深度缓冲
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSV.Get());
}
void mGraphics::EndFrame()
{
pSwap->Present(1u, 0u);
}
void mGraphics::ClearBuffer(float red ,float green,float blue)
{
float color[4]{ red,green,blue,1.f };
pContext->ClearRenderTargetView(pTarget.Get(), color);
pContext->ClearDepthStencilView(pDSV.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);//每帧开始时清除深度缓冲
}
void mGraphics::DrawTriangle(float angle,float z)
{
//顶点着色器常量缓冲
struct ConstantBuffer
{
DirectX::XMMATRIX transform;
};
ConstantBuffer cBuffer{
{
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationZ(angle)* DirectX::XMMatrixRotationX(angle)*DirectX::XMMatrixRotationY(0)
* DirectX::XMMatrixTranslation(0,0,z)
* DirectX::XMMatrixScaling(3.f / 4.f / 4,1.f / 4,1.f / 4)
)
}
};
Microsoft::WRL::ComPtr < ID3D11Buffer> pConstantBuffer;
D3D11_BUFFER_DESC cbd{};
cbd.ByteWidth = sizeof(ConstantBuffer); //字节宽度,缓冲区的总大小,
cbd.Usage = D3D11_USAGE_DYNAMIC; //该Buffer的用法,常用该默认值,表示GPU可读写,其他:GPU读,GPU读,CPU仅写,
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; //缓冲绑定pipeline的阶段,作为 顶点/索引/常量缓冲 绑定
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; //CPU可否更改,0表示不需要CPU访问功能
cbd.MiscFlags = 0u; //其他标志:0表示不使用
cbd.StructureByteStride = 0u; //Buffer中,单组数据的大小
D3D11_SUBRESOURCE_DATA csd{};
csd.pSysMem = &cBuffer;
pDevice->CreateBuffer(&cbd, &csd, &pConstantBuffer);
pContext->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
//创建顶点Buffer
struct Vertex {
struct { float x; float y; float z; }position;
struct { unsigned char r; unsigned char g; unsigned char b; }color;
};
Vertex vertices[] = {
{{0.5,0.5,},},
{{0.5,-0.5,},{255,255,255}},
{{-0.5,0.5,0},{0,0,255}}
};
D3D11_BUFFER_DESC bd{};
bd.ByteWidth = sizeof(vertices); //字节宽度,缓冲区的总大小,
bd.Usage = D3D11_USAGE_DEFAULT; //该Buffer的用法,常用该默认值,表示GPU可读写,其他:GPU读,GPU读,CPU仅写,
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; //缓冲绑定pipeline的阶段,作为 顶点/索引/常量缓冲 绑定
bd.CPUAccessFlags = 0u; //CPU可否更改,0表示不需要CPU访问功能
bd.MiscFlags = 0u; //其他标志:0表示不使用
bd.StructureByteStride = sizeof(Vertex); //Buffer中,单组数据的大小
D3D11_SUBRESOURCE_DATA sd{};
sd.pSysMem = vertices; //初始化数据,表示使用vertices数组中的数据初始化Buffer,
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
pDevice->CreateBuffer(&bd, &sd, &pVertexBuffer); //ComPtr用于填充,使用 & 获取地址,而不是GetAddressOf函数
//绑定顶点缓冲区到pipeline
const UINT stride = sizeof(Vertex);
const UINT offset = 0u;
pContext->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset);//参数:StartSlot,NumBuffer,缓冲区指针,步幅 单组顶点数据大小,从缓冲区的第几个字节开始
//索引缓冲区
unsigned int index[] = {
0,1,2
};
D3D11_BUFFER_DESC ibd{};
ibd.ByteWidth = sizeof(index);
ibd.Usage = D3D11_USAGE_DEFAULT;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u;
ibd.StructureByteStride = sizeof(unsigned int);
D3D11_SUBRESOURCE_DATA isd{};
isd.pSysMem = index;
Microsoft::WRL::ComPtr< ID3D11Buffer>pIndexBuffer;
pDevice->CreateBuffer(&ibd, &isd, &pIndexBuffer);
pContext->IASetIndexBuffer(pIndexBuffer.Get(),DXGI_FORMAT_R32_UINT, 0);
Microsoft::WRL::ComPtr<ID3DBlob> pBlob;
//创建,读取,绑定顶点着色器
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;// 用作函数输出,指向顶点着色器
D3DReadFileToBlob(L"VertexShader.cso", &pBlob); //从目录中 加载cso文件,注意要把hlsl文件编译方式:"$(ProjectDir)%filename).cso"
pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pVertexShader);
pContext->VSSetShader(pVertexShader.Get(), nullptr, 0u);
//顶点输入布局input layout
Microsoft::WRL::ComPtr<ID3D11InputLayout>pInputLayout;
const D3D11_INPUT_ELEMENT_DESC ied[] =
{
{
"Position",0, //"Position"和0 表示vertexS中的"Position"语义,1表示"Position1"语义
DXGI_FORMAT_R32G32B32_FLOAT,
0,
0u,
D3D11_INPUT_PER_VERTEX_DATA,0
},
{
"Color",0,
DXGI_FORMAT_R8G8B8A8_UNORM, //0到255 转为 0到1,
0,
12u, //偏移一组数据中的 float x,y ,z
D3D11_INPUT_PER_VERTEX_DATA,0
}
};
pDevice->CreateInputLayout(
ied, //描述数组
(UINT)std::size(ied), //描述数组元素数量,表示 Buffer中一组顶点数据如何分段 此处float x,y输入语义Position,unsigned char r,g,b,输入语义Color
pBlob->GetBufferPointer(), //pBlob现在仍然读取的是 VertexShader.cso文件的信息,这个参数用于检查 用户给的信息是 否与 着色器文件 相符 生成编译期的警告
pBlob->GetBufferSize(), //数据的字节大小
&pInputLayout //用作输出layout的指针
);
pContext->IASetInputLayout(pInputLayout.Get());
//创建,读取,绑定像素着色器
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;// 用作函数输出,指向像素着色器
D3DReadFileToBlob(L"PixelShader.cso", &pBlob); //从目录中 加载cso文件,注意要把hlsl文件编译方式:"$(ProjectDir)%filename).cso"
pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pPixelShader);
pContext->PSSetShader(pPixelShader.Get(), nullptr, 0u);
// //绑定渲染目标,作为输出
// pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), nullptr); //注意:对ComPtr取地址,“&PTarget”表示先release掉,再返回地址
//归一化设备坐标(NDC)映射到屏幕,配置视口,1/4大小,居中
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);
//拓扑设置
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//绘制
pContext->DrawIndexed((UINT)std::size(index), 0u, 0u);
}

//mGraphics.h
#pragma once
#include<Windows.h>
#include<d3d11.h>
#include <wrl/client.h>
#include<exception>
#include<string>
class mGraphics
{
public:
mGraphics(HWND outputWindowHandle);
mGraphics(const mGraphics&) = delete;
mGraphics& operator=(const mGraphics&) = delete;
~mGraphics() = default;
void EndFrame();
void ClearBuffer(float red, float green, float blue);
public:
void DrawTriangle(float angle);
private:
HWND outputWindowHandle;
Microsoft::WRL::ComPtr<ID3D11Device> pDevice;
Microsoft::WRL::ComPtr<IDXGISwapChain> pSwap;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> pContext;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> pTarget;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> pDSV;
};
//mGraphics.cpp
#include"mGraphics.h"
#include<xutility> //std::size
#include<DirectXMath.h> //Matrix Vetor
#include<d3dcompiler.h>
namespace dx = DirectX;
namespace wrl = Microsoft::WRL;
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dcompiler.lib")
mGraphics::mGraphics(HWND outputWindowHandle)
:outputWindowHandle(outputWindowHandle)
{
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 0;
sd.BufferDesc.RefreshRate.Denominator = 0;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = outputWindowHandle;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0;
D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&sd,
&pSwap,
&pDevice,
nullptr,
&pContext
);
Microsoft::WRL::ComPtr<ID3D11Resource> pBackBuffer;
pSwap->GetBuffer(0, __uuidof(ID3D11Resource), &pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer.Get(), nullptr, &pTarget);
D3D11_DEPTH_STENCIL_DESC dsDesc;
dsDesc.DepthEnable = TRUE;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
Microsoft::WRL::ComPtr<ID3D11DepthStencilState> pDSState;
pDevice->CreateDepthStencilState(&dsDesc, &pDSState);
pContext->OMSetDepthStencilState(pDSState.Get(), 1u);
//create texture2D that used to depth buffer
D3D11_TEXTURE2D_DESC texDesc{};
texDesc.Width = 800;
texDesc.Height = 600;
texDesc.MipLevels = 1u;
texDesc.ArraySize = 1u;
texDesc.Format = DXGI_FORMAT_D32_FLOAT;
texDesc.SampleDesc.Count = 1u;
texDesc.SampleDesc.Quality = 0u;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
Microsoft::WRL::ComPtr<ID3D11Texture2D> pDepthStencil;
pDevice->CreateTexture2D(&texDesc, nullptr, &pDepthStencil);
D3D11_DEPTH_STENCIL_VIEW_DESC DSVDesc{};
DSVDesc.Format = DXGI_FORMAT_D32_FLOAT;
DSVDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
DSVDesc.Texture2D.MipSlice = 0u;
pDevice->CreateDepthStencilView(pDepthStencil.Get(), &DSVDesc, &pDSV);
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSV.Get());
}
void mGraphics::EndFrame()
{
pSwap->Present(1u, 0u);
}
void mGraphics::ClearBuffer(float red ,float green,float blue)
{
float color[4]{ red,green,blue,1.f };
pContext->ClearRenderTargetView(pTarget.Get(), color);
pContext->ClearDepthStencilView(pDSV.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
}
void mGraphics::DrawTriangle(float angle)
{
//bind vertexbuffer
struct Vertex
{
struct
{
float x;float y;float z;
}postion;
};
Vertex vertexBuffer[]
{
{ -1.0f,-1.0f,-1.0f },
{ 1.0f,-1.0f,-1.0f },
{ -1.0f,1.0f,-1.0f },
{ 1.0f,1.0f,-1.0f },
{ -1.0f,-1.0f,1.0f },
{ 1.0f,-1.0f,1.0f },
{ -1.0f,1.0f,1.0f },
{ 1.0f,1.0f,1.0f },
};
D3D11_BUFFER_DESC vertexBufferDesc{};
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.ByteWidth = sizeof(vertexBuffer);
vertexBufferDesc.CPUAccessFlags = 0u;
vertexBufferDesc.MiscFlags = 0u;
vertexBufferDesc.StructureByteStride = sizeof(Vertex);
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA subresource{};
subresource.pSysMem = vertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
pDevice->CreateBuffer(&vertexBufferDesc, &subresource, &pVertexBuffer);
const UINT stride = sizeof(Vertex);
const UINT offset = 0u;
pContext->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(),&stride,&offset);
//bind index buffer
const unsigned short indices[]
{//SV_PrimitiveID:每三个一组从0编号到11,
0,2,1, 2,3,1, 1,3,5, 3,7,5, 2,6,3, 3,6,7, 4,5,7, 4,7,6, 0,4,2, 2,4,6, 0,1,4, 1,5,4
};
D3D11_BUFFER_DESC indexBufferDesc{};
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.ByteWidth = sizeof(indices);
indexBufferDesc.CPUAccessFlags = 0u;
indexBufferDesc.MiscFlags = 0u;
indexBufferDesc.StructureByteStride = sizeof(unsigned short);
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA indexSubresource{};
indexSubresource.pSysMem = indices;
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
pDevice->CreateBuffer(&indexBufferDesc, &indexSubresource, &pIndexBuffer);
pContext->IASetIndexBuffer(pIndexBuffer.Get(),DXGI_FORMAT::DXGI_FORMAT_R16_UINT,0u);
//bind vertex shader cbuffer
struct VConstantBuffer
{
DirectX::XMMATRIX transform;
};
VConstantBuffer cBuffer =
{
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationZ(angle) *
DirectX::XMMatrixRotationX(angle) *
DirectX::XMMatrixTranslation(0,0,2) *
DirectX::XMMatrixScaling(3.f/4.f*1.f/4.f,1.f/4.f,1.f/4.f)
)
};
D3D11_BUFFER_DESC vertexConstBufferDesc{};
vertexConstBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
vertexConstBufferDesc.ByteWidth = sizeof(VConstantBuffer);
vertexConstBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vertexConstBufferDesc.MiscFlags = 0u;
vertexConstBufferDesc.StructureByteStride = 0u;
vertexConstBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
D3D11_SUBRESOURCE_DATA vertexConstBufferSubresource{};
vertexConstBufferSubresource.pSysMem = &cBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexConstBuffer;
pDevice->CreateBuffer(&vertexConstBufferDesc, &vertexConstBufferSubresource, &pVertexConstBuffer);
pContext->VSSetConstantBuffers(0u, 1u, pVertexConstBuffer.GetAddressOf());
//bind pixel shader cbuffer
struct PConstantBuffer
{
struct Color
{
float r, g, b, a;
};
Color surfaceColor[6];
};
PConstantBuffer pixelCBuffer{
{
{1.0f,0.0f,1.0f},{1.0f,0.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,1.0f,0.0f},{0.0f,1.0f,1.0f}
}
};
D3D11_BUFFER_DESC pixelShaderBufferDesc;
pixelShaderBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
pixelShaderBufferDesc.ByteWidth = sizeof(PConstantBuffer);
pixelShaderBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
pixelShaderBufferDesc.MiscFlags = 0u;
pixelShaderBufferDesc.StructureByteStride = 0u;
pixelShaderBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
D3D11_SUBRESOURCE_DATA pixelContantBufferSubresource{};
pixelContantBufferSubresource.pSysMem = &pixelCBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> pPixelConstBuffer;
pDevice->CreateBuffer(&pixelShaderBufferDesc,&pixelContantBufferSubresource,&pPixelConstBuffer);
pContext->PSSetConstantBuffers(0u,1u,pPixelConstBuffer.GetAddressOf());
//vertex shader
Microsoft::WRL::ComPtr<ID3DBlob> pBlob;
D3DReadFileToBlob(L"VertexShader.cso", &pBlob);
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pVertexShader);
pContext->VSSetShader(pVertexShader.Get(), nullptr, 0u);
//vertex shader input layout
D3D11_INPUT_ELEMENT_DESC ied[]
{
{"Position",0u,DXGI_FORMAT_R32G32B32_FLOAT,0u,0u,D3D11_INPUT_PER_VERTEX_DATA,0u}
};
Microsoft::WRL::ComPtr<ID3D11InputLayout> pInputLayout;
pDevice->CreateInputLayout(ied, (UINT)std::size(ied), pBlob->GetBufferPointer(), pBlob->GetBufferSize(), &pInputLayout);
pContext->IASetInputLayout(pInputLayout.Get());
//pixel shader
D3DReadFileToBlob(L"PixelShader.cso", &pBlob);
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;
pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pPixelShader);
pContext->PSSetShader(pPixelShader.Get(), nullptr, 0u);
//viewport
D3D11_VIEWPORT vp{};
vp.Height = 600.f;
vp.Width = 800.f;
vp.TopLeftX = 0.f;
vp.TopLeftY = 0.f;
vp.MaxDepth = 1;
vp.MinDepth = 0;
pContext->RSSetViewports(1u, &vp);
//primitive topology
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//draw
pContext->DrawIndexed((UINT)std::size(indices), 0u, 0u);
}
//VertexShader.hlsl
cbuffer CBuf
{
matrix transform;
};
float4 main(float3 pos : Position) : SV_Position
{
return mul(float4(pos,1.0f),transform);
}
//PixelShader.hlsl
cbuffer CBuf
{
float4 face_colors[6];
};
float4 main(uint tid : SV_PrimitiveID/*要将文件属性的Shader Model改为4或支持该语义的其它*/) : SV_Target
{
return face_colors[tid / 2];
}
