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 4 commits
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

Google LLC
Imperial College London
Mostafa Ashraf
3 changes: 3 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Google LLC

Imperial College London
Alastair F. Donaldson

Independent contributors via Google Summer of Code
Mostafa Ashraf
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
100 changes: 100 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version1.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2021 The ShaderTrap Project Authors
#
# 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 data_values
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 original_data_values
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 data_values 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(std430, binding = 0) readonly buffer data_values {
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(std430, 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 data_values original_data_values

ASSERT_EQUAL BUFFERS expected result
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
103 changes: 103 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version2.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2021 The ShaderTrap Project Authors
#
# 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 data_values
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 original_data_values
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 data_values 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(std430, binding = 0) readonly buffer data_values {
uint[256] histo;
};
layout(std430, binding = 1) buffer result {
uint[16] res;
};
void main() {
// Same as version 1, but it's another counting method.
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 256; j++) {
if(histo[j] == i) {
res[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_v2.txt" FORMAT "contents: " uint 16

ASSERT_EQUAL BUFFERS data_values original_data_values

ASSERT_EQUAL BUFFERS expected result
124 changes: 124 additions & 0 deletions examples/GLES31/computeHistogramOfValues_version3.shadertrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Copyright 2021 The ShaderTrap Project Authors
#
# 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 data_values
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 original_data_values
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 data_values 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(std430, binding = 0) readonly buffer data_values {
// Can access each component in uvec4 like: histo[n].x, histo[n].y, histo[n].z, histo[n].w
// and this is called swizzling.
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
uvec4[64] histo;
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
};
layout(std430, binding = 1) buffer result {
uvec4[4] res;
};

void main() {
// Example of starting to support counting at same time(using iterations).
for (int i = 0; i < 16; i += 4) {
Mostafa-ashraf19 marked this conversation as resolved.
Show resolved Hide resolved
for (int j = 0; j < 64; j++) {
for (int k = 0; k < 4; k++) {
if (int(i / 4) == 0 && (histo[j][k] == 0 || histo[j][k] == 1 ||
histo[j][k] == 2 || histo[j][k] == 3)) {
res[int(i / 4)][histo[j][k] % 4]++;
}

else if (int(i / 4) == 1 && (histo[j][k] == 4 || histo[j][k] == 5 ||
histo[j][k] == 6 || histo[j][k] == 7)) {
res[int(i / 4)][histo[j][k] % 4]++;
}

else if (int(i / 4) == 2 && (histo[j][k] == 8 || histo[j][k] == 9 ||
histo[j][k] == 10 || histo[j][k] == 11)) {
res[int(i / 4)][histo[j][k] % 4]++;
}

else if (int(i / 4) == 3 && (histo[j][k] == 12 || histo[j][k] == 13 ||
histo[j][k] == 14 || histo[j][k] == 15)) {
res[int(i / 4)][histo[j][k] % 4]++;
}
}
}
}
}
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 data_values original_data_values

ASSERT_EQUAL BUFFERS expected result
Loading