Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShaderTrap: Examples of computing a histogram of values #68

Merged
merged 17 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version1.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#Copyright 2021 Mostafa Ashraf
#
#Licensed under the Apache License, Version 2.0(the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#https: // www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

GL 4.5

CREATE_BUFFER histogram
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
SIZE_BYTES 1024
INIT_VALUES
uint 4 10 14 3 3 13 8 2 10 7 0 0 0 2 5 0
uint 13 7 8 12 9 2 11 14 2 2 8 6 15 0 15 6
uint 2 2 0 3 6 10 7 11 7 11 2 4 11 5 15 12
uint 1 15 11 1 13 12 5 1 11 3 11 1 2 1 6 1
uint 15 15 13 2 7 2 12 8 4 5 11 7 11 4 10 15
uint 7 14 3 2 15 7 12 12 6 13 4 12 3 5 5 10
uint 7 13 1 6 3 12 3 3 11 0 2 12 14 13 10 15
uint 3 1 11 0 8 4 15 13 4 14 8 5 7 10 8 6
uint 10 9 0 7 15 11 13 5 7 2 9 11 2 15 14 12
uint 6 14 11 0 2 14 5 12 15 11 8 10 9 11 3 14
uint 9 13 9 6 10 2 7 13 10 3 11 1 9 10 6 4
uint 8 14 10 7 2 2 5 7 11 6 5 13 2 3 10 11
uint 12 3 14 3 1 3 9 14 8 5 13 2 15 12 15 11
uint 0 10 9 0 7 15 9 4 0 6 13 14 14 6 13 11
uint 10 7 14 7 8 10 13 10 13 7 7 10 13 0 3 15
uint 5 12 1 1 5 2 2 2 4 9 11 2 10 1 7 9

CREATE_BUFFER result
SIZE_BYTES 64
INIT_VALUES
uint 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

CREATE_BUFFER expected
SIZE_BYTES 64
INIT_VALUES
uint 14 13 25 17 10 14 13 21 11 12 20 22 14 18 15 17

BIND_SHADER_STORAGE_BUFFER BUFFER histogram BINDING 0
BIND_SHADER_STORAGE_BUFFER BUFFER result BINDING 1
BIND_SHADER_STORAGE_BUFFER BUFFER expected BINDING 2

DECLARE_SHADER shader KIND COMPUTE
#version 450

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) buffer histogram {
afd marked this conversation as resolved.
Show resolved Hide resolved
uint[256] histo;
afd marked this conversation as resolved.
Show resolved Hide resolved
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
};
layout(binding = 1) buffer result {
uint[16] res;
};

void main() {
// Getting the frequencies of values.
for (int i = 0; i < 256; i++) {
res[histo[i]]++;
}
}
END

COMPILE_SHADER shader_compiled SHADER shader
CREATE_PROGRAM compute_prog SHADERS shader_compiled

RUN_COMPUTE PROGRAM compute_prog NUM_GROUPS 1 1 1

DUMP_BUFFER_TEXT BUFFER result FILE "out_v1.txt" FORMAT "contents: " uint 16

ASSERT_EQUAL BUFFERS expected result
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
80 changes: 80 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version2.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#Copyright 2021 Mostafa Ashraf
#
#Licensed under the Apache License, Version 2.0(the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#https: // www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

# This example is for getting frequencies of values in the form of a histogram.
GL 4.5

CREATE_BUFFER histogram
SIZE_BYTES 1024
INIT_VALUES
uint 4 10 14 3 3 13 8 2 10 7 0 0 0 2 5 0
uint 13 7 8 12 9 2 11 14 2 2 8 6 15 0 15 6
uint 2 2 0 3 6 10 7 11 7 11 2 4 11 5 15 12
uint 1 15 11 1 13 12 5 1 11 3 11 1 2 1 6 1
uint 15 15 13 2 7 2 12 8 4 5 11 7 11 4 10 15
uint 7 14 3 2 15 7 12 12 6 13 4 12 3 5 5 10
uint 7 13 1 6 3 12 3 3 11 0 2 12 14 13 10 15
uint 3 1 11 0 8 4 15 13 4 14 8 5 7 10 8 6
uint 10 9 0 7 15 11 13 5 7 2 9 11 2 15 14 12
uint 6 14 11 0 2 14 5 12 15 11 8 10 9 11 3 14
uint 9 13 9 6 10 2 7 13 10 3 11 1 9 10 6 4
uint 8 14 10 7 2 2 5 7 11 6 5 13 2 3 10 11
uint 12 3 14 3 1 3 9 14 8 5 13 2 15 12 15 11
uint 0 10 9 0 7 15 9 4 0 6 13 14 14 6 13 11
uint 10 7 14 7 8 10 13 10 13 7 7 10 13 0 3 15
uint 5 12 1 1 5 2 2 2 4 9 11 2 10 1 7 9

