From 4cae0cb418660aef4b47b19a0d89168759ddf9b3 Mon Sep 17 00:00:00 2001 From: turjuque Date: Thu, 16 Jun 2016 01:47:07 +0200 Subject: Fix texelFetch for older opengl version. --- source/blender/gpu/GPU_texture.h | 1 + source/blender/gpu/intern/gpu_pbr.c | 4 ++-- source/blender/gpu/intern/gpu_texture.c | 16 +++++++++++++ .../shaders/gpu_shader_downsample_maxz_frag.glsl | 27 ++++++++++++++-------- .../shaders/gpu_shader_material_bsdf_glossy.glsl | 2 +- .../shaders/gpu_shader_material_new_shading.glsl | 4 ++++ .../gpu/shaders/gpu_shader_material_utils.glsl | 26 ++++++++++++++++++--- 7 files changed, 65 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h index 06752fc73a4..571bd8938a4 100644 --- a/source/blender/gpu/GPU_texture.h +++ b/source/blender/gpu/GPU_texture.h @@ -65,6 +65,7 @@ GPUTexture *GPU_texture_create_1D(int w, const float *pixels, char err_out[256]) GPUTexture *GPU_texture_create_2D(int w, int h, const float *pixels, GPUHDRType hdr, char err_out[256]); GPUTexture *GPU_texture_create_3D(int w, int h, int depth, int channels, const float *fpixels); GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256]); +GPUTexture *GPU_texture_create_depth_buffer(int w, int h, char err_out[256]); GPUTexture *GPU_texture_create_vsm_shadow_map(int size, char err_out[256]); GPUTexture *GPU_texture_create_2D_procedural(int w, int h, const float *pixels, bool repeat, bool filtering, int channels, char err_out[256]); GPUTexture *GPU_texture_create_1D_procedural(int w, const float *pixels, int channels, char err_out[256]); diff --git a/source/blender/gpu/intern/gpu_pbr.c b/source/blender/gpu/intern/gpu_pbr.c index 30c25a4bb38..307855a25ec 100644 --- a/source/blender/gpu/intern/gpu_pbr.c +++ b/source/blender/gpu/intern/gpu_pbr.c @@ -97,7 +97,7 @@ static GPUScreenBuffer *gpu_scenebuf_create(int width, int height, bool depth_on } if (depth_only) { - buf->tex = GPU_texture_create_depth(width, height, NULL); + buf->tex = GPU_texture_create_depth_buffer(width, height, NULL); if (!buf->tex) { GPU_scenebuf_free(buf); return NULL; @@ -115,7 +115,7 @@ static GPUScreenBuffer *gpu_scenebuf_create(int width, int height, bool depth_on } } else { - buf->depth = GPU_texture_create_depth(width, height, NULL); + buf->depth = GPU_texture_create_depth_buffer(width, height, NULL); if (!buf->depth) { GPU_scenebuf_free(buf); return NULL; diff --git a/source/blender/gpu/intern/gpu_texture.c b/source/blender/gpu/intern/gpu_texture.c index 9ba984fb77e..37f874ee512 100644 --- a/source/blender/gpu/intern/gpu_texture.c +++ b/source/blender/gpu/intern/gpu_texture.c @@ -618,6 +618,22 @@ GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256]) return tex; } + + +GPUTexture *GPU_texture_create_depth_buffer(int w, int h, char err_out[256]) +{ + GPUTexture *tex = GPU_texture_create_nD(w, h, 2, NULL, 1, GPU_HDR_NONE, 1, 0, err_out); + + if (tex) { + /* Now we tweak some of the settings */ + glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_NONE); + + GPU_texture_unbind(tex); + } + return tex; +} + + GPUTexture *GPU_texture_create_depth_multisample(int w, int h, int samples, char err_out[256]) { GPUTexture *tex = GPU_texture_create_nD(w, h, 2, NULL, 1, GPU_HDR_NONE, 1, samples, err_out); diff --git a/source/blender/gpu/shaders/gpu_shader_downsample_maxz_frag.glsl b/source/blender/gpu/shaders/gpu_shader_downsample_maxz_frag.glsl index 3229b41ffdb..3279023670c 100644 --- a/source/blender/gpu/shaders/gpu_shader_downsample_maxz_frag.glsl +++ b/source/blender/gpu/shaders/gpu_shader_downsample_maxz_frag.glsl @@ -31,14 +31,23 @@ float minmax(float a, float b) #endif } +float texelFetchLowermip(ivec2 texelPos) +{ +#if __VERSION__ < 130 + return texture2DLod(lowermip, (texelPos * lowermipsize + 0.5) / lowermipsize, 0.0).r; +#else + return texelFetch(lowermip, texelPos, 0).r; +#endif +} + void main() { vec4 texels; ivec2 texelPos = ivec2(gl_FragCoord.xy) * 2; - texels.x = texelFetch(lowermip, texelPos, 0).r; - texels.y = texelFetch(lowermip, texelPos + ivec2(1, 0), 0).r; - texels.z = texelFetch(lowermip, texelPos + ivec2(1, 1), 0).r; - texels.w = texelFetch(lowermip, texelPos + ivec2(0, 1), 0).r; + texels.x = texelFetchLowermip(texelPos); + texels.y = texelFetchLowermip(texelPos + ivec2(1, 0)); + texels.z = texelFetchLowermip(texelPos + ivec2(1, 1)); + texels.w = texelFetchLowermip(texelPos + ivec2(0, 1)); float minmaxz = minmax(texels.x, texels.y, texels.z, texels.w); vec3 extra; @@ -46,17 +55,17 @@ void main() if (((lowermipsize.x & 1) != 0) && (int(gl_FragCoord.x) == lowermipsize.x-3)) { /* if both edges are odd, fetch the top-left corner texel */ if (((lowermipsize.y & 1) != 0) && (int(gl_FragCoord.y) == lowermipsize.y-3)) { - extra.z = texelFetch(lowermip, texelPos + ivec2(-1, -1), 0).r; + extra.z = texelFetchLowermip(texelPos + ivec2(-1, -1)); minmaxz = minmax(minmaxz, extra.z); } - extra.x = texelFetch(lowermip, texelPos + ivec2(0, -1), 0).r; - extra.y = texelFetch(lowermip, texelPos + ivec2(1, -1), 0).r; + extra.x = texelFetchLowermip(texelPos + ivec2(0, -1)); + extra.y = texelFetchLowermip(texelPos + ivec2(1, -1)); minmaxz = minmax(minmaxz, extra.x, extra.y); } /* if we are reducing an odd-height texture then fetch the edge texels */ else if (((lowermipsize.y & 1) != 0) && (int(gl_FragCoord.y) == lowermipsize.y-3)) { - extra.x = texelFetch(lowermip, texelPos + ivec2(0, -1), 0).r; - extra.y = texelFetch(lowermip, texelPos + ivec2(1, -1), 0).r; + extra.x = texelFetchLowermip(texelPos + ivec2(0, -1)); + extra.y = texelFetchLowermip(texelPos + ivec2(1, -1)); minmaxz = minmax(minmaxz, extra.x, extra.y); } diff --git a/source/blender/gpu/shaders/gpu_shader_material_bsdf_glossy.glsl b/source/blender/gpu/shaders/gpu_shader_material_bsdf_glossy.glsl index 7a941d2387c..33c683268fb 100644 --- a/source/blender/gpu/shaders/gpu_shader_material_bsdf_glossy.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material_bsdf_glossy.glsl @@ -564,7 +564,7 @@ void env_sampling_glossy_sharp( bool hit = raycast(viewpos, vL, hitstep, hitpixel, hitco); float contrib = ssr_contribution(viewpos, hitstep, hit, hitco); - vec4 sample_ssr = texelFetch(unfscenebuf, ivec2(hitpixel.xy), 0); + vec4 sample_ssr = bufferFetch(unfscenebuf, ivec2(hitpixel.xy), 0); srgb_to_linearrgb(sample_ssr, sample_ssr); result = mix(sample_probe.rgb, sample_ssr.rgb, contrib); diff --git a/source/blender/gpu/shaders/gpu_shader_material_new_shading.glsl b/source/blender/gpu/shaders/gpu_shader_material_new_shading.glsl index a92f17879a7..fcfa879e3d6 100644 --- a/source/blender/gpu/shaders/gpu_shader_material_new_shading.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material_new_shading.glsl @@ -502,7 +502,11 @@ void node_tex_image(vec3 co, sampler2D ima, out vec4 color, out float alpha) void node_tex_image_closest(vec3 co, sampler2D ima, vec2 res, out vec4 color, out float alpha) { +#if __VERSION__ < 130 + color = texture2DLod(ima, (floor(co.xy * res) + 0.5) / res, 0.0); +#else color = texelFetch(ima, ivec2(fract(co.xy) * res), 0); +#endif alpha = color.a; } diff --git a/source/blender/gpu/shaders/gpu_shader_material_utils.glsl b/source/blender/gpu/shaders/gpu_shader_material_utils.glsl index f5c8b6f2e08..74b03fd2e35 100644 --- a/source/blender/gpu/shaders/gpu_shader_material_utils.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material_utils.glsl @@ -69,6 +69,15 @@ float hypot(float x, float y) { return sqrt(x*x + y*y); } float inverse_distance(vec3 V) { return max( 1 / length(V), 1e-8); } +vec4 bufferFetch(sampler2D buffer, ivec2 texelpos, int lod) +{ +#if __VERSION__ < 130 + return texture2DLod(buffer, (vec2(texelpos) + 0.5) / (unfclip.zw / pow(2.0, float(lod))), float(lod)); +#else + return texelFetch(buffer, texelpos, lod); +#endif +} + /* --------- Noise Utils Functions --------- */ void generated_from_orco(vec3 orco, out vec3 generated) @@ -176,7 +185,7 @@ float linear_depth(float z) float backface_depth(ivec2 texelpos, int lod) { - float depth = linear_depth(texelFetch(unfbackfacebuf, texelpos, lod).r); + float depth = linear_depth(bufferFetch(unfbackfacebuf, texelpos, lod).r); /* background case */ if (depth == 1.0) @@ -187,7 +196,7 @@ float backface_depth(ivec2 texelpos, int lod) float frontface_depth(ivec2 texelpos, int lod) { - float depth = linear_depth(texelFetch(unfdepthbuf, texelpos, lod).r); + float depth = linear_depth(bufferFetch(unfdepthbuf, texelpos, lod).r); /* background case */ if (depth == 1.0) @@ -815,8 +824,13 @@ vec4 sample_refract(vec3 L) void setup_noise(vec2 fragcoord) { - ivec2 texel = ivec2(mod(fragcoord.x, 64), mod(fragcoord.y, 64)); + const int NOISE_SIZE = 64; + ivec2 texel = ivec2(mod(fragcoord.x, NOISE_SIZE), mod(fragcoord.y, NOISE_SIZE)); +#if __VERSION__ < 130 + jitternoise = texture2DLod(unfjitter, (vec2(texel) + 0.5) / float(NOISE_SIZE), 0.0).rg; /* Global variable */ +#else jitternoise = texelFetch(unfjitter, texel, 0).rg; /* Global variable */ +#endif } vec3 hammersley_3d(float i, float invsamplenbr) @@ -828,7 +842,13 @@ vec3 hammersley_3d(float i, float invsamplenbr) int u = int(mod(i + jitternoise.y * BSDF_SAMPLES, BSDF_SAMPLES)); Xi.yz = texelFetch(unflutsamples, u, 0).rg; + Xi.yz = texelFetch(unflutsamples, u, 0).rg; +#if __VERSION__ < 130 + Xi.yz = texture1DLod(unflutsamples, (float(u) + 0.5) / float(BSDF_SAMPLES), 0.0).rg; /* Global variable */ +#else + Xi.yz = texelFetch(unflutsamples, u, 0).rg; /* Global variable */ +#endif return Xi; } -- cgit v1.2.3