summaryrefslogtreecommitdiff
path: root/gfx/src/core/texture.c
blob: 89f7ec0ec69504eb54ffd6df330e46cd07f83b74 (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
#include "texture.h"

#include <gfx_assert.h>

#include <error.h>
#include <math/defs.h>

#include <stdbool.h>

bool gfx_init_texture(Texture* texture, const TextureDesc* desc) {
  assert(texture);
  assert(desc);

  glGenTextures(1, &texture->id);
  if (!texture->id) {
    log_error("glGenTextures() failed");
    return false;
  }
  texture->target = to_GL_dimension(desc->dimension);
  glBindTexture(texture->target, texture->id);

  // glTexStorageXD
  const int levels =
      desc->mipmaps
          ? max(max(log2(desc->width), log2(desc->height)), log2(desc->depth)) +
                1
          : 1;
  const GLenum internal_format = to_GL_internal_format(desc->format);
  switch (texture->target) {
  case GL_TEXTURE_2D:
  case GL_TEXTURE_CUBE_MAP:
    glTexStorage2D(
        texture->target, levels, internal_format, desc->width, desc->height);
    break;
  default:
    FAIL("Unhandled texture dimension");
    gfx_del_texture(texture);
    return false;
  }

  texture->format = to_GL_format(desc->format);
  texture->type   = to_GL_type(desc->format);
  texture->width  = desc->width;
  texture->height = desc->height;
  gfx_update_texture(texture, &desc->data);

  // gfx_update_texture() unbinds the texture at the end, so re-bind it here.
  glBindTexture(texture->target, texture->id);

  // Mipmaps.
  if (desc->mipmaps) {
    glGenerateMipmap(texture->target);
  }

  // Texture filtering.
  const bool linear = desc->filtering == LinearFiltering;
  GLenum     min    = desc->mipmaps ? (linear ? GL_LINEAR_MIPMAP_LINEAR
                                              : GL_NEAREST_MIPMAP_NEAREST)
                                    : (linear ? GL_LINEAR : GL_NEAREST);
  GLenum     mag    = linear ? GL_LINEAR : GL_NEAREST;
  glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, min);
  glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, mag);

  // Texture wrapping.
  GLenum wrap = GL_INVALID_ENUM;
  switch (desc->wrap) {
  case Repeat:
    wrap = GL_REPEAT;
    break;
  case ClampToEdge:
    wrap = GL_CLAMP_TO_EDGE;
    break;
  }
  glTexParameteri(texture->target, GL_TEXTURE_WRAP_R, wrap);
  glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, wrap);
  glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, wrap);

  glBindTexture(texture->target, 0);
  return true;
}

void gfx_del_texture(Texture* texture) {
  assert(texture);

  if (texture->id) {
    glDeleteTextures(1, &texture->id);
    texture->id = 0;
  }
}

void gfx_update_texture(Texture* texture, const TextureDataDesc* desc) {
  assert(texture);
  assert(desc);

  glBindTexture(texture->target, texture->id);

  // glTexSubImageXD
  switch (texture->target) {
  case GL_TEXTURE_2D:
    if (desc->pixels) {
      glTexSubImage2D(
          GL_TEXTURE_2D, /*level=*/0, /*xoffset=*/0,
          /*yoffset=*/0, texture->width, texture->height, texture->format,
          texture->type, desc->pixels);
    }
    break;
  case GL_TEXTURE_CUBE_MAP:
    for (int i = 0; i < 6; ++i) {
      const void* pixels = *(&desc->cubemap.pixels_pos_x + i);
      if (pixels) {
        glTexSubImage2D(
            GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, /*level=*/0, /*xoffset=*/0,
            /*yoffset=*/0, texture->width, texture->height, texture->format,
            texture->type, pixels);
      }
    }
    break;
  default:
    FAIL("Unhandled texture dimension");
    break;
  }

  glBindTexture(texture->target, 0);
}

GLenum to_GL_dimension(TextureDimension dim) {
  switch (dim) {
  case Texture2D:
    return GL_TEXTURE_2D;
  case TextureCubeMap:
    return GL_TEXTURE_CUBE_MAP;
  default:
    FAIL("Unhandled TextureDimension");
    return GL_INVALID_ENUM;
  }
}

GLenum to_GL_internal_format(TextureFormat format) {
  switch (format) {
  case TextureDepth:
    return GL_DEPTH_COMPONENT;
  case TextureRG16:
    return GL_RG16;
  case TextureRG16F:
    return GL_RG16F;
  case TextureRGB8:
    return GL_RGB8;
  case TextureR11G11B10F:
    return GL_R11F_G11F_B10F;
  case TextureRGBA8:
    return GL_RGBA8;
  case TextureSRGB8:
    return GL_SRGB8;
  case TextureSRGBA8:
    return GL_SRGB8_ALPHA8;
  default:
    FAIL("Unhandled TextureFormat");
    return GL_INVALID_ENUM;
  }
}

GLenum to_GL_format(TextureFormat format) {
  switch (format) {
  case TextureDepth:
    return GL_DEPTH_COMPONENT;
  case TextureRG16:
  case TextureRG16F:
    return GL_RG;
  case TextureRGB8:
  case TextureR11G11B10F:
  case TextureSRGB8:
    return GL_RGB;
  case TextureRGBA8:
  case TextureSRGBA8:
    return GL_RGBA;
  default:
    FAIL("Unhandled TextureFormat");
    return GL_INVALID_ENUM;
  }
}

GLenum to_GL_type(TextureFormat format) {
  switch (format) {
  case TextureDepth:
  case TextureRG16F:
  case TextureR11G11B10F:
    return GL_FLOAT;
  case TextureRG16:
  case TextureRGB8:
  case TextureRGBA8:
  case TextureSRGB8:
  case TextureSRGBA8:
    return GL_UNSIGNED_BYTE;
  default:
    FAIL("Unhandled TextureFormat");
    return GL_INVALID_ENUM;
  }
}

GLenum to_GL_cubemap_face(CubemapFace face) {
  switch (face) {
  case CubemapFacePosX:
    return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  case CubemapFaceNegX:
    return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
  case CubemapFacePosY:
    return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
  case CubemapFaceNegY:
    return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
  case CubemapFacePosZ:
    return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
  case CubemapFaceNegZ:
    return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
  default:
    FAIL("Unhandled CubemapFace");
    return GL_INVALID_ENUM;
  }
}