Commit: dfd192ce41f7e4923db0642f22a587862656bbdd
Author: Clément Foucault
Date:   Tue Jul 10 13:17:32 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBdfd192ce41f7e4923db0642f22a587862656bbdd

GPUTexture: Refactor of texture creation & new feature

- Texture creation now requires explicit data type.
- GPU_texture_add_mipmap enable explicit mipmap upload.
- GPU_texture_get_mipmap_size can be used to get the size of a mipmap level
  of an existing GPUTexture
- GPU_texture_read let you read back data from a gpu texture.

===================================================================

M       source/blender/blenfont/intern/blf_glyph.c
M       source/blender/blenkernel/intern/studiolight.c
M       source/blender/gpu/GPU_texture.h
M       source/blender/gpu/intern/gpu_texture.c

===================================================================

diff --git a/source/blender/blenfont/intern/blf_glyph.c 
b/source/blender/blenfont/intern/blf_glyph.c
index 84388bedb7b..8dc11443124 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -239,7 +239,7 @@ static void blf_glyph_cache_texture(FontBLF *font, 
GlyphCacheBLF *gc)
        }
 
        unsigned char *pixels = MEM_callocN((size_t)gc->p2_width * 
(size_t)gc->p2_height, "BLF texture init");
-       GPUTexture *tex = GPU_texture_create_2D(gc->p2_width, gc->p2_height, 
GPU_R8, (const float *)pixels, error);
+       GPUTexture *tex = GPU_texture_create_nD(gc->p2_width, gc->p2_height, 0, 
2, pixels, GPU_R8, GPU_DATA_UNSIGNED_BYTE, 0, false, error);
        MEM_freeN(pixels);
        gc->textures[gc->texture_current] = tex;
        GPU_texture_bind(tex, 0);
@@ -476,7 +476,7 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, 
float y)
                        BLI_assert(g->height > 0);
                }
 
-               GPU_texture_update_sub(g->tex, g->bitmap, g->offset_x, 
g->offset_y, 0, g->width, g->height, 0);
+               GPU_texture_update_sub(g->tex, GPU_DATA_UNSIGNED_BYTE, 
g->bitmap, g->offset_x, g->offset_y, 0, g->width, g->height, 0);
 
                g->uv[0][0] = ((float)g->offset_x) / ((float)gc->p2_width);
                g->uv[0][1] = ((float)g->offset_y) / ((float)gc->p2_height);
diff --git a/source/blender/blenkernel/intern/studiolight.c 
b/source/blender/blenkernel/intern/studiolight.c
index dbe5228b6ea..200c50e5c23 100644
--- a/source/blender/blenkernel/intern/studiolight.c
+++ b/source/blender/blenkernel/intern/studiolight.c
@@ -239,8 +239,8 @@ static void 
studiolight_create_equirectangular_radiance_gputexture(StudioLight *
                                offset3 += 3;
                                offset4 += 4;
                        }
