[Bf-blender-cvs] [bdda53fdb29] master: GPU: Texture: Replace internal sampler state by explicit state object

Clément Foucault noreply at git.blender.org
Tue Jun 2 10:48:00 CEST 2020


Commit: bdda53fdb29d9f015df2db66a0cda98b0c19e1ff
Author: Clément Foucault
Date:   Tue Jun 2 10:47:45 2020 +0200
Branches: master
https://developer.blender.org/rBbdda53fdb29d9f015df2db66a0cda98b0c19e1ff

GPU: Texture: Replace internal sampler state by explicit state object

This makes it easier to track as well as allowing us to sample the same
texture with different sampling parameters (which should fix the related
T73942 in the long run).

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D7831

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

M	source/blender/blenkernel/intern/studiolight.c
M	source/blender/draw/engines/eevee/eevee_lightcache.c
M	source/blender/draw/engines/eevee/eevee_subsurface.c
M	source/blender/draw/engines/eevee/eevee_volumes.c
M	source/blender/draw/engines/workbench/workbench_effect_antialiasing.c
M	source/blender/draw/intern/draw_manager_texture.c
M	source/blender/gpu/GPU_texture.h
M	source/blender/gpu/intern/gpu_draw.c
M	source/blender/gpu/intern/gpu_extensions.c
M	source/blender/gpu/intern/gpu_texture.c

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

diff --git a/source/blender/blenkernel/intern/studiolight.c b/source/blender/blenkernel/intern/studiolight.c
index aef274f7d91..4892e8d6ede 100644
--- a/source/blender/blenkernel/intern/studiolight.c
+++ b/source/blender/blenkernel/intern/studiolight.c
@@ -503,10 +503,8 @@ static void studiolight_create_equirect_radiance_gputexture(StudioLight *sl)
     sl->equirect_radiance_gputexture = GPU_texture_create_2d(
         ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, NULL);
     GPUTexture *tex = sl->equirect_radiance_gputexture;
-    GPU_texture_bind(tex, 0);
     GPU_texture_filter_mode(tex, true);
     GPU_texture_wrap_mode(tex, true, true);
-    GPU_texture_unbind(tex);
   }
   sl->flag |= STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE;
 }
@@ -567,10 +565,8 @@ static void studiolight_create_equirect_irradiance_gputexture(StudioLight *sl)
     sl->equirect_irradiance_gputexture = GPU_texture_create_2d(
         ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, NULL);
     GPUTexture *tex = sl->equirect_irradiance_gputexture;
-    GPU_texture_bind(tex, 0);
     GPU_texture_filter_mode(tex, true);
     GPU_texture_wrap_mode(tex, true, true);
-    GPU_texture_unbind(tex);
   }
   sl->flag |= STUDIOLIGHT_EQUIRECT_IRRADIANCE_GPUTEXTURE;
 }
diff --git a/source/blender/draw/engines/eevee/eevee_lightcache.c b/source/blender/draw/engines/eevee/eevee_lightcache.c
index 614c749b9aa..198d06d845c 100644
--- a/source/blender/draw/engines/eevee/eevee_lightcache.c
+++ b/source/blender/draw/engines/eevee/eevee_lightcache.c
@@ -379,9 +379,7 @@ static bool eevee_lightcache_static_load(LightCache *lcache)
                                                 0,
                                                 false,
                                                 NULL);
-    GPU_texture_bind(lcache->grid_tx.tex, 0);
     GPU_texture_filter_mode(lcache->grid_tx.tex, true);
-    GPU_texture_unbind(lcache->grid_tx.tex);
   }
 
   if (lcache->cube_tx.tex == NULL) {
@@ -406,13 +404,11 @@ static bool eevee_lightcache_static_load(LightCache *lcache)
                                                   NULL);
     }
 
-    GPU_texture_bind(lcache->cube_tx.tex, 0);
-    GPU_texture_mipmap_mode(lcache->cube_tx.tex, true, true);
     for (int mip = 0; mip < lcache->mips_len; mip++) {
       GPU_texture_add_mipmap(
           lcache->cube_tx.tex, GPU_DATA_10_11_11_REV, mip + 1, lcache->cube_mips[mip].data);
     }
-    GPU_texture_unbind(lcache->cube_tx.tex);
+    GPU_texture_mipmap_mode(lcache->cube_tx.tex, true, true);
   }
   return true;
 }
