1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2016 by Mike Erwin.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup gpu
22 *
23 * GPU shader interface (C --> GLSL)
24 */
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_span.hh"
29 #include "BLI_vector.hh"
30
31 #include "gpu_shader_interface.hh"
32
33 namespace blender::gpu {
34
ShaderInterface(void)35 ShaderInterface::ShaderInterface(void)
36 {
37 /* TODO(fclem): add unique ID for debugging. */
38 }
39
~ShaderInterface(void)40 ShaderInterface::~ShaderInterface(void)
41 {
42 /* Free memory used by name_buffer. */
43 MEM_freeN(name_buffer_);
44 MEM_freeN(inputs_);
45 }
46
sort_input_list(MutableSpan<ShaderInput> dst)47 static void sort_input_list(MutableSpan<ShaderInput> dst)
48 {
49 if (dst.size() == 0) {
50 return;
51 }
52
53 Vector<ShaderInput> inputs_vec = Vector<ShaderInput>(dst.size());
54 MutableSpan<ShaderInput> src = inputs_vec.as_mutable_span();
55 src.copy_from(dst);
56
57 /* Simple sorting by going through the array and selecting the biggest element each time. */
58 for (uint i = 0; i < dst.size(); i++) {
59 ShaderInput *input_src = &src[0];
60 for (uint j = 1; j < src.size(); j++) {
61 if (src[j].name_hash > input_src->name_hash) {
62 input_src = &src[j];
63 }
64 }
65 dst[i] = *input_src;
66 input_src->name_hash = 0;
67 }
68 }
69
70 /* Sorts all inputs inside their respective array.
71 * This is to allow fast hash collision detection.
72 * See ShaderInterface::input_lookup for more details. */
sort_inputs(void)73 void ShaderInterface::sort_inputs(void)
74 {
75 sort_input_list(MutableSpan<ShaderInput>(inputs_, attr_len_));
76 sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_, ubo_len_));
77 sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_));
78 }
79
debug_print(void)80 void ShaderInterface::debug_print(void)
81 {
82 Span<ShaderInput> attrs = Span<ShaderInput>(inputs_, attr_len_);
83 Span<ShaderInput> ubos = Span<ShaderInput>(inputs_ + attr_len_, ubo_len_);
84 Span<ShaderInput> uniforms = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_);
85 char *name_buf = name_buffer_;
86 const char format[] = " | %.8x : %4d : %s\n";
87
88 if (attrs.size() > 0) {
89 printf("\n Attributes :\n");
90 }
91 for (const ShaderInput &attr : attrs) {
92 printf(format, attr.name_hash, attr.location, name_buf + attr.name_offset);
93 }
94
95 if (uniforms.size() > 0) {
96 printf("\n Uniforms :\n");
97 }
98 for (const ShaderInput &uni : uniforms) {
99 /* Bypass samplers. */
100 if (uni.binding == -1) {
101 printf(format, uni.name_hash, uni.location, name_buf + uni.name_offset);
102 }
103 }
104
105 if (ubos.size() > 0) {
106 printf("\n Uniform Buffer Objects :\n");
107 }
108 for (const ShaderInput &ubo : ubos) {
109 printf(format, ubo.name_hash, ubo.binding, name_buf + ubo.name_offset);
110 }
111
112 if (enabled_tex_mask_ > 0) {
113 printf("\n Samplers :\n");
114 }
115 for (const ShaderInput &samp : uniforms) {
116 /* Bypass uniforms. */
117 if (samp.binding != -1) {
118 printf(format, samp.name_hash, samp.binding, name_buf + samp.name_offset);
119 }
120 }
121
122 printf("\n");
123 }
124
125 } // namespace blender::gpu
126