X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=shaders%2Fscene_water_fast.h;h=1bd4ed64d22c14dd43e498fd5c6e207e6bac9d66;hb=98b9bcf0e10bc02cf679d03fa269613e140ba878;hp=bab38cd07110035514c9cc33225dda8181a37741;hpb=791f807111a1f740f745c67db642aa7a8bee56e8;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/shaders/scene_water_fast.h b/shaders/scene_water_fast.h index bab38cd..1bd4ed6 100644 --- a/shaders/scene_water_fast.h +++ b/shaders/scene_water_fast.h @@ -88,12 +88,16 @@ static struct vg_shader _shader_scene_water_fast = { "flat in ivec4 light_indices;\n" "\n" "uniform samplerBuffer uLightsArray;\n" +"uniform usampler3D uLightsIndex;\n" "\n" "#line 1 1 \n" "layout (location = 0) out vec4 oColour;\n" "\n" "layout (std140) uniform ub_world_lighting\n" "{\n" +" vec4 g_cube_min;\n" +" vec4 g_cube_inv_range;\n" +"\n" " vec4 g_light_colours[3];\n" " vec4 g_light_directions[3];\n" " vec4 g_ambient_colour;\n" @@ -209,16 +213,19 @@ static struct vg_shader _shader_scene_water_fast = { " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n" "}\n" "\n" -"vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n" +"vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,\n" " vec3 light_colour, vec3 light_pos )\n" "{\n" -" vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n" +" vec3 light_delta = light_pos-aWorldCo;\n" "\n" -" float quadratic = dot(light_delta,light_delta);\n" -" float attenuation = 1.0f/( 1.0f + quadratic );\n" -" attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n" +" float dist2 = dot(light_delta,light_delta);\n" "\n" -" return light_colour*attenuation;\n" +" float quadratic = dist2*100.0;\n" +" float attenuation = 1.0f/( 1.0f + quadratic );\n" +" attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );\n" +"\n" +" float falloff = max( 0.0, 1.0-(dist2*max_dist) );\n" +" return light_colour * attenuation * falloff;\n" "}\n" "\n" "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n" @@ -239,7 +246,7 @@ static struct vg_shader _shader_scene_water_fast = { " return light_colour*attenuation*falloff;\n" "}\n" "\n" -"#line 12 0 \n" +"#line 13 0 \n" "#line 1 2 \n" "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n" "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n" @@ -386,7 +393,7 @@ static struct vg_shader _shader_scene_water_fast = { "}\n" "\n" "\n" -"#line 13 0 \n" +"#line 14 0 \n" "\n" "float sdLine( vec3 p, vec3 a, vec3 b )\n" "{\n" @@ -413,6 +420,69 @@ static struct vg_shader _shader_scene_water_fast = { " return mix( vfrag, colour, min( 1.0, dist ) );\n" "}\n" "\n" +"vec3 rand33(vec3 p3)\n" +"{\n" +" p3 = fract(p3 * vec3(.1031, .1030, .0973));\n" +" p3 += dot(p3, p3.yxz+33.33);\n" +" return fract((p3.xxy + p3.yxx)*p3.zyx);\n" +"}\n" +"\n" +"vec3 scene_calculate_light( int light_index, \n" +" vec3 halfview, vec3 co, vec3 normal )\n" +"{\n" +" vec4 light_colour = texelFetch( uLightsArray, light_index+0 );\n" +" vec4 light_co = texelFetch( uLightsArray, light_index+1 );\n" +" vec4 light_dir = texelFetch( uLightsArray, light_index+2 );\n" +"\n" +" vec3 light_delta = light_co.xyz-co;\n" +" float dist2 = dot(light_delta,light_delta);\n" +"\n" +" light_delta = normalize( light_delta );\n" +"\n" +" float quadratic = dist2*100.0;\n" +" float attenuation = 1.0f/( 1.0f + quadratic );\n" +" attenuation *= max( dot( light_delta, normal ), 0.0 );\n" +"\n" +" float falloff = max( 0.0, 1.0-(dist2*light_co.w) );\n" +"\n" +" if( light_dir.w < 0.999999 )\n" +" {\n" +" float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );\n" +" falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );\n" +" }\n" +"\n" +" return light_colour.rgb * attenuation * falloff;\n" +"}\n" +"\n" +"vec3 scene_calculate_packed_light_patch( uint packed_index, \n" +" vec3 halfview, vec3 co, vec3 normal )\n" +"{\n" +" uint light_count = packed_index & 0x3u;\n" +"\n" +" vec3 l = vec3(0.0);\n" +"\n" +" if( light_count >= 1u )\n" +" {\n" +" int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );\n" +" int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );\n" +" int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );\n" +"\n" +" l += scene_calculate_light( index_0, halfview, co, normal );\n" +"\n" +" if( light_count >= 2u )\n" +" {\n" +" l += scene_calculate_light( index_1, halfview, co, normal );\n" +"\n" +" if( light_count >= 3u )\n" +" {\n" +" l += scene_calculate_light( index_2, halfview, co, normal );\n" +" }\n" +" }\n" +" }\n" +"\n" +" return l;\n" +"}\n" +"\n" "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n" "{\n" " world_info world;\n" @@ -424,85 +494,45 @@ static struct vg_shader _shader_scene_water_fast = { " halfview /= fdist;\n" "\n" " vec3 total_light = vec3(0.0);\n" -"\n" -"\n" " float world_shadow = newlight_compute_sun_shadow( world.sun_dir \n" -" * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n" +" * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n" " float board_shadow = compute_board_shadow();\n" "\n" " total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), \n" " halfview, world );\n" "\n" -" //total_light += scene_lighting_old( wnormal, world );\n" -"\n" -" // Compute the other lights that exist in the map, not effected by the sun\n" -" // shadow\n" -"\n" -" // read lights\n" -" vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );\n" -" vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );\n" -" vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );\n" -" vec4 light_co_0 = texelFetch( uLightsArray, light_indices.x*3+1 );\n" -" vec4 light_co_1 = texelFetch( uLightsArray, light_indices.y*3+1 );\n" -" vec4 light_co_2 = texelFetch( uLightsArray, light_indices.z*3+1 );\n" -" vec4 light_dir_0 = texelFetch( uLightsArray, light_indices.x*3+2 );\n" -" vec4 light_dir_1 = texelFetch( uLightsArray, light_indices.y*3+2 );\n" -" vec4 light_dir_2 = texelFetch( uLightsArray, light_indices.z*3+2 );\n" +" vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;\n" +" cube_coord = floor( cube_coord );\n" "\n" " if( g_debug_indices == 1 )\n" " {\n" -" float rings = min( fract(distance(light_co_0.xyz,aWorldCo)),\n" -" min( fract(distance(light_co_1.xyz,aWorldCo)),\n" -" fract(distance(light_co_2.xyz,aWorldCo)) ) \n" -" );\n" -" \n" -" return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),\n" -" fract(light_indices.z * 0.125 )) + (rings-0.5) * 0.25;\n" +" return rand33(cube_coord);\n" " }\n" "\n" " if( g_debug_complexity == 1 )\n" " {\n" -" return vec3(1.0,0.0,0.0) * ( light_indices.w/3.0 );\n" -" }\n" +" ivec3 coord = ivec3( cube_coord );\n" +" uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n" "\n" -" if( light_indices.w >= 1 )\n" -" {\n" -" total_light += newlight_compute_spot\n" -" ( \n" -" wnormal, halfview,\n" -" light_colour_0.rgb,\n" -" light_co_0.xyz,\n" -" light_dir_0\n" -" ) * board_shadow \n" -" * step( world.day_phase, light_colour_0.w );\n" -"\n" -" if( light_indices.w >= 2 )\n" -" {\n" -" total_light += newlight_compute_spot\n" -" ( \n" -" wnormal, halfview,\n" -" light_colour_1.rgb,\n" -" light_co_1.xyz,\n" -" light_dir_1\n" -" ) * board_shadow\n" -" * step( world.day_phase, light_colour_1.w );\n" -"\n" -" if( light_indices.w >= 3 )\n" -" {\n" -" total_light += newlight_compute_spot\n" -" ( \n" -" wnormal, halfview,\n" -" light_colour_2.rgb,\n" -" light_co_2.xyz,\n" -" light_dir_2\n" -" ) * board_shadow\n" -" * step( world.day_phase, light_colour_2.w );\n" -" }\n" -" }\n" +" uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);\n" +" return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );\n" " }\n" "\n" -" vec3 fog_colour = scene_sky( -halfview, world );\n" +" // FIXME: this should absolutely must be clamped!\n" " \n" +" ivec3 coord = ivec3( cube_coord );\n" +" uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n" +"\n" +" total_light += \n" +" scene_calculate_packed_light_patch( index_sample.x,\n" +" halfview, aWorldCo, wnormal ) \n" +" * board_shadow;\n" +" total_light += \n" +" scene_calculate_packed_light_patch( index_sample.y,\n" +" halfview, aWorldCo, wnormal )\n" +" * board_shadow;\n" +"\n" +" vec3 fog_colour = scene_sky( -halfview, world );\n" " return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n" "}\n" "\n" @@ -586,6 +616,7 @@ static GLuint _uniform_scene_water_fast_uBoard1; static GLuint _uniform_scene_water_fast_uShoreColour; static GLuint _uniform_scene_water_fast_uOceanColour; static GLuint _uniform_scene_water_fast_uLightsArray; +static GLuint _uniform_scene_water_fast_uLightsIndex; static GLuint _uniform_scene_water_fast_g_world_depth; static void shader_scene_water_fast_uMdl(m4x3f m){ glUniformMatrix4x3fv(_uniform_scene_water_fast_uMdl,1,GL_FALSE,(float*)m); @@ -640,6 +671,7 @@ static void shader_scene_water_fast_link(void){ _uniform_scene_water_fast_uShoreColour = glGetUniformLocation( _shader_scene_water_fast.id, "uShoreColour" ); _uniform_scene_water_fast_uOceanColour = glGetUniformLocation( _shader_scene_water_fast.id, "uOceanColour" ); _uniform_scene_water_fast_uLightsArray = glGetUniformLocation( _shader_scene_water_fast.id, "uLightsArray" ); + _uniform_scene_water_fast_uLightsIndex = glGetUniformLocation( _shader_scene_water_fast.id, "uLightsIndex" ); _uniform_scene_water_fast_g_world_depth = glGetUniformLocation( _shader_scene_water_fast.id, "g_world_depth" ); } #endif /* SHADER_scene_water_fast_H */