nxdk_pgraph_tests
A collection of tests for the Xbox nv2a graphics processor
 
Loading...
Searching...
No Matches
vertex_buffer.h
1#ifndef NXDK_PGRAPH_TESTS__VERTEX_BUFFER_H_
2#define NXDK_PGRAPH_TESTS__VERTEX_BUFFER_H_
3
4#include <cstdint>
5#include <vector>
6
7#include "xbox_math_types.h"
8
9using namespace XboxMath;
10
11#define TO_BGRA(float_vals) \
12 (((uint32_t)((float_vals)[3] * 255.0f) << 24) + ((uint32_t)((float_vals)[0] * 255.0f) << 16) + \
13 ((uint32_t)((float_vals)[1] * 255.0f) << 8) + ((uint32_t)((float_vals)[2] * 255.0f)))
14
15#pragma pack(1)
16typedef struct Vertex {
17 float pos[4];
18 float weight;
19 float normal[3];
20 float diffuse[4];
21 float specular[4];
22 float fog_coord;
23 float point_size;
24 float back_diffuse[4];
25 float back_specular[4];
26 float texcoord0[4];
27 float texcoord1[4];
28 float texcoord2[4];
29 float texcoord3[4];
30
31 inline void SetPosition(const float* value) { memcpy(pos, value, sizeof(pos)); }
32
33 inline void SetPosition(float x, float y, float z, float w = 1.0f) {
34 pos[0] = x;
35 pos[1] = y;
36 pos[2] = z;
37 pos[3] = w;
38 }
39
40 inline void SetWeight(float w) { weight = w; }
41
42 inline void SetNormal(const float* value) { memcpy(normal, value, sizeof(normal)); }
43
44 inline void SetNormal(float x, float y, float z) {
45 normal[0] = x;
46 normal[1] = y;
47 normal[2] = z;
48 }
49
50 inline void SetDiffuse(const float* value) { memcpy(diffuse, value, sizeof(diffuse)); }
51
52 inline void SetDiffuse(float r, float g, float b, float a = 1.0f) {
53 diffuse[0] = r;
54 diffuse[1] = g;
55 diffuse[2] = b;
56 diffuse[3] = a;
57 }
58
59 inline void SetSpecular(const float* value) { memcpy(specular, value, sizeof(specular)); }
60
61 inline void SetSpecular(float r, float g, float b, float a = 1.0f) {
62 specular[0] = r;
63 specular[1] = g;
64 specular[2] = b;
65 specular[3] = a;
66 }
67
68 inline void SetFogCoord(float v) { fog_coord = v; }
69 inline void SetPointSize(float v) { point_size = v; }
70
71 inline void SetBackDiffuse(const float* value) { memcpy(back_diffuse, value, sizeof(back_diffuse)); }
72
73 inline void SetBackDiffuse(float r, float g, float b, float a = 1.0f) {
74 back_diffuse[0] = r;
75 back_diffuse[1] = g;
76 back_diffuse[2] = b;
77 back_diffuse[3] = a;
78 }
79
80 inline void SetBackSpecular(const float* value) { memcpy(back_specular, value, sizeof(back_specular)); }
81
82 inline void SetBackSpecular(float r, float g, float b, float a = 1.0f) {
83 back_specular[0] = r;
84 back_specular[1] = g;
85 back_specular[2] = b;
86 back_specular[3] = a;
87 }
88
89 inline void SetTexCoord0(const float* value) { memcpy(texcoord0, value, sizeof(texcoord0)); }
90
91 inline void SetTexCoord0(const float u, const float v) {
92 texcoord0[0] = u;
93 texcoord0[1] = v;
94 }
95
96 inline void SetTexCoord0(const float u, const float v, const float s, const float q) {
97 texcoord0[0] = u;
98 texcoord0[1] = v;
99 texcoord0[2] = s;
100 texcoord0[3] = q;
101 }
102
103 inline void SetTexCoord1(const float* value) { memcpy(texcoord1, value, sizeof(texcoord1)); }
104
105 inline void SetTexCoord1(const float u, const float v) {
106 texcoord1[0] = u;
107 texcoord1[1] = v;
108 }
109
110 inline void SetTexCoord1(const float u, const float v, const float s, const float q) {
111 texcoord1[0] = u;
112 texcoord1[1] = v;
113 texcoord1[2] = s;
114 texcoord1[3] = q;
115 }
116
117 inline void SetTexCoord2(const float* value) { memcpy(texcoord2, value, sizeof(texcoord2)); }
118
119 inline void SetTexCoord2(const float u, const float v) {
120 texcoord2[0] = u;
121 texcoord2[1] = v;
122 }
123
124 inline void SetTexCoord2(const float u, const float v, const float s, const float q) {
125 texcoord2[0] = u;
126 texcoord2[1] = v;
127 texcoord2[2] = s;
128 texcoord2[3] = q;
129 }
130
131 inline void SetTexCoord3(const float* value) { memcpy(texcoord3, value, sizeof(texcoord3)); }
132
133 inline void SetTexCoord3(const float u, const float v) {
134 texcoord3[0] = u;
135 texcoord3[1] = v;
136 }
137
138 inline void SetTexCoord3(const float u, const float v, const float s, const float q) {
139 texcoord3[0] = u;
140 texcoord3[1] = v;
141 texcoord3[2] = s;
142 texcoord3[3] = q;
143 }
144
145 inline void SetDiffuseGrey(float val) { SetDiffuse(val, val, val); }
146
147 void SetDiffuseGrey(float val, float alpha) { SetDiffuse(val, val, val, alpha); }
148
149 inline void SetSpecularGrey(float val) { SetSpecular(val, val, val); }
150
151 void SetSpecularGrey(float val, float alpha) { SetSpecular(val, val, val, alpha); }
152
153 uint32_t GetDiffuseARGB() const { return TO_BGRA(diffuse); }
154
155 uint32_t GetSpecularARGB() const { return TO_BGRA(specular); }
156
157 void Translate(float x, float y, float z, float w);
158} Vertex;
159#pragma pack()
160
161struct Color {
162 Color() = default;
163 Color(float red, float green, float blue, float alpha = 1.0f) : r(red), g(green), b(blue), a(alpha) {}
164
165 void SetRGB(float red, float green, float blue) {
166 r = red;
167 g = green;
168 b = blue;
169 }
170
171 void SetRGBA(float red, float green, float blue, float alpha) {
172 r = red;
173 g = green;
174 b = blue;
175 a = alpha;
176 }
177
178 void SetGrey(float val) {
179 r = val;
180 g = val;
181 b = val;
182 }
183
184 void SetGreyA(float val, float alpha) {
185 r = val;
186 g = val;
187 b = val;
188 a = alpha;
189 }
190
191 uint32_t AsBGRA() const {
192 float vals[4] = {r, g, b, a};
193 return TO_BGRA(vals);
194 }
195
196 float r{0.0f};
197 float g{0.0f};
198 float b{0.0};
199 float a{1.0};
200};
201
202class TestHost;
203
205 public:
206 explicit VertexBuffer(uint32_t num_vertices);
208
209 // Returns a new VertexBuffer containing vertices suitable for rendering as triangles by treating the contents of this
210 // buffer as a triangle strip.
211 [[nodiscard]] std::shared_ptr<VertexBuffer> ConvertFromTriangleStripToTriangles() const;
212
213 Vertex* Lock();
214 void Unlock();
215
216 [[nodiscard]] uint32_t GetNumVertices() const { return num_vertices_; }
217
218 void SetCacheValid(bool valid = true) { cache_valid_ = valid; }
219 [[nodiscard]] bool IsCacheValid() const { return cache_valid_; }
220
221 void Linearize(float texture_width, float texture_height);
222
223 // Defines a triangle with the give 3-element vertices.
224 void DefineTriangleCCW(uint32_t start_index, const float* one, const float* two, const float* three);
225 void DefineTriangleCCW(uint32_t start_index, const float* one, const float* two, const float* three,
226 const float* normal_one, const float* normal_two, const float* normal_three);
227 void DefineTriangleCCW(uint32_t start_index, const float* one, const float* two, const float* three,
228 const Color& one_diffuse, const Color& two_diffuse, const Color& three_diffuse);
229 void DefineTriangleCCW(uint32_t start_index, const float* one, const float* two, const float* three,
230 const float* normal_one, const float* normal_two, const float* normal_three,
231 const Color& diffuse_one, const Color& diffuse_two, const Color& diffuse_three);
232 void DefineTriangle(uint32_t start_index, const float* one, const float* two, const float* three);
233 void DefineTriangle(uint32_t start_index, const float* one, const float* two, const float* three,
234 const float* normal_one, const float* normal_two, const float* normal_three);
235 void DefineTriangle(uint32_t start_index, const float* one, const float* two, const float* three,
236 const Color& diffuse_one, const Color& diffuse_two, const Color& diffuse_three);
237 void DefineTriangle(uint32_t start_index, const float* one, const float* two, const float* three,
238 const float* normal_one, const float* normal_two, const float* normal_three,
239 const Color& diffuse_one, const Color& diffuse_two, const Color& diffuse_three);
240
241 // Defines a quad made of two coplanar triangles (i.e., 6 vertices suitable for rendering as triangle primitives)
242 void DefineBiTriCCW(uint32_t start_index, float left, float top, float right, float bottom);
243 void DefineBiTriCCW(uint32_t start_index, float left, float top, float right, float bottom, float z);
244 void DefineBiTriCCW(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
245 float lr_z, float ur_z);
246 void DefineBiTriCCW(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
247 float lr_z, float ur_z, const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse,
248 const Color& ur_diffuse);
249 void DefineBiTriCCW(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
250 float lr_z, float ur_z, const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse,
251 const Color& ur_diffuse, const Color& ul_specular, const Color& ll_specular,
252 const Color& lr_specular, const Color& ur_specular);
253
254 void DefineBiTri(uint32_t start_index, float left, float top, float right, float bottom);
255 void DefineBiTri(uint32_t start_index, float left, float top, float right, float bottom, float z);
256 void DefineBiTri(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
257 float lr_z, float ur_z);
258 void DefineBiTri(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
259 float lr_z, float ur_z, const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse,
260 const Color& ur_diffuse);
261 void DefineBiTri(uint32_t start_index, float left, float top, float right, float bottom, float ul_z, float ll_z,
262 float lr_z, float ur_z, const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse,
263 const Color& ur_diffuse, const Color& ul_specular, const Color& ll_specular,
264 const Color& lr_specular, const Color& ur_specular);
265
266 inline void DefineBiTri(uint32_t start_index, const vector_t ul, const vector_t ll, const vector_t lr,
267 const vector_t ur) {
268 Color diffuse(1.0, 1.0, 1.0, 1.0);
269 Color specular(1.0, 1.0, 1.0, 1.0);
270 DefineBiTri(start_index, ul, ll, lr, ur, diffuse, diffuse, diffuse, diffuse, specular, specular, specular,
271 specular);
272 }
273
274 inline void DefineBiTri(uint32_t start_index, const vector_t ul, const vector_t ll, const vector_t lr,
275 const vector_t ur, const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse,
276 const Color& ur_diffuse) {
277 Color specular(1.0, 1.0, 1.0, 1.0);
278 DefineBiTri(start_index, ul, ll, lr, ur, ul_diffuse, ll_diffuse, lr_diffuse, ur_diffuse, specular, specular,
279 specular, specular);
280 }
281
282 void DefineBiTri(uint32_t start_index, const vector_t ul, const vector_t ll, const vector_t lr, const vector_t ur,
283 const Color& ul_diffuse, const Color& ll_diffuse, const Color& lr_diffuse, const Color& ur_diffuse,
284 const Color& ul_specular, const Color& ll_specular, const Color& lr_specular,
285 const Color& ur_specular);
286
287 void SetDiffuse(uint32_t vertex_index, const Color& color);
288 void SetSpecular(uint32_t vertex_index, const Color& color);
289
290 inline void SetPositionIncludesW(bool enabled = true) { position_count_ = enabled ? 4 : 3; }
291 inline void SetTexCoord0Count(uint32_t val) { tex0_coord_count_ = val; }
292 inline void SetTexCoord1Count(uint32_t val) { tex1_coord_count_ = val; }
293 inline void SetTexCoord2Count(uint32_t val) { tex2_coord_count_ = val; }
294 inline void SetTexCoord3Count(uint32_t val) { tex3_coord_count_ = val; }
295
296 void Translate(float x, float y, float z, float w = 0.0f);
297
298 private:
299 friend class TestHost;
300
301 uint32_t num_vertices_;
302 Vertex* linear_vertex_buffer_ = nullptr; // texcoords 0 to kFramebufferWidth/kFramebufferHeight
303 Vertex* normalized_vertex_buffer_ = nullptr; // texcoords normalized 0 to 1
304
305 // Number of components in the vertex position (3 or 4).
306 uint32_t position_count_ = 3;
307
308 // Number of components in the vertex texcoord fields.
309 uint32_t tex0_coord_count_ = 2;
310 uint32_t tex1_coord_count_ = 2;
311 uint32_t tex2_coord_count_ = 2;
312 uint32_t tex3_coord_count_ = 2;
313
314 bool cache_valid_{false}; // Indicates whether the HW should be forced to reload this buffer.
315};
316
317#endif // NXDK_PGRAPH_TESTS__VERTEX_BUFFER_H_
Definition test_host.h:47
Definition vertex_buffer.h:204
Definition vertex_buffer.h:161
Definition vertex_buffer.h:16