Skip to content

Commit be58804

Browse files
committed
New vertex fetching method
1 parent 05eb5c3 commit be58804

File tree

3 files changed

+102
-53
lines changed

3 files changed

+102
-53
lines changed

examples_tests/41.VisibilityBuffer/main.cpp

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ using namespace core;
1717
using namespace asset;
1818
using namespace video;
1919

20-
constexpr const char* SHADER_OVERRIDES[2] =
21-
{
20+
constexpr const char* SHADER_OVERRIDES =
2221
R"(
2322
#define _NBL_VERT_INPUTS_DEFINED_
24-
#define _NBL_VERT_MAIN_DEFINED_
25-
#define _NBL_VERT_OUTPUTS_DEFINED_
26-
#define _NBL_VERT_SET1_BINDINGS_DEFINED_
2723
28-
#define VirtualAttribute_t uvec2
24+
//#define nbl_glsl_VirtualAttribute_t uint
2925
3026
vec4 nbl_glsl_decodeRGB10A2_UNORM(in uint x)
3127
{
@@ -66,34 +62,36 @@ layout(set = 0, binding = 5) readonly buffer VertexNormalOffsetTable
6662
int dataOffsetTable[];
6763
} vertexNormal;
6864
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
7769
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+
//..
7975
80-
R"(
81-
void main()
76+
vec3 nbl_glsl_fetchVtxPos()
8277
{
8378
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+
{
8784
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+
}
8987
88+
vec3 nbl_glsl_fetchVtxNormal()
89+
{
9090
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);
9292
}
93-
)"
9493
95-
}
96-
;
94+
)";
9795

