Skip to content

Commit d0274d9

Browse files
authored
Merge branch 'master' into ali_lib_shader2
2 parents 1abda13 + 07172eb commit d0274d9

File tree

11 files changed

+352
-24
lines changed

11 files changed

+352
-24
lines changed

62_CAD/DrawResourcesFiller.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ struct DrawResourcesFiller
6666
void setGlyphMSDFTextureFunction(const GetGlyphMSDFTextureFunc& func);
6767
void setHatchFillMSDFTextureFunction(const GetHatchFillPatternMSDFTextureFunc& func);
6868

69+
// TODO[Przemek]: try to draft up a `CTriangleMesh` Class in it's own header (like CPolyline), simplest form is basically two cpu buffers (1 array of uint index buffer, 1 array of float64_t3 vertexBuffer)
70+
// TODO[Przemek]: Then have a `drawMesh` function here similar to drawXXX's below, this will fit both vertex and index buffer in the `geometryBuffer`.
71+
// take a `SIntendedSubmitInfo` like others, but don't use it as I don't want you to handle anything regarding autoSubmit
72+
// somehow retrieve or calculate the geometry buffer offsets of your vertex and index buffer to be used outside for binding purposes
73+
6974
//! this function fills buffers required for drawing a polyline and submits a draw through provided callback when there is not enough memory.
7075
void drawPolyline(const CPolylineBase& polyline, const LineStyleInfo& lineStyleInfo, SIntendedSubmitInfo& intendedNextSubmit);
7176

@@ -193,6 +198,10 @@ struct DrawResourcesFiller
193198

194199
uint32_t addLineStyle_SubmitIfNeeded(const LineStyleInfo& lineStyle, SIntendedSubmitInfo& intendedNextSubmit);
195200

201+
// TODO[Przemek]: Read after reading the fragment shader comments and having a basic understanding of the relationship between "mainObject" and our programmable blending resolve:
202+
// Use `addMainObject_SubmitIfNeeded` to push your single mainObject you'll be using for the enitre triangle mesh (this will ensure overlaps between triangles of the same mesh is resolved correctly)
203+
// Delete comment when you understand this
204+
196205
// [ADVANCED] Do not use this function unless you know what you're doing (It may cause auto submit)
197206
// Never call this function multiple times in a row before indexing it in a drawable, because future auto-submits may invalidate mainObjects, so do them one by one, for example:
198207
// Valid: addMainObject1 --> addXXX(mainObj1) ---> addMainObject2 ---> addXXX(mainObj2) ....

