[Bf-blender-cvs] [04d78564598] temp-gpu-compute-shaders: Use proposed GPU_compute_dispatch
Jeroen Bakker
noreply at git.blender.org
Fri Apr 23 10:26:21 CEST 2021
Commit: 04d78564598b036f84b383700e2d6ad0f6715ca1
Author: Jeroen Bakker
Date: Fri Apr 9 11:03:24 2021 +0200
Branches: temp-gpu-compute-shaders
https://developer.blender.org/rB04d78564598b036f84b383700e2d6ad0f6715ca1
Use proposed GPU_compute_dispatch
===================================================================
M source/blender/gpu/GPU_compute.h
M source/blender/gpu/intern/gpu_compute.cc
M source/blender/gpu/opengl/gl_backend.hh
M source/blender/gpu/tests/gpu_shader_test.cc
===================================================================
diff --git a/source/blender/gpu/GPU_compute.h b/source/blender/gpu/GPU_compute.h
index ec81e60ec5f..88230e8a065 100644
--- a/source/blender/gpu/GPU_compute.h
+++ b/source/blender/gpu/GPU_compute.h
@@ -20,11 +20,13 @@
#pragma once
+#include "GPU_shader.h"
+
#ifdef __cplusplus
extern "C" {
#endif
-void GPU_compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len);
+void GPU_compute_dispatch(GPUShader *shader, int groups_x_len, int groups_y_len, int groups_z_len);
#ifdef __cplusplus
}
diff --git a/source/blender/gpu/intern/gpu_compute.cc b/source/blender/gpu/intern/gpu_compute.cc
index e07a17d14a4..6c5f6bafee1 100644
--- a/source/blender/gpu/intern/gpu_compute.cc
+++ b/source/blender/gpu/intern/gpu_compute.cc
@@ -26,9 +26,10 @@
extern "C" {
#endif
-void GPU_compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
+void GPU_compute_dispatch(GPUShader *shader, int groups_x_len, int groups_y_len, int groups_z_len)
{
blender::gpu::GPUBackend &gpu_backend = *blender::gpu::GPUBackend::get();
+ GPU_shader_bind(shader);
gpu_backend.compute_dispatch(groups_x_len, groups_y_len, groups_z_len);
}
diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh
index 7a11a11dd8f..e9dcdffced0 100644
--- a/source/blender/gpu/opengl/gl_backend.hh
+++ b/source/blender/gpu/opengl/gl_backend.hh
@@ -129,6 +129,7 @@ class GLBackend : public GPUBackend {
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) override
{
+ GLContext::get()->state_manager_active_get()->apply_state();
GLCompute::dispatch(groups_x_len, groups_y_len, groups_z_len);
}
diff --git a/source/blender/gpu/tests/gpu_shader_test.cc b/source/blender/gpu/tests/gpu_shader_test.cc
index 957210e2e40..9245aad1416 100644
--- a/source/blender/gpu/tests/gpu_shader_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_test.cc
@@ -23,17 +23,20 @@ TEST_F(GPUTest, gpu_shader_compute)
}
static constexpr int SIZE = 512;
+
+ /* Build compute shader. */
const char *compute_glsl = R"(
layout(local_size_x = 1, local_size_y = 1) in;
-layout(rgba32f, binding = 0) uniform image1D img_output;
+layout(rgba32f, binding = 0) uniform image2D img_output;
void main() {
// base pixel colour for image
vec4 pixel = vec4(1.0, 0.5, 0.2, 1.0);
// output to a specific pixel in the image
- imageStore(img_output, int(gl_GlobalInvocationID.x), pixel);}
+ imageStore(img_output, ivec2(gl_GlobalInvocationID.xy), pixel);
+}
)";
@@ -41,30 +44,32 @@ void main() {
compute_glsl, nullptr, nullptr, "gpu_shader_compute");
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);
EXPECT_NE(texture, nullptr);
GPU_shader_bind(shader);
- int binding = GPU_shader_get_texture_binding(shader, "img_output");
- GPU_texture_image_bind(texture, binding);
- GPU_compute_dispatch(SIZE, SIZE, 1);
+ GPU_texture_image_bind(texture, GPU_shader_get_texture_binding(shader, "img_output"));
- GPU_memory_barrier(GPU_BARRIER_SHADER_IMAGE_ACCESS);
+ /* Dispatch compute task. */
+ GPU_compute_dispatch(shader, SIZE, SIZE, 1);
+ /* Check if compute has been done. */
float *data = static_cast<float *>(GPU_texture_read(texture, GPU_DATA_FLOAT, 0));
EXPECT_NE(data, nullptr);
-
- for (int index = 0; index < SIZE * SIZE * 4; index++) {
- printf("%d: %f\n", index, data[index]);
+ for (int index = 0; index < SIZE * SIZE; index++) {
+ EXPECT_FLOAT_EQ(data[index * 4 + 0], 1.0f);
+ EXPECT_FLOAT_EQ(data[index * 4 + 1], 0.5f);
+ EXPECT_FLOAT_EQ(data[index * 4 + 2], 0.2f);
+ EXPECT_FLOAT_EQ(data[index * 4 + 3], 1.0f);
}
-
MEM_freeN(data);
+ /* Cleanup. */
GPU_shader_unbind();
GPU_texture_unbind(texture);
GPU_texture_free(texture);
-
GPU_shader_free(shader);
}
More information about the Bf-blender-cvs
mailing list