Commit: b132e3b3ce1c5ecc836d22f4c4cba90e0ed2edcc Author: Jason Fielder Date: Thu Dec 1 15:33:54 2022 +0100 Branches: master https://developer.blender.org/rBb132e3b3ce1c5ecc836d22f4c4cba90e0ed2edcc
Cycles: use GPU module for viewport display To make GPU backends other than OpenGL work. Adds required pixel buffer and fence objects to GPU module. Authored by Apple: Michael Parkin-White Ref T96261 Ref T92212 Reviewed By: fclem, brecht Differential Revision: https://developer.blender.org/D16042 =================================================================== M intern/cycles/blender/display_driver.cpp M intern/cycles/blender/display_driver.h M source/blender/gpu/GPU_shader.h M source/blender/gpu/GPU_state.h M source/blender/gpu/GPU_texture.h M source/blender/gpu/intern/gpu_backend.hh M source/blender/gpu/intern/gpu_shader.cc M source/blender/gpu/intern/gpu_state.cc M source/blender/gpu/intern/gpu_state_private.hh M source/blender/gpu/intern/gpu_texture.cc M source/blender/gpu/intern/gpu_texture_private.hh M source/blender/gpu/metal/mtl_backend.hh M source/blender/gpu/metal/mtl_backend.mm M source/blender/gpu/metal/mtl_command_buffer.mm M source/blender/gpu/metal/mtl_context.hh M source/blender/gpu/metal/mtl_state.hh M source/blender/gpu/metal/mtl_state.mm M source/blender/gpu/metal/mtl_texture.hh M source/blender/gpu/metal/mtl_texture.mm M source/blender/gpu/opengl/gl_backend.hh M source/blender/gpu/opengl/gl_state.cc M source/blender/gpu/opengl/gl_state.hh M source/blender/gpu/opengl/gl_texture.cc M source/blender/gpu/opengl/gl_texture.hh M source/blender/render/RE_engine.h M source/blender/render/intern/engine.cc =================================================================== diff --git a/intern/cycles/blender/display_driver.cpp b/intern/cycles/blender/display_driver.cpp index e2be4f85a9b..2469046eccf 100644 --- a/intern/cycles/blender/display_driver.cpp +++ b/intern/cycles/blender/display_driver.cpp @@ -5,9 +5,14 @@ #include "device/device.h" #include "util/log.h" +#include "util/math.h" #include "util/opengl.h" -#include "GPU_platform.h" +#include "GPU_context.h" +#include "GPU_immediate.h" +#include "GPU_shader.h" +#include "GPU_state.h" +#include "GPU_texture.h" #include "RE_engine.h" @@ -30,8 +35,9 @@ unique_ptr<BlenderDisplayShader> BlenderDisplayShader::create(BL::RenderEngine & int BlenderDisplayShader::get_position_attrib_location() { if (position_attribute_location_ == -1) { - const uint shader_program = get_shader_program(); - position_attribute_location_ = glGetAttribLocation(shader_program, position_attribute_name); + GPUShader *shader_program = get_shader_program(); + position_attribute_location_ = GPU_shader_get_attribute(shader_program, + position_attribute_name); } return position_attribute_location_; } @@ -39,8 +45,9 @@ int BlenderDisplayShader::get_position_attrib_location() int BlenderDisplayShader::get_tex_coord_attrib_location() { if (tex_coord_attribute_location_ == -1) { - const uint shader_program = get_shader_program(); - tex_coord_attribute_location_ = glGetAttribLocation(shader_program, tex_coord_attribute_name); + GPUShader *shader_program = get_shader_program(); + tex_coord_attribute_location_ = GPU_shader_get_attribute(shader_program, + tex_coord_attribute_name); } return tex_coord_attribute_location_; } @@ -79,100 +86,42 @@ static const char *FALLBACK_FRAGMENT_SHADER = " fragColor = texture(image_texture, texCoord_interp);\n" "}\n\0"; -static void shader_print_errors(const char *task, const char *log, const char *code) +static GPUShader *compile_fallback_shader(void) { - LOG(ERROR) << "Shader: " << task << " error:"; - LOG(ERROR) << "===== shader string ===="; - - stringstream stream(code); - string partial; - - int line = 1; - while (getline(stream, partial, '\n')) { - if (line < 10) { - LOG(ERROR) << " " << line << " " << partial; - } - else { - LOG(ERROR) << line << " " << partial; - } - line++; - } - LOG(ERROR) << log; -} - -static int compile_fallback_shader(void) -{ - const struct Shader { - const char *source; - const GLenum type; - } shaders[2] = {{FALLBACK_VERTEX_SHADER, GL_VERTEX_SHADER}, - {FALLBACK_FRAGMENT_SHADER, GL_FRAGMENT_SHADER}}; - - const GLuint program = glCreateProgram(); - - for (int i = 0; i < 2; i++) { - const GLuint shader = glCreateShader(shaders[i].type); - - string source_str = shaders[i].source; - const char *c_str = source_str.c_str(); - - glShaderSource(shader, 1, &c_str, NULL); - glCompileShader(shader); - - GLint compile_status; - glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); - - if (!compile_status) { - GLchar log[5000]; - GLsizei length = 0; - glGetShaderInfoLog(shader, sizeof(log), &length, log); - shader_print_errors("compile", log, c_str); - return 0; - } - - glAttachShader(program, shader); - } - - /* Link output. */ - glBindFragDataLocation(program, 0, "fragColor"); - - /* Link and error check. */ - glLinkProgram(program); - - /* TODO(sergey): Find a way to nicely de-duplicate the error checking. */ - GLint link_status; - glGetProgramiv(program, GL_LINK_STATUS, &link_status); - if (!link_status) { - GLchar log[5000]; - GLsizei length = 0; - /* TODO(sergey): Is it really program passed to glGetShaderInfoLog? */ - glGetShaderInfoLog(program, sizeof(log), &length, log); - shader_print_errors("linking", log, FALLBACK_VERTEX_SHADER); - shader_print_errors("linking", log, FALLBACK_FRAGMENT_SHADER); - return 0; - } - - return program; + /* NOTE: Compilation errors are logged to console. */ + GPUShader *shader = GPU_shader_create(FALLBACK_VERTEX_SHADER, + FALLBACK_FRAGMENT_SHADER, + nullptr, + nullptr, + nullptr, + "FallbackCyclesBlitShader"); + return shader; } -void BlenderFallbackDisplayShader::bind(int width, int height) +GPUShader *BlenderFallbackDisplayShader::bind(int width, int height) { create_shader_if_needed(); if (!shader_program_) { - return; + return nullptr; } - glUseProgram(shader_program_); - glUniform1i(image_texture_location_, 0); - glUniform2f(fullscreen_location_, width, height); + /* Bind shader now to enable uniform assignment. */ + GPU_shader_bind(shader_program_); + GPU_shader_uniform_int(shader_program_, image_texture_location_, 0); + float size[2]; + size[0] = width; + size[1] = height; + GPU_shader_uniform_vector(shader_program_, fullscreen_location_, 2, 1, size); + return shader_program_; } void BlenderFallbackDisplayShader::unbind() { + GPU_shader_unbind(); } -uint BlenderFallbackDisplayShader::get_shader_program() +GPUShader *BlenderFallbackDisplayShader::get_shader_program() { return shader_program_; } @@ -187,19 +136,18 @@ void BlenderFallbackDisplayShader::create_shader_if_needed() shader_program_ = compile_fallback_shader(); if (!shader_program_) { + LOG(ERROR) << "Failed to compile fallback shader"; return; } - glUseProgram(shader_program_); - - image_texture_location_ = glGetUniformLocation(shader_program_, "image_texture"); + image_texture_location_ = GPU_shader_get_uniform(shader_program_, "image_texture"); if (image_texture_location_ < 0) { LOG(ERROR) << "Shader doesn't contain the 'image_texture' uniform."; destroy_shader(); return; } - fullscreen_location_ = glGetUniformLocation(shader_program_, "fullscreen"); + fullscreen_location_ = GPU_shader_get_uniform(shader_program_, "fullscreen"); if (fullscreen_location_ < 0) { LOG(ERROR) << "Shader doesn't contain the 'fullscreen' uniform."; destroy_shader(); @@ -209,8 +157,10 @@ void BlenderFallbackDisplayShader::create_shader_if_needed() void BlenderFallbackDisplayShader::destroy_shader() { - glDeleteProgram(shader_program_); - shader_program_ = 0; + if (shader_program_) { + GPU_shader_free(shader_program_); + shader_program_ = nullptr; + } } /* -------------------------------------------------------------------- @@ -224,9 +174,10 @@ BlenderDisplaySpaceShader::BlenderDisplaySpaceShader(BL::RenderEngine &b_engine, DCHECK(b_engine_.support_display_space_shader(b_scene_)); } -void BlenderDisplaySpaceShader::bind(int /*width*/, int /*height*/) +GPUShader *BlenderDisplaySpaceShader::bind(int /*width*/, int /*height*/) { b_engine_.bind_display_space_shader(b_scene_); + return GPU_shader_get_bound(); } void BlenderDisplaySpaceShader::unbind() @@ -234,12 +185,11 @@ void BlenderDisplaySpaceShader::unbind() b_engine_.unbind_display_space_shader(); } -uint BlenderDisplaySpaceShader::get_shader_program() +GPUShader *BlenderDisplaySpaceShader::get_shader_program() { if (!shader_program_) { - glGetIntegerv(GL_CURRENT_PROGRAM, reinterpret_cast<int *>(&shader_program_)); + shader_program_ = GPU_shader_get_bound(); } - if (!shader_program_) { LOG(ERROR) << "Error retrieving shader program for display space shader."; } @@ -252,34 +202,34 @@ uint BlenderDisplaySpaceShader::get_shader_program() */ /* Higher level representation of a texture from the graphics library. */ -class GLTexture { +class DisplayGPUTexture { public: - /* Global counter for all allocated OpenGL textures used by instances of this class. */ + /* Global counter for all allocated GPUTextures used by instances of this class. */ static inline std::atomic<int> num_used = 0; - GLTexture() = default; + DisplayGPUTexture() = default; - ~GLTexture() + ~DisplayGPUTexture() { - assert(gl_id == 0); + assert(gpu_texture == nullptr); } - GLTexture(const GLTexture &other) = delete; - GLTexture &operator=(GLTexture &other) = delete; + DisplayGPUTexture(const DisplayGPUTexture &other) = delete; + DisplayGPUTexture &operator=(DisplayGPUTexture &other) = delete; - GLTexture(GLTexture &&other) noexcept - : gl_id(other.gl_id), width(other.width), height(other.height) + DisplayGPUTexture(DisplayGPUTexture &&other) noexcept + : gpu_texture(other.gpu_texture), width(other.width), height(other.height) { other.reset(); } - GLTexture &operator=(GLTexture &&other) + DisplayGPUTexture &operator=(DisplayGPUTexture &&other) { if (this == &other) { return *this; } - gl_id = other.gl_id; + gpu_texture = other.gpu_texture; width = other.width; height = other.height; @@ -288,55 +238,56 @@ class GLTexture { return *this; } - bool gl_resources_ensure() + bool gpu_resources_ensure() { - if (gl_id) { + if (gpu_texture) { return true; } - /* Create texture. */ - glGenTextures(1, &gl_id); - if (!gl_id) { + /* Texture must have a minimum size of 1x1. */ + gpu_texture = GPU_texture_create_2d( + "CyclesBlitTexture", max(width, 1), max(height, 1), 1, GPU_RGBA16F, nullptr); + + if (!gpu_texture) { LOG(ERROR) << "Error creating texture."; return false; } - /* Configure the texture. */ - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, gl_id); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - /* Clamp to edge so that precision issues when zoomed out (which forces linear interpolation) - * does not cause unwanted repetition. */ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - glBindTexture(GL_TEXTURE_2D, 0); + GPU_texture_filter_mode(g @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs
