[Bf-blender-cvs] [cc0e98534d9] temp-gpu-compute-shaders: More research inside test cases.

Jeroen Bakker noreply at git.blender.org
Mon Apr 26 15:15:13 CEST 2021


Commit: cc0e98534d96f482967efe86b05f516c2b6688d9
Author: Jeroen Bakker
Date:   Mon Apr 26 15:14:57 2021 +0200
Branches: temp-gpu-compute-shaders
https://developer.blender.org/rBcc0e98534d96f482967efe86b05f516c2b6688d9

More research inside test cases.

===================================================================

M	source/blender/gpu/tests/gpu_shader_test.cc

===================================================================

diff --git a/source/blender/gpu/tests/gpu_shader_test.cc b/source/blender/gpu/tests/gpu_shader_test.cc
index 3e778ca3e10..f74b5feead4 100644
--- a/source/blender/gpu/tests/gpu_shader_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_test.cc
@@ -15,7 +15,7 @@
 
 namespace blender::gpu::tests {
 
-TEST_F(GPUTest, gpu_shader_compute)
+TEST_F(GPUTest, gpu_shader_compute_2d)
 {
 
   if (!GPU_compute_shader_support()) {
@@ -43,12 +43,12 @@ void main() {
 )";
 
   GPUShader *shader = GPU_shader_create_compute(
-      compute_glsl, nullptr, nullptr, "gpu_shader_compute");
+      compute_glsl, nullptr, nullptr, "gpu_shader_compute_2d");
   EXPECT_NE(shader, nullptr);
 
   /* Create texture to store result and attach to shader. */
   GPUTexture *texture = GPU_texture_create_2d(
-      "gpu_shader_compute", SIZE, SIZE, 0, GPU_RGBA32F, nullptr);
+      "gpu_shader_compute_2d", SIZE, SIZE, 0, GPU_RGBA32F, nullptr);
   EXPECT_NE(texture, nullptr);
 
   GPU_shader_bind(shader);
@@ -76,7 +76,7 @@ void main() {
   GPU_shader_free(shader);
 }
 
-TEST_F(GPUTest, gpu_shader_compute_write_vbo)
+TEST_F(GPUTest, gpu_shader_compute_1d)
 {
 
   if (!GPU_compute_shader_support()) {
@@ -92,44 +92,108 @@ TEST_F(GPUTest, gpu_shader_compute_write_vbo)
 
 layout(local_size_x = 1) in;
 
-struct VBOData {
-  vec4 pos;
-};
+layout(rgba32f, binding = 1) uniform image1D outputVboData;
+
+void main() {
+  int index = int(gl_GlobalInvocationID.x);
+  vec4 pos = vec4(gl_GlobalInvocationID.x);
+  imageStore(outputVboData, index, pos);
+}
+
+)";
+
+  GPUShader *shader = GPU_shader_create_compute(
+      compute_glsl, nullptr, nullptr, "gpu_shader_compute_1d");
+  EXPECT_NE(shader, nullptr);
+
+  /* Construct Texture. */
+  GPUTexture *texture = GPU_texture_create_1d("gpu_shader_compute_1d", SIZE, 0, GPU_RGBA32F, NULL);
+  EXPECT_NE(texture, nullptr);
+
+  GPU_shader_bind(shader);
+  GPU_texture_image_bind(texture, GPU_shader_get_texture_binding(shader, "outputVboData"));
 
-layout(std140, binding = 1) buffer outputVboData {
-  VBOData data[];
+  /* Dispatch compute task. */
+  GPU_compute_dispatch(shader, SIZE, 1, 1);
+
+  /* Check if compute has been done. */
+  GPU_memory_barrier(GPU_BARRIER_TEXTURE_FETCH);
+
+  /* Create texture to load back result. */
+  float *data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
+  EXPECT_NE(data, nullptr);
+  for (int index = 0; index < SIZE; index++) {
+    float expected_value = index;
+    EXPECT_FLOAT_EQ(data[index * 4 + 0], expected_value);
+    EXPECT_FLOAT_EQ(data[index * 4 + 1], expected_value);
+    EXPECT_FLOAT_EQ(data[index * 4 + 2], expected_value);
+    EXPECT_FLOAT_EQ(data[index * 4 + 3], expected_value);
+  }
+  MEM_freeN(data);
+
+  /* Cleanup. */
+  GPU_shader_unbind();
+  GPU_texture_unbind(texture);
+  GPU_texture_free(texture);
+  GPU_shader_free(shader);
+}
+
+TEST_F(GPUTest, gpu_shader_compute_vbo)
+{
+
+  if (!GPU_compute_shader_support()) {
+    /* We can't test as a the platform does not support compute shaders. */
+    std::cout << "Skipping compute shader test: platform not supported";
+    return;
+  }
+
+  static constexpr uint SIZE = 128;
+
+  /* Build compute shader. */
+  const char *compute_glsl = R"(
+
+layout(local_size_x = 1) in;
+
+layout(std430, binding = 0) buffer outputVboData
+{
+  vec4 Positions[];
 };
 
 void main() {
   int index = int(gl_GlobalInvocationID.x);
   vec4 pos = vec4(gl_GlobalInvocationID.x);
-  data[index].pos = pos;
+  Positions[index] = pos;
 }
 
 )";
 
   GPUShader *shader = GPU_shader_create_compute(
-      compute_glsl, nullptr, nullptr, "gpu_shader_compute");
+      compute_glsl, nullptr, nullptr, "gpu_shader_compute_vbo");
   EXPECT_NE(shader, nullptr);
+  GPU_shader_bind(shader);
 
   /* Construct VBO. */
   static GPUVertFormat format = {0};
   GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
   /* TODO: Should add a new GPU_USAGE_TYPE. (device only) that won't upload the data. */
-  GPUVertBuf *vbo = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_STATIC);
+  GPUVertBuf *vbo = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_DYNAMIC);
   GPU_vertbuf_data_alloc(vbo, SIZE);
-
-  GPU_shader_bind(shader);
-  GPU_shader_attach_vertex_buffer(shader, vbo, 1);
+  /* Store vbo with known values for easier debugging. */
+  float dummy[4] = {0.1f, 0.2f, 0.3f, 0.4f};
+  for (int i = 0; i < SIZE; i++) {
+    GPU_vertbuf_vert_set(vbo, i, dummy);
+  }
+  GPU_vertbuf_use(vbo);
+  GPU_shader_attach_vertex_buffer(shader, vbo, 0);
 
   /* Dispatch compute task. */
   GPU_compute_dispatch(shader, SIZE, 1, 1);
 
   /* Check if compute has been done. */
-  GPU_memory_barrier(GPU_BARRIER_SHADER_STORAGE | GPU_BARRIER_VERTEX_ATTRIB_ARRAY);
+  GPU_memory_barrier(GPU_BARRIER_SHADER_STORAGE | GPU_BARRIER_TEXTURE_FETCH);
 
   /* Create texture to load back result. */
-  GPUTexture *texture = GPU_texture_create_from_vertbuf("gpu_shader_compute_write_vbo", vbo);
+  GPUTexture *texture = GPU_texture_create_from_vertbuf("gpu_shader_compute_vbo", vbo);
   EXPECT_NE(texture, nullptr);
   float *data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
   EXPECT_NE(data, nullptr);



More information about the Bf-blender-cvs mailing list