forked from chaoticbob/tinyrenderers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbuffer.h
210 lines (164 loc) · 5.86 KB
/
cbuffer.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef __cplusplus
#error "C++ is required"
#endif
#ifndef CBUFFER_H
#define CBUFFER_H
#include <cstring>
#include "camera.h"
#include "transform.h"
namespace tr {
/*! @class ConstantBuffer
*/
template <typename ConstantDataT>
class ConstantBuffer {
public:
ConstantDataT data = {};
ConstantBuffer() {}
~ConstantBuffer() {}
uint32_t GetDataSize() const {
uint32_t size = sizeof(data);
return size;
}
const ConstantDataT* GetData() const {
const ConstantDataT* p_data = &data;
return p_data;
}
void Write(void* p_dst_buffer) {
const void* p_src_buffer = GetData();
size_t size = GetDataSize();
assert((size % 4) == 0);
std::memcpy(p_dst_buffer, p_src_buffer, size);
}
};
/*! @struct ViewData
*/
struct ViewTransformData {
float4x4 model_matrix; // float4x4 in HLSL and C++
float4x4 view_matrix; // float4x4 in HLSL and C++
float4x4 projection_matrix; // float4x4 in HLSL and C++
float4x4 model_view_matrix; // float4x4 in HLSL and C++
float4x4 view_projection_matrix; // float4x4 in HLSL and C++
float4x4 model_view_projection_matrix; // float4x4 in HLSL and C++
float3x4 normal_matrix_world_space; // float3x3 in HLSL, float3x4 in C++ to pad for alignment
float3x4 normal_matrix_view_space; // float3x3 in HLSL, float3x4 in C++ to pad for alignment
float4 view_direction; // float3 in HLSL, float4 in C++ to pad for alignment
float4 color; // float3 in HLSL, float4 in C++ to pad for alignment
};
/*! @class ViewTransformBuffer
*/
class ViewTransformBuffer : public ConstantBuffer<ViewTransformData> {
public:
ViewTransformBuffer() {}
~ViewTransformBuffer() {}
void SetTransform(const Transform& transform) {
data.model_matrix = transform.GetModelMatrix();
data.model_view_matrix = data.view_matrix
* data.model_matrix;
data.model_view_projection_matrix = data.projection_matrix
* data.view_matrix
* data.model_matrix;
float3x3 normal = float3x3(glm::transpose(glm::inverse(data.model_matrix)));
data.normal_matrix_world_space[0] = float4(normal[0], 0.0f);
data.normal_matrix_world_space[1] = float4(normal[1], 0.0f);
data.normal_matrix_world_space[2] = float4(normal[2], 0.0f);
normal = float3x3(glm::transpose(glm::inverse(data.model_view_matrix)));
data.normal_matrix_view_space[0] = float4(normal[0], 0.0f);
data.normal_matrix_view_space[1] = float4(normal[1], 0.0f);
data.normal_matrix_view_space[2] = float4(normal[2], 0.0f);
}
void SetCamera(const Camera& camera) {
data.view_matrix = camera.GetViewMatrix();
data.projection_matrix = camera.GetProjectionMatrix();
data.model_view_matrix = data.view_matrix
* data.model_matrix;
data.view_projection_matrix = data.projection_matrix
* data.view_matrix;
data.model_view_projection_matrix = data.projection_matrix
* data.view_matrix
* data.model_matrix;
data.view_direction = float4(camera.GetViewDirection(), 0);
float3x3 normal = float3x3(glm::transpose(glm::inverse(data.model_view_matrix)));
data.normal_matrix_view_space[0] = float4(normal[0], 0.0f);
data.normal_matrix_view_space[1] = float4(normal[1], 0.0f);
data.normal_matrix_view_space[2] = float4(normal[2], 0.0f);
}
void SetColor(const float3& color) {
data.color = float4(color, 0);
}
void SetColor(float r, float g, float b) {
data.color = float4(r, g, b, 0);
}
};
/*! @struct BlinnPhongData
*/
struct BlinnPhongData {
// float3 in HLSL, float4 in C++ to pad for alignment
float4 base_color; // 0.82, 0.67, 0.16
float4 specular_color; // 1.00, 1.00, 1.00
float4 specular_power; // 12.0, 12.0, 12.0
float4 kA; // 0.30, 0.30, 0.30
float4 kD; // 0.50, 0.50, 0.50
float4 kS; // 1.00, 1.00, 1.00
};
/*! @class BlingPhongBuffer
*/
class BlinnPhongBuffer : public ConstantBuffer<BlinnPhongData> {
public:
BlinnPhongBuffer() {}
~BlinnPhongBuffer() {}
void SetBaseColor(const float3& base_color) {
data.base_color = float4(base_color, 0);
}
void SetSpecularColor(const float3& specular_color) {
data.specular_color = float4(specular_color, 0);
}
void SetSpecularPower(const float3& specular_power) {
data.specular_power = float4(specular_power, 0);
}
void SetKA(const float3& kA) {
data.kA = float4(kA, 0);
}
void SetKD(const float3& kD) {
data.kD = float4(kD, 0);
}
void SetKS(const float3& kS) {
data.kS = float4(kS, 0);
}
};
/*! @struct GGXData
*/
struct GGXData {
// float3 in HLSL, float4 in C++ to pad for alignment
float4 base_color; // 0.82, 0.67, 0.16
float4 metallic; // 0.00, 1.00, 0.00
float4 subsurface; // 0.00, 1.00, 0.00
float4 specular; // 0.00, 1.00, 0.50
float4 roughness; // 0.00, 1.00, 0.50
float4 specular_tint; // 0.00, 1.00, 0.00
float4 anisotropic; // 0.00, 1.00, 0.00
float4 sheen; // 0.00, 1.00, 0.00
float4 sheen_tint; // 0.00, 1.00, 0.50
float4 clearcoat; // 0.00, 1.00, 0.00
float4 clearcoat_gloss; // 0.00, 1.00, 1.00
float4 kA; // 0.30, 0.30, 0.30
float4 kD; // 0.50, 0.50, 0.50
float4 kS; // 1.00, 1.00, 1.00
};
/*! @class GGXBuffer
*/
class GGXBuffer : public ConstantBuffer<GGXData> {
public:
GGXBuffer();
~GGXBuffer();
void SetKA(const float3& kA) {
data.kA = float4(kA, 0);
}
void SetKD(const float3& kD) {
data.kD = float4(kD, 0);
}
void SetKS(const float3& kS) {
data.kS = float4(kS, 0);
}
};
} // namespace tr
#endif // CBUFFER_H