@@ -17,15 +17,11 @@ using namespace core;
17
17
using namespace asset ;
18
18
using namespace video ;
19
19
20
- constexpr const char * SHADER_OVERRIDES[2 ] =
21
- {
20
+ constexpr const char * SHADER_OVERRIDES =
22
21
R"(
23
22
#define _NBL_VERT_INPUTS_DEFINED_
24
- #define _NBL_VERT_MAIN_DEFINED_
25
- #define _NBL_VERT_OUTPUTS_DEFINED_
26
- #define _NBL_VERT_SET1_BINDINGS_DEFINED_
27
23
28
- #define VirtualAttribute_t uvec2
24
+ // #define nbl_glsl_VirtualAttribute_t uint
29
25
30
26
vec4 nbl_glsl_decodeRGB10A2_UNORM(in uint x)
31
27
{
@@ -66,34 +62,36 @@ layout(set = 0, binding = 5) readonly buffer VertexNormalOffsetTable
66
62
int dataOffsetTable[];
67
63
} vertexNormal;
68
64
69
- layout(push_constant, row_major) uniform PushConstants
70
- {
71
- mat4 vp;
72
- } pc;
73
-
74
- //outputs
75
- layout(location = 0) out vec3 normal;
76
- layout(location = 1) out vec2 uv;
65
+ #define _NBL_BASIC_VTX_ATTRIB_FETCH_FUCTIONS_DEFINED_
66
+ #define _NBL_POS_FETCH_FUNCTION_DEFINED
67
+ #define _NBL_UV_FETCH_FUNCTION_DEFINED
68
+ #define _NBL_NORMAL_FETCH_FUNCTION_DEFINED
77
69
78
- )" ,
70
+ //vec4 nbl_glsl_readAttrib(uint offset)
71
+ //ivec4 nbl_glsl_readAttrib(uint offset)
72
+ //uvec4 nbl_glsl_readAttrib(uint offset)
73
+ //vec3 nbl_glsl_readAttrib(uint offset)
74
+ //..
79
75
80
- R"(
81
- void main()
76
+ vec3 nbl_glsl_fetchVtxPos()
82
77
{
83
78
int vtxPosOffset = int(gl_VertexIndex) + vertexPosition.dataOffsetTable[gl_DrawID];
84
- vec3 pos = texelFetch(MeshPackedData_R32G32B32_SFLOAT, vtxPosOffset).xyz;
85
- gl_Position = nbl_glsl_pseudoMul4x4with3x1(pc.vp, pos);
86
-
79
+ return texelFetch(MeshPackedData_R32G32B32_SFLOAT, vtxPosOffset).xyz;
80
+ }
81
+
82
+ vec2 nbl_glsl_fetchVtxUV()
83
+ {
87
84
int vtxUVOffset = int(gl_VertexIndex) + vertexUV.dataOffsetTable[gl_DrawID];
88
- uv = texelFetch(MeshPackedData_R32G32_SFLOAT, vtxUVOffset).xy;
85
+ return texelFetch(MeshPackedData_R32G32_SFLOAT, vtxUVOffset).xy;
86
+ }
89
87
88
+ vec3 nbl_glsl_fetchVtxNormal()
89
+ {
90
90
int vtxNormOffset = int(gl_VertexIndex) + vertexNormal.dataOffsetTable[gl_DrawID];
91
- normal = normalize(nbl_glsl_decodeRGB10A2_SNORM(texelFetch(MeshPackedData_A2B10G10R10_SNORM_PACK32, vtxNormOffset).x).xyz);
91
+ return normalize(nbl_glsl_decodeRGB10A2_SNORM(texelFetch(MeshPackedData_A2B10G10R10_SNORM_PACK32, vtxNormOffset).x).xyz);
92
92
}
93
- )"
94
93
95
- }
96
- ;
94
+ )" ;
97
95
98
96
struct DataOffsetTable
99
97
{
@@ -112,11 +110,9 @@ core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedVertexShader(
112
110
113
111
size_t firstNewlineAfterVersion = resultShaderSrc.find (" \n " , resultShaderSrc.find (" #version " ));
114
112
115
- const std::string buffersDef = SHADER_OVERRIDES[0 ];
116
- const std::string mainDef = SHADER_OVERRIDES[1 ];
113
+ const std::string customSrcCode = SHADER_OVERRIDES;
117
114
118
- resultShaderSrc.insert (firstNewlineAfterVersion, buffersDef);
119
- resultShaderSrc += mainDef;
115
+ resultShaderSrc.insert (firstNewlineAfterVersion, customSrcCode);
120
116
resultShaderSrc.replace (resultShaderSrc.find (" #version 430 core" ), sizeof (" #version 430 core" ), " #version 460 core\n " );
121
117
122
118
auto unspecNew = core::make_smart_refctd_ptr<asset::ICPUShader>(resultShaderSrc.c_str ());
@@ -141,7 +137,7 @@ struct DrawIndexedIndirectInput
141
137
};
142
138
143
139
144
- void packMeshBuffersV2 (video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*>& meshBuffers, DrawIndexedIndirectInput& output, std::array<DataOffsetTable, 3 >& offsetTable)
140
+ void packMeshBuffers (video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*>& meshBuffers, DrawIndexedIndirectInput& output, std::array<DataOffsetTable, 3 >& offsetTable)
145
141
{
146
142
using MeshPacker = CCPUMeshPackerV2<DrawElementsIndirectCommand_t>;
147
143
@@ -178,7 +174,7 @@ void packMeshBuffersV2(video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*
178
174
output.maxCount = offsetTableSz;
179
175
output.stride = sizeof (DrawElementsIndirectCommand_t);
180
176
181
- mp.generateGLSLBufferDefinitions (0u );
177
+ auto glsl = mp.generateGLSLBufferDefinitions (0u );
182
178
183
179
/* DrawElementsIndirectCommand_t* mdiPtr = static_cast<DrawElementsIndirectCommand_t*>(packerDataStore.MDIDataBuffer->getPointer());
184
180
uint16_t* idxBuffPtr = static_cast<uint16_t*>(packerDataStore.indexBuffer->getPointer());
@@ -202,49 +198,59 @@ void packMeshBuffersV2(video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*
202
198
offsetTable[0 ].offsetBuffer .buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem (sizeof (uint32_t ) * offsetTableLocal.size (), static_cast <void *>(offsetTableLocal.data ()));
203
199
204
200
for (uint32_t i = 0u ; i < offsetTableLocal.size (); i++)
205
- offsetTableLocal[i] = cdot[i].attribOffset [2 ];
201
+ offsetTableLocal[i] = cdot[i].attribOffset [1 ];
206
202
207
203
offsetTable[1 ].offsetBuffer .offset = 0u ;
208
204
offsetTable[1 ].offsetBuffer .buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem (sizeof (uint32_t ) * offsetTableLocal.size (), static_cast <void *>(offsetTableLocal.data ()));
209
205
210
206
for (uint32_t i = 0u ; i < offsetTableLocal.size (); i++)
211
- offsetTableLocal[i] = cdot[i].attribOffset [3 ];
207
+ offsetTableLocal[i] = cdot[i].attribOffset [2 ];
212
208
213
209
offsetTable[2 ].offsetBuffer .offset = 0u ;
214
210
offsetTable[2 ].offsetBuffer .buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem (sizeof (uint32_t ) * offsetTableLocal.size (), static_cast <void *>(offsetTableLocal.data ()));
215
211
}
216
212
217
213
void setPipeline (IVideoDriver* driver, ICPUSpecializedShader* vs, ICPUSpecializedShader* fs,
218
- core::smart_refctd_ptr<IGPUBuffer>& vtxBuffer, std::array<DataOffsetTable, 3 >& dataOffsetBuffers,
219
- core::smart_refctd_ptr<IGPUDescriptorSet>& outputGPUDescriptorSet,
214
+ core::smart_refctd_ptr<IGPUBuffer>& vtxBuffer, core::smart_refctd_ptr<IGPUBuffer>& outputUBO, std::array<DataOffsetTable, 3 >& dataOffsetBuffers,
215
+ core::smart_refctd_ptr<IGPUDescriptorSet>& outputGPUDescriptorSet0,
216
+ core::smart_refctd_ptr<IGPUDescriptorSet>& outputGPUDescriptorSet1,
220
217
core::smart_refctd_ptr<IGPURenderpassIndependentPipeline>& outputGpuPipeline)
221
218
{
222
219
ICPUSpecializedShader* cpuShaders[2 ] = { vs, fs };
223
220
auto gpuShaders = driver->getGPUObjectsFromAssets (cpuShaders, cpuShaders + 2 );
224
221
225
- core::smart_refctd_ptr<IGPUDescriptorSetLayout> dsLayout;
222
+ core::smart_refctd_ptr<IGPUDescriptorSetLayout> ds0Layout;
223
+ core::smart_refctd_ptr<IGPUDescriptorSetLayout> ds1Layout;
226
224
{
227
225
IGPUDescriptorSetLayout::SBinding b[6 ];
228
226
b[0 ].binding = 0u ; b[1 ].binding = 1u ; b[2 ].binding = 2u ; b[3 ].binding = 3u ; b[4 ].binding = 4u ; b[5 ].binding = 5u ;
229
227
b[0 ].type = b[1 ].type = b[2 ].type = EDT_UNIFORM_TEXEL_BUFFER;
230
228
b[3 ].type = b[4 ].type = b[5 ].type = EDT_STORAGE_BUFFER;
231
229
b[0 ].stageFlags = b[1 ].stageFlags = b[2 ].stageFlags = b[3 ].stageFlags = b[4 ].stageFlags = b[5 ].stageFlags = ISpecializedShader::ESS_VERTEX;
232
230
b[0 ].count = b[1 ].count = b[2 ].count = b[3 ].count = b[4 ].count = b[5 ].count = 1u ;
233
- dsLayout = driver->createGPUDescriptorSetLayout (b, b + 6u );
231
+ ds0Layout = driver->createGPUDescriptorSetLayout (b, b + 6u );
232
+
233
+ IGPUDescriptorSetLayout::SBinding b2;
234
+ b2.binding = 0 ;
235
+ b2.type = EDT_UNIFORM_BUFFER;
236
+ b2.stageFlags = ISpecializedShader::ESS_VERTEX;
237
+ b2.count = 1u ;
238
+ ds1Layout = driver->createGPUDescriptorSetLayout (&b2, &b2 + 1 );
234
239
}
235
240
236
241
asset::SPushConstantRange pcRange = { asset::ISpecializedShader::ESS_VERTEX, 0u , sizeof (core::matrix4SIMD) };
237
- auto pipelineLayout = driver->createGPUPipelineLayout (&pcRange, &pcRange + 1 , core::smart_refctd_ptr (dsLayout ));
242
+ auto pipelineLayout = driver->createGPUPipelineLayout (nullptr , nullptr , core::smart_refctd_ptr (ds0Layout) , core::smart_refctd_ptr (ds1Layout ));
238
243
239
- outputGPUDescriptorSet = driver->createGPUDescriptorSet (std::move (dsLayout));
244
+ outputGPUDescriptorSet0 = driver->createGPUDescriptorSet (std::move (ds0Layout));
245
+ outputGPUDescriptorSet1 = driver->createGPUDescriptorSet (std::move (ds1Layout));
240
246
{
241
247
IGPUDescriptorSet::SWriteDescriptorSet w[6 ];
242
248
w[0 ].arrayElement = w[1 ].arrayElement = w[2 ].arrayElement = w[3 ].arrayElement = w[4 ].arrayElement = w[5 ].arrayElement = 0u ;
243
249
w[0 ].count = w[1 ].count = w[2 ].count = w[3 ].count = w[4 ].count = w[5 ].count = 1u ;
244
250
w[0 ].binding = 0u ; w[1 ].binding = 1u ; w[2 ].binding = 2u ; w[3 ].binding = 3u ; w[4 ].binding = 4u ; w[5 ].binding = 4u ;
245
251
w[0 ].descriptorType = w[1 ].descriptorType = w[2 ].descriptorType = EDT_UNIFORM_TEXEL_BUFFER;
246
252
w[3 ].descriptorType = w[4 ].descriptorType = w[5 ].descriptorType = EDT_STORAGE_BUFFER;
247
- w[0 ].dstSet = w[1 ].dstSet = w[2 ].dstSet = w[3 ].dstSet = w[4 ].dstSet = w[5 ].dstSet = outputGPUDescriptorSet .get ();
253
+ w[0 ].dstSet = w[1 ].dstSet = w[2 ].dstSet = w[3 ].dstSet = w[4 ].dstSet = w[5 ].dstSet = outputGPUDescriptorSet0 .get ();
248
254
249
255
IGPUDescriptorSet::SDescriptorInfo info[6 ];
250
256
@@ -280,8 +286,27 @@ void setPipeline(IVideoDriver* driver, ICPUSpecializedShader* vs, ICPUSpecialize
280
286
w[5 ].info = &info[5 ];
281
287
282
288
driver->updateDescriptorSets (6u , w, 0u , nullptr );
289
+
290
+ IGPUDescriptorSet::SWriteDescriptorSet w2;
291
+ w2.arrayElement = 0u ;
292
+ w2.count = 1u ;
293
+ w2.binding = 0u ;
294
+ w2.descriptorType = EDT_UNIFORM_BUFFER;
295
+ w2.dstSet = outputGPUDescriptorSet1.get ();
296
+
297
+ outputUBO = driver->createDeviceLocalGPUBufferOnDedMem (sizeof (SBasicViewParameters));
298
+
299
+ IGPUDescriptorSet::SDescriptorInfo info2;
300
+ info2.buffer .offset = 0u ;
301
+ info2.buffer .size = outputUBO->getSize ();
302
+ info2.desc = core::smart_refctd_ptr (outputUBO);
303
+ w2.info = &info2;
304
+
305
+ driver->updateDescriptorSets (1u , &w2, 0u , nullptr );
283
306
}
284
307
308
+
309
+
285
310
IGPUSpecializedShader* shaders[2 ] = { gpuShaders->operator [](0 ).get (), gpuShaders->operator [](1 ).get () };
286
311
287
312
outputGpuPipeline = driver->createGPURenderpassIndependentPipeline (
@@ -346,7 +371,9 @@ int main()
346
371
meshBuffers.push_back (mesh_raw->getMeshBufferVector ()[i].get ());
347
372
348
373
core::smart_refctd_ptr<IGPURenderpassIndependentPipeline> gpuPipeline;
349
- core::smart_refctd_ptr<IGPUDescriptorSet> ds;
374
+ core::smart_refctd_ptr<IGPUDescriptorSet> ds0;
375
+ core::smart_refctd_ptr<IGPUDescriptorSet> ds1;
376
+ core::smart_refctd_ptr<IGPUBuffer> ubo;
350
377
DrawIndexedIndirectInput mdiCallParams;
351
378
{
352
379
auto * pipeline = meshBuffers[0 ]->getPipeline ();
@@ -356,9 +383,9 @@ int main()
356
383
ICPUSpecializedShader* fs = IAsset::castDown<ICPUSpecializedShader>(am->getAsset (" ../shader.frag" , lp).getContents ().begin ()->get ());
357
384
std::array<DataOffsetTable, 3 > offsetTable;
358
385
359
- packMeshBuffersV2 (driver, meshBuffers, mdiCallParams, offsetTable);
386
+ packMeshBuffers (driver, meshBuffers, mdiCallParams, offsetTable);
360
387
361
- setPipeline (driver, vs.get (), fs, mdiCallParams.vtxBuffer .buffer , offsetTable, ds , gpuPipeline);
388
+ setPipeline (driver, vs.get (), fs, mdiCallParams.vtxBuffer .buffer , ubo, offsetTable, ds0, ds1 , gpuPipeline);
362
389
}
363
390
364
391
// ! we want to move around the scene and view it from different angles
@@ -374,16 +401,24 @@ int main()
374
401
uint64_t lastFPSTime = 0 ;
375
402
while (device->run () && receiver.keepOpen ())
376
403
{
404
+ video::IGPUDescriptorSet* ds[]{ ds0.get (), ds1.get () };
377
405
driver->bindGraphicsPipeline (gpuPipeline.get ());
378
- driver->bindDescriptorSets (video::EPBP_GRAPHICS, gpuPipeline->getLayout (), 0u , 1u , &ds. get () , nullptr );
406
+ driver->bindDescriptorSets (video::EPBP_GRAPHICS, gpuPipeline->getLayout (), 0u , 2u , ds , nullptr );
379
407
380
408
driver->beginScene (true , true , video::SColor (255 , 0 , 0 , 255 ));
381
409
382
410
// ! This animates (moves) the camera and sets the transforms
383
411
camera->OnAnimate (std::chrono::duration_cast<std::chrono::milliseconds>(device->getTimer ()->getTime ()).count ());
384
412
camera->render ();
385
413
386
- driver->pushConstants (gpuPipeline->getLayout (), asset::ISpecializedShader::ESS_VERTEX, 0u , sizeof (core::matrix4SIMD), camera->getConcatenatedMatrix ().pointer ());
414
+ SBasicViewParameters uboData;
415
+
416
+ memcpy (uboData.MVP , camera->getConcatenatedMatrix ().pointer (), sizeof (core::matrix4SIMD));
417
+ memcpy (uboData.MV , camera->getViewMatrix ().pointer (), sizeof (core::matrix3x4SIMD));
418
+ memcpy (uboData.NormalMat , camera->getViewMatrix ().pointer (), sizeof (core::matrix3x4SIMD));
419
+
420
+ driver->updateBufferRangeViaStagingBuffer (ubo.get (), 0u , sizeof (SBasicViewParameters), &uboData);
421
+
387
422
SBufferBinding<IGPUBuffer> vtxBufferBindings[IGPUMeshBuffer::MAX_ATTR_BUF_BINDING_COUNT];
388
423
vtxBufferBindings[0 ] = mdiCallParams.vtxBuffer ;
389
424
driver->drawIndexedIndirect (vtxBufferBindings, mdiCallParams.mode , mdiCallParams.indexType , mdiCallParams.idxBuff .get (), mdiCallParams.indirectDrawBuff .get (), mdiCallParams.offset , mdiCallParams.maxCount , mdiCallParams.stride );
0 commit comments