]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/texture3d_backend.cpp
Move all OpenGL-specific code to a separate directory
[libs/gl.git] / source / backends / opengl / texture3d_backend.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/ext_texture3d.h>
4 #include <msp/gl/extensions/ext_texture_array.h>
5 #include "gl.h"
6 #include "texture3d.h"
7 #include "texture3d_backend.h"
8
9 namespace Msp {
10 namespace GL {
11
12 OpenGLTexture3D::OpenGLTexture3D():
13         Texture(GL_TEXTURE_3D)
14 {
15         static Require _req(EXT_texture3D);
16 }
17
18 OpenGLTexture3D::OpenGLTexture3D(unsigned t):
19         Texture(t)
20 { }
21
22 void OpenGLTexture3D::allocate()
23 {
24         unsigned width = static_cast<const Texture3D *>(this)->width;
25         unsigned height = static_cast<const Texture3D *>(this)->height;
26         unsigned depth = static_cast<const Texture3D *>(this)->depth;
27         unsigned levels = static_cast<const Texture3D *>(this)->levels;
28
29         GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
30         if(ARB_texture_storage)
31         {
32                 if(ARB_direct_state_access)
33                         glTextureStorage3D(id, levels, gl_fmt, width, height, depth);
34                 else
35                 {
36                         bind_scratch();
37                         glTexStorage3D(target, levels, gl_fmt, width, height, depth);
38                 }
39         }
40         else
41         {
42                 bind_scratch();
43                 GLenum comp = get_gl_components(get_components(storage_fmt));
44                 GLenum type = get_gl_type(get_component_type(storage_fmt));
45                 for(unsigned i=0; i<levels; ++i)
46                 {
47                         auto lv_size = static_cast<const Texture3D *>(this)->get_level_size(i);
48                         glTexImage3D(target, i, gl_fmt, lv_size.x, lv_size.y, lv_size.z, 0, comp, type, 0);
49                 }
50                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
51         }
52
53         apply_swizzle();
54 }
55
56 void OpenGLTexture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
57 {
58         GLenum comp = get_gl_components(get_components(storage_fmt));
59         GLenum type = get_gl_type(get_component_type(storage_fmt));
60         if(ARB_direct_state_access)
61                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
62         else
63         {
64                 bind_scratch();
65                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
66         }
67 }
68
69 bool OpenGLTexture3D::is_array() const
70 {
71         return target==GL_TEXTURE_2D_ARRAY;
72 }
73
74 } // namespace GL
75 } // namespace Msp