summaryrefslogtreecommitdiff
path: root/src/rendering.cc
blob: e916d31a17408e2c6dd5d3f0c9b04f69d3a79e12 (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
#include "rendering.h"

#include <glad.h>

#include <cstring>

#include "cglm/types.h"
#include "utils.h"

bool wayc_image_init(texture_t* texture, image_type_e type,
                     image_format_e format, u32 width, u32 height) {
  wayc_notnull(texture);

  glCreateTextures(type, 1, texture);
  if (*texture == 0) return false;

  switch (type) {
    case IMAGE_TYPE_2D:
      // TODO: add error checking
      glTextureStorage2D(*texture, 1, format, width, height);
      break;
    default:
      break;
  }

  return true;
}

bool wayc_image_upload(texture_t texture, ivec2 offset, u32 width, u32 height,
                       image_type_e type, image_format_e format,
                       image_data_type_e data_type, i32 alignment,
                       const u8* data) {
  wayc_notnull(data);

  glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);

  switch (type) {
    case IMAGE_TYPE_2D:
      // TODO: add error checking
      glTextureSubImage2D(texture, 0, offset[0], offset[1], width, height,
                          format, data_type, data);
      break;
    default:
      break;
  }

  return true;
}

void wayc_image_use(texture_t texture, u32 slot) {
  glBindTextureUnit(slot, texture);
}

void wayc_image_deinit(texture_t* texture) {
  wayc_notnull(texture);
  glDeleteTextures(1, texture);
}

bool wayc_sampler_init(sampler_t* sampler, sample_filter_e filter,
                       sample_wrap_e wrap) {
  wayc_notnull(sampler);

  glCreateSamplers(1, sampler);
  if (*sampler == 0) return false;

  glSamplerParameteri(*sampler, GL_TEXTURE_MIN_FILTER, filter);
  glSamplerParameteri(*sampler, GL_TEXTURE_MAG_FILTER, filter);
  glSamplerParameteri(*sampler, GL_TEXTURE_WRAP_S, wrap);
  glSamplerParameteri(*sampler, GL_TEXTURE_WRAP_T, wrap);

  return true;
}

void wayc_sampler_use(sampler_t sampler, u32 slot) {
  glBindSampler(slot, sampler);
}

void wayc_sampler_deinit(sampler_t* sampler) { glDeleteSamplers(1, sampler); }

bool wayc_atlas_init(atlas_s* atlas, image_format_e format, u32 width,
                     u32 height) {
  wayc_notnull(atlas);
  memset(atlas, 0, sizeof(*atlas));

  bool success = false;

  texture_t texture;
  bool ok = wayc_image_init(&texture, IMAGE_TYPE_2D, format, width, height);
  if (!ok) return false;
  wayc_defer_cond(wayc_image_deinit(&texture);, success, true);

  sampler_t sampler;
  ok = wayc_sampler_init(&sampler, SAMPLE_FILTER_NEAREST,
                         SAMPLE_WRAP_CLAMP_TO_EDGE);
  if (!ok) return false;

  atlas->texture = texture;
  atlas->sampler = sampler;
  atlas->width = width;
  atlas->height = height;

  success = true;
  return true;
}

void wayc_atlas_use(atlas_s* atlas, u32 slot) {
  wayc_image_use(atlas->texture, slot);
  wayc_sampler_use(atlas->sampler, slot);
}

bool wayc_atlas_set(atlas_s* atlas, image_data_type_e data_type, ivec2 offset,
                    u32 width, u32 height, i32 alignment, const u8* data) {
  wayc_notnull(atlas);
  wayc_notnull(data);

  u32 final_x = offset[0] + width;
  u32 final_y = offset[1] + height;
  if (final_x > atlas->width || final_y > atlas->height) return false;

  wayc_image_upload(atlas->texture, offset, width, height, IMAGE_TYPE_2D,
                    atlas->format, data_type, alignment, data);
  return true;
}

void wayc_atlas_deinit(atlas_s* atlas) {
  wayc_notnull(atlas);
  wayc_sampler_deinit(&atlas->sampler);
  wayc_image_deinit(&atlas->texture);
}

static inline void wayc_atlas_packer_wrap(struct atlas_packer_s* packer) {
  WAYC_X(packer->cursor) = 0;
  WAYC_Y(packer->cursor) += packer->row_height;
  packer->row_height = 0;
}

bool wayc_atlas_packer_allocate(struct atlas_packer_s* packer, u32 width,
                                u32 height, ivec2 out) {
  wayc_notnull(packer);

  struct atlas_s* atlas = packer->atlas;
  wayc_notnull(atlas);

  if (width > atlas->width || height > atlas->height) return false;

  if (WAYC_X(packer->cursor) + width > atlas->width)
    wayc_atlas_packer_wrap(packer);

  if (WAYC_Y(packer->cursor) + height > atlas->height) return false;

  glm_ivec2_copy(packer->cursor, out);
  WAYC_X(packer->cursor) += width;
  packer->row_height = wayc_max(packer->row_height, height);

  return true;
}

bool wayc_vbo_init(vbo_t* vbo, usize size) {
  wayc_notnull(vbo);

  glCreateBuffers(1, vbo);
  if (*vbo == 0) return false;

  glNamedBufferStorage(*vbo, size, NULL, GL_DYNAMIC_STORAGE_BIT);
  return true;
}

bool wayc_vbo_upload(vbo_t vbo, usize offset, usize size, const u8* data) {
  wayc_notnull(data);

  glNamedBufferSubData(vbo, offset, size, data);  // TODO: add error checking
  return true;
}

void wayc_vbo_use(vbo_t vbo) { glBindBuffer(GL_ARRAY_BUFFER, vbo); }

void wayc_vbo_deinit(vbo_t* vbo) {
  wayc_notnull(vbo);
  glDeleteBuffers(1, vbo);
}

bool wayc_ebo_init(ebo_t* ebo, usize size) {
  wayc_notnull(ebo);

  glCreateBuffers(1, ebo);
  if (*ebo == 0) return false;

  glNamedBufferStorage(*ebo, size, NULL, GL_DYNAMIC_STORAGE_BIT);
  return true;
}

bool wayc_ebo_upload(ebo_t ebo, usize offset, usize size, const u8* data) {
  wayc_notnull(data);

  glNamedBufferSubData(ebo, offset, size, data);
  return true;
}

void wayc_ebo_use(ebo_t ebo) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); }

void wayc_ebo_deinit(ebo_t* ebo) {
  wayc_notnull(ebo);
  glDeleteBuffers(1, ebo);
}