CREATE_BUFFER result
SIZE_BYTES 64
INIT_VALUES
uint 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

CREATE_BUFFER expected
SIZE_BYTES 64
INIT_VALUES
uint 224 208 400 272 160 224 208 336 176 192 320 352 224 288 240 272
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved

BIND_SHADER_STORAGE_BUFFER BUFFER histogram BINDING 0
BIND_SHADER_STORAGE_BUFFER BUFFER result BINDING 1
BIND_SHADER_STORAGE_BUFFER BUFFER expected BINDING 2

DECLARE_SHADER shader KIND COMPUTE
#version 450

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) buffer histogram {
uint[256] histo;
};
layout(binding = 1) buffer result {
uint[16] res;
};
void main() {
// Same as version 1, but for each iteration.
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 256; j++) {
res[histo[j]]++;
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
END

COMPILE_SHADER shader_compiled SHADER shader
CREATE_PROGRAM compute_prog SHADERS shader_compiled

RUN_COMPUTE PROGRAM compute_prog NUM_GROUPS 1 1 1

DUMP_BUFFER_TEXT BUFFER result FILE "out_v2.txt" FORMAT "contents: " uint 16

ASSERT_EQUAL BUFFERS expected result
88 changes: 88 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version3.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#Copyright 2021 Mostafa Ashraf
#
#Licensed under the Apache License, Version 2.0(the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#https: // www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

#This example is for getting frequencies of values in the form of a histogram.
GL 4.5

CREATE_BUFFER histogram
SIZE_BYTES 1024
INIT_VALUES
uint 4 10 14 3 3 13 8 2 10 7 0 0 0 2 5 0
uint 13 7 8 12 9 2 11 14 2 2 8 6 15 0 15 6
uint 2 2 0 3 6 10 7 11 7 11 2 4 11 5 15 12
uint 1 15 11 1 13 12 5 1 11 3 11 1 2 1 6 1
uint 15 15 13 2 7 2 12 8 4 5 11 7 11 4 10 15
uint 7 14 3 2 15 7 12 12 6 13 4 12 3 5 5 10
uint 7 13 1 6 3 12 3 3 11 0 2 12 14 13 10 15
uint 3 1 11 0 8 4 15 13 4 14 8 5 7 10 8 6
uint 10 9 0 7 15 11 13 5 7 2 9 11 2 15 14 12
uint 6 14 11 0 2 14 5 12 15 11 8 10 9 11 3 14
uint 9 13 9 6 10 2 7 13 10 3 11 1 9 10 6 4
uint 8 14 10 7 2 2 5 7 11 6 5 13 2 3 10 11
uint 12 3 14 3 1 3 9 14 8 5 13 2 15 12 15 11
uint 0 10 9 0 7 15 9 4 0 6 13 14 14 6 13 11
uint 10 7 14 7 8 10 13 10 13 7 7 10 13 0 3 15
uint 5 12 1 1 5 2 2 2 4 9 11 2 10 1 7 9

CREATE_BUFFER result
SIZE_BYTES 64
INIT_VALUES
uint 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

CREATE_BUFFER expected
SIZE_BYTES 64
INIT_VALUES
uint 12 12 12 20 8 12 8 24 28 36 12 24 16 20 16 16

BIND_SHADER_STORAGE_BUFFER BUFFER histogram BINDING 0
BIND_SHADER_STORAGE_BUFFER BUFFER result BINDING 1
BIND_SHADER_STORAGE_BUFFER BUFFER expected BINDING 2

DECLARE_SHADER shader KIND COMPUTE
#version 450

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) buffer histogram {
// Can access each uvec4 as: histo[n].x, histo[n].y, histo[n].z, histo[n].w
// and this is called swizzling. x,y,z,w referring to the first,
// second, third, and fourth components, respectively.
uvec4[64] histo;
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
};
layout(binding = 1) buffer result {
// uvec4[16] it was uvec4[4], but both gives me same results!
uvec4[16] res;
};

void main() {
// Starting to support computations at same.
for (int i = 0; i < 16; i+=4) {
for (int j = 0; j < 64; j++) {
res[histo[j].x].x++;
res[histo[j].y].y++;
res[histo[j].z].z++;
res[histo[j].w].w++;
}
}
}
END

