ebruneton / precomputed_atmospheric_scattering

This project provides a new implementation of our EGSR 2008 paper "Precomputed Atmospheric Scattering".
BSD 3-Clause "New" or "Revised" License
908 stars 120 forks source link

Weird texture binding #8

Closed stavenko closed 7 years ago

stavenko commented 7 years ago

Hello. I'm trying to reimplement your solution in javascript and found this:

    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
        delta_irradiance_texture, 0);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
        irradiance_texture_, 0);
    glDrawBuffers(2, kDrawBuffers);
    glViewport(0, 0, IRRADIANCE_TEXTURE_WIDTH, IRRADIANCE_TEXTURE_HEIGHT);
    compute_indirect_irradiance.Use();
    compute_indirect_irradiance.BindTexture3d(
        "single_rayleigh_scattering_texture",
        delta_rayleigh_scattering_texture,
        0);
    compute_indirect_irradiance.BindTexture3d(
        "single_mie_scattering_texture", delta_mie_scattering_texture, 1);
    compute_indirect_irradiance.BindTexture3d(
        "multiple_scattering_texture", delta_multiple_scattering_texture, 2);
    compute_indirect_irradiance.BindInt("scattering_order", scattering_order);
    glEnablei(GL_BLEND, 1);
    glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
    glBlendFuncSeparate(GL_ONE, GL_ONE, GL_ONE, GL_ONE);
    DrawQuad();
    glDisablei(GL_BLEND, 1);

There is two 3d textures binded to this shader "multiple_scattering_texture" and "single_rayleigh_scattering_texture". But, there is a line in model.cc file

  GLuint delta_multiple_scattering_texture = delta_rayleigh_scattering_texture;

This is same texture from shader's perspective, but it used strangely differrently. At least here this two textures treated as different textures.

       result += GetScattering(atmosphere, single_rayleigh_scattering_texture,
          single_mie_scattering_texture, multiple_scattering_texture,         
 r, omega.z, mu_s, nu, ray_r_theta_intersects_ground,
          scattering_order) *
              omega.z * domega;

in ComputeIndirectIrradiance function.

So, could you please explain why is it made so? Is it correct and I could use same textures in my code, Or this is an error and indirect irradiance shader should get some other texture (maybe scattering_texture_).

ebruneton commented 7 years ago

This is an optimization to save some GPU memory during precomputations. Indeed, delta_multiple_scattering_texture is only needed to compute scattering order 3 or more, while delta_rayleigh_scattering_texture and delta_mie_scattering_texture are only needed to compute double scattering. Therefore, to save memory, we can store delta_rayleigh_scattering_texture and delta_multiple_scattering_texture in the same GPU texture.

You can check that this does not change the result, by comparing with a version of the code where

GLuint delta_multiple_scattering_texture = delta_rayleigh_scattering_texture;

is replaced with

GLuint delta_multiple_scattering_texture = NewTexture3d( SCATTERING_TEXTURE_WIDTH, SCATTERING_TEXTURE_HEIGHT, SCATTERING_TEXTURE_DEPTH, GL_RGB, halfprecision);