summaryrefslogtreecommitdiff
path: root/gfx/src/core/shader_program.c
blob: 3cbe48d0b36978d3a878ad5679f3fdf04dd951f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "shader_program.h"

#include "gl_util.h"
#include "shader.h"
#include "texture.h"
#include <gfx_assert.h>

#include <log/log.h>

#include <stdlib.h>
#include <string.h>

/// Creates an OpenGL shader program.
/// Returns non-zero on success, 0 on failure.
static GLuint create_program(GLuint vertex_shader, GLuint fragment_shader) {
  const GLuint prog = glCreateProgram();
  if (prog == 0) {
    LOGE("Failed creating shader program");
    return 0;
  }
  glAttachShader(prog, vertex_shader);
  glAttachShader(prog, fragment_shader);
  glLinkProgram(prog);
  GLint result;
  glGetProgramiv(prog, GL_LINK_STATUS, &result);
  if (result == GL_FALSE) {
    GLint log_len;
    glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &log_len);
    if (log_len > 0) {
      char* log = calloc(log_len, sizeof(char));
      glGetProgramInfoLog(prog, log_len, NULL, log);
      LOGE("Failed creating shader program: %s", log);
      free(log);
    } else {
      LOGE("Failed creating shader program");
    }
    glDeleteProgram(prog);
    return 0;
  }
  ASSERT_GL;
  return prog;
}

bool gfx_build_shader_program(
    ShaderProgram* prog, const ShaderProgramDesc* desc) {
  assert(prog);
  assert(desc);

  prog->id = create_program(desc->vertex_shader->id, desc->fragment_shader->id);
  return prog->id != 0;
}

void gfx_del_shader_program(ShaderProgram* prog) {
  assert(prog);

  if (prog->id) {
    glDeleteProgram(prog->id);
    prog->id = 0;
  }
  ASSERT_GL;
}

void gfx_activate_shader_program(const ShaderProgram* prog) {
  assert(prog);
  glUseProgram(prog->id);
  ASSERT_GL;
}

void gfx_deactivate_shader_program(const ShaderProgram* prog) {
  assert(prog);
  glUseProgram(0);
  ASSERT_GL;
}

static void set_texture_uniform(
    GLuint prog, const char* name, int texture_unit, const Texture* texture) {
  assert(prog != 0);
  assert(name);
  assert(texture);

  const GLint location = glGetUniformLocation(prog, name);
  if (location >= 0) {
    glActiveTexture(GL_TEXTURE0 + texture_unit);
    glBindTexture(texture->target, texture->id);
    glUniform1i(location, texture_unit);
  }
}

static void set_mat4_uniform(
    GLuint prog, const char* name, const mat4* mats, size_t count) {
  assert(prog != 0);
  assert(name);
  assert(mats);

  const GLint location = glGetUniformLocation(prog, name);
  if (location >= 0) {
    glUniformMatrix4fv(location, count, GL_FALSE, (const float*)mats);
  }
}

static void set_vec3_uniform(GLuint prog, const char* name, vec3 value) {
  assert(prog != 0);
  assert(name);

  const GLint location = glGetUniformLocation(prog, name);
  if (location >= 0) {
    glUniform3f(location, value.x, value.y, value.z);
  }
}

static void set_vec4_uniform(GLuint prog, const char* name, vec4 value) {
  assert(prog != 0);
  assert(name);

  const GLint location = glGetUniformLocation(prog, name);
  if (location >= 0) {
    glUniform4f(location, value.x, value.y, value.z, value.w);
  }
}

static void set_float_uniform(GLuint prog, const char* name, float value) {
  assert(prog != 0);
  assert(name);

  const GLint location = glGetUniformLocation(prog, name);
  if (location >= 0) {
    glUniform1f(location, value);
  }
}