COMPILE_SHADER shader_compiled SHADER shader
CREATE_PROGRAM compute_prog SHADERS shader_compiled

RUN_COMPUTE PROGRAM compute_prog NUM_GROUPS 1 1 1

DUMP_BUFFER_TEXT BUFFER result FILE "out_v3.txt" FORMAT "contents: " uint 16

ASSERT_EQUAL BUFFERS expected result
111 changes: 111 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version4.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#Copyright 2021 Mostafa Ashraf
#
#Licensed under the Apache License, Version 2.0(the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#https: // www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

#This example is for getting frequencies of values in the form of a histogram.
GL 4.5

CREATE_BUFFER histogram
SIZE_BYTES 1024
INIT_VALUES
uint 4 10 14 3 3 13 8 2 10 7 0 0 0 2 5 0
uint 13 7 8 12 9 2 11 14 2 2 8 6 15 0 15 6
uint 2 2 0 3 6 10 7 11 7 11 2 4 11 5 15 12
uint 1 15 11 1 13 12 5 1 11 3 11 1 2 1 6 1
uint 15 15 13 2 7 2 12 8 4 5 11 7 11 4 10 15
uint 7 14 3 2 15 7 12 12 6 13 4 12 3 5 5 10
uint 7 13 1 6 3 12 3 3 11 0 2 12 14 13 10 15
uint 3 1 11 0 8 4 15 13 4 14 8 5 7 10 8 6
uint 10 9 0 7 15 11 13 5 7 2 9 11 2 15 14 12
uint 6 14 11 0 2 14 5 12 15 11 8 10 9 11 3 14
uint 9 13 9 6 10 2 7 13 10 3 11 1 9 10 6 4
uint 8 14 10 7 2 2 5 7 11 6 5 13 2 3 10 11
uint 12 3 14 3 1 3 9 14 8 5 13 2 15 12 15 11
uint 0 10 9 0 7 15 9 4 0 6 13 14 14 6 13 11
uint 10 7 14 7 8 10 13 10 13 7 7 10 13 0 3 15
uint 5 12 1 1 5 2 2 2 4 9 11 2 10 1 7 9

CREATE_BUFFER result
SIZE_BYTES 64
INIT_VALUES
uint 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

CREATE_BUFFER expected
SIZE_BYTES 64
INIT_VALUES
uint 486 0 0 0 0 482 0 0 0 0 533 0 0 0 0 453

BIND_SHADER_STORAGE_BUFFER BUFFER histogram BINDING 0
BIND_SHADER_STORAGE_BUFFER BUFFER result BINDING 1
BIND_SHADER_STORAGE_BUFFER BUFFER expected BINDING 2

DECLARE_SHADER shader KIND COMPUTE
#version 450

layout(local_size_x = 4, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) buffer histogram {
// Can access each uvec4 as: histo[n].x, histo[n].y, histo[n].z, histo[n].w
// and this is called swizzling. x,y,z,w referring to the first,
// second, third, and fourth components, respectively.
uvec4[64] histo;
};
layout(binding = 1) buffer result {
uvec4[16] res;
};

void main() {
// Like version 3 but use 4 parallel invocations each one responsible for a range of values.
if (uint(gl_LocalInvocationID.x) == 0) {
for (int j = 0; j < 64; j++) {
res[0].x += histo[j].x;
res[1].y += histo[j].y;
res[2].z += histo[j].z;
res[3].w += histo[j].w;
}
}
else if (uint(gl_LocalInvocationID.x) == 1) {
for (int j = 0; j < 64; j++) {
res[4].x += histo[j].x;
res[5].y += histo[j].y;
res[6].z += histo[j].z;
res[7].w += histo[j].w;
}
}
else if (uint(gl_LocalInvocationID.x) == 2) {
for (int j = 0; j < 64; j++) {
res[8].x += histo[j].x;
res[9].y += histo[j].y;
res[10].z += histo[j].z;
res[11].w += histo[j].w;
}
}
else if (uint(gl_LocalInvocationID.x) == 3) {
for (int j = 0; j < 64; j++) {
res[12].x += histo[j].x;
res[13].y += histo[j].y;
res[14].z += histo[j].z;
res[15].w += histo[j].w;
}
}
}
END

COMPILE_SHADER shader_compiled SHADER shader
CREATE_PROGRAM compute_prog SHADERS shader_compiled

RUN_COMPUTE PROGRAM compute_prog NUM_GROUPS 1 1 1

DUMP_BUFFER_TEXT BUFFER result FILE "out_v4.txt" FORMAT "contents: " uint 16

ASSERT_EQUAL BUFFERS expected result
Loading