62_CAD/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,8 +1380,17 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
13801380
const uint32_t currentIndexCount = drawResourcesFiller.getDrawObjectCount() * 6u;
13811381
IGPUDescriptorSet* descriptorSets[] = { descriptorSet0.get(), descriptorSet1.get() };
13821382
cb->bindDescriptorSets(asset::EPBP_GRAPHICS, pipelineLayout.get(), 0u, 2u, descriptorSets);
1383+
1384+
// TODO[Przemek]: based on our call bind index buffer you uploaded to part of the `drawResourcesFiller.gpuDrawBuffers.geometryBuffer`
1385+
// Vertices will be pulled based on baseBDAPointer of where you uploaded the vertex + the VertexID in the vertex shader.
13831386
cb->bindIndexBuffer({ .offset = 0u, .buffer = drawResourcesFiller.gpuDrawBuffers.indexBuffer.get() }, asset::EIT_32BIT);
1387+
1388+
// TODO[Przemek]: binding the same pipelie, no need to change.
13841389
cb->bindGraphicsPipeline(graphicsPipeline.get());
1390+
1391+
// TODO[Przemek]: contour settings, height shading settings, base bda pointers will need to be pushed via pushConstants before the draw currently as it's the easiest thing to do.
1392+
1393+
// TODO[Przemek]: draw parameters needs to reflect the mesh involved
13851394
cb->drawIndexed(currentIndexCount, 1u, 0u, 0u, 0u);
13861395
if (fragmentShaderInterlockEnabled)
13871396
{
@@ -1479,6 +1488,9 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
14791488

14801489
void addObjects(SIntendedSubmitInfo& intendedNextSubmit)
14811490
{
1491+
1492+
// TODO[Przemek]: add your own case, you won't call any other drawResourcesFiller function, only drawMesh with your custom made Mesh (for start it can be a single triangle)
1493+
14821494
// we record upload of our objects and if we failed to allocate we submit everything
14831495
if (!intendedNextSubmit.valid())
14841496
{

62_CAD/shaders/globals.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ static_assert(sizeof(CurveBox) == 80u);
260260
NBL_CONSTEXPR uint32_t InvalidRigidSegmentIndex = 0xffffffff;
261261
NBL_CONSTEXPR float InvalidStyleStretchValue = nbl::hlsl::numeric_limits<float>::infinity;
262262

263+
264+
// TODO[Przemek]: we will need something similar to LineStyles but related to heigh shading settings which is user customizable (like LineStyle stipple patterns) and requires upper_bound to figure out the color based on height value.
265+
// We'll discuss that later or what it will be looking like and how it's gonna get passed to our shaders.
266+
263267
// The color parameter is also used for styling non-curve objects such as text glyphs and hatches with solid color
264268
struct LineStyle
265269
{

62_CAD/shaders/main_pipeline/common.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct PrecomputedRootFinder
6262
}
6363
};
6464

65+
// TODO[Przemek]: your triangle mesh passed parameters from vtx to fragment will need to be here (e.g. height).
66+
// As always try to reuse parameters and try not to introduce new ones
6567
struct PSInput
6668
{
6769
float4 position : SV_Position;

62_CAD/shaders/main_pipeline/fragment_shader.hlsl

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,13 @@ typedef StyleClipper< nbl::hlsl::shapes::Line<float> > LineStyleClipper;
338338
// textureColor: color sampled from a texture
339339
// useStyleColor: instead of writing and reading from colorStorage, use main object Idx to find the style color for the object.
340340
template<bool FragmentShaderPixelInterlock>
341-
float32_t4 calculateFinalColor(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 textureColor);
341+
float32_t4 calculateFinalColor(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 textureColor, bool colorFromTexture);
342342

343343
template<>
344-
float32_t4 calculateFinalColor<false>(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 localTextureColor)
344+
float32_t4 calculateFinalColor<false>(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 localTextureColor, bool colorFromTexture)
345345
{
346346
uint32_t styleIdx = mainObjects[currentMainObjectIdx].styleIdx;
347-
const bool colorFromStyle = styleIdx != InvalidStyleIdx;
348-
if (colorFromStyle)
347+
if (!colorFromTexture)
349348
{
350349
float32_t4 col = lineStyles[styleIdx].color;
351350
col.w *= localAlpha;
@@ -355,10 +354,9 @@ float32_t4 calculateFinalColor<false>(const uint2 fragCoord, const float localAl
355354
return float4(localTextureColor, localAlpha);
356355
}
357356
template<>
358-
float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 localTextureColor)
357+
float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 localTextureColor, bool colorFromTexture)
359358
{
360359
float32_t4 color;
361-
362360
nbl::hlsl::spirv::beginInvocationInterlockEXT();
363361

364362
const uint32_t packedData = pseudoStencil[fragCoord];
@@ -367,19 +365,26 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlp
367365
const uint32_t storedQuantizedAlpha = nbl::hlsl::glsl::bitfieldExtract<uint32_t>(packedData,0,AlphaBits);
368366
const uint32_t storedMainObjectIdx = nbl::hlsl::glsl::bitfieldExtract<uint32_t>(packedData,AlphaBits,MainObjectIdxBits);
369367
// if geomID has changed, we resolve the SDF alpha (draw using blend), else accumulate
370-
const bool resolve = currentMainObjectIdx != storedMainObjectIdx;
371-
uint32_t resolveStyleIdx = mainObjects[storedMainObjectIdx].styleIdx;
372-
const bool resolveColorFromStyle = resolveStyleIdx != InvalidStyleIdx;
368+
const bool differentMainObject = currentMainObjectIdx != storedMainObjectIdx; // meaning current pixel's main object is different than what is already stored
369+
const bool resolve = differentMainObject && storedMainObjectIdx != InvalidMainObjectIdx;
370+
uint32_t toResolveStyleIdx = InvalidStyleIdx;
373371

374372
// load from colorStorage only if we want to resolve color from texture instead of style
375373
// sampling from colorStorage needs to happen in critical section because another fragment may also want to store into it at the same time + need to happen before store
376-
if (resolve && !resolveColorFromStyle)
377-
color = float32_t4(unpackR11G11B10_UNORM(colorStorage[fragCoord]), 1.0f);
378-
379-
if (resolve || localQuantizedAlpha > storedQuantizedAlpha)
374+
if (resolve)
375+
{
376+
toResolveStyleIdx = mainObjects[storedMainObjectIdx].styleIdx;
377+
if (toResolveStyleIdx == InvalidStyleIdx) // if style idx to resolve is invalid, then it means we should resolve from color
378+
color = float32_t4(unpackR11G11B10_UNORM(colorStorage[fragCoord]), 1.0f);
379+
}
380+
381+
// If current localAlpha is higher than what is already stored in pseudoStencil we will update the value in pseudoStencil or the color in colorStorage, this is equivalent to programmable blending MAX operation.
382+
// OR If previous pixel has a different ID than current's (i.e. previous either empty/invalid or a differnet mainObject), we should update our alpha and color storages.
383+
if (differentMainObject || localQuantizedAlpha > storedQuantizedAlpha)
380384
{
381385
pseudoStencil[fragCoord] = nbl::hlsl::glsl::bitfieldInsert<uint32_t>(localQuantizedAlpha,currentMainObjectIdx,AlphaBits,MainObjectIdxBits);
382-
colorStorage[fragCoord] = packR11G11B10_UNORM(localTextureColor);
386+
if (colorFromTexture) // writing color from texture
387+
colorStorage[fragCoord] = packR11G11B10_UNORM(localTextureColor);
383388
}
384389

385390
nbl::hlsl::spirv::endInvocationInterlockEXT();
@@ -389,8 +394,8 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlp
389394

390395
// draw with previous geometry's style's color or stored in texture buffer :kek:
391396
// we don't need to load the style's color in critical section because we've already retrieved the style index from the stored main obj
392-
if (resolveColorFromStyle)
393-
color = lineStyles[resolveStyleIdx].color;
397+
if (toResolveStyleIdx != InvalidStyleIdx) // if toResolveStyleIdx is valid then that means our resolved color should come from line style
398+
color = lineStyles[toResolveStyleIdx].color;
394399
color.a *= float(storedQuantizedAlpha) / 255.f;
395400

396401
return color;
@@ -401,10 +406,12 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlp
401406
float4 fragMain(PSInput input) : SV_TARGET
402407
{
403408
float localAlpha = 0.0f;
409+
float3 textureColor = float3(0, 0, 0); // color sampled from a texture
410+
411+
// TODO[Przemek]: Disable All the object rendering paths if you want.
404412
ObjectType objType = input.getObjType();
405413
const uint32_t currentMainObjectIdx = input.getMainObjectIdx();
406414
const MainObject mainObj = mainObjects[currentMainObjectIdx];
407-
float3 textureColor = float3(0, 0, 0); // color sampled from a texture
408415

409416
// figure out local alpha with sdf
410417
if (objType == ObjectType::LINE || objType == ObjectType::QUAD_BEZIER || objType == ObjectType::POLYLINE_CONNECTOR)
@@ -639,5 +646,10 @@ float4 fragMain(PSInput input) : SV_TARGET
639646
if (localAlpha <= 0)
640647
discard;
641648

642-
return calculateFinalColor<nbl::hlsl::jit::device_capabilities::fragmentShaderPixelInterlock>(fragCoord, localAlpha, currentMainObjectIdx, textureColor);
649+
const bool colorFromTexture = objType == ObjectType::IMAGE;
650+
651+
// TODO[Przemek]: But make sure you're still calling this, correctly calculating alpha and texture color.
652+
// you can add 1 main object and push via DrawResourcesFiller like we already do for other objects (this go in the mainObjects StorageBuffer) and then set the currentMainObjectIdx to 0 here
653+
// having 1 main object temporarily means that all triangle meshes will be treated as a unified object in blending operations.
654+
return calculateFinalColor<nbl::hlsl::jit::device_capabilities::fragmentShaderPixelInterlock>(fragCoord, localAlpha, currentMainObjectIdx, textureColor, colorFromTexture);
643655
}

62_CAD/shaders/main_pipeline/resolve_alphas.hlsl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,29 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord)
2323
const uint32_t storedQuantizedAlpha = nbl::hlsl::glsl::bitfieldExtract<uint32_t>(packedData,0,AlphaBits);
2424
const uint32_t storedMainObjectIdx = nbl::hlsl::glsl::bitfieldExtract<uint32_t>(packedData,AlphaBits,MainObjectIdxBits);
2525
pseudoStencil[fragCoord] = nbl::hlsl::glsl::bitfieldInsert<uint32_t>(0, InvalidMainObjectIdx, AlphaBits, MainObjectIdxBits);
26+
27+
// if geomID has changed, we resolve the SDF alpha (draw using blend), else accumulate
28+
const bool resolve = storedMainObjectIdx != InvalidMainObjectIdx;
29+
uint32_t toResolveStyleIdx = InvalidStyleIdx;
2630

27-
uint32_t resolveStyleIdx = mainObjects[storedMainObjectIdx].styleIdx;
28-
const bool resolveColorFromStyle = resolveStyleIdx != InvalidStyleIdx;
29-
if (!resolveColorFromStyle)
30-
color = float32_t4(unpackR11G11B10_UNORM(colorStorage[fragCoord]), 1.0f);
31+
// load from colorStorage only if we want to resolve color from texture instead of style
32+
// sampling from colorStorage needs to happen in critical section because another fragment may also want to store into it at the same time + need to happen before store
33+
if (resolve)
34+
{
35+
toResolveStyleIdx = mainObjects[storedMainObjectIdx].styleIdx;
36+
if (toResolveStyleIdx == InvalidStyleIdx) // if style idx to resolve is invalid, then it means we should resolve from color
37+
color = float32_t4(unpackR11G11B10_UNORM(colorStorage[fragCoord]), 1.0f);
38+
}
3139

3240
nbl::hlsl::spirv::endInvocationInterlockEXT();
3341

34-
if (resolveColorFromStyle)
35-
color = lineStyles[resolveStyleIdx].color;
42+
if (!resolve)
43+
discard;
44+
45+
// draw with previous geometry's style's color or stored in texture buffer :kek:
46+
// we don't need to load the style's color in critical section because we've already retrieved the style index from the stored main obj
47+
if (toResolveStyleIdx != InvalidStyleIdx) // if toResolveStyleIdx is valid then that means our resolved color should come from line style
48+
color = lineStyles[toResolveStyleIdx].color;
3649
color.a *= float(storedQuantizedAlpha) / 255.f;
3750

3851
return color;

62_CAD/shaders/main_pipeline/vertex_shader.hlsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ void dilateHatch<false>(out float2 outOffsetVec, out float2 outUV, const float2
8787

8888
PSInput main(uint vertexID : SV_VertexID)
8989
{
90+
// TODO[Przemek]: Disable Everything here and do your own thing as we already discussed, but let's have the same PSInput data passed to fragment.
91+
// your programmable pulling will use the baseVertexBufferAddress BDA address and `vertexID` to RawBufferLoad it's vertex.
92+
// ~~Later, most likely We will require pulling all 3 vertices of the triangle, that's where you need to know which triangle you're currently on, and instead of objectID = vertexID/4 which we currently do, you will do vertexID/3 and pull all 3 of it's vertices.~~
93+
// Ok, brainfart, a vertex can belong to multiple triangles, I was thinking of AA but triangles share vertices, nevermind my comment above.
94+
9095
const uint vertexIdx = vertexID & 0x3u;
9196
const uint objectID = vertexID >> 2;
9297

68_JpegLoading/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
include(common RESULT_VARIABLE RES)
2+
if(NOT RES)
3+
message(FATAL_ERROR "common.cmake not found. Should be in {repo_root}/cmake directory")
4+
endif()
5+
6+
nbl_create_executable_project("" "" "" "" "${NBL_EXECUTABLE_PROJECT_CREATION_PCH_TARGET}")
7+
8+
if(NBL_EMBED_BUILTIN_RESOURCES)
9+
set(_BR_TARGET_ ${EXECUTABLE_NAME}_builtinResourceData)
10+
set(RESOURCE_DIR "app_resources")
11+
12+
get_filename_component(_SEARCH_DIRECTORIES_ "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
13+
get_filename_component(_OUTPUT_DIRECTORY_SOURCE_ "${CMAKE_CURRENT_BINARY_DIR}/src" ABSOLUTE)
14+
get_filename_component(_OUTPUT_DIRECTORY_HEADER_ "${CMAKE_CURRENT_BINARY_DIR}/include" ABSOLUTE)
15+
16+
file(GLOB_RECURSE BUILTIN_RESOURCE_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DIR}" CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DIR}/*")
17+
foreach(RES_FILE ${BUILTIN_RESOURCE_FILES})
18+
LIST_BUILTIN_RESOURCE(RESOURCES_TO_EMBED "${RES_FILE}")
19+
endforeach()
20+
21+
ADD_CUSTOM_BUILTIN_RESOURCES(${_BR_TARGET_} RESOURCES_TO_EMBED "${_SEARCH_DIRECTORIES_}" "${RESOURCE_DIR}" "nbl::this_example::builtin" "${_OUTPUT_DIRECTORY_HEADER_}" "${_OUTPUT_DIRECTORY_SOURCE_}")
22+
23+
LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} ${_BR_TARGET_})
24+
endif()
25+
26+
add_dependencies(${EXECUTABLE_NAME} argparse)
27+
target_include_directories(${EXECUTABLE_NAME} PUBLIC $<TARGET_PROPERTY:argparse,INTERFACE_INCLUDE_DIRECTORIES>)

0 commit comments

Comments
 (0)