-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceContext.cpp
More file actions
137 lines (119 loc) · 4.89 KB
/
DeviceContext.cpp
File metadata and controls
137 lines (119 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "DeviceContext.h"
DeviceContext::DeviceContext(ID3D11DeviceContext* device_context, RenderSystem* system)
{
mRenderSystem = system;
mDeviceContext = device_context;
}
DeviceContext::~DeviceContext()
{
mDeviceContext->Release();
}
void DeviceContext::clearRenderTargetColor(const SwapChainPtr& swap_chain, float red, float green, float blue, float alpha)
{
FLOAT clear_color[] = { red, green, blue, alpha };
mDeviceContext->ClearRenderTargetView(swap_chain->mRtv, clear_color);
mDeviceContext->ClearDepthStencilView(swap_chain->mDsv, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);
mDeviceContext->OMSetRenderTargets(1, &swap_chain->mRtv, swap_chain->mDsv);
}
// --------------------------------------------------
// Draw functions -> executes every stage in the graphics pipeline when Draw() is called
// The triangles are drawn in a clockwise orientation
// It must be clockwise in order the know if the triangles are facing towards the camera
// --------------------------------------------------
void DeviceContext::drawTriangleList(UINT vertex_count, UINT start_vertex_index)
{
mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
mDeviceContext->Draw(vertex_count, start_vertex_index);
}
void DeviceContext::drawIndexedTriangleList(UINT index_count, UINT start_vertex_index, UINT start_index_location)
{
mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
mDeviceContext->DrawIndexed(index_count, start_index_location, start_vertex_index);
}
void DeviceContext::drawTriangleStrip(UINT vertex_count, UINT start_vertex_index)
{
mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
mDeviceContext->Draw(vertex_count, start_vertex_index);
}
// --------------------------------------------------
void DeviceContext::clearRenderTargetColor(const TexturePtr& render_target, float red, float green, float blue, float alpha)
{
if (render_target->mType != Texture::Type::RenderTarget) return;
FLOAT clear_color[] = { red, green, blue, alpha };
mDeviceContext->ClearRenderTargetView(render_target->mRenderTargetView, clear_color);
}
void DeviceContext::clearDepthStencil(const TexturePtr& depth_stencil)
{
if (depth_stencil->mType != Texture::Type::DepthStencil) return;
mDeviceContext->ClearDepthStencilView(depth_stencil->mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);
}
void DeviceContext::setRenderTarget(const TexturePtr& render_target, const TexturePtr& depth_stencil)
{
if (render_target->mType != Texture::Type::RenderTarget) return;
if (depth_stencil->mType != Texture::Type::DepthStencil) return;
mDeviceContext->OMSetRenderTargets(
1,
&render_target->mRenderTargetView,
depth_stencil->mDepthStencilView
);
}
void DeviceContext::setVertexBuffer(const VertexBufferPtr& vertex_buffer)
{
UINT stride = vertex_buffer->mSizeVertex;
UINT offset = 0;
mDeviceContext->IASetVertexBuffers(0, 1, &vertex_buffer->mBuffer, &stride, &offset);
mDeviceContext->IASetInputLayout(vertex_buffer->mLayout);
}
void DeviceContext::setVertexShader(const VertexShaderPtr& vertex_shader)
{
mDeviceContext->VSSetShader(vertex_shader->mVs, nullptr, 0);
}
void DeviceContext::setPixelShader(const PixelShaderPtr& pixel_shader)
{
mDeviceContext->PSSetShader(pixel_shader->mPs, nullptr, 0);
}
void DeviceContext::setTexture(const VertexShaderPtr& vertex_shader, const TexturePtr* texture, unsigned int num_textures)
{
ID3D11ShaderResourceView* list_res[32];
ID3D11SamplerState* list_sampler[32];
for (UINT i = 0; i < num_textures; i++)
{
list_res[i] = texture[i]->mShaderResView;
list_sampler[i] = texture[i]->mSamplerState;
}
mDeviceContext->VSSetShaderResources(0, num_textures, list_res);
mDeviceContext->VSSetSamplers(0, num_textures, list_sampler);
}
void DeviceContext::setTexture(const PixelShaderPtr& pixel_shader, const TexturePtr* texture, unsigned int num_textures)
{
ID3D11ShaderResourceView* list_res[32];
ID3D11SamplerState* list_sampler[32];
for (UINT i = 0; i < num_textures; i++)
{
list_res[i] = texture[i]->mShaderResView;
list_sampler[i] = texture[i]->mSamplerState;
}
mDeviceContext->PSSetShaderResources(0, num_textures, list_res);
mDeviceContext->PSSetSamplers(0, num_textures, list_sampler);
}
void DeviceContext::setConstantBuffer(const VertexShaderPtr& vertex_shader, const ConstantBufferPtr& buffer)
{
mDeviceContext->VSSetConstantBuffers(0, 1, &buffer->mBuffer);
}
void DeviceContext::setConstantBuffer(const PixelShaderPtr& pixel_shader, const ConstantBufferPtr& buffer)
{
mDeviceContext->PSSetConstantBuffers(0, 1, &buffer->mBuffer);
}
void DeviceContext::setIndexBuffer(const IndexBufferPtr& index_buffer)
{
mDeviceContext->IASetIndexBuffer(index_buffer->mBuffer, DXGI_FORMAT_R32_UINT, 0);
}
void DeviceContext::setViewportSize(UINT width, UINT height)
{
D3D11_VIEWPORT vp = {};
vp.Width = (FLOAT)width;
vp.Height = (FLOAT)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
mDeviceContext->RSSetViewports(1, &vp);
}