1 #include "nexus3d_impl.h"
5 static unsigned int gltextype(enum nex_tex_type type);
6 static unsigned int glpixfmt(enum nex_pixfmt fmt);
7 static unsigned int glformat(enum nex_pixfmt fmt);
8 static unsigned int gltype(enum nex_pixfmt fmt);
9 /*static unsigned int glcubeface(enum nex_cube_face face);*/
10 static unsigned int miplevels(int x, int y, int z);
12 nex_texture *nex_alloc_texture(enum nex_tex_type type)
16 if(!(tex = calloc(1, sizeof *tex))) {
20 glCreateTextures(gltextype(type), 1, &tex->tex);
24 void nex_free_texture(nex_texture *tex)
28 glDeleteTextures(1, &tex->tex);
33 void nex_tex_storage1d(nex_texture *tex, int sz, enum nex_pixfmt fmt)
35 glTextureStorage1D(tex->tex, miplevels(sz, 0, 0), glpixfmt(fmt), sz);
38 void nex_tex_storage2d(nex_texture *tex, int xsz, int ysz, enum nex_pixfmt fmt)
40 int levels = miplevels(xsz, ysz, 0);
41 glTextureStorage2D(tex->tex, levels, glpixfmt(fmt), xsz, ysz);
44 void nex_tex_storage3d(nex_texture *tex, int xsz, int ysz, int zsz, enum nex_pixfmt fmt)
46 int levels = miplevels(xsz, ysz, zsz);
47 glTextureStorage3D(tex->tex, levels, glpixfmt(fmt), xsz, ysz, zsz);
50 void nex_tex_storagecube(nex_texture *tex, int xsz, int ysz, enum nex_pixfmt fmt)
52 nex_tex_storage2d(tex, xsz, ysz, fmt);
56 void nex_tex_update1d(nex_texture *tex, int lvl, int x, int width,
57 enum nex_pixfmt pixfmt, void *data, long pitch)
59 int fmt = glformat(pixfmt);
60 int type = gltype(pixfmt);
61 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
62 glTextureSubImage1D(tex->tex, lvl, x, width, fmt, type, data);
63 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
66 void nex_tex_update2d(nex_texture *tex, int lvl, int x, int y, int width, int height,
67 enum nex_pixfmt pixfmt, void *data, long pitch)
69 int fmt = glformat(pixfmt);
70 int type = gltype(pixfmt);
71 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
72 glTextureSubImage2D(tex->tex, lvl, x, y, width, height, fmt, type, data);
73 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
76 void nex_tex_update3d(nex_texture *tex, int lvl, int x, int y, int z, int width,
77 int height, int depth, enum nex_pixfmt pixfmt, void *data, long pitch)
79 int fmt = glformat(pixfmt);
80 int type = gltype(pixfmt);
81 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
82 glTextureSubImage3D(tex->tex, lvl, x, y, z, width, height, depth, fmt, type,
84 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
88 void nex_tex_updatecube(nex_texture *tex, int lvl, enum nex_cube_face face,
89 int x, int y, int width, int height, enum nex_pixfmt pixfmt, void *data,
92 int fmt = glformat(pixfmt);
93 int type = gltype(pixfmt);
94 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
95 glTextureSubImage3D(tex->tex, lvl, x, y, face, width, height, 1, fmt, type,
97 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
100 void bind_texture(int tunit, nex_texture *tex)
102 glBindTextureUnit(tunit, tex->tex);
106 static unsigned int gltextype(enum nex_tex_type type)
110 return GL_TEXTURE_1D;
112 return GL_TEXTURE_2D;
114 return GL_TEXTURE_3D;
116 return GL_TEXTURE_CUBE_MAP;
123 static unsigned int glpixfmt(enum nex_pixfmt fmt)
135 return GL_SRGB_ALPHA;
137 return GL_LUMINANCE16F_ARB;
149 static unsigned int glcubeface(enum nex_cube_face face)
153 return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
155 return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
157 return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
159 return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
161 return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
163 return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
171 static unsigned int glformat(enum nex_pixfmt fmt)
191 static unsigned int gltype(enum nex_pixfmt fmt)
193 if(fmt == NEX_GREYF || fmt == NEX_RGBF || fmt == NEX_RGBAF) {
196 return GL_UNSIGNED_BYTE;
199 static unsigned int miplevels(int x, int y, int z)