-                       sl->equirectangular_radiance_gputexture = 
GPU_texture_create_2D(
-                               ibuf->x, ibuf->y, GPU_R11F_G11F_B10F, 
sl->gpu_matcap_3components, error);
+                       sl->equirectangular_radiance_gputexture = 
GPU_texture_create_nD(
+                               ibuf->x, ibuf->y, 0, 2, 
sl->gpu_matcap_3components, GPU_R11F_G11F_B10F, GPU_DATA_FLOAT, 0, false, 
error);
                }
                else {
                        sl->equirectangular_radiance_gputexture = 
GPU_texture_create_2D(
diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h
index 341abeba72f..3ed9014d431 100644
--- a/source/blender/gpu/GPU_texture.h
+++ b/source/blender/gpu/GPU_texture.h
@@ -144,8 +144,23 @@ typedef enum GPUTextureFormat {
        GPU_DEPTH_COMPONENT16,
 } GPUTextureFormat;
 
+typedef enum GPUDataFormat {
+       GPU_DATA_FLOAT,
+       GPU_DATA_INT,
+       GPU_DATA_UNSIGNED_INT,
+       GPU_DATA_UNSIGNED_BYTE,
+       GPU_DATA_UNSIGNED_INT_24_8,
+       GPU_DATA_10_11_11_REV,
+} GPUDataFormat;
+
 unsigned int GPU_texture_memory_usage_get(void);
 
+/* TODO make it static function again. (create function with GPUDataFormat 
exposed) */
+GPUTexture *GPU_texture_create_nD(
+        int w, int h, int d, int n, const void *pixels,
+        GPUTextureFormat tex_format, GPUDataFormat gpu_data_format, int 
samples,
+        const bool can_rescale, char err_out[256]);
+
 GPUTexture *GPU_texture_create_1D(
         int w, GPUTextureFormat data_type, const float *pixels, char 
err_out[256]);
 GPUTexture *GPU_texture_create_2D(
@@ -168,11 +183,15 @@ GPUTexture *GPU_texture_from_blender(
         struct Image *ima, struct ImageUser *iuser, int textarget, bool 
is_data, double time);
 GPUTexture *GPU_texture_from_preview(struct PreviewImage *prv, int mipmap);
 
-void GPU_texture_update(GPUTexture *tex, const void *pixels);
+void GPU_texture_add_mipmap(GPUTexture *tex, GPUDataFormat gpu_data_format, 
int miplvl, const void *pixels);
+
+void GPU_texture_update(GPUTexture *tex, GPUDataFormat data_format, const void 
*pixels);
 void GPU_texture_update_sub(
-        GPUTexture *tex, const void *pixels,
+        GPUTexture *tex, GPUDataFormat gpu_data_format, const void *pixels,
         int offset_x, int offset_y, int offset_z, int width, int height, int 
depth);
 
+void *GPU_texture_read(GPUTexture *tex, GPUDataFormat gpu_data_format, int 
miplvl);
+
 void GPU_invalid_tex_init(void);
 void GPU_invalid_tex_bind(int mode);
 void GPU_invalid_tex_free(void);
@@ -202,6 +221,7 @@ int GPU_texture_detach_framebuffer(GPUTexture *tex, struct 
GPUFrameBuffer *fb);
 int GPU_texture_target(const GPUTexture *tex);
 int GPU_texture_width(const GPUTexture *tex);
 int GPU_texture_height(const GPUTexture *tex);
+int GPU_texture_layers(const GPUTexture *tex);
 GPUTextureFormat GPU_texture_format(const GPUTexture *tex);
 int GPU_texture_samples(const GPUTexture *tex);
 bool GPU_texture_cube(const GPUTexture *tex);
@@ -210,6 +230,8 @@ bool GPU_texture_stencil(const GPUTexture *tex);
 bool GPU_texture_integer(const GPUTexture *tex);
 int GPU_texture_opengl_bindcode(const GPUTexture *tex);
 
+void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *size);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/gpu/intern/gpu_texture.c 
b/source/blender/gpu/intern/gpu_texture.c
index b7385bdf07c..d9248e06dfb 100644
--- a/source/blender/gpu/intern/gpu_texture.c
+++ b/source/blender/gpu/intern/gpu_texture.c
@@ -134,73 +134,159 @@ unsigned int GPU_texture_memory_usage_get(void)
 
 /* -------------------------------- */
 
-static GLenum gpu_texture_get_format(
-        int components, GPUTextureFormat data_type,
-        GLenum *format, GLenum *data_format, GPUTextureFormatFlag 
*format_flag, unsigned int *bytesize)
+static int gpu_get_component_count(GPUTextureFormat format)
+{
+       switch (format) {
+               case GPU_RGBA8:
+               case GPU_RGBA16F:
+               case GPU_RGBA16:
+               case GPU_RGBA32F:
+                       return 4;
+               case GPU_RGB16F:
+               case GPU_R11F_G11F_B10F:
+                       return 3;
+               case GPU_RG8:
+               case GPU_RG16:
+               case GPU_RG16F:
+               case GPU_RG16I:
+               case GPU_RG16UI:
+               case GPU_RG32F:
+                       return 2;
+               default:
+                       return 1;
+       }
+}
+
+/* Definitely not complete, edit according to the gl specification. */
+static void gpu_validate_data_format(GPUTextureFormat tex_format, 
GPUDataFormat data_format)
+{
+       if (ELEM(tex_format, GPU_DEPTH_COMPONENT24,
+                           GPU_DEPTH_COMPONENT16,
+                           GPU_DEPTH_COMPONENT32F))
+       {
+               BLI_assert(data_format == GPU_DATA_FLOAT);
+       }
+       else if (tex_format == GPU_DEPTH24_STENCIL8) {
+               BLI_assert(data_format == GPU_DATA_UNSIGNED_INT_24_8);
+       }
+       else {
+               /* Integer formats */
+               if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, 
GPU_R16UI, GPU_R32UI)) {
+                       if (ELEM(tex_format, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) 
{
+                               BLI_assert(data_format == 
GPU_DATA_UNSIGNED_INT);
+                       }
+                       else {
+                               BLI_assert(data_format == GPU_DATA_INT);
+                       }
+               }
+               /* Byte formats */
+               else if (ELEM(tex_format, GPU_R8, GPU_RG8, GPU_RGBA8)) {
+                       BLI_assert(ELEM(data_format, GPU_DATA_UNSIGNED_BYTE, 
GPU_DATA_FLOAT));
+               }
+               /* Special case */
+               else if (ELEM(tex_format, GPU_R11F_G11F_B10F)) {
+                       BLI_assert(ELEM(data_format, GPU_DATA_10_11_11_REV, 
GPU_DATA_FLOAT));
+               }
+               /* Float formats */
+               else {
+                       BLI_assert(ELEM(data_format, GPU_DATA_FLOAT));
+               }
+       }
+}
+
+static GPUDataFormat gpu_get_data_format_from_tex_format(GPUTextureFormat 
tex_format)
+{
+       if (ELEM(tex_format, GPU_DEPTH_COMPONENT24,
+                           GPU_DEPTH_COMPONENT16,
+                           GPU_DEPTH_COMPONENT32F))
+       {
+               return GPU_DATA_FLOAT;
+       }
+       else if (tex_format == GPU_DEPTH24_STENCIL8) {
+               return GPU_DATA_UNSIGNED_INT_24_8;
+       }
+       else {
+               /* Integer formats */
+               if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, 
GPU_R16UI, GPU_R32UI)) {
+                       if (ELEM(tex_format, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) 
{
+                               return GPU_DATA_UNSIGNED_INT;
+                       }
+                       else {
+                               return GPU_DATA_INT;
+                       }
+               }
+               /* Byte formats */
+               else if (ELEM(tex_format, GPU_R8)) {
+                       return GPU_DATA_UNSIGNED_BYTE;
+               }
+               /* Special case */
+               else if (ELEM(tex_format, GPU_R11F_G11F_B10F)) {
+                       return GPU_DATA_10_11_11_REV;
+               }
+               else {
+                       return GPU_DATA_FLOAT;
+               }
+       }
+}
+
+/* Definitely not complete, edit according to the gl specification. */
+static GLenum gpu_get_gl_dataformat(GPUTextureFormat data_type, 
GPUTextureFormatFlag *format_flag)
 {
        if (ELEM(data_type, GPU_DEPTH_COMPONENT24,
                            GPU_DEPTH_COMPONENT16,
                            GPU_DEPTH_COMPONENT32F))
        {
                *format_flag |= GPU_FORMAT_DEPTH;
-               *data_format = GL_FLOAT;
-               *format = GL_DEPTH_COMPONENT;
+               return GL_DEPTH_COMPONENT;
        }
        else if (data_type == GPU_DEPTH24_STENCIL8) {
                *format_flag |= GPU_FORMAT_DEPTH | GPU_FORMAT_STENCIL;
-               *data_format = GL_UNSIGNED_INT_24_8;
-               *format = GL_DEPTH_STENCIL;
+               return GL_DEPTH_STENCIL;
        }
        else {
                /* Integer formats */
                if (ELEM(data_type, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, 
GPU_R32UI)) {
-                       if (ELEM(data_type, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) {
-                               *data_format = GL_UNSIGNED_INT;
-                       }
-                       else {
-                               *data_format = GL_INT;
-                       }
-
                        *format_flag |= GPU_FORMAT_INTEGER;
 
-                       switch (components) {
-                               case 1: *format = GL_RED_INTEGER; break;
-                               case 2: *format = GL_RG_INTEGER; break;
-                               case 3: *format = GL_RGB_INTEGER; break;
-                               case 4: *format = GL_RGBA_INTEGER; break;
-                               default: break;
+                       switch (gpu_get_component_count(data_type)) {
+                               case 1: return GL_RED_INTEGER; break;
+                               case 2: return GL_RG_INTEGER; break;
+                               case 3: return GL_RGB_INTEGER; break;
+                               case 4: return GL_RGBA_INTEGER; break;
                        }
                }
                else if (ELEM(data_type, GPU_R8)) {
-                       *data_format = GL_UNSIGNED_BYTE;
-                       *format = GL_RED;
+                       *format_flag |= GPU_FORMAT_FLOAT;
+                       return GL_RED;
                }
                else {
-                       *data_format = GL_FLOAT;
                        *format_flag |= GPU_FORMAT_FLOAT;
 
-                       switch (components) {
-                               case 1: *format = GL_RED; break;
-                               case 2: *format = GL_RG; break;
-                               case 3: *format = GL_RGB; break;
-                               case 4: *format = GL_RGBA; break;
-                               default: break;
+                       switch (gpu_get_component_count(data_type)) {
+                               case 1: return GL_RED; break;
+                               case 2: return GL_RG; break;
+                               case 3: return GL_RGB; break;
+                               case 4: return GL_RGBA; break;
                        }
                }
        }
 
+       BLI_assert(0);
+       *format_flag |= GPU_FORMAT_FLOAT;
+       return GL_RGBA;
+}
+
+static unsigned int gpu_get_bytesize(GPUTextureFormat data_type)
+{
        switch (data_type) {
                case GPU_RGBA32F:
-                       *bytesize = 32;
-                       break;
+                       return 32;
                case GPU_RG32F:
                case GPU_RGBA16F:
                case GPU_RGBA16:
-                       *bytesize = 16;
-                       break;
+                       return 16;
                case GPU_RGB16F:
-                       *bytesize = 12;
-                       break;
+

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to