-
Notifications
You must be signed in to change notification settings - Fork 667
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
Remove shader variants from 16bit arithmetic sample #1232
Open
tomadamatkinson
wants to merge
1
commit into
KhronosGroup:main
Choose a base branch
from
tomadamatkinson:16bit_arithmetic_variants
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
shaders/16bit_arithmetic/compute_buffer_fp16_fallback.comp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#version 450 | ||
/* Copyright (c) 2020-2024, Arm Limited and Contributors | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
// Allows us to use float16_t for arithmetic purposes. | ||
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require | ||
|
||
// Allows us to use int16_t, uint16_t and float16_t for buffers. | ||
#extension GL_EXT_shader_16bit_storage : require | ||
|
||
layout(local_size_x = 8, local_size_y = 8) in; | ||
|
||
layout(constant_id = 0) const uint WIDTH = 1; | ||
layout(constant_id = 1) const uint HEIGHT = 1; | ||
|
||
layout(set = 0, binding = 0) readonly buffer SSBO | ||
{ | ||
// It is possible to use native 16-bit types in SSBOs and UBOs. We could use uvec2 here and unpack manually. | ||
// The key feature of 16-bit storage is to allow scalar access to 16-bit values however. | ||
// Avoiding extra unpacking and packing can also be useful. | ||
f16vec4 blob_data[]; | ||
}; | ||
|
||
layout(rgba16f, set = 0, binding = 1) writeonly uniform mediump image2D o_results; | ||
|
||
layout(push_constant) uniform Registers | ||
{ | ||
// Fallback for implementations which do not support PushConstant16. | ||
uint num_blobs; | ||
float seed; | ||
ivec2 range; | ||
} | ||
registers; | ||
|
||
// This is very arbitrary. Expends a ton of arithmetic to compute | ||
// something that looks similar to a lens flare. | ||
f16vec4 compute_blob(f16vec2 pos, f16vec4 blob, float16_t seed) | ||
{ | ||
f16vec2 offset = pos - blob.xy; | ||
f16vec4 rg_offset = offset.xxyy * f16vec4(0.95hf, 1.0hf, 0.95hf, 1.0hf); | ||
f16vec4 bs_offset = offset.xxyy * f16vec4(1.05hf, 1.1hf + seed, 1.05hf, 1.1hf + seed); | ||
|
||
f16vec4 rg_dot = rg_offset * rg_offset; | ||
f16vec4 bs_dot = bs_offset * bs_offset; | ||
|
||
// Dot products can be somewhat awkward in FP16, since the result is a scalar 16-bit value, and we don't want that. | ||
// To that end, we compute at least two dot products side by side, and rg_offset and bs_offset are swizzled | ||
// such that we avoid swizzling across a 32-bit boundary. | ||
f16vec4 dots = f16vec4(rg_dot.xy + rg_dot.zw, bs_dot.xy + bs_dot.zw) * blob.w; | ||
|
||
// Now we have square distances to blob center. | ||
|
||
// Gotta have some FMAs, right? :D | ||
dots = dots * dots + dots; | ||
dots = dots * dots + dots; | ||
dots = dots * dots + dots; | ||
dots = dots * dots + dots; | ||
dots = dots * dots + dots; | ||
dots = dots * dots + dots; | ||
|
||
f16vec4 parabolas = max(f16vec4(1.0hf, 1.0hf, 1.0hf, 0.9hf) - dots, f16vec4(0.0hf)); | ||
parabolas -= parabolas.w; | ||
parabolas = max(parabolas, f16vec4(0.0hf)); | ||
return parabolas; | ||
} | ||
|
||
void main() | ||
{ | ||
uint num_blobs = uint(registers.num_blobs); | ||
|
||
float x = float(gl_GlobalInvocationID.x) / float(WIDTH) - 0.5; | ||
float y = float(gl_GlobalInvocationID.y) / float(HEIGHT) - 0.5; | ||
f16vec2 pos = f16vec2(x, y); | ||
f16vec4 result = f16vec4(0.0hf); | ||
float16_t seed = float16_t(registers.seed); | ||
ivec2 range = ivec2(registers.range); | ||
|
||
const float16_t EXPAND_FACTOR = 0.3hf; | ||
float16_t stride = seed * EXPAND_FACTOR; | ||
|
||
for (uint i = 0; i < num_blobs; i++) | ||
{ | ||
f16vec4 blob = blob_data[i]; | ||
|
||
// Get as much mileage out of the buffer load as possible. | ||
for (int y = -range.y; y <= range.y; y++) | ||
for (int x = -range.x; x <= range.x; x++) | ||
result += compute_blob(pos + stride * f16vec2(x, y), blob, seed); | ||
} | ||
|
||
imageStore(o_results, ivec2(gl_GlobalInvocationID.xy), result); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace tabs with spaces to reduce those whitespace changes.