9896
struct DataOffsetTable
9997
{
@@ -112,11 +110,9 @@ core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedVertexShader(
112110

113111
size_t firstNewlineAfterVersion = resultShaderSrc.find("\n", resultShaderSrc.find("#version "));
114112

115-
const std::string buffersDef = SHADER_OVERRIDES[0];
116-
const std::string mainDef = SHADER_OVERRIDES[1];
113+
const std::string customSrcCode = SHADER_OVERRIDES;
117114

118-
resultShaderSrc.insert(firstNewlineAfterVersion, buffersDef);
119-
resultShaderSrc += mainDef;
115+
resultShaderSrc.insert(firstNewlineAfterVersion, customSrcCode);
120116
resultShaderSrc.replace(resultShaderSrc.find("#version 430 core"), sizeof("#version 430 core"), "#version 460 core\n");
121117

122118
auto unspecNew = core::make_smart_refctd_ptr<asset::ICPUShader>(resultShaderSrc.c_str());
@@ -141,7 +137,7 @@ struct DrawIndexedIndirectInput
141137
};
142138

143139

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)
145141
{
146142
using MeshPacker = CCPUMeshPackerV2<DrawElementsIndirectCommand_t>;
147143

@@ -178,7 +174,7 @@ void packMeshBuffersV2(video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*
178174
output.maxCount = offsetTableSz;
179175
output.stride = sizeof(DrawElementsIndirectCommand_t);
180176

181-
mp.generateGLSLBufferDefinitions(0u);
177+
auto glsl = mp.generateGLSLBufferDefinitions(0u);
182178

183179
/*DrawElementsIndirectCommand_t* mdiPtr = static_cast<DrawElementsIndirectCommand_t*>(packerDataStore.MDIDataBuffer->getPointer());
184180
uint16_t* idxBuffPtr = static_cast<uint16_t*>(packerDataStore.indexBuffer->getPointer());
@@ -202,49 +198,59 @@ void packMeshBuffersV2(video::IVideoDriver* driver, core::vector<ICPUMeshBuffer*
202198
offsetTable[0].offsetBuffer.buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(sizeof(uint32_t) * offsetTableLocal.size(), static_cast<void*>(offsetTableLocal.data()));
203199

204200
for (uint32_t i = 0u; i < offsetTableLocal.size(); i++)
205-
offsetTableLocal[i] = cdot[i].attribOffset[2];
201+
offsetTableLocal[i] = cdot[i].attribOffset[1];
206202

207203
offsetTable[1].offsetBuffer.offset = 0u;
208204
offsetTable[1].offsetBuffer.buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(sizeof(uint32_t) * offsetTableLocal.size(), static_cast<void*>(offsetTableLocal.data()));
209205

210206
for (uint32_t i = 0u; i < offsetTableLocal.size(); i++)
211-
offsetTableLocal[i] = cdot[i].attribOffset[3];
207+
offsetTableLocal[i] = cdot[i].attribOffset[2];
212208

213209
offsetTable[2].offsetBuffer.offset = 0u;
214210
offsetTable[2].offsetBuffer.buffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(sizeof(uint32_t) * offsetTableLocal.size(), static_cast<void*>(offsetTableLocal.data()));
215211
}
216212

217213
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,
220217
core::smart_refctd_ptr<IGPURenderpassIndependentPipeline>& outputGpuPipeline)
221218
{
222219
ICPUSpecializedShader* cpuShaders[2] = { vs, fs };
223220
auto gpuShaders = driver->getGPUObjectsFromAssets(cpuShaders, cpuShaders + 2);
224221

225-
core::smart_refctd_ptr<IGPUDescriptorSetLayout> dsLayout;
222+
core::smart_refctd_ptr<IGPUDescriptorSetLayout> ds0Layout;
223+
core::smart_refctd_ptr<IGPUDescriptorSetLayout> ds1Layout;
226224
{
227225
IGPUDescriptorSetLayout::SBinding b[6];
228226
b[0].binding = 0u; b[1].binding = 1u; b[2].binding = 2u; b[3].binding = 3u; b[4].binding = 4u; b[5].binding = 5u;
229227
b[0].type = b[1].type = b[2].type = EDT_UNIFORM_TEXEL_BUFFER;
230228
b[3].type = b[4].type = b[5].type = EDT_STORAGE_BUFFER;
231229
b[0].stageFlags = b[1].stageFlags = b[2].stageFlags = b[3].stageFlags = b[4].stageFlags = b[5].stageFlags = ISpecializedShader::ESS_VERTEX;
232230
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);
234239
}
235240

236241
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));
238243

239-
outputGPUDescriptorSet = driver->createGPUDescriptorSet(std::move(dsLayout));
244+
outputGPUDescriptorSet0 = driver->createGPUDescriptorSet(std::move(ds0Layout));
245+
outputGPUDescriptorSet1 = driver->createGPUDescriptorSet(std::move(ds1Layout));
240246
{
241247
IGPUDescriptorSet::SWriteDescriptorSet w[6];
242248
w[0].arrayElement = w[1].arrayElement = w[2].arrayElement = w[3].arrayElement = w[4].arrayElement = w[5].arrayElement = 0u;
243249
w[0].count = w[1].count = w[2].count = w[3].count = w[4].count = w[5].count = 1u;
244250
w[0].binding = 0u; w[1].binding = 1u; w[2].binding = 2u; w[3].binding = 3u; w[4].binding = 4u; w[5].binding = 4u;
245251
w[0].descriptorType = w[1].descriptorType = w[2].descriptorType = EDT_UNIFORM_TEXEL_BUFFER;
246252
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();
248254

249255
IGPUDescriptorSet::SDescriptorInfo info[6];
250256

@@ -280,8 +286,27 @@ void setPipeline(IVideoDriver* driver, ICPUSpecializedShader* vs, ICPUSpecialize
280286
w[5].info = &info[5];
281287

282288
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);
283306
}
284307

308+
309+
285310
IGPUSpecializedShader* shaders[2] = { gpuShaders->operator[](0).get(), gpuShaders->operator[](1).get() };
286311

287312
outputGpuPipeline = driver->createGPURenderpassIndependentPipeline(
@@ -346,7 +371,9 @@ int main()
346371
meshBuffers.push_back(mesh_raw->getMeshBufferVector()[i].get());
347372

348373
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;
350377
DrawIndexedIndirectInput mdiCallParams;
351378
{
352379
auto* pipeline = meshBuffers[0]->getPipeline();
@@ -356,9 +383,9 @@ int main()
356383
ICPUSpecializedShader* fs = IAsset::castDown<ICPUSpecializedShader>(am->getAsset("../shader.frag", lp).getContents().begin()->get());
357384
std::array<DataOffsetTable, 3> offsetTable;
358385

359-
packMeshBuffersV2(driver, meshBuffers, mdiCallParams, offsetTable);
386+
packMeshBuffers(driver, meshBuffers, mdiCallParams, offsetTable);
360387

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);
362389
}
363390

364391
//! we want to move around the scene and view it from different angles
@@ -374,16 +401,24 @@ int main()
374401
uint64_t lastFPSTime = 0;
375402
while (device->run() && receiver.keepOpen())
376403
{
404+
video::IGPUDescriptorSet* ds[]{ ds0.get(), ds1.get() };
377405
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);
379407

380408
driver->beginScene(true, true, video::SColor(255, 0, 0, 255));
381409

382410
//! This animates (moves) the camera and sets the transforms
383411
camera->OnAnimate(std::chrono::duration_cast<std::chrono::milliseconds>(device->getTimer()->getTime()).count());
384412
camera->render();
385413

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+
387422
SBufferBinding<IGPUBuffer> vtxBufferBindings[IGPUMeshBuffer::MAX_ATTR_BUF_BINDING_COUNT];
388423
vtxBufferBindings[0] = mdiCallParams.vtxBuffer;
389424
driver->drawIndexedIndirect(vtxBufferBindings, mdiCallParams.mode, mdiCallParams.indexType, mdiCallParams.idxBuff.get(), mdiCallParams.indirectDrawBuff.get(), mdiCallParams.offset, mdiCallParams.maxCount, mdiCallParams.stride);

examples_tests/41.VisibilityBuffer/shader.frag

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#version 460 core
22

3-
layout(location = 0) in vec3 normal;
4-
layout(location = 1) in vec2 uv;
3+
layout (location = 0) in vec3 LocalPos;
4+
layout (location = 1) in vec3 ViewPos;
5+
layout (location = 2) in vec3 Normal;
6+
layout (location = 3) in vec2 UV;
57

68
layout(location = 0) out vec4 color;
79

810
void main()
911
{
10-
vec3 colorTmp = clamp(dot(vec3(0.0, 1.0, 0.0), normal), 0, 1) * vec3(1.0) + vec3(0.2);
12+
vec3 colorTmp = clamp(dot(vec3(0.0, 1.0, 0.0), Normal), 0, 1) * vec3(1.0) + vec3(0.2);
1113
color = vec4(colorTmp, 1.0);
1214

1315
//color = vec4(1.0);

include/nbl/builtin/shader/loader/mtl/vertex_impl.glsl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ layout (location = 3) out vec2 UV;
2424
#include <nbl/builtin/glsl/utils/common.glsl>
2525
#include <nbl/builtin/glsl/utils/transform.glsl>
2626

27+
#ifndef _NBL_BASIC_VTX_ATTRIB_FETCH_FUCTIONS_DEFINED_
28+
#ifndef _NBL_POS_FETCH_FUNCTION_DEFINED
29+
vec3 nbl_glsl_fetchVtxPos() { return vPos; }
30+
#endif
31+
#if !defined(_NBL_UV_FETCH_FUNCTION_DEFINED) && !defined(_NO_UV)
32+
vec2 nbl_glsl_fetchVtxUV() { return vUV; }
33+
#endif
34+
#ifndef _NBL_NORMAL_FETCH_FUNCTION_DEFINED
35+
vec3 nbl_glsl_fetchVtxNormal() { return vNormal; }
36+
#endif
37+
#endif
38+
2739
#ifndef _NBL_VERT_SET1_BINDINGS_DEFINED_
2840
#define _NBL_VERT_SET1_BINDINGS_DEFINED_
2941
layout (set = 1, binding = 0, row_major, std140) uniform UBO
@@ -36,13 +48,13 @@ layout (set = 1, binding = 0, row_major, std140) uniform UBO
3648
#define _NBL_VERT_MAIN_DEFINED_
3749
void main()
3850
{
39-
LocalPos = vPos;
40-
gl_Position = nbl_glsl_pseudoMul4x4with3x1(CamData.params.MVP, vPos);
41-
ViewPos = nbl_glsl_pseudoMul3x4with3x1(CamData.params.MV, vPos);
51+
LocalPos = nbl_glsl_fetchVtxPos();
52+
gl_Position = nbl_glsl_pseudoMul4x4with3x1(CamData.params.MVP, LocalPos);
53+
ViewPos = nbl_glsl_pseudoMul3x4with3x1(CamData.params.MV, LocalPos);
4254
mat3 normalMat = nbl_glsl_SBasicViewParameters_GetNormalMat(CamData.params.NormalMatAndEyePos);
43-
Normal = normalMat*normalize(vNormal);
55+
Normal = normalMat*normalize(nbl_glsl_fetchVtxNormal());
4456
#ifndef _NO_UV
45-
UV = vUV;
57+
UV = nbl_glsl_fetchVtxUV();
4658
#endif
4759
}
4860
#endif //_NBL_VERT_MAIN_DEFINED_

0 commit comments

Comments
 (0)