void gfx_apply_uniforms(const ShaderProgram* prog) {
  assert(prog);

  int next_texture_unit = 0;
  for (int i = 0; i < prog->num_uniforms; ++i) {
    const ShaderUniform* uniform = &prog->uniforms[i];
    switch (uniform->type) {
    case UniformTexture:
      set_texture_uniform(
          prog->id, uniform->name.str, next_texture_unit,
          uniform->value.texture);
      next_texture_unit++;
      break;
    case UniformMat4:
      set_mat4_uniform(prog->id, uniform->name.str, &uniform->value.mat4, 1);
      break;
    case UniformVec3:
      set_vec3_uniform(prog->id, uniform->name.str, uniform->value.vec3);
      break;
    case UniformVec4:
      set_vec4_uniform(prog->id, uniform->name.str, uniform->value.vec4);
      break;
    case UniformFloat:
      set_float_uniform(prog->id, uniform->name.str, uniform->value.scalar);
      break;
    case UniformMat4Array:
      set_mat4_uniform(
          prog->id, uniform->name.str, uniform->value.array.values,
          uniform->value.array.count);
      break;
    }
  }
}

// Get the ShaderUniform object by name from the shader program if it already
// exists, or allocate a new one otherwise.
static ShaderUniform* get_or_allocate_uniform(
    ShaderProgram* prog, const char* name) {
  assert(prog);
  assert(name);

  // First search for the uniform in the list.
  for (int i = 0; i < prog->num_uniforms; ++i) {
    ShaderUniform* uniform = &prog->uniforms[i];
    if (sstring_eq_cstr(uniform->name, name)) {
      return uniform;
    }
  }

  // Create the uniform if it does not exist.
  if (prog->num_uniforms == GFX_MAX_UNIFORMS_PER_SHADER) {
    FAIL("Exceeded the maximum number of uniforms per shader. Please increase "
         "this value.");
    return 0;
  }
  ShaderUniform* uniform = &prog->uniforms[prog->num_uniforms];
  prog->num_uniforms++;
  return uniform;
}

// The functions below save the value of a uniform in the shader program. If the
// uniform does not even exist, then there is no need to store the value.

void gfx_set_texture_uniform(
    ShaderProgram* prog, const char* name, const Texture* texture) {
  assert(prog);
  assert(name);
  assert(texture);

  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name          = sstring_make(name);
  uniform->type          = UniformTexture;
  uniform->value.texture = texture;
}

void gfx_set_mat4_uniform(
    ShaderProgram* prog, const char* name, const mat4* mat) {
  assert(prog);
  assert(name);
  assert(mat);

  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name       = sstring_make(name);
  uniform->type       = UniformMat4;
  uniform->value.mat4 = *mat;
}

void gfx_set_vec3_uniform(ShaderProgram* prog, const char* name, vec3 value) {
  assert(prog);
  assert(name);

  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name       = sstring_make(name);
  uniform->type       = UniformVec3;
  uniform->value.vec3 = value;
}

void gfx_set_vec4_uniform(ShaderProgram* prog, const char* name, vec4 value) {
  assert(prog);
  assert(name);

  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name       = sstring_make(name);
  uniform->type       = UniformVec4;
  uniform->value.vec4 = value;
}

void gfx_set_float_uniform(ShaderProgram* prog, const char* name, float value) {
  assert(prog);
  assert(name);

  // No need to store the uniform on our side if it does not exist in the
  // program.
  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name         = sstring_make(name);
  uniform->type         = UniformFloat;
  uniform->value.scalar = value;
}

void gfx_set_mat4_array_uniform(
    ShaderProgram* prog, const char* name, const mat4* mats, size_t count) {
  assert(prog);
  assert(name);
  assert(mats);
  
  const GLint location = glGetUniformLocation(prog->id, name);
  if (location < 0) {
    return;
  }
  ShaderUniform* uniform = get_or_allocate_uniform(prog, name);
  assert(uniform);
  uniform->name               = sstring_make(name);
  uniform->type               = UniformMat4Array;
  uniform->value.array.count  = count;
  uniform->value.array.values = mats;
}