-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathrhiresources.cpp
More file actions
631 lines (541 loc) · 20.1 KB
/
rhiresources.cpp
File metadata and controls
631 lines (541 loc) · 20.1 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Copyright (C) 2024 Michael Torrie and the QtAgOpenGPS Dev Team
// SPDX-License-Identifier: GNU General Public License v3.0 or later
//
#include "rhiresources.h"
#include <QFile>
#include <QImage>
#include <QDebug>
#include <rhi/qrhi.h>
#ifdef LOCAL_QML
#define PREFIX "local:"
#else
#define PREFIX ":/AOG"
#endif
// RhiPipeline implementation
void RhiPipeline::destroy()
{
delete pipeline;
delete bindings;
delete uniformBuffer;
pipeline = nullptr;
bindings = nullptr;
uniformBuffer = nullptr;
}
// RhiPipelineSet implementation
void RhiPipelineSet::destroy()
{
delete points;
delete lines;
delete lineStrip;
delete triangles;
delete triangleStrip;
delete bindings;
delete uniformBuffer;
points = nullptr;
lines = nullptr;
lineStrip = nullptr;
triangles = nullptr;
triangleStrip = nullptr;
bindings = nullptr;
uniformBuffer = nullptr;
}
QRhiGraphicsPipeline* RhiPipelineSet::getPipeline(QRhiGraphicsPipeline::Topology topology) const
{
switch (topology) {
case QRhiGraphicsPipeline::Points:
return points;
case QRhiGraphicsPipeline::Lines:
return lines;
case QRhiGraphicsPipeline::LineStrip:
return lineStrip;
case QRhiGraphicsPipeline::Triangles:
return triangles;
case QRhiGraphicsPipeline::TriangleStrip:
return triangleStrip;
case QRhiGraphicsPipeline::TriangleFan:
return triangleStrip; // Fallback - not all backends support TriangleFan
default:
qWarning() << "Unsupported topology:" << topology;
return nullptr;
}
}
// RhiTextureData implementation
void RhiTextureData::destroy()
{
delete texture;
delete sampler;
texture = nullptr;
sampler = nullptr;
}
// RhiResources implementation
RhiResources::RhiResources()
{
m_textures.resize(RHI_TEXTURE_COUNT);
}
RhiResources::~RhiResources()
{
destroy();
}
bool RhiResources::initialize(QRhi *rhi, QRhiRenderPassDescriptor *renderPass)
{
if (m_initialized) {
qWarning() << "RhiResources already initialized";
return false;
}
if (!rhi || !renderPass) {
qWarning() << "Invalid QRhi or render pass descriptor";
return false;
}
m_rhi = rhi;
m_renderPass = renderPass;
qDebug() << "Initializing RhiResources...";
qDebug() << " Backend:" << m_rhi->backend();
qDebug() << " Driver:" << m_rhi->driverInfo();
// Initialize textures first (needed for pipelines that use them)
if (!initializeTextures()) {
qWarning() << "Failed to initialize textures";
destroy();
return false;
}
// Initialize pipelines
if (!initializePipelines()) {
qWarning() << "Failed to initialize pipelines";
destroy();
return false;
}
m_initialized = true;
qDebug() << "RhiResources initialized successfully";
return true;
}
bool RhiResources::initializeWithoutPipelines(QRhi *rhi)
{
if (m_initialized) {
qWarning() << "RhiResources already initialized";
return false;
}
if (!rhi) {
qWarning() << "Invalid QRhi";
return false;
}
m_rhi = rhi;
qDebug() << "Initializing RhiResources (without pipelines)...";
qDebug() << " Backend:" << m_rhi->backend();
qDebug() << " Driver:" << m_rhi->driverInfo();
// Initialize textures first
if (!initializeTextures()) {
qWarning() << "Failed to initialize textures";
destroy();
return false;
}
// Mark as initialized even without pipelines
m_initialized = true;
qDebug() << "RhiResources initialized (pipelines deferred)";
return true;
}
bool RhiResources::ensurePipelines(QRhiRenderPassDescriptor *renderPass)
{
if (!m_rhi) {
qWarning() << "Cannot create pipelines without QRhi instance";
return false;
}
if (!renderPass) {
qWarning() << "Cannot create pipelines without render pass descriptor";
return false;
}
// If pipelines already created, verify they're compatible
if (m_colorPipelineSet.isValid()) {
if (m_renderPass != renderPass) {
qWarning() << "Render pass changed - pipelines may be incompatible!";
qWarning() << "Consider destroying and recreating RhiResources";
}
return true; // Already created
}
qDebug() << "Creating pipelines with render pass descriptor...";
m_renderPass = renderPass;
// Initialize pipelines
if (!initializePipelines()) {
qWarning() << "Failed to initialize pipelines";
return false;
}
qDebug() << "Pipelines created successfully";
return true;
}
void RhiResources::destroy()
{
if (!m_initialized) {
return;
}
qDebug() << "Destroying RhiResources...";
// Destroy pipeline sets
m_colorPipelineSet.destroy();
m_colorsPipelineSet.destroy();
m_texturePipelineSet.destroy();
// Destroy textures
for (auto &tex : m_textures) {
tex.destroy();
}
m_rhi = nullptr;
m_renderPass = nullptr;
m_initialized = false;
}
RhiTextureData &RhiResources::texture(int index)
{
Q_ASSERT(index >= 0 && index < m_textures.size());
return m_textures[index];
}
const RhiTextureData &RhiResources::texture(int index) const
{
Q_ASSERT(index >= 0 && index < m_textures.size());
return m_textures[index];
}
QShader RhiResources::loadShader(const QString &name)
{
QFile f(QString(PREFIX "/shaders/%1.qsb").arg(name));
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open shader file:" << name;
qWarning() << " Path:" << f.fileName();
return QShader();
}
QShader shader = QShader::fromSerialized(f.readAll());
if (!shader.isValid()) {
qWarning() << "Failed to deserialize shader:" << name;
return QShader();
}
qDebug() << " Loaded shader:" << name;
return shader;
}
bool RhiResources::initializePipelines()
{
qDebug() << "Initializing pipelines...";
if (!createColorPipeline()) {
qWarning() << "Failed to create color pipeline";
return false;
}
if (!createColorsPipeline()) {
qWarning() << "Failed to create colors pipeline";
return false;
}
if (!createTexturePipeline()) {
qWarning() << "Failed to create texture pipeline";
return false;
}
qDebug() << " All pipelines created successfully";
return true;
}
bool RhiResources::createColorPipeline()
{
qDebug() << " Creating color pipeline...";
// Create uniform buffer
m_colorPipelineSet.uniformBuffer = m_rhi->newBuffer(
QRhiBuffer::Dynamic,
QRhiBuffer::UniformBuffer,
sizeof(ColorUniforms)
);
if (!m_colorPipelineSet.uniformBuffer->create()) {
qWarning() << "Failed to create color uniform buffer";
return false;
}
// Create shader resource bindings
m_colorPipelineSet.bindings = m_rhi->newShaderResourceBindings();
m_colorPipelineSet.bindings->setBindings({
QRhiShaderResourceBinding::uniformBuffer(
0,
QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
m_colorPipelineSet.uniformBuffer
)
});
if (!m_colorPipelineSet.bindings->create()) {
qWarning() << "Failed to create color shader resource bindings";
return false;
}
// Load shaders
QShader vertShader = loadShader("color_vshader.vert");
QShader fragShader = loadShader("color_fshader.frag");
if (!vertShader.isValid() || !fragShader.isValid()) {
qWarning() << "Failed to load color shaders";
return false;
}
// Vertex input layout (vec3 position)
QRhiVertexInputLayout inputLayout;
inputLayout.setBindings({
{ 3 * sizeof(float) } // Stride for vec3
});
inputLayout.setAttributes({
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 } // binding, location, format, offset
});
// Create pipeline for Lines topology (primary use case for color pipeline)
m_colorPipelineSet.lines = m_rhi->newGraphicsPipeline();
m_colorPipelineSet.lines->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_colorPipelineSet.lines->setVertexInputLayout(inputLayout);
m_colorPipelineSet.lines->setShaderResourceBindings(m_colorPipelineSet.bindings);
m_colorPipelineSet.lines->setRenderPassDescriptor(m_renderPass);
m_colorPipelineSet.lines->setTopology(QRhiGraphicsPipeline::Lines);
m_colorPipelineSet.lines->setDepthTest(true);
m_colorPipelineSet.lines->setDepthWrite(true);
if (!m_colorPipelineSet.lines->create()) {
qWarning() << "Failed to create color lines graphics pipeline";
return false;
}
// Create pipeline for Triangles topology
m_colorPipelineSet.triangles = m_rhi->newGraphicsPipeline();
m_colorPipelineSet.triangles->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_colorPipelineSet.triangles->setVertexInputLayout(inputLayout);
m_colorPipelineSet.triangles->setShaderResourceBindings(m_colorPipelineSet.bindings);
m_colorPipelineSet.triangles->setRenderPassDescriptor(m_renderPass);
m_colorPipelineSet.triangles->setTopology(QRhiGraphicsPipeline::Triangles);
m_colorPipelineSet.triangles->setDepthTest(true);
m_colorPipelineSet.triangles->setDepthWrite(true);
if (!m_colorPipelineSet.triangles->create()) {
qWarning() << "Failed to create color triangles graphics pipeline";
return false;
}
qDebug() << " Color pipeline created";
return true;
}
bool RhiResources::createColorsPipeline()
{
qDebug() << " Creating colors (interpolated) pipeline...";
// Create uniform buffer
m_colorsPipelineSet.uniformBuffer = m_rhi->newBuffer(
QRhiBuffer::Dynamic,
QRhiBuffer::UniformBuffer,
sizeof(ColorsUniforms)
);
if (!m_colorsPipelineSet.uniformBuffer->create()) {
qWarning() << "Failed to create colors uniform buffer";
return false;
}
// Create shader resource bindings
m_colorsPipelineSet.bindings = m_rhi->newShaderResourceBindings();
m_colorsPipelineSet.bindings->setBindings({
QRhiShaderResourceBinding::uniformBuffer(
0,
QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
m_colorsPipelineSet.uniformBuffer
)
});
if (!m_colorsPipelineSet.bindings->create()) {
qWarning() << "Failed to create colors shader resource bindings";
return false;
}
// Load shaders
QShader vertShader = loadShader("colors_vshader.vert");
QShader fragShader = loadShader("colors_fshader.frag");
if (!vertShader.isValid() || !fragShader.isValid()) {
qWarning() << "Failed to load colors shaders";
return false;
}
// Vertex input layout (vec3 position + vec4 color)
QRhiVertexInputLayout inputLayout;
inputLayout.setBindings({
{ 7 * sizeof(float) } // Stride for vec3 + vec4
});
inputLayout.setAttributes({
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, // position
{ 0, 1, QRhiVertexInputAttribute::Float4, 3 * sizeof(float) } // color
});
// Create pipeline for Triangles topology (primary use case for colors pipeline)
m_colorsPipelineSet.triangles = m_rhi->newGraphicsPipeline();
m_colorsPipelineSet.triangles->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_colorsPipelineSet.triangles->setVertexInputLayout(inputLayout);
m_colorsPipelineSet.triangles->setShaderResourceBindings(m_colorsPipelineSet.bindings);
m_colorsPipelineSet.triangles->setRenderPassDescriptor(m_renderPass);
m_colorsPipelineSet.triangles->setTopology(QRhiGraphicsPipeline::Triangles);
m_colorsPipelineSet.triangles->setDepthTest(true);
m_colorsPipelineSet.triangles->setDepthWrite(true);
if (!m_colorsPipelineSet.triangles->create()) {
qWarning() << "Failed to create colors triangles graphics pipeline";
return false;
}
// Create pipeline for TriangleStrip topology
m_colorsPipelineSet.triangleStrip = m_rhi->newGraphicsPipeline();
m_colorsPipelineSet.triangleStrip->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_colorsPipelineSet.triangleStrip->setVertexInputLayout(inputLayout);
m_colorsPipelineSet.triangleStrip->setShaderResourceBindings(m_colorsPipelineSet.bindings);
m_colorsPipelineSet.triangleStrip->setRenderPassDescriptor(m_renderPass);
m_colorsPipelineSet.triangleStrip->setTopology(QRhiGraphicsPipeline::TriangleStrip);
m_colorsPipelineSet.triangleStrip->setDepthTest(true);
m_colorsPipelineSet.triangleStrip->setDepthWrite(true);
if (!m_colorsPipelineSet.triangleStrip->create()) {
qWarning() << "Failed to create colors triangleStrip graphics pipeline";
return false;
}
qDebug() << " Colors pipeline created";
return true;
}
bool RhiResources::createTexturePipeline()
{
qDebug() << " Creating texture pipeline...";
// Create uniform buffer
m_texturePipelineSet.uniformBuffer = m_rhi->newBuffer(
QRhiBuffer::Dynamic,
QRhiBuffer::UniformBuffer,
sizeof(TextureUniforms)
);
if (!m_texturePipelineSet.uniformBuffer->create()) {
qWarning() << "Failed to create texture uniform buffer";
return false;
}
// Create shader resource bindings (texture will be set dynamically per draw)
// For now, use the font texture as default
m_texturePipelineSet.bindings = m_rhi->newShaderResourceBindings();
m_texturePipelineSet.bindings->setBindings({
QRhiShaderResourceBinding::uniformBuffer(
0,
QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
m_texturePipelineSet.uniformBuffer
),
QRhiShaderResourceBinding::sampledTexture(
1,
QRhiShaderResourceBinding::FragmentStage,
m_textures[RHI_FONT].texture,
m_textures[RHI_FONT].sampler
)
});
if (!m_texturePipelineSet.bindings->create()) {
qWarning() << "Failed to create texture shader resource bindings";
return false;
}
// Load shaders
QShader vertShader = loadShader("colortex_vshader.vert");
QShader fragShader = loadShader("colortex_fshader.frag");
if (!vertShader.isValid() || !fragShader.isValid()) {
qWarning() << "Failed to load texture shaders";
return false;
}
// Vertex input layout (vec3 position + vec2 texcoord)
QRhiVertexInputLayout inputLayout;
inputLayout.setBindings({
{ 5 * sizeof(float) } // Stride for vec3 + vec2
});
inputLayout.setAttributes({
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, // position
{ 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) } // texcoord
});
// Blending for textures (especially font rendering)
QRhiGraphicsPipeline::TargetBlend blend;
blend.enable = true;
blend.srcColor = QRhiGraphicsPipeline::SrcAlpha;
blend.dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha;
blend.srcAlpha = QRhiGraphicsPipeline::One;
blend.dstAlpha = QRhiGraphicsPipeline::OneMinusSrcAlpha;
// Create pipeline for Triangles topology (primary use case for texture pipeline)
m_texturePipelineSet.triangles = m_rhi->newGraphicsPipeline();
m_texturePipelineSet.triangles->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_texturePipelineSet.triangles->setVertexInputLayout(inputLayout);
m_texturePipelineSet.triangles->setShaderResourceBindings(m_texturePipelineSet.bindings);
m_texturePipelineSet.triangles->setRenderPassDescriptor(m_renderPass);
m_texturePipelineSet.triangles->setTopology(QRhiGraphicsPipeline::Triangles);
m_texturePipelineSet.triangles->setTargetBlends({ blend });
m_texturePipelineSet.triangles->setDepthTest(true);
m_texturePipelineSet.triangles->setDepthWrite(true);
if (!m_texturePipelineSet.triangles->create()) {
qWarning() << "Failed to create texture triangles graphics pipeline";
return false;
}
// Create pipeline for TriangleStrip topology
m_texturePipelineSet.triangleStrip = m_rhi->newGraphicsPipeline();
m_texturePipelineSet.triangleStrip->setShaderStages({
{ QRhiShaderStage::Vertex, vertShader },
{ QRhiShaderStage::Fragment, fragShader }
});
m_texturePipelineSet.triangleStrip->setVertexInputLayout(inputLayout);
m_texturePipelineSet.triangleStrip->setShaderResourceBindings(m_texturePipelineSet.bindings);
m_texturePipelineSet.triangleStrip->setRenderPassDescriptor(m_renderPass);
m_texturePipelineSet.triangleStrip->setTopology(QRhiGraphicsPipeline::TriangleStrip);
m_texturePipelineSet.triangleStrip->setTargetBlends({ blend });
m_texturePipelineSet.triangleStrip->setDepthTest(true);
m_texturePipelineSet.triangleStrip->setDepthWrite(true);
if (!m_texturePipelineSet.triangleStrip->create()) {
qWarning() << "Failed to create texture triangleStrip graphics pipeline";
return false;
}
qDebug() << " Texture pipeline created";
return true;
}
bool RhiResources::initializeTextures()
{
qDebug() << "Initializing textures...";
// Texture file paths matching glutils.cpp order
struct TextureInfo {
QString path;
RhiTextures index;
};
const TextureInfo textureFiles[] = {
{ PREFIX "/images/textures/floor.png", RHI_FLOOR },
{ PREFIX "/images/textures/Font.png", RHI_FONT },
{ PREFIX "/images/textures/Lift.png", RHI_HYDLIFT },
{ PREFIX "/images/textures/z_Tractor.png", RHI_TRACTOR },
{ PREFIX "/images/textures/z_QuestionMark.png", RHI_QUESTION_MARK },
{ PREFIX "/images/textures/FrontWheels.png", RHI_FRONT_WHEELS },
{ PREFIX "/images/textures/Tractor4WDFront.png", RHI_TRACTOR_4WD_FRONT },
{ PREFIX "/images/textures/Tractor4WDRear.png", RHI_TRACTOR_4WD_REAR },
{ PREFIX "/images/textures/Harvester.png", RHI_HARVESTER },
{ PREFIX "/images/textures/z_Tool.png", RHI_TOOLWHEELS },
{ PREFIX "/images/textures/z_Tire.png", RHI_TIRE }
};
for (const auto &info : textureFiles) {
QImage img(info.path);
if (img.isNull()) {
qWarning() << "Failed to load texture:" << info.path;
return false;
}
// Convert to RGBA8 format
img = img.convertToFormat(QImage::Format_RGBA8888);
// Save font texture dimensions
if (info.index == RHI_FONT) {
m_fontTextureWidth = img.width();
m_fontTextureHeight = img.height();
qDebug() << " Font texture size:" << m_fontTextureWidth << "x" << m_fontTextureHeight;
}
// Create texture
QRhiTexture *tex = m_rhi->newTexture(
QRhiTexture::RGBA8,
img.size(),
1,
QRhiTexture::UsedAsTransferSource
);
if (!tex->create()) {
qWarning() << "Failed to create texture for:" << info.path;
delete tex;
return false;
}
// Create sampler
QRhiSampler *sampler = m_rhi->newSampler(
QRhiSampler::Linear,
QRhiSampler::Linear,
QRhiSampler::None,
QRhiSampler::ClampToEdge,
QRhiSampler::ClampToEdge
);
if (!sampler->create()) {
qWarning() << "Failed to create sampler for:" << info.path;
delete tex;
delete sampler;
return false;
}
// Upload texture data (will be done in first frame)
// For now, just store the image data to be uploaded later
// Note: In actual implementation, you'll need to upload via QRhiResourceUpdateBatch
m_textures[info.index].texture = tex;
m_textures[info.index].sampler = sampler;
qDebug() << " Loaded texture:" << info.path;
}
qDebug() << " All textures loaded successfully";
return true;
}