diff options
-rw-r--r-- | tests/opengl.py | 5 | ||||
-rw-r--r-- | tests/spec/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/spec/amd_gpu_shader_half_float/CMakeLists.gl.txt | 14 | ||||
-rw-r--r-- | tests/spec/amd_gpu_shader_half_float/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/spec/amd_gpu_shader_half_float/half-float-explicit-offset-bufferstorage.c | 260 | ||||
-rw-r--r-- | tests/spec/amd_gpu_shader_half_float/half_float_util.c | 167 | ||||
-rw-r--r-- | tests/spec/amd_gpu_shader_half_float/half_float_util.h | 33 |
7 files changed, 481 insertions, 0 deletions
diff --git a/tests/opengl.py b/tests/opengl.py index ec67aa4dc..14de6e13e 100644 --- a/tests/opengl.py +++ b/tests/opengl.py @@ -3924,6 +3924,11 @@ with profile.test_list.group_manager( g(['arb_seamless_cubemap-three-faces-average']) with profile.test_list.group_manager( + PiglitGLTest, + grouptools.join('spec', 'amd_gpu_shader_half_float')) as g: + g(['amd_gpu_shader_half_float-explicit-offset-bufferstorage'], 'explicit-offset-bufferstorage') + +with profile.test_list.group_manager( PiglitGLTest, grouptools.join('spec', 'AMD_pinned_memory')) as g: g(['amd_pinned_memory', 'offset=0'], 'offset=0') g(['amd_pinned_memory', 'increment-offset'], 'increment-offset') diff --git a/tests/spec/CMakeLists.txt b/tests/spec/CMakeLists.txt index 035f43f74..ea56aa516 100644 --- a/tests/spec/CMakeLists.txt +++ b/tests/spec/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory (amd_compressed_atc_texture) add_subdirectory (amd_framebuffer_multisample_advanced) add_subdirectory (amd_depth_clamp_separate) +add_subdirectory (amd_gpu_shader_half_float) add_subdirectory (amd_performance_monitor) add_subdirectory (amd_pinned_memory) add_subdirectory (arb_arrays_of_arrays) diff --git a/tests/spec/amd_gpu_shader_half_float/CMakeLists.gl.txt b/tests/spec/amd_gpu_shader_half_float/CMakeLists.gl.txt new file mode 100644 index 000000000..0301f4de1 --- /dev/null +++ b/tests/spec/amd_gpu_shader_half_float/CMakeLists.gl.txt @@ -0,0 +1,14 @@ +include_directories( + ${GLEXT_INCLUDE_DIR} + ${OPENGL_INCLUDE_PATH} + ${piglit_SOURCE_DIR}/tests/util +) + +link_libraries ( + piglitutil_${piglit_target_api} + ${OPENGL_gl_LIBRARY} +) + +piglit_add_executable (amd_gpu_shader_half_float-explicit-offset-bufferstorage half-float-explicit-offset-bufferstorage.c half_float_util.c) + +# vim: ft=cmake: diff --git a/tests/spec/amd_gpu_shader_half_float/CMakeLists.txt b/tests/spec/amd_gpu_shader_half_float/CMakeLists.txt new file mode 100644 index 000000000..144a306f4 --- /dev/null +++ b/tests/spec/amd_gpu_shader_half_float/CMakeLists.txt @@ -0,0 +1 @@ +piglit_include_target_api() diff --git a/tests/spec/amd_gpu_shader_half_float/half-float-explicit-offset-bufferstorage.c b/tests/spec/amd_gpu_shader_half_float/half-float-explicit-offset-bufferstorage.c new file mode 100644 index 000000000..3b8aedcc1 --- /dev/null +++ b/tests/spec/amd_gpu_shader_half_float/half-float-explicit-offset-bufferstorage.c @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2014 VMware, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/** @file half-float-explicit-offset-bufferstorage.c + * + * This is a copy of the arb_uniform_buffer_object bufferstorage test updated + * to make use of explicit offsets and half floats. + * + * Test rendering with UBOs. We draw four squares with different + * positions, sizes, rotations and colors where those parameters come + * from UBOs. Same as rendering.c, except that the UBOs are + * persistently mapped. + */ + +#include "piglit-util-gl.h" + +#include "half_float_util.h" + +PIGLIT_GL_TEST_CONFIG_BEGIN + + config.supports_gl_core_version = 40; + config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA; + +PIGLIT_GL_TEST_CONFIG_END + +static const char vert_shader_text[] = + "#version 400\n" + "#extension GL_ARB_enhanced_layouts : require\n" + "#extension GL_AMD_gpu_shader_half_float : require\n" + "\n" + "in vec4 piglit_vertex;\n" + "\n" + "layout(std140) uniform;\n" + "layout(std140) uniform ub_pos_size {\n" + " layout(offset = 0) float16_t size;\n" + " layout(offset = 2) float16_t pos1;\n" + " layout(offset = 6) float16_t pos2;\n" + "};\n" + "uniform ub_rot {float rotation; };\n" + "\n" + "void main()\n" + "{\n" + " mat2 m;\n" + " m[0][0] = m[1][1] = cos(rotation); \n" + " m[0][1] = sin(rotation); \n" + " m[1][0] = -m[0][1]; \n" + " gl_Position.xy = m * piglit_vertex.xy * vec2(size) + vec2(pos1, pos2);\n" + " gl_Position.zw = vec2(0, 1);\n" + "}\n"; + +static const char frag_shader_text[] = + "#version 400\n" + "#extension GL_ARB_enhanced_layouts : require\n" + "#extension GL_AMD_gpu_shader_half_float : require\n" + "\n" + "layout(std140) uniform;\n" + "layout(std140) uniform ub_color {\n" + " layout(offset = 0) vec4 color;\n" + " layout(offset = 22) float16_t color_scale;\n" + "} named_ub;\n" + "\n" + "void main()\n" + "{\n" + " gl_FragColor = named_ub.color * float(named_ub.color_scale);\n" + "}\n"; + +#define NUM_SQUARES 4 +#define NUM_UBOS 3 + +/* Square positions and sizes */ +static const float pos_size[NUM_SQUARES][3] = { + { 0.1, -0.5, -0.5 }, + { 0.2, 0.5, -0.5 }, + { 0.3, -0.5, 0.5 }, + { 0.4, 0.5, 0.5 } +}; + +/* Square color and color_scales */ +static const float color[NUM_SQUARES][4] = { + { 2.0, 0.0, 0.0, 1.0 }, + { 0.0, 4.0, 0.0, 1.0 }, + { 0.0, 0.0, 5.0, 1.0 }, + { 0.2, 0.2, 0.2, 0.2 } +}; + +static const float color_scale[NUM_SQUARES] = { + 0.50, + 0.25, + 0.20, + 5.00 +}; + +/* Square rotations */ +static const float rotation[NUM_SQUARES] = { + 0.0, + 0.1, + 0.2, + 0.3 +}; + +static GLuint prog; +static GLuint buffers[NUM_UBOS]; +static void *ubos[NUM_UBOS]; + +static void +setup_ubos(void) +{ + static const char *names[NUM_UBOS] = { + "ub_pos_size", + "ub_color", + "ub_rot" + }; + int i; + + glGenBuffers(NUM_UBOS, buffers); + + for (i = 0; i < NUM_UBOS; i++) { + GLint index, size; + + /* query UBO index */ + index = glGetUniformBlockIndex(prog, names[i]); + + /* query UBO size */ + glGetActiveUniformBlockiv(prog, index, + GL_UNIFORM_BLOCK_DATA_SIZE, &size); + + printf("UBO %s: index = %d, size = %d\n", + names[i], index, size); + + /* Allocate UBO */ + glBindBuffer(GL_UNIFORM_BUFFER, buffers[i]); + glBufferStorage(GL_UNIFORM_BUFFER, size, NULL, + GL_MAP_WRITE_BIT | + GL_MAP_PERSISTENT_BIT | + GL_MAP_COHERENT_BIT | + GL_DYNAMIC_STORAGE_BIT); + + piglit_check_gl_error(GL_NO_ERROR); + + ubos[i] = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size, + GL_MAP_WRITE_BIT | + GL_MAP_PERSISTENT_BIT | + GL_MAP_COHERENT_BIT); + + piglit_check_gl_error(GL_NO_ERROR); + + if (!ubos[i]) + piglit_report_result(PIGLIT_FAIL); + + /* Attach UBO */ + glBindBufferBase(GL_UNIFORM_BUFFER, i, buffers[i]); + glUniformBlockBinding(prog, index, i); + + if (!piglit_check_gl_error(GL_NO_ERROR)) + piglit_report_result(PIGLIT_FAIL); + } +} + +void +piglit_init(int argc, char **argv) +{ + piglit_require_extension("GL_ARB_enhanced_layouts"); + piglit_require_extension("GL_AMD_gpu_shader_half_float"); + + prog = piglit_build_simple_program(vert_shader_text, frag_shader_text); + assert(prog); + glUseProgram(prog); + + setup_ubos(); + + glClearColor(0.2, 0.2, 0.2, 0.2); +} + +static bool +probe(int x, int y, int color_index) +{ + float expected[4]; + + /* mul color by color_scale */ + expected[0] = color[color_index][0] * color_scale[color_index]; + expected[1] = color[color_index][1] * color_scale[color_index]; + expected[2] = color[color_index][2] * color_scale[color_index]; + expected[3] = color[color_index][3] * color_scale[color_index]; + + return piglit_probe_pixel_rgba(x, y, expected); +} + + +enum piglit_result +piglit_display(void) +{ + GLsync fence; + bool pass = true; + int x0 = piglit_width / 4; + int x1 = piglit_width * 3 / 4; + int y0 = piglit_height / 4; + int y1 = piglit_height * 3 / 4; + int i; + + glViewport(0, 0, piglit_width, piglit_height); + + glClear(GL_COLOR_BUFFER_BIT); + + for (i = 0; i < NUM_SQUARES; i++) { + /* Wait for any previous rendering to finish before + * updating the UBOs + */ + fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); + glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, + GL_TIMEOUT_IGNORED); + + /* Load UBO data */ + uint16_t half_float_val = _mesa_float_to_half_slow(pos_size[i][0]); + memcpy(ubos[0], &half_float_val, 2); + + half_float_val = _mesa_float_to_half_slow(pos_size[i][1]); + memcpy((uint8_t *)ubos[0] + 2, &half_float_val, 2); + + half_float_val = _mesa_float_to_half_slow(pos_size[i][2]); + memcpy((uint8_t *)ubos[0] + 6, &half_float_val, 2); + + memcpy(ubos[1], color[i], sizeof(color[0])); + + half_float_val = _mesa_float_to_half_slow(color_scale[i]); + memcpy(((uint8_t *)ubos[1]) + 22, &half_float_val, 2); + + memcpy(ubos[2], &rotation[i], sizeof(rotation[0])); + + piglit_draw_rect(-1, -1, 2, 2); + } + + pass = probe(x0, y0, 0) && pass; + pass = probe(x1, y0, 1) && pass; + pass = probe(x0, y1, 2) && pass; + pass = probe(x1, y1, 3) && pass; + + piglit_present_results(); + + return pass ? PIGLIT_PASS : PIGLIT_FAIL; +} diff --git a/tests/spec/amd_gpu_shader_half_float/half_float_util.c b/tests/spec/amd_gpu_shader_half_float/half_float_util.c new file mode 100644 index 000000000..a4b5ffd8a --- /dev/null +++ b/tests/spec/amd_gpu_shader_half_float/half_float_util.c @@ -0,0 +1,167 @@ +/* + * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. + * Copyright 2015 Philip Taylor <philip@zaynar.co.uk> + * Copyright 2018 Advanced Micro Devices, Inc. + * Copyright (C) 2018-2019 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + + /* These functions were copied from Mesas util library. The only difference + * is the custom optimised _mesa_roundevenf() calls were replaced with + * lrintf() + */ + +#include <assert.h> +#include <math.h> +#include <stdint.h> + +#include "half_float_util.h" + +typedef union { float f; int32_t i; uint32_t u; } fi_type; + +/** + * Convert a 4-byte float to a 2-byte half float. + * + * Not all float32 values can be represented exactly as a float16 value. We + * round such intermediate float32 values to the nearest float16. When the + * float32 lies exactly between to float16 values, we round to the one with + * an even mantissa. + * + * This rounding behavior has several benefits: + * - It has no sign bias. + * + * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's + * GPU ISA. + * + * - By reproducing the behavior of the GPU (at least on Intel hardware), + * compile-time evaluation of constant packHalf2x16 GLSL expressions will + * result in the same value as if the expression were executed on the GPU. + */ +uint16_t +_mesa_float_to_half_slow(float val) +{ + const fi_type fi = {val}; + const int flt_m = fi.i & 0x7fffff; + const int flt_e = (fi.i >> 23) & 0xff; + const int flt_s = (fi.i >> 31) & 0x1; + int s, e, m = 0; + uint16_t result; + + /* sign bit */ + s = flt_s; + + /* handle special cases */ + if ((flt_e == 0) && (flt_m == 0)) { + /* zero */ + /* m = 0; - already set */ + e = 0; + } + else if ((flt_e == 0) && (flt_m != 0)) { + /* denorm -- denorm float maps to 0 half */ + /* m = 0; - already set */ + e = 0; + } + else if ((flt_e == 0xff) && (flt_m == 0)) { + /* infinity */ + /* m = 0; - already set */ + e = 31; + } + else if ((flt_e == 0xff) && (flt_m != 0)) { + /* Retain the top bits of a NaN to make sure that the quiet/signaling + * status stays the same. + */ + m = flt_m >> 13; + if (!m) + m = 1; + e = 31; + } + else { + /* regular number */ + const int new_exp = flt_e - 127; + if (new_exp < -14) { + /* The float32 lies in the range (0.0, min_normal16) and is rounded + * to a nearby float16 value. The result will be either zero, subnormal, + * or normal. + */ + e = 0; + m = lrintf((1 << 24) * fabsf(fi.f)); + } + else if (new_exp > 15) { + /* map this value to infinity */ + /* m = 0; - already set */ + e = 31; + } + else { + /* The float32 lies in the range + * [min_normal16, max_normal16 + max_step16) + * and is rounded to a nearby float16 value. The result will be + * either normal or infinite. + */ + e = new_exp + 15; + m = lrintf(flt_m / (float) (1 << 13)); + } + } + + assert(0 <= m && m <= 1024); + if (m == 1024) { + /* The float32 was rounded upwards into the range of the next exponent, + * so bump the exponent. This correctly handles the case where f32 + * should be rounded up to float16 infinity. + */ + ++e; + m = 0; + } + + result = (s << 15) | (e << 10) | m; + return result; +} + +/** + * Convert a 2-byte half float to a 4-byte float. + * Based on code from: + * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html + */ +float +_mesa_half_to_float_slow(uint16_t val) +{ + fi_type infnan; + fi_type magic; + fi_type f32; + + infnan.u = 0x8f << 23; + infnan.f = 65536.0f; + magic.u = 0xef << 23; + + /* Exponent / Mantissa */ + f32.u = (val & 0x7fff) << 13; + + /* Adjust */ + f32.f *= magic.f; + /* XXX: The magic mul relies on denorms being available */ + + /* Inf / NaN */ + if (f32.f >= infnan.f) + f32.u |= 0xff << 23; + + /* Sign */ + f32.u |= (uint32_t)(val & 0x8000) << 16; + + return f32.f; +}
\ No newline at end of file diff --git a/tests/spec/amd_gpu_shader_half_float/half_float_util.h b/tests/spec/amd_gpu_shader_half_float/half_float_util.h new file mode 100644 index 000000000..f0f50fd22 --- /dev/null +++ b/tests/spec/amd_gpu_shader_half_float/half_float_util.h @@ -0,0 +1,33 @@ +/* + * Copyright © 2015 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef _HALF_FLOAT_H_ +#define _HALF_FLOAT_H_ + +#define FP16_ONE ((uint16_t) 0x3c00) +#define FP16_ZERO ((uint16_t) 0) + +uint16_t _mesa_float_to_half_slow(float val); +float _mesa_half_to_float_slow(uint16_t val); + +#endif /* _HALF_FLOAT_H_ */ |