[Bf-blender-cvs] [421d41dc2ff] new-object-types: Smoke: put density/color in separate textrues, fixes for workbench shader

Brecht Van Lommel noreply at git.blender.org
Sat Feb 15 22:27:33 CET 2020


Commit: 421d41dc2ffd14d9ddcf9d4675212d6ca502f01a
Author: Brecht Van Lommel
Date:   Thu Feb 13 17:37:44 2020 +0100
Branches: new-object-types
https://developer.blender.org/rB421d41dc2ffd14d9ddcf9d4675212d6ca502f01a

Smoke: put density/color in separate textrues, fixes for workbench shader

This is more in line with standard grids and means we don't have to make as
many special exceptions in the shading code.

The workbench shader was also changed to fix bugs where squared density was
used, and the smoke color would affect the density.

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

M	source/blender/blenloader/intern/readfile.c
M	source/blender/draw/engines/eevee/eevee_volumes.c
M	source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
M	source/blender/draw/engines/workbench/workbench_volume.c
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/intern/gpu_codegen.c
M	source/blender/gpu/intern/gpu_draw_smoke.c
M	source/blender/gpu/shaders/material/gpu_shader_material_volume_info.glsl
M	source/blender/makesdna/DNA_fluid_types.h
M	source/blender/nodes/shader/nodes/node_shader_attribute.c
M	source/blender/nodes/shader/nodes/node_shader_volume_info.c
M	source/blender/nodes/shader/nodes/node_shader_volume_principled.c

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index a23df405865..a9f9927e5c7 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5526,7 +5526,8 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb, Object *ob)
 
         mmd->domain->fluid = NULL;
         mmd->domain->fluid_mutex = BLI_rw_mutex_alloc();
-        mmd->domain->tex = NULL;
+        mmd->domain->tex_density = NULL;
+        mmd->domain->tex_color = NULL;
         mmd->domain->tex_shadow = NULL;
         mmd->domain->tex_flame = NULL;
         mmd->domain->tex_flame_coba = NULL;
diff --git a/source/blender/draw/engines/eevee/eevee_volumes.c b/source/blender/draw/engines/eevee/eevee_volumes.c
index 72d90498edf..25b8a0ad7c1 100644
--- a/source/blender/draw/engines/eevee/eevee_volumes.c
+++ b/source/blender/draw/engines/eevee/eevee_volumes.c
@@ -61,6 +61,7 @@ static struct {
   GPUTexture *depth_src;
 
   GPUTexture *dummy_density;
+  GPUTexture *dummy_color;
   GPUTexture *dummy_flame;
 
   GPUTexture *dummy_scatter;
@@ -140,10 +141,13 @@ static void eevee_create_shader_volumes(void)
                                                             e_data.volumetric_common_lib,
                                                             NULL);
 
-  float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
-  e_data.dummy_density = DRW_texture_create_3d(1, 1, 1, GPU_RGBA8, DRW_TEX_WRAP, color);
+  const float density = 0.0f;
+  e_data.dummy_density = DRW_texture_create_3d(1, 1, 1, GPU_R8, DRW_TEX_WRAP, &density);
 
-  float flame = 0.0f;
+  const float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+  e_data.dummy_color = DRW_texture_create_3d(1, 1, 1, GPU_RGBA8, DRW_TEX_WRAP, color);
+
+  const float flame = 0.0f;
   e_data.dummy_flame = DRW_texture_create_3d(1, 1, 1, GPU_R8, DRW_TEX_WRAP, &flame);
 }
 
@@ -460,6 +464,7 @@ void EEVEE_volumes_cache_object_add(EEVEE_ViewLayerData *sldata,
 
     // TODO: why a texture reference? probably there is no way to dynamically update it.
     DRW_shgroup_uniform_texture_ref(grp, "sampdensity", &density->texture);
+    DRW_shgroup_uniform_texture_ref(grp, "sampcolor", &e_data.dummy_color);
     DRW_shgroup_uniform_texture_ref(grp, "sampflame", &e_data.dummy_flame);
 
     DRW_shgroup_uniform_vec3(grp, "volumeColor", white, 1);
@@ -504,7 +509,9 @@ void EEVEE_volumes_cache_object_add(EEVEE_ViewLayerData *sldata,
     }
 
     DRW_shgroup_uniform_texture_ref(
-        grp, "sampdensity", mds->tex ? &mds->tex : &e_data.dummy_density);
+        grp, "sampdensity", mds->tex_density ? &mds->tex_density : &e_data.dummy_density);
+    DRW_shgroup_uniform_texture_ref(
+        grp, "sampcolor", mds->tex_color ? &mds->tex_color : &e_data.dummy_color);
     DRW_shgroup_uniform_texture_ref(
         grp, "sampflame", mds->tex_flame ? &mds->tex_flame : &e_data.dummy_flame);
 
@@ -526,6 +533,7 @@ void EEVEE_volumes_cache_object_add(EEVEE_ViewLayerData *sldata,
   }
   else {
     DRW_shgroup_uniform_texture(grp, "sampdensity", e_data.dummy_density);
+    DRW_shgroup_uniform_texture(grp, "sampcolor", e_data.dummy_color);
     DRW_shgroup_uniform_texture(grp, "sampflame", e_data.dummy_flame);
     DRW_shgroup_uniform_vec3(grp, "volumeColor", white, 1);
     DRW_shgroup_uniform_vec2(grp, "unftemperature", (float[2]){0.0f, 1.0f}, 1);
@@ -768,6 +776,7 @@ void EEVEE_volumes_free(void)
 
   DRW_TEXTURE_FREE_SAFE(e_data.dummy_density);
   DRW_TEXTURE_FREE_SAFE(e_data.dummy_flame);
+  DRW_TEXTURE_FREE_SAFE(e_data.dummy_color);
 
   DRW_SHADER_FREE_SAFE(e_data.volumetric_clear_sh);
   DRW_SHADER_FREE_SAFE(e_data.scatter_sh);
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
index 09593d3543b..7483ccbad7c 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
@@ -138,8 +138,9 @@ void volume_properties(vec3 ls_pos, out vec3 scattering, out float extinction)
   float shadows = sample_volume_texture(shadowTexture, co).r;
   vec4 density = sample_volume_texture(densityTexture, co); /* rgb: color, a: density */
 
-  scattering = density.rgb * (density.a * densityScale) * activeColor;
+  scattering = density.rgb * densityScale;
   extinction = max(1e-4, dot(scattering, vec3(0.33333)));
+  scattering *= activeColor;
 
   /* Scale shadows in log space and clamp them to avoid completely black shadows. */
   scattering *= exp(clamp(log(shadows) * densityScale * 0.1, -2.5, 0.0)) * M_PI;
diff --git a/source/blender/draw/engines/workbench/workbench_volume.c b/source/blender/draw/engines/workbench/workbench_volume.c
index f55e0085076..581f9a65995 100644
--- a/source/blender/draw/engines/workbench/workbench_volume.c
+++ b/source/blender/draw/engines/workbench/workbench_volume.c
@@ -157,7 +157,8 @@ static void workbench_volume_modifier_cache_populate(WORKBENCH_Data *vedata,
     GPU_create_smoke(mmd, 1);
   }
 
-  if ((!mds->use_coba && mds->tex == NULL) || (mds->use_coba && mds->tex_field == NULL)) {
+  if ((!mds->use_coba && (mds->tex_density == NULL && mds->tex_color == NULL)) ||
+      (mds->use_coba && mds->tex_field == NULL)) {
     return;
   }
 
@@ -212,7 +213,8 @@ static void workbench_volume_modifier_cache_populate(WORKBENCH_Data *vedata,
     static float white[3] = {1.0f, 1.0f, 1.0f};
     bool use_constant_color = ((mds->active_fields & FLUID_DOMAIN_ACTIVE_COLORS) == 0 &&
                                (mds->active_fields & FLUID_DOMAIN_ACTIVE_COLOR_SET) != 0);
-    DRW_shgroup_uniform_texture(grp, "densityTexture", mds->tex);
+    DRW_shgroup_uniform_texture(
+        grp, "densityTexture", (mds->tex_color) ? mds->tex_color : mds->tex_density);
     DRW_shgroup_uniform_texture(grp, "shadowTexture", mds->tex_shadow);
     DRW_shgroup_uniform_texture(
         grp, "flameTexture", (mds->tex_flame) ? mds->tex_flame : e_data.dummy_tex);
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index 075829065df..f44695bb5cc 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -100,11 +100,12 @@ typedef enum eGPUBuiltin {
   GPU_INVERSE_LOC_TO_VIEW_MATRIX = (1 << 14),
   GPU_OBJECT_INFO = (1 << 15),
   GPU_VOLUME_DENSITY = (1 << 16),
-  GPU_VOLUME_FLAME = (1 << 17),
-  GPU_VOLUME_TEMPERATURE = (1 << 18),
-  GPU_BARYCENTRIC_TEXCO = (1 << 19),
-  GPU_BARYCENTRIC_DIST = (1 << 20),
-  GPU_WORLD_NORMAL = (1 << 21),
+  GPU_VOLUME_COLOR = (1 << 17),
+  GPU_VOLUME_FLAME = (1 << 18),
+  GPU_VOLUME_TEMPERATURE = (1 << 19),
+  GPU_BARYCENTRIC_TEXCO = (1 << 20),
+  GPU_BARYCENTRIC_DIST = (1 << 21),
+  GPU_WORLD_NORMAL = (1 << 22),
 } eGPUBuiltin;
 
 typedef enum eGPUMatFlag {
diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c
index ed6bf20cff4..b6cf51316d5 100644
--- a/source/blender/gpu/intern/gpu_codegen.c
+++ b/source/blender/gpu/intern/gpu_codegen.c
@@ -282,6 +282,9 @@ static const char *gpu_builtin_name(eGPUBuiltin builtin)
   else if (builtin == GPU_VOLUME_DENSITY) {
     return "sampdensity";
   }
+  else if (builtin == GPU_VOLUME_COLOR) {
+    return "sampcolor";
+  }
   else if (builtin == GPU_VOLUME_FLAME) {
     return "sampflame";
   }
@@ -357,7 +360,8 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
           name = gpu_builtin_name(input->builtin);
 
           if (BLI_str_startswith(name, "samp")) {
-            if ((input->builtin == GPU_VOLUME_DENSITY) || (input->builtin == GPU_VOLUME_FLAME)) {
+            if ((input->builtin == GPU_VOLUME_DENSITY) || (input->builtin == GPU_VOLUME_COLOR) ||
+                (input->builtin == GPU_VOLUME_FLAME)) {
               BLI_dynstr_appendf(ds, "uniform sampler3D %s;\n", name);
             }
           }
diff --git a/source/blender/gpu/intern/gpu_draw_smoke.c b/source/blender/gpu/intern/gpu_draw_smoke.c
index 19ef05835d8..aa7789b3342 100644
--- a/source/blender/gpu/intern/gpu_draw_smoke.c
+++ b/source/blender/gpu/intern/gpu_draw_smoke.c
@@ -122,10 +122,10 @@ static GPUTexture *create_transfer_function(int type, const struct ColorBand *co
   return tex;
 }
 
-static void swizzle_texture_channel_rrrr(GPUTexture *tex)
+static void swizzle_texture_channel_single(GPUTexture *tex)
 {
   GPU_texture_bind(tex, 0);
-  GPU_texture_swizzle_channel_rrrr(tex);
+  GPU_texture_swizzle_channel_auto(tex, 1);
   GPU_texture_unbind(tex);
 }
 
@@ -183,60 +183,59 @@ static GPUTexture *create_field_texture(FluidDomainSettings *mds)
   GPUTexture *tex = GPU_texture_create_nD(
       mds->res[0], mds->res[1], mds->res[2], 3, field, GPU_R8, GPU_DATA_FLOAT, 0, true, NULL);
 
-  swizzle_texture_channel_rrrr(tex);
+  swizzle_texture_channel_single(tex);
   return tex;
 }
 
 static GPUTexture *create_density_texture(FluidDomainSettings *mds, int highres)
 {
-  float *data = NULL, *source;
-  int cell_count = (highres) ? manta_smoke_turbulence_get_cells(mds->fluid) : mds->total_cells;
+  int *dim = (highres) ? mds->res_noise : mds->res;
+
+  float *data;
+  if (highres) {
+    data = manta_smoke_turbulence_get_density(mds->fluid);
+  }
+  else {
+    data = manta_smoke_get_density(mds->fluid);
+  }
+
+  GPUTexture *tex = GPU_texture_create_nD(
+      dim[0], dim[1], dim[2], 3, data, GPU_R8, GPU_DATA_FLOAT, 0, true, NULL);
+
+  swizzle_texture_channel_single(tex);
+
+  return tex;
+}
+
+static GPUTexture *create_color_texture(FluidDomainSettings *mds, int highres)
+{
   const bool has_color = (highres) ? manta_smoke_turbulence_has_colors(mds->fluid) :
                                      manta_smoke_has_colors(mds->fluid);
+
+  if (!has_color) {
+    return NULL;
+  }
+
+  int cell_count = (highres) ? manta_smoke_turbulence_get_cells(mds->fluid) : mds->total_cells;
   int *dim = (highres) ? mds->res_noise : mds->res;
-  eGPUTextureFormat format = (has_color) ? GPU_RGBA8 : GPU_R8;
+  float *data = MEM_callocN(sizeof(float) * cell_count * 4, "smokeColorTexture");
 
-  if (has_color) {
-    data = MEM_callocN(sizeof(float) * cell_count * 4, "smokeColorTexture");
+  if (data == NULL) {
+    return NULL;
   }
 
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list