Skip to content

Commit d478a92

Browse files
Merge pull request #105 from nipunG314/master
Counting Sort Example
2 parents af12a53 + 16e606c commit d478a92

File tree

9 files changed

+681
-2
lines changed

9 files changed

+681
-2
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "media"]
22
path = media
3-
url = git@github.com:Devsh-Graphics-Programming/Nabla-Example-And-Tests-Media.git
3+
url = git@github.com:nipunG314/Nabla-Example-And-Tests-Media.git

10_CountingSort/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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}" "${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()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef _COUNTING_SORT_COMMON_INCLUDED_
6+
#define _COUNTING_SORT_COMMON_INCLUDED_
7+
8+
struct CountingPushData
9+
{
10+
uint64_t inputKeyAddress;
11+
uint64_t inputValueAddress;
12+
uint64_t histogramAddress;
13+
uint64_t outputKeyAddress;
14+
uint64_t outputValueAddress;
15+
uint32_t dataElementCount;
16+
uint32_t elementsPerWT;
17+
uint32_t minimum;
18+
uint32_t maximum;
19+
};
20+
21+
#endif
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "nbl/builtin/hlsl/bda/bda_accessor.hlsl"
2+
#include "nbl/builtin/hlsl/sort/counting.hlsl"
3+
#include "app_resources/common.hlsl"
4+
5+
[[vk::push_constant]] CountingPushData pushData;
6+
7+
using Ptr = nbl::hlsl::bda::__ptr < uint32_t >;
8+
using PtrAccessor = nbl::hlsl::BdaAccessor < uint32_t >;
9+
10+
groupshared uint32_t sdata[BucketCount];
11+
12+
struct SharedAccessor
13+
{
14+
uint32_t get(const uint32_t index)
15+
{
16+
return sdata[index];
17+
}
18+
19+
void set(const uint32_t index, const uint32_t value)
20+
{
21+
sdata[index] = value;
22+
}
23+
24+
uint32_t atomicAdd(const uint32_t index, const uint32_t value)
25+
{
26+
return nbl::hlsl::glsl::atomicAdd(sdata[index], value);
27+
}
28+
29+
void workgroupExecutionAndMemoryBarrier()
30+
{
31+
nbl::hlsl::glsl::barrier();
32+
}
33+
};
34+
35+
uint32_t3 nbl::hlsl::glsl::gl_WorkGroupSize()
36+
{
37+
return uint32_t3(WorkgroupSize, 1, 1);
38+
}
39+
40+
[numthreads(WorkgroupSize,1,1)]
41+
void main(uint32_t3 ID : SV_GroupThreadID, uint32_t3 GroupID : SV_GroupID)
42+
{
43+
nbl::hlsl::sort::CountingParameters < uint32_t > params;
44+
params.dataElementCount = pushData.dataElementCount;
45+
params.elementsPerWT = pushData.elementsPerWT;
46+
params.minimum = pushData.minimum;
47+
params.maximum = pushData.maximum;
48+
49+
using Counter = nbl::hlsl::sort::counting < WorkgroupSize, BucketCount, PtrAccessor, PtrAccessor, PtrAccessor, SharedAccessor>;
50+
Counter counter = Counter::create(nbl::hlsl::glsl::gl_WorkGroupID().x);
51+
52+
const Ptr input_ptr = Ptr::create(pushData.inputKeyAddress);
53+
const Ptr histogram_ptr = Ptr::create(pushData.histogramAddress);
54+
55+
PtrAccessor input_accessor = PtrAccessor::create(input_ptr);
56+
PtrAccessor histogram_accessor = PtrAccessor::create(histogram_ptr);
57+
SharedAccessor shared_accessor;
58+
counter.histogram(
59+
input_accessor,
60+
histogram_accessor,
61+
shared_accessor,
62+
params
63+
);
64+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "nbl/builtin/hlsl/bda/bda_accessor.hlsl"
2+
#include "nbl/builtin/hlsl/sort/counting.hlsl"
3+
#include "app_resources/common.hlsl"
4+
5+
[[vk::push_constant]] CountingPushData pushData;
6+
7+
using Ptr = nbl::hlsl::bda::__ptr < uint32_t >;
8+
using PtrAccessor = nbl::hlsl::BdaAccessor < uint32_t >;
9+
10+
groupshared uint32_t sdata[BucketCount];
11+
12+
struct SharedAccessor
13+
{
14+
uint32_t get(const uint32_t index)
15+
{
16+
return sdata[index];
17+
}
18+
19+
void set(const uint32_t index, const uint32_t value)
20+
{
21+
sdata[index] = value;
22+
}
23+
24+
uint32_t atomicAdd(const uint32_t index, const uint32_t value)
25+
{
26+
return nbl::hlsl::glsl::atomicAdd(sdata[index], value);
27+
}
28+
29+
void workgroupExecutionAndMemoryBarrier()
30+
{
31+
nbl::hlsl::glsl::barrier();
32+
}
33+
};
34+
35+
struct DoublePtrAccessor
36+
{
37+
static DoublePtrAccessor create(const PtrAccessor input, const PtrAccessor output)
38+
{
39+
DoublePtrAccessor accessor;
40+
accessor.input = input;
41+
accessor.output = output;
42+
return accessor;
43+
}
44+
45+
uint32_t get(const uint64_t index)
46+
{
47+
return input.get(index);
48+
}
49+
50+
void set(const uint64_t index, const uint32_t value)
51+
{
52+
output.set(index, value);
53+
}
54+
55+
PtrAccessor input, output;
56+
};
57+
58+
uint32_t3 nbl::hlsl::glsl::gl_WorkGroupSize()
59+
{
60+
return uint32_t3(WorkgroupSize, 1, 1);
61+
}
62+
63+
[numthreads(WorkgroupSize, 1, 1)]
64+
void main(uint32_t3 ID : SV_GroupThreadID, uint32_t3 GroupID : SV_GroupID)
65+
{
66+
nbl::hlsl::sort::CountingParameters < uint32_t > params;
67+
params.dataElementCount = pushData.dataElementCount;
68+
params.elementsPerWT = pushData.elementsPerWT;
69+
params.minimum = pushData.minimum;
70+
params.maximum = pushData.maximum;
71+
72+
using Counter = nbl::hlsl::sort::counting < WorkgroupSize, BucketCount, DoublePtrAccessor, DoublePtrAccessor, PtrAccessor, SharedAccessor>;
73+
Counter counter = Counter::create(nbl::hlsl::glsl::gl_WorkGroupID().x);
74+
75+
const Ptr input_key_ptr = Ptr::create(pushData.inputKeyAddress);
76+
const Ptr input_value_ptr = Ptr::create(pushData.inputValueAddress);
77+
const Ptr histogram_ptr = Ptr::create(pushData.histogramAddress);
78+
const Ptr output_key_ptr = Ptr::create(pushData.outputKeyAddress);
79+
const Ptr output_value_ptr = Ptr::create(pushData.outputValueAddress);
80+
81+
DoublePtrAccessor key_accessor = DoublePtrAccessor::create(
82+
PtrAccessor::create(input_key_ptr),
83+
PtrAccessor::create(output_key_ptr)
84+
);
85+
DoublePtrAccessor value_accessor = DoublePtrAccessor::create(
86+
PtrAccessor::create(input_value_ptr),
87+
PtrAccessor::create(output_value_ptr)
88+
);
89+
PtrAccessor histogram_accessor = PtrAccessor::create(histogram_ptr);
90+
SharedAccessor shared_accessor;
91+
counter.scatter(
92+
key_accessor,
93+
value_accessor,
94+
histogram_accessor,
95+
shared_accessor,
96+
params
97+
);
98+
}

10_CountingSort/config.json.template

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"enableParallelBuild": true,
3+
"threadsPerBuildProcess" : 2,
4+
"isExecuted": false,
5+
"scriptPath": "",
6+
"cmake": {
7+
"configurations": [ "Release", "Debug", "RelWithDebInfo" ],
8+
"buildModes": [],
9+
"requiredOptions": []
10+
},
11+
"profiles": [
12+
{
13+
"backend": "vulkan", // should be none
14+
"platform": "windows",
15+
"buildModes": [],
16+
"runConfiguration": "Release", // we also need to run in Debug nad RWDI because foundational example
17+
"gpuArchitectures": []
18+
}
19+
],
20+
"dependencies": [],
21+
"data": [
22+
{
23+
"dependencies": [],
24+
"command": [""],
25+
"outputs": []
26+
}
27+
]
28+
}

0 commit comments

Comments
 (0)