2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2016 by Mike Erwin. All rights reserved. */
|
2018-07-17 14:46:44 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup gpu
|
2018-07-17 14:46:44 +02:00
|
|
|
*
|
2018-07-18 00:12:21 +02:00
|
|
|
* GPU shader interface (C --> GLSL)
|
2018-07-17 14:46:44 +02:00
|
|
|
*/
|
2017-03-02 21:27:51 -05:00
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
2018-07-31 16:54:58 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
#include "BLI_span.hh"
|
|
|
|
|
#include "BLI_vector.hh"
|
2020-07-30 23:49:13 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
#include "gpu_shader_interface.hh"
|
2017-03-02 21:27:51 -05:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
namespace blender::gpu {
|
2020-06-04 13:43:28 +02:00
|
|
|
|
2021-04-08 11:07:12 +02:00
|
|
|
/* TODO(fclem): add unique ID for debugging. */
|
|
|
|
|
ShaderInterface::ShaderInterface() = default;
|
2017-04-15 19:19:00 -04:00
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
ShaderInterface::~ShaderInterface()
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-08-20 13:05:22 +02:00
|
|
|
/* Free memory used by name_buffer. */
|
2022-09-22 17:27:51 +02:00
|
|
|
MEM_SAFE_FREE(name_buffer_);
|
|
|
|
|
MEM_SAFE_FREE(inputs_);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-06-01 12:26:27 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
static void sort_input_list(MutableSpan<ShaderInput> dst)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-08-20 13:05:22 +02:00
|
|
|
if (dst.size() == 0) {
|
|
|
|
|
return;
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
2017-06-01 12:26:27 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
Vector<ShaderInput> inputs_vec = Vector<ShaderInput>(dst.size());
|
|
|
|
|
MutableSpan<ShaderInput> src = inputs_vec.as_mutable_span();
|
|
|
|
|
src.copy_from(dst);
|
2017-10-04 17:36:52 +05:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
/* Simple sorting by going through the array and selecting the biggest element each time. */
|
|
|
|
|
for (uint i = 0; i < dst.size(); i++) {
|
2022-06-10 10:29:35 +02:00
|
|
|
ShaderInput *input_src = src.data();
|
2020-08-20 13:05:22 +02:00
|
|
|
for (uint j = 1; j < src.size(); j++) {
|
2020-06-02 12:11:39 +02:00
|
|
|
if (src[j].name_hash > input_src->name_hash) {
|
|
|
|
|
input_src = &src[j];
|
|
|
|
|
}
|
2017-10-06 01:50:16 +02:00
|
|
|
}
|
2020-06-02 12:11:39 +02:00
|
|
|
dst[i] = *input_src;
|
|
|
|
|
input_src->name_hash = 0;
|
2017-10-06 01:50:16 +02:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-10-06 01:50:16 +02:00
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
void ShaderInterface::sort_inputs()
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2021-12-09 20:01:47 +11:00
|
|
|
/* Sorts all inputs inside their respective array.
|
|
|
|
|
* This is to allow fast hash collision detection.
|
|
|
|
|
* See `ShaderInterface::input_lookup` for more details. */
|
|
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
sort_input_list(MutableSpan<ShaderInput>(inputs_, attr_len_));
|
|
|
|
|
sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_, ubo_len_));
|
|
|
|
|
sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_));
|
2021-12-17 08:16:06 +01:00
|
|
|
sort_input_list(
|
|
|
|
|
MutableSpan<ShaderInput>(inputs_ + attr_len_ + ubo_len_ + uniform_len_, ssbo_len_));
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-03-02 21:27:51 -05:00
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
void ShaderInterface::debug_print()
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-08-20 13:05:22 +02:00
|
|
|
Span<ShaderInput> attrs = Span<ShaderInput>(inputs_, attr_len_);
|
|
|
|
|
Span<ShaderInput> ubos = Span<ShaderInput>(inputs_ + attr_len_, ubo_len_);
|
|
|
|
|
Span<ShaderInput> uniforms = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_);
|
2021-05-26 16:49:17 +02:00
|
|
|
Span<ShaderInput> ssbos = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_ + uniform_len_,
|
|
|
|
|
ssbo_len_);
|
2020-08-20 13:05:22 +02:00
|
|
|
char *name_buf = name_buffer_;
|
|
|
|
|
const char format[] = " | %.8x : %4d : %s\n";
|
2017-10-05 18:26:50 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
if (attrs.size() > 0) {
|
|
|
|
|
printf("\n Attributes :\n");
|
2019-04-06 14:23:25 +02:00
|
|
|
}
|
2020-08-20 13:05:22 +02:00
|
|
|
for (const ShaderInput &attr : attrs) {
|
|
|
|
|
printf(format, attr.name_hash, attr.location, name_buf + attr.name_offset);
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
if (uniforms.size() > 0) {
|
|
|
|
|
printf("\n Uniforms :\n");
|
2020-06-04 15:28:35 +02:00
|
|
|
}
|
2020-08-20 13:05:22 +02:00
|
|
|
for (const ShaderInput &uni : uniforms) {
|
|
|
|
|
/* Bypass samplers. */
|
|
|
|
|
if (uni.binding == -1) {
|
|
|
|
|
printf(format, uni.name_hash, uni.location, name_buf + uni.name_offset);
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
if (ubos.size() > 0) {
|
|
|
|
|
printf("\n Uniform Buffer Objects :\n");
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2020-08-20 13:05:22 +02:00
|
|
|
for (const ShaderInput &ubo : ubos) {
|
|
|
|
|
printf(format, ubo.name_hash, ubo.binding, name_buf + ubo.name_offset);
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
2017-10-06 14:57:21 +02:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
if (enabled_tex_mask_ > 0) {
|
|
|
|
|
printf("\n Samplers :\n");
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
2020-08-20 13:05:22 +02:00
|
|
|
for (const ShaderInput &samp : uniforms) {
|
|
|
|
|
/* Bypass uniforms. */
|
|
|
|
|
if (samp.binding != -1) {
|
|
|
|
|
printf(format, samp.name_hash, samp.binding, name_buf + samp.name_offset);
|
2020-06-02 12:11:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 16:49:17 +02:00
|
|
|
if (ssbos.size() > 0) {
|
|
|
|
|
printf("\n Shader Storage Objects :\n");
|
|
|
|
|
}
|
|
|
|
|
for (const ShaderInput &ssbo : ssbos) {
|
|
|
|
|
printf(format, ssbo.name_hash, ssbo.binding, name_buf + ssbo.name_offset);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
printf("\n");
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2017-04-12 17:56:26 -04:00
|
|
|
|
2020-08-20 13:05:22 +02:00
|
|
|
} // namespace blender::gpu
|