diff --git a/source/blender/draw/engines/eevee/eevee_subsurface.c b/source/blender/draw/engines/eevee/eevee_subsurface.c
index 4f334812a8e..0726f1822cc 100644
--- a/source/blender/draw/engines/eevee/eevee_subsurface.c
+++ b/source/blender/draw/engines/eevee/eevee_subsurface.c
@@ -350,23 +350,15 @@ void EEVEE_subsurface_compute(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     if (!DRW_pass_is_empty(psl->sss_translucency_ps)) {
       /* We sample the shadow-maps using normal sampler. We need to disable Comparison mode.
        * TODO(fclem) avoid this by using sampler objects.*/
-      GPU_texture_bind(sldata->shadow_cube_pool, 0);
       GPU_texture_compare_mode(sldata->shadow_cube_pool, false);
-      GPU_texture_unbind(sldata->shadow_cube_pool);
-      GPU_texture_bind(sldata->shadow_cascade_pool, 0);
       GPU_texture_compare_mode(sldata->shadow_cascade_pool, false);
-      GPU_texture_unbind(sldata->shadow_cascade_pool);
 
       GPU_framebuffer_bind(fbl->sss_translucency_fb);
       DRW_draw_pass(psl->sss_translucency_ps);
 
       /* Reset original state. */
-      GPU_texture_bind(sldata->shadow_cube_pool, 0);
       GPU_texture_compare_mode(sldata->shadow_cube_pool, true);
-      GPU_texture_unbind(sldata->shadow_cube_pool);
-      GPU_texture_bind(sldata->shadow_cascade_pool, 0);
       GPU_texture_compare_mode(sldata->shadow_cascade_pool, true);
-      GPU_texture_unbind(sldata->shadow_cascade_pool);
     }
 
     /* 1. horizontal pass */
diff --git a/source/blender/draw/engines/eevee/eevee_volumes.c b/source/blender/draw/engines/eevee/eevee_volumes.c
index 83bd4fcf8d2..55e7b0eb0e3 100644
--- a/source/blender/draw/engines/eevee/eevee_volumes.c
+++ b/source/blender/draw/engines/eevee/eevee_volumes.c
@@ -775,12 +775,8 @@ void EEVEE_volumes_compute(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
 
     /* We sample the shadow-maps using shadow sampler. We need to enable Comparison mode.
      * TODO(fclem) avoid this by using sampler objects.*/
-    GPU_texture_bind(sldata->shadow_cube_pool, 0);
     GPU_texture_compare_mode(sldata->shadow_cube_pool, true);
-    GPU_texture_unbind(sldata->shadow_cube_pool);
-    GPU_texture_bind(sldata->shadow_cascade_pool, 0);
     GPU_texture_compare_mode(sldata->shadow_cascade_pool, true);
-    GPU_texture_unbind(sldata->shadow_cascade_pool);
 
     GPU_framebuffer_bind(fbl->volumetric_fb);
     DRW_draw_pass(psl->volumetric_world_ps);
diff --git a/source/blender/draw/engines/workbench/workbench_effect_antialiasing.c b/source/blender/draw/engines/workbench/workbench_effect_antialiasing.c
index cb8eb7d1e92..0e896c4b7bb 100644
--- a/source/blender/draw/engines/workbench/workbench_effect_antialiasing.c
+++ b/source/blender/draw/engines/workbench/workbench_effect_antialiasing.c
@@ -266,13 +266,8 @@ void workbench_antialiasing_engine_init(WORKBENCH_Data *vedata)
                                                 false,
                                                 NULL);
 
-      GPU_texture_bind(txl->smaa_search_tx, 0);
       GPU_texture_filter_mode(txl->smaa_search_tx, true);
-      GPU_texture_unbind(txl->smaa_search_tx);
-
-      GPU_texture_bind(txl->smaa_area_tx, 0);
       GPU_texture_filter_mode(txl->smaa_area_tx, true);
-      GPU_texture_unbind(txl->smaa_area_tx);
     }
   }
   else {
diff --git a/source/blender/draw/intern/draw_manager_texture.c b/source/blender/draw/intern/draw_manager_texture.c
index 810a2e9389b..77b0462303d 100644
--- a/source/blender/draw/intern/draw_manager_texture.c
+++ b/source/blender/draw/intern/draw_manager_texture.c
@@ -61,17 +61,17 @@ static bool drw_texture_format_supports_framebuffer(eGPUTextureFormat format)
 
 void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags)
 {
-  GPU_texture_bind(tex, 0);
   if (flags & DRW_TEX_MIPMAP) {
     GPU_texture_mipmap_mode(tex, true, flags & DRW_TEX_FILTER);
+    GPU_texture_bind(tex, 0);
     GPU_texture_generate_mipmap(tex);
+    GPU_texture_unbind(tex);
   }
   else {
     GPU_texture_filter_mode(tex, flags & DRW_TEX_FILTER);
   }
   GPU_texture_wrap_mode(tex, flags & DRW_TEX_WRAP, true);
   GPU_texture_compare_mode(tex, flags & DRW_TEX_COMPARE);
-  GPU_texture_unbind(tex);
 }
 
 GPUTexture *DRW_texture_create_1d(int w,
diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h
index 3d99a3c8a56..a1e00793857 100644
--- a/source/blender/gpu/GPU_texture.h
+++ b/source/blender/gpu/GPU_texture.h
@@ -41,6 +41,31 @@ struct PreviewImage;
 struct GPUFrameBuffer;
 typedef struct GPUTexture GPUTexture;
 
+/* GPU Samplers state
+ * - Specify the sampler state to bind a texture with.
+ * - Internally used by textures.
+ * - All states are created at startup to avoid runtime costs.
+ */
+
+typedef enum eGPUSamplerState {
+  GPU_SAMPLER_FILTER = (1 << 0),
+  GPU_SAMPLER_MIPMAP = (1 << 1),
+  GPU_SAMPLER_REPEAT_S = (1 << 2),
+  GPU_SAMPLER_REPEAT_T = (1 << 3),
+  GPU_SAMPLER_REPEAT_R = (1 << 4),
+  GPU_SAMPLER_CLAMP_BORDER = (1 << 5), /* Clamp to border color instead of border texel. */
+  GPU_SAMPLER_COMPARE = (1 << 6),
+  GPU_SAMPLER_ANISO = (1 << 7),
+  /* Don't use that. */
+  GPU_SAMPLER_MAX = (1 << 8),
+} eGPUSamplerState;
+
+#define GPU_SAMPLER_DEFAULT GPU_SAMPLER_FILTER
+#define GPU_SAMPLER_REPEAT (GPU_SAMPLER_REPEAT_S | GPU_SAMPLER_REPEAT_T | GPU_SAMPLER_REPEAT_R)
+
+void GPU_samplers_init(void);
+void GPU_samplers_free(void);
+
 /* GPU Texture
  * - always returns unsigned char RGBA textures
  * - if texture with non square dimensions is created, depending on the
diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c
index 7871907a7d4..7a9b4bafc74 100644
--- a/source/blender/gpu/intern/gpu_draw.c
+++ b/source/blender/gpu/intern/gpu_draw.c
@@ -161,7 +161,7 @@ static GLenum gpu_get_mipmap_filter(bool mag)
 void GPU_set_anisotropic(Main *bmain, float value)
 {
   if (GTS.anisotropic != value) {
-    GPU_free_images(bmain);
+    GPU_samplers_free();
 
     /* Clamp value to the maximum value the graphics card supports */
     const float max = GPU_max_texture_anisotropy();
@@ -170,6 +170,8 @@ void GPU_set_anisotropic(Main *bmain, float value)
     }
 
     GTS.anisotropic = value;
+
+    GPU_samplers_init();
   }
 }
 
@@ -449,22 +451,12 @@ static uint gpu_texture_create_tile_array(Image *ima, ImBuf *main_ibuf)
     BKE_image_release_ibuf(ima, ibuf, NULL);
   }
 
-  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
-
   if (GPU_get_mipmap()) {
     glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
-    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
     if (ima) {
       ima->gpuflag |= IMA_GPU_MIPMAP_COMPLETE;
     }
   }
-  else {
-    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-  }
-
-  if (GLEW_EXT_texture_filter_anisotropic) {
-    glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY_EXT, GPU_get_anisotropic());
-  }
 
   glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
 
@@ -1098,18 +1090,12 @@ void GPU_create_gl_tex(uint *bind,
           GL_TEXTURE_2D, 0, internal_format, rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, rect);
     }
 
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
-
     if (GPU_get_mipmap() && mipmap) {
       glGenerateMipmap(GL_TEXTURE_2D);
-      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
       if (ima) {
         ima->gpuflag |= IMA_GPU_MIPMAP_COMPLETE;
       }
     }
-    else {
-      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-    }
   }
   else if (textarget == GL_TEXTURE_CUBE_MAP) {
     int w = rectw / 3, h = recth / 2;
@@ -1132,22 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list