This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/gpu/intern/gpu_shader.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

783 lines
22 KiB
C++
Raw Normal View History

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup gpu
2018-08-30 01:56:08 +10:00
*/
#include "MEM_guardedalloc.h"
#include "BLI_string_utils.h"
#include "GPU_capabilities.h"
#include "GPU_matrix.h"
#include "GPU_platform.h"
2020-08-14 15:20:35 +02:00
#include "gpu_backend.hh"
#include "gpu_context_private.hh"
GPUShaderCreateInfo for interface abstraction This is a first part of the Shader Create Info system could be. A shader create info provides a way to define shader structure, resources and interfaces. This makes for a quick way to provide backend agnostic binding informations while also making shader variations easy to declare. - Clear source input (only one file). Cleans up the GPU api since we can create a shader from one descriptor - Resources and interfaces are generated by the backend (much simpler than parsing). - Bindings are explicit from position in the array. - GPUShaderInterface becomes a trivial translation of enums and string copy. - No external dependency to third party lib. - Cleaner code, less fragmentation of resources in several libs. - Easy to modify / extend at runtime. - no parser involve, very easy to code. - Does not hold any data, can be static and kept on disc. - Could hold precompiled bytecode for static shaders. This also includes a new global dependency system. GLSL shaders can include other sources by using #pragma BLENDER_REQUIRE(...). This patch already migrated several builtin shaders. Other shaders should be migrated one at a time, and could be done inside master. There is a new compile directive `WITH_GPU_SHADER_BUILDER` this is an optional directive for linting shaders to increase turn around time. What is remaining: - pyGPU API {T94975} - Migration of other shaders. This could be a community effort. Reviewed By: jbakker Maniphest Tasks: T94975 Differential Revision: https://developer.blender.org/D13360
2022-01-17 14:32:03 +01:00
#include "gpu_shader_create_info.hh"
#include "gpu_shader_create_info_private.hh"
#include "gpu_shader_dependency_private.h"
2020-08-14 15:20:35 +02:00
#include "gpu_shader_private.hh"
GPUShaderCreateInfo for interface abstraction This is a first part of the Shader Create Info system could be. A shader create info provides a way to define shader structure, resources and interfaces. This makes for a quick way to provide backend agnostic binding informations while also making shader variations easy to declare. - Clear source input (only one file). Cleans up the GPU api since we can create a shader from one descriptor - Resources and interfaces are generated by the backend (much simpler than parsing). - Bindings are explicit from position in the array. - GPUShaderInterface becomes a trivial translation of enums and string copy. - No external dependency to third party lib. - Cleaner code, less fragmentation of resources in several libs. - Easy to modify / extend at runtime. - no parser involve, very easy to code. - Does not hold any data, can be static and kept on disc. - Could hold precompiled bytecode for static shaders. This also includes a new global dependency system. GLSL shaders can include other sources by using #pragma BLENDER_REQUIRE(...). This patch already migrated several builtin shaders. Other shaders should be migrated one at a time, and could be done inside master. There is a new compile directive `WITH_GPU_SHADER_BUILDER` this is an optional directive for linting shaders to increase turn around time. What is remaining: - pyGPU API {T94975} - Migration of other shaders. This could be a community effort. Reviewed By: jbakker Maniphest Tasks: T94975 Differential Revision: https://developer.blender.org/D13360
2022-01-17 14:32:03 +01:00
#include <string>
2020-07-29 16:07:51 +02:00
extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[];
GPUShaderCreateInfo for interface abstraction This is a first part of the Shader Create Info system could be. A shader create info provides a way to define shader structure, resources and interfaces. This makes for a quick way to provide backend agnostic binding informations while also making shader variations easy to declare. - Clear source input (only one file). Cleans up the GPU api since we can create a shader from one descriptor - Resources and interfaces are generated by the backend (much simpler than parsing). - Bindings are explicit from position in the array. - GPUShaderInterface becomes a trivial translation of enums and string copy. - No external dependency to third party lib. - Cleaner code, less fragmentation of resources in several libs. - Easy to modify / extend at runtime. - no parser involve, very easy to code. - Does not hold any data, can be static and kept on disc. - Could hold precompiled bytecode for static shaders. This also includes a new global dependency system. GLSL shaders can include other sources by using #pragma BLENDER_REQUIRE(...). This patch already migrated several builtin shaders. Other shaders should be migrated one at a time, and could be done inside master. There is a new compile directive `WITH_GPU_SHADER_BUILDER` this is an optional directive for linting shaders to increase turn around time. What is remaining: - pyGPU API {T94975} - Migration of other shaders. This could be a community effort. Reviewed By: jbakker Maniphest Tasks: T94975 Differential Revision: https://developer.blender.org/D13360
2022-01-17 14:32:03 +01:00
namespace blender::gpu {
std::string Shader::defines_declare(const shader::ShaderCreateInfo &info) const
{
std::string defines;
for (const auto &def : info.defines_) {
defines += "#define ";
defines += def[0];
defines += " ";
defines += def[1];
defines += "\n";
}
return defines;
}
} // namespace blender::gpu
2020-08-14 15:20:35 +02:00
using namespace blender;
using namespace blender::gpu;
static bool gpu_shader_srgb_uniform_dirty_get();
2020-08-14 15:20:35 +02:00
/* -------------------------------------------------------------------- */
/** \name Creation / Destruction
* \{ */
Shader::Shader(const char *sh_name)
{
2020-08-14 15:20:35 +02:00
BLI_strncpy(this->name, sh_name, sizeof(this->name));
}
2020-08-14 15:20:35 +02:00
Shader::~Shader()
{
delete interface;
}
2020-08-14 15:20:35 +02:00
static void standard_defines(Vector<const char *> &sources)
{
2020-08-14 15:20:35 +02:00
BLI_assert(sources.size() == 0);
/* Version needs to be first. Exact values will be added by implementation. */
sources.append("version");
GPUShaderCreateInfo for interface abstraction This is a first part of the Shader Create Info system could be. A shader create info provides a way to define shader structure, resources and interfaces. This makes for a quick way to provide backend agnostic binding informations while also making shader variations easy to declare. - Clear source input (only one file). Cleans up the GPU api since we can create a shader from one descriptor - Resources and interfaces are generated by the backend (much simpler than parsing). - Bindings are explicit from position in the array. - GPUShaderInterface becomes a trivial translation of enums and string copy. - No external dependency to third party lib. - Cleaner code, less fragmentation of resources in several libs. - Easy to modify / extend at runtime. - no parser involve, very easy to code. - Does not hold any data, can be static and kept on disc. - Could hold precompiled bytecode for static shaders. This also includes a new global dependency system. GLSL shaders can include other sources by using #pragma BLENDER_REQUIRE(...). This patch already migrated several builtin shaders. Other shaders should be migrated one at a time, and could be done inside master. There is a new compile directive `WITH_GPU_SHADER_BUILDER` this is an optional directive for linting shaders to increase turn around time. What is remaining: - pyGPU API {T94975} - Migration of other shaders. This could be a community effort. Reviewed By: jbakker Maniphest Tasks: T94975 Differential Revision: https://developer.blender.org/D13360
2022-01-17 14:32:03 +01:00
/* Define to identify code usage in shading language. */
sources.append("#define GPU_SHADER\n");
/* some useful defines to detect GPU type */
2019-04-22 09:32:37 +10:00
if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define GPU_ATI\n");
2019-04-22 09:32:37 +10:00
}
else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define GPU_NVIDIA\n");
2019-04-22 09:32:37 +10:00
}
else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define GPU_INTEL\n");
2019-04-22 09:32:37 +10:00
}
/* some useful defines to detect OS type */
2019-04-22 09:32:37 +10:00
if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_WIN, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define OS_WIN\n");
2019-04-22 09:32:37 +10:00
}
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_MAC, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define OS_MAC\n");
2019-04-22 09:32:37 +10:00
}
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
2020-08-14 15:20:35 +02:00
sources.append("#define OS_UNIX\n");
}
2020-08-14 15:20:35 +02:00
if (GPU_crappy_amd_driver()) {
sources.append("#define GPU_DEPRECATED_AMD_DRIVER\n");
}
}
2020-08-14 15:20:35 +02:00
GPUShader *GPU_shader_create_ex(const char *vertcode,
2018-07-18 23:09:31 +10:00
const char *fragcode,
2020-08-14 15:20:35 +02:00
const char *geomcode,
const char *computecode,
2018-07-18 23:09:31 +10:00
const char *libcode,
const char *defines,
const eGPUShaderTFBType tf_type,
2018-07-18 23:09:31 +10:00
const char **tf_names,
const int tf_count,
const char *shname)
{
/* At least a vertex shader and a fragment shader are required, or only a compute shader. */
BLI_assert(((fragcode != nullptr) && (vertcode != nullptr) && (computecode == nullptr)) ||
((fragcode == nullptr) && (vertcode == nullptr) && (geomcode == nullptr) &&
(computecode != nullptr)));
2020-08-14 15:20:35 +02:00
Shader *shader = GPUBackend::get()->shader_alloc(shname);
2020-08-14 15:20:35 +02:00
if (vertcode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_VERTEX_SHADER\n");
sources.append("#define IN_OUT out\n");
if (geomcode) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
2019-04-22 09:32:37 +10:00
if (defines) {
2020-08-14 15:20:35 +02:00
sources.append(defines);
2019-04-22 09:32:37 +10:00
}
2020-08-14 15:20:35 +02:00
sources.append(vertcode);
2020-08-14 15:20:35 +02:00
shader->vertex_shader_from_glsl(sources);
}
if (fragcode) {
2020-08-14 15:20:35 +02:00
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_FRAGMENT_SHADER\n");
sources.append("#define IN_OUT in\n");
if (geomcode) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
2019-04-22 09:32:37 +10:00
if (defines) {
2020-08-14 15:20:35 +02:00
sources.append(defines);
2019-04-22 09:32:37 +10:00
}
if (libcode) {
2020-08-14 15:20:35 +02:00
sources.append(libcode);
2019-04-22 09:32:37 +10:00
}
2020-08-14 15:20:35 +02:00
sources.append(fragcode);
2020-08-14 15:20:35 +02:00
shader->fragment_shader_from_glsl(sources);
}
2020-08-14 15:20:35 +02:00
if (geomcode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_GEOMETRY_SHADER\n");
2019-04-22 09:32:37 +10:00
if (defines) {
2020-08-14 15:20:35 +02:00
sources.append(defines);
2019-04-22 09:32:37 +10:00
}
2020-08-14 15:20:35 +02:00
sources.append(geomcode);
2020-08-14 15:20:35 +02:00
shader->geometry_shader_from_glsl(sources);
}
if (computecode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_COMPUTE_SHADER\n");
if (defines) {
sources.append(defines);
}
if (libcode) {
sources.append(libcode);
}
sources.append(computecode);
shader->compute_shader_from_glsl(sources);
}
if (tf_names != nullptr && tf_count > 0) {
BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
2020-08-14 15:20:35 +02:00
shader->transform_feedback_names_set(Span<const char *>(tf_names, tf_count), tf_type);
}
2020-08-14 15:20:35 +02:00
if (!shader->finalize()) {
delete shader;
return nullptr;
2020-08-14 15:20:35 +02:00
};
return wrap(shader);
2020-08-14 15:20:35 +02:00
}
2020-08-14 15:20:35 +02:00
void GPU_shader_free(GPUShader *shader)
{
delete unwrap(shader);
}
2020-08-14 15:20:35 +02:00
/** \} */
2020-08-14 15:20:35 +02:00
/* -------------------------------------------------------------------- */
/** \name Creation utils
* \{ */
GPUShader *GPU_shader_create(const char *vertcode,
const char *fragcode,
const char *geomcode,
const char *libcode,
const char *defines,
const char *shname)
{
return GPU_shader_create_ex(vertcode,
fragcode,
geomcode,
nullptr,
libcode,
defines,
GPU_SHADER_TFB_NONE,
nullptr,
0,
shname);
}
GPUShader *GPU_shader_create_compute(const char *computecode,
const char *libcode,
const char *defines,
const char *shname)
{
return GPU_shader_create_ex(nullptr,
nullptr,
nullptr,
computecode,
libcode,
defines,
GPU_SHADER_TFB_NONE,
nullptr,
0,
shname);
2020-08-14 15:20:35 +02:00
}
GPUShaderCreateInfo for interface abstraction This is a first part of the Shader Create Info system could be. A shader create info provides a way to define shader structure, resources and interfaces. This makes for a quick way to provide backend agnostic binding informations while also making shader variations easy to declare. - Clear source input (only one file). Cleans up the GPU api since we can create a shader from one descriptor - Resources and interfaces are generated by the backend (much simpler than parsing). - Bindings are explicit from position in the array. - GPUShaderInterface becomes a trivial translation of enums and string copy. - No external dependency to third party lib. - Cleaner code, less fragmentation of resources in several libs. - Easy to modify / extend at runtime. - no parser involve, very easy to code. - Does not hold any data, can be static and kept on disc. - Could hold precompiled bytecode for static shaders. This also includes a new global dependency system. GLSL shaders can include other sources by using #pragma BLENDER_REQUIRE(...). This patch already migrated several builtin shaders. Other shaders should be migrated one at a time, and could be done inside master. There is a new compile directive `WITH_GPU_SHADER_BUILDER` this is an optional directive for linting shaders to increase turn around time. What is remaining: - pyGPU API {T94975} - Migration of other shaders. This could be a community effort. Reviewed By: jbakker Maniphest Tasks: T94975 Differential Revision: https://developer.blender.org/D13360
2022-01-17 14:32:03 +01:00
GPUShader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
{
using namespace blender::gpu::shader;
const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info);
const_cast<ShaderCreateInfo &>(info).finalize();
/* At least a vertex shader and a fragment shader are required, or only a compute shader. */
if (info.compute_source_.is_empty()) {
if (info.vertex_source_.is_empty()) {
printf("Missing vertex shader in %s.\n", info.name_.c_str());
}
if (info.fragment_source_.is_empty()) {
printf("Missing fragment shader in %s.\n", info.name_.c_str());
}
BLI_assert(!info.vertex_source_.is_empty() && !info.fragment_source_.is_empty());
}
else {
if (!info.vertex_source_.is_empty()) {
printf("Compute shader has vertex_source_ shader attached in %s.\n", info.name_.c_str());
}
if (!info.geometry_source_.is_empty()) {
printf("Compute shader has geometry_source_ shader attached in %s.\n", info.name_.c_str());
}
if (!info.fragment_source_.is_empty()) {
printf("Compute shader has fragment_source_ shader attached in %s.\n", info.name_.c_str());
}
BLI_assert(info.vertex_source_.is_empty() && info.geometry_source_.is_empty() &&
info.fragment_source_.is_empty());
}
Shader *shader = GPUBackend::get()->shader_alloc(info.name_.c_str());
std::string defines = shader->defines_declare(info);
std::string resources = shader->resources_declare(info);
char *shader_shared_utils = nullptr;
defines += "#define USE_GPU_SHADER_CREATE_INFO\n";
Vector<char *> typedefs;
for (auto filename : info.typedef_sources_) {
typedefs.append(gpu_shader_dependency_get_source(filename.c_str()));
}
if (!typedefs.is_empty()) {
shader_shared_utils = gpu_shader_dependency_get_source("gpu_shader_shared_utils.h");
}
if (!info.vertex_source_.is_empty()) {
uint32_t builtins = 0;
std::string interface = shader->vertex_interface_declare(info);
char *code = gpu_shader_dependency_get_resolved_source(info.vertex_source_.c_str(), &builtins);
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_VERTEX_SHADER\n");
if (!info.geometry_source_.is_empty()) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
sources.append(defines.c_str());
if (!typedefs.is_empty()) {
sources.append(shader_shared_utils);
}
for (auto *types : typedefs) {
sources.append(types);
}
sources.append(resources.c_str());
sources.append(interface.c_str());
sources.append(code);
shader->vertex_shader_from_glsl(sources);
free(code);
}
if (!info.fragment_source_.is_empty()) {
uint32_t builtins = 0;
std::string interface = shader->fragment_interface_declare(info);
char *code = gpu_shader_dependency_get_resolved_source(info.fragment_source_.c_str(),
&builtins);
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_FRAGMENT_SHADER\n");
if (!info.geometry_source_.is_empty()) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
sources.append(defines.c_str());
if (!typedefs.is_empty()) {
sources.append(shader_shared_utils);
}
for (auto *types : typedefs) {
sources.append(types);
}
sources.append(resources.c_str());
sources.append(interface.c_str());
sources.append(code);
shader->fragment_shader_from_glsl(sources);
free(code);
}
if (!info.geometry_source_.is_empty()) {
uint32_t builtins = 0;
std::string interface = shader->geometry_interface_declare(info);
std::string layout = shader->geometry_layout_declare(info);
char *code = gpu_shader_dependency_get_resolved_source(info.geometry_source_.c_str(),
&builtins);
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_GEOMETRY_SHADER\n");
sources.append(defines.c_str());
if (!typedefs.is_empty()) {
sources.append(shader_shared_utils);
}
for (auto *types : typedefs) {
sources.append(types);
}
sources.append(resources.c_str());
sources.append(layout.c_str());
sources.append(interface.c_str());
sources.append(code);
shader->geometry_shader_from_glsl(sources);
free(code);
}
if (!info.compute_source_.is_empty()) {
uint32_t builtins = 0;
char *code = gpu_shader_dependency_get_resolved_source(info.compute_source_.c_str(),
&builtins);
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_COMPUTE_SHADER\n");
sources.append(defines.c_str());
if (!typedefs.is_empty()) {
sources.append(shader_shared_utils);
}
for (auto *types : typedefs) {
sources.append(types);
}
sources.append(resources.c_str());
sources.append(code);
shader->compute_shader_from_glsl(sources);
free(code);
}
for (auto *types : typedefs) {
free(types);
}
if (shader_shared_utils) {
free(shader_shared_utils);
}
if (!shader->finalize(&info)) {
delete shader;
return nullptr;
}
return wrap(shader);
}
2020-08-14 15:20:35 +02:00
GPUShader *GPU_shader_create_from_python(const char *vertcode,
const char *fragcode,
const char *geomcode,
const char *libcode,
const char *defines,
const char *name)
2020-08-14 15:20:35 +02:00
{
char *libcodecat = nullptr;
2020-08-14 15:20:35 +02:00
if (libcode == nullptr) {
2020-08-14 15:20:35 +02:00
libcode = datatoc_gpu_shader_colorspace_lib_glsl;
}
2020-08-14 15:20:35 +02:00
else {
libcode = libcodecat = BLI_strdupcat(libcode, datatoc_gpu_shader_colorspace_lib_glsl);
}
/* Use pyGPUShader as default name for shader. */
const char *shname = name != nullptr ? name : "pyGPUShader";
2020-08-14 15:20:35 +02:00
GPUShader *sh = GPU_shader_create_ex(vertcode,
fragcode,
geomcode,
nullptr,
libcode,
defines,
GPU_SHADER_TFB_NONE,
nullptr,
0,
shname);
2020-08-14 15:20:35 +02:00
MEM_SAFE_FREE(libcodecat);
return sh;
}
static const char *string_join_array_maybe_alloc(const char **str_arr, bool *r_is_alloc)
{
bool is_alloc = false;
if (str_arr == nullptr) {
*r_is_alloc = false;
return nullptr;
}
/* Skip empty strings (avoid alloc if we can). */
while (str_arr[0] && str_arr[0][0] == '\0') {
str_arr++;
}
int i;
for (i = 0; str_arr[i]; i++) {
if (i != 0 && str_arr[i][0] != '\0') {
is_alloc = true;
}
}
*r_is_alloc = is_alloc;
if (is_alloc) {
return BLI_string_join_arrayN(str_arr, i);
}
return str_arr[0];
}
struct GPUShader *GPU_shader_create_from_arrays_impl(
const struct GPU_ShaderCreateFromArray_Params *params, const char *func, int line)
{
2019-03-16 19:48:28 +01:00
struct {
const char *str;
bool is_alloc;
} str_dst[4] = {{nullptr}};
const char **str_src[4] = {params->vert, params->frag, params->geom, params->defs};
for (int i = 0; i < ARRAY_SIZE(str_src); i++) {
str_dst[i].str = string_join_array_maybe_alloc(str_src[i], &str_dst[i].is_alloc);
}
char name[64];
BLI_snprintf(name, sizeof(name), "%s_%d", func, line);
GPUShader *sh = GPU_shader_create(
str_dst[0].str, str_dst[1].str, str_dst[2].str, nullptr, str_dst[3].str, name);
for (auto &i : str_dst) {
if (i.is_alloc) {
MEM_freeN((void *)i.str);
}
}
return sh;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Binding
* \{ */
2020-08-14 15:20:35 +02:00
void GPU_shader_bind(GPUShader *gpu_shader)
{
Shader *shader = unwrap(gpu_shader);
Context *ctx = Context::get();
if (ctx->shader != shader) {
ctx->shader = shader;
2020-08-14 15:20:35 +02:00
shader->bind();
GPU_matrix_bind(gpu_shader);
GPU_shader_set_srgb_uniform(gpu_shader);
}
else {
if (gpu_shader_srgb_uniform_dirty_get()) {
GPU_shader_set_srgb_uniform(gpu_shader);
}
if (GPU_matrix_dirty_get()) {
GPU_matrix_bind(gpu_shader);
}
}
}
void GPU_shader_unbind()
{
#ifndef NDEBUG
Context *ctx = Context::get();
2020-08-14 15:20:35 +02:00
if (ctx->shader) {
ctx->shader->unbind();
2020-08-14 15:20:35 +02:00
}
ctx->shader = nullptr;
#endif
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Shader name
* \{ */
const char *GPU_shader_get_name(GPUShader *shader)
{
return unwrap(shader)->name_get();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform feedback
2020-08-14 15:20:35 +02:00
*
* TODO(fclem): Should be replaced by compute shaders.
* \{ */
2020-08-14 15:20:35 +02:00
bool GPU_shader_transform_feedback_enable(GPUShader *shader, GPUVertBuf *vertbuf)
{
return unwrap(shader)->transform_feedback_enable(vertbuf);
}
2020-08-14 15:20:35 +02:00
void GPU_shader_transform_feedback_disable(GPUShader *shader)
{
unwrap(shader)->transform_feedback_disable();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Uniforms / Resource location
* \{ */
int GPU_shader_get_uniform(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *uniform = interface->uniform_get(name);
return uniform ? uniform->location : -1;
}
int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
{
ShaderInterface *interface = unwrap(shader)->interface;
return interface->uniform_builtin((GPUUniformBuiltin)builtin);
}
int GPU_shader_get_builtin_block(GPUShader *shader, int builtin)
{
ShaderInterface *interface = unwrap(shader)->interface;
return interface->ubo_builtin((GPUUniformBlockBuiltin)builtin);
}
int GPU_shader_get_ssbo(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *ssbo = interface->ssbo_get(name);
return ssbo ? ssbo->location : -1;
}
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *ubo = interface->ubo_get(name);
2017-10-06 14:57:21 +02:00
return ubo ? ubo->location : -1;
}
int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *ubo = interface->ubo_get(name);
return ubo ? ubo->binding : -1;
}
int GPU_shader_get_texture_binding(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *tex = interface->uniform_get(name);
return tex ? tex->binding : -1;
}
int GPU_shader_get_attribute(GPUShader *shader, const char *name)
{
ShaderInterface *interface = unwrap(shader)->interface;
const ShaderInput *attr = interface->attr_get(name);
return attr ? attr->location : -1;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Getters
* \{ */
int GPU_shader_get_program(GPUShader *shader)
{
return unwrap(shader)->program_handle_get();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Uniforms setters
* \{ */
void GPU_shader_uniform_vector(
2020-08-14 15:20:35 +02:00
GPUShader *shader, int loc, int len, int arraysize, const float *value)
{
unwrap(shader)->uniform_float(loc, len, arraysize, value);
}
void GPU_shader_uniform_vector_int(
2020-08-14 15:20:35 +02:00
GPUShader *shader, int loc, int len, int arraysize, const int *value)
{
unwrap(shader)->uniform_int(loc, len, arraysize, value);
}
2020-08-11 15:59:02 +02:00
void GPU_shader_uniform_int(GPUShader *shader, int location, int value)
{
GPU_shader_uniform_vector_int(shader, location, 1, 1, &value);
}
void GPU_shader_uniform_float(GPUShader *shader, int location, float value)
{
GPU_shader_uniform_vector(shader, location, 1, 1, &value);
}
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_int(sh, loc, value);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value)
{
GPU_shader_uniform_1i(sh, name, value ? 1 : 0);
}
void GPU_shader_uniform_2f(GPUShader *sh, const char *name, float x, float y)
{
const float data[2] = {x, y};
GPU_shader_uniform_2fv(sh, name, data);
}
void GPU_shader_uniform_3f(GPUShader *sh, const char *name, float x, float y, float z)
{
const float data[3] = {x, y, z};
GPU_shader_uniform_3fv(sh, name, data);
}
void GPU_shader_uniform_4f(GPUShader *sh, const char *name, float x, float y, float z, float w)
{
const float data[4] = {x, y, z, w};
GPU_shader_uniform_4fv(sh, name, data);
}
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
2020-08-11 15:59:02 +02:00
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_float(sh, loc, value);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_2fv(GPUShader *sh, const char *name, const float data[2])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 2, 1, data);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_3fv(GPUShader *sh, const char *name, const float data[3])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 3, 1, data);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_4fv(GPUShader *sh, const char *name, const float data[4])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 4, 1, data);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_mat4(GPUShader *sh, const char *name, const float data[4][4])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 16, 1, (const float *)data);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_2fv_array(GPUShader *sh, const char *name, int len, const float (*val)[2])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 2, len, (const float *)val);
2020-08-11 15:59:02 +02:00
}
void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, const float (*val)[4])
{
const int loc = GPU_shader_get_uniform(sh, name);
GPU_shader_uniform_vector(sh, loc, 4, len, (const float *)val);
2020-08-11 15:59:02 +02:00
}
/** \} */
/* -------------------------------------------------------------------- */
2020-07-30 08:31:25 +10:00
/** \name sRGB Rendering Workaround
*
2020-07-30 08:31:25 +10:00
* The viewport overlay frame-buffer is sRGB and will expect shaders to output display referred
* Linear colors. But other frame-buffers (i.e: the area frame-buffers) are not sRGB and require
* the shader output color to be in sRGB space
* (assumed display encoded color-space as the time of writing).
* For this reason we have a uniform to switch the transform on and off depending on the current
2020-07-30 08:31:25 +10:00
* frame-buffer color-space.
* \{ */
static int g_shader_builtin_srgb_transform = 0;
static bool g_shader_builtin_srgb_is_dirty = false;
2021-05-03 10:22:12 +02:00
static bool gpu_shader_srgb_uniform_dirty_get()
{
return g_shader_builtin_srgb_is_dirty;
}
2020-08-14 15:20:35 +02:00
void GPU_shader_set_srgb_uniform(GPUShader *shader)
{
int32_t loc = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_SRGB_TRANSFORM);
if (loc != -1) {
2020-08-14 15:20:35 +02:00
GPU_shader_uniform_vector_int(shader, loc, 1, 1, &g_shader_builtin_srgb_transform);
}
g_shader_builtin_srgb_is_dirty = false;
}
void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear)
{
if (g_shader_builtin_srgb_transform != use_srgb_to_linear) {
g_shader_builtin_srgb_transform = use_srgb_to_linear;
g_shader_builtin_srgb_is_dirty = true;
}
}
/** \} */