-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputAssemblerStage.cpp
147 lines (119 loc) · 3.27 KB
/
InputAssemblerStage.cpp
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
138
139
140
141
142
143
144
145
146
147
#include "Pipeline.h"
#include "InputAssemblerStage.h"
#include <cassert>
InputAssemblerStage::InputAssemblerStage(ID3D11DeviceContext* context)
: InputAssemblerStage(context, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT)
{
}
InputAssemblerStage::InputAssemblerStage(ID3D11DeviceContext* context, uint32_t count)
: context_(context)
, buffer_count_(count)
{
assert(context_ != nullptr);
vertex_buffers_ = new VertexBufferContainerType(buffer_count_);
strides_ = new std::vector<uint32_t>(buffer_count_);
offsets_ = new std::vector<uint32_t>(buffer_count_);
}
InputAssemblerStage::~InputAssemblerStage()
{
delete vertex_buffers_;
delete strides_;
delete offsets_;
}
void InputAssemblerStage::SetVertexBuffer(uint32_t slot, ID3D11Buffer* buffer, uint32_t stride, uint32_t offset)
{
if (!IsInitialized())
return;
if (buffer == nullptr)
return;
if (slot >= buffer_count_)
return;
RemoveBuffers();
(*vertex_buffers_)[slot] = buffer;
(*strides_)[slot] = stride;
(*offsets_)[slot] = offset;
start_slot_ = slot;
slot_count_ = 1;
Updated(true);
}
void InputAssemblerStage::SetVertexBuffers(uint32_t start_slot, uint32_t count, ID3D11Buffer** buffers,
uint32_t* strides, uint32_t* offsets)
{
if (!IsInitialized())
return;
if (buffers == nullptr || strides == nullptr || offsets == nullptr)
return;
if (start_slot + count >= buffer_count_)
return;
RemoveBuffers();
const auto buffer_begin = vertex_buffers_->begin() + start_slot;
for (uint32_t i = 0; i < count; ++i)
{
*(buffer_begin + i) = buffers[i];
}
const auto stride_begin = strides_->begin() + start_slot;
for (uint32_t i = 0; i < count; ++i)
{
*(stride_begin + i) = strides[i];
}
const auto offset_begin = offsets_->begin() + start_slot;
for (uint32_t i = 0; i < count; ++i)
{
*(offset_begin + i) = offsets[i];
}
start_slot_ = start_slot;
slot_count_ = count;
Updated(true);
}
void InputAssemblerStage::SetIndexBuffer(ID3D11Buffer *buffer, DXGI_FORMAT format, uint32_t offset)
{
if (!IsInitialized())
return;
if (buffer == nullptr)
return;
index_buffer_ = buffer;
index_format_ = format;
index_offset_ = offset;
Updated(true);
}
void InputAssemblerStage::SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY topology)
{
if (!IsInitialized())
return;
topology_ = topology;
Updated(true);
}
void InputAssemblerStage::SetInputLayout(ID3D11InputLayout *layout)
{
if (!IsInitialized())
return;
input_layout_ = layout;
Updated(true);
}
void InputAssemblerStage::RemoveBuffers()
{
for (auto& buffer : *vertex_buffers_)
{
buffer = nullptr;
}
for (auto& stride : *strides_)
{
stride = 0;
}
for (auto& offset : *offsets_)
{
offset = 0;
}
index_buffer_ = nullptr;
Updated(true);
}
void InputAssemblerStage::ApplyState()
{
if (!IsUpdated())
return;
context_->IASetVertexBuffers(start_slot_, slot_count_, &(*vertex_buffers_)[0], &(*strides_)[0], &(*offsets_)[0]);
context_->IASetIndexBuffer(index_buffer_, index_format_, index_offset_);
context_->IASetPrimitiveTopology(topology_);
context_->IASetInputLayout(input_layout_);
Updated(false);
}