Skip to content

Commit

Permalink
Fix gtao shader in webotsJS (#5504)
Browse files Browse the repository at this point in the history
* fix gtao

* Update src/wren/PostProcessingEffect.cpp

Co-authored-by: Yannick Goumaz <[email protected]>

Co-authored-by: Yannick Goumaz <[email protected]>
  • Loading branch information
Benjamin Délèze and ygoumaz authored Nov 15, 2022
1 parent 0ccb754 commit c09ece6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/wren/glsl_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef enum WrGlslLayoutUniform {
WR_GLSL_LAYOUT_UNIFORM_COLOR_PER_VERTEX,
WR_GLSL_LAYOUT_UNIFORM_POINT_SIZE,
WR_GLSL_LAYOUT_UNIFORM_CHANNEL_COUNT,
WR_GLSL_LAYOUT_UNIFORM_GTAO,
WR_GLSL_LAYOUT_UNIFORM_COUNT
} WrGlslLayoutUniform;

Expand Down
2 changes: 1 addition & 1 deletion resources/web/wwi/wren/WbWrenShaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class WbWrenShaders {
_wr_shader_program_use_uniform(WbWrenShaders.gShaders[WbWrenShaders.SHADER.SHADER_GTAO],
Enum.WR_GLSL_LAYOUT_UNIFORM_TEXTURE1);
_wr_shader_program_use_uniform(WbWrenShaders.gShaders[WbWrenShaders.SHADER.SHADER_GTAO],
Enum.WR_GLSL_LAYOUT_UNIFORM_TEXTURE2);
Enum.WR_GLSL_LAYOUT_UNIFORM_GTAO);
_wr_shader_program_use_uniform(WbWrenShaders.gShaders[WbWrenShaders.SHADER.SHADER_GTAO],
Enum.WR_GLSL_LAYOUT_UNIFORM_VIEWPORT_SIZE);

Expand Down
5 changes: 3 additions & 2 deletions resources/wren/shaders/gtao.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ precision highp float;
#define PI 3.1415926535897932
#define PI_HALF 1.5707963267948966

uniform sampler2D inputTextures[3];
uniform sampler2D inputTextures[2];
uniform sampler2D gtaoTexture;

uniform float radius;
uniform bool flipNormalY;
Expand Down Expand Up @@ -127,7 +128,7 @@ void main() {

// fetch noises and calculate jittered slice angle
ivec2 loc = ivec2(gl_FragCoord.xy);
vec2 noises = texelFetch(inputTextures[2], loc % 4, 0).rg;
vec2 noises = texelFetch(gtaoTexture, loc % 4, 0).rg;
float sliceAngle = (params.x + noises.x) * PI;
float currentStep = mod(params.y + noises.y, 1.0) * (stepsize - 1.0) + 1.0;
vec3 searchDirection = vec3(cos(sliceAngle), sin(sliceAngle), 0.0);
Expand Down
2 changes: 1 addition & 1 deletion src/webots/wren/WbWrenShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ WrShaderProgram *WbWrenShaders::gtaoShader() {

wr_shader_program_use_uniform(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_TEXTURE0);
wr_shader_program_use_uniform(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_TEXTURE1);
wr_shader_program_use_uniform(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_TEXTURE2);
wr_shader_program_use_uniform(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_GTAO);
wr_shader_program_use_uniform(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_VIEWPORT_SIZE);

wr_shader_program_use_uniform_buffer(gShaders[SHADER_GTAO], WR_GLSL_LAYOUT_UNIFORM_BUFFER_CAMERA_TRANSFORMS);
Expand Down
2 changes: 1 addition & 1 deletion src/wren/GlslLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace wren {
"inputTextures[0]", "inputTextures[1]", "inputTextures[2]", "inputTextures[3]", "inputTextures[4]", "inputTextures[5]",
"inputTextures[6]", "inputTextures[7]", "inputTextures[8]", "inputTextures[9]", "inputTextures[10]", "inputTextures[11]",
"inputTextures[12]", "cubeTextures[0]", "cubeTextures[1]", "iterationNumber", "modelTransform", "textureTransform",
"viewportSize", "colorPerVertex", "pointSize", "channelCount"};
"viewportSize", "colorPerVertex", "pointSize", "channelCount", "gtaoTexture"};

const std::vector<const char *> gUniformBufferNames = {"PhongMaterial", "PbrMaterial", "Lights", "LightRenderable",
"CameraTransforms", "Fog", "Overlay"};
Expand Down
8 changes: 7 additions & 1 deletion src/wren/PostProcessingEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ namespace wren {
mInputTextures[i]->setTextureUnit(i);
mInputTextures[i]->bind(mInputTextureParams[i]);

const int locationTexture =
int locationTexture =
mProgram->uniformLocation(static_cast<WrGlslLayoutUniform>(WR_GLSL_LAYOUT_UNIFORM_TEXTURE0 + i));

// Special gtao case to bypass a chrome driver bug where texelFetch does not work with textures coming from array:
// https://community.amd.com/t5/archives-discussions/bug-report-texelfetch-shader-crash-on-msaa-fbo/td-p/87124
if (locationTexture == -1 && i == 2)
locationTexture = mProgram->uniformLocation(static_cast<WrGlslLayoutUniform>(WR_GLSL_LAYOUT_UNIFORM_GTAO));

assert(locationTexture >= 0);
glUniform1i(locationTexture, mInputTextures[i]->textureUnit());
}
Expand Down

0 comments on commit c09ece6

Please sign in to comment.