Test - Compute shaders for raymarching

This commit is contained in:
Emmanuel BENOîT 2017-12-25 10:12:17 +01:00
parent 9b13b8e1c6
commit eb2e83a568
3 changed files with 231 additions and 13 deletions

View file

@ -18,6 +18,12 @@
(program prg-fullscreen "fullscreen.v.glsl") (program prg-fullscreen "fullscreen.v.glsl")
(set use-compute 1) (set use-compute 1)
(set render-compute-size-x 8)
(set render-compute-size-y 8)
(set combine-compute-size-x 8)
(set combine-compute-size-y 32)
(call scene-init) (call scene-init)
(call dof-init) (call dof-init)
(call bloom-init) (call bloom-init)
@ -40,14 +46,22 @@
# Scene # Scene
(fn scene-init () (fn scene-init ()
(texture tx-scene-output rgb-f16 $vp-width $vp-height)
(texture tx-scene-depth r-f16 $vp-width $vp-height) (texture tx-scene-depth r-f16 $vp-width $vp-height)
(if $use-compute (
(texture tx-scene-output rgba-f16 $vp-width $vp-height)
(program prg-scene-p1 "scene.c.glsl")
(pipeline pl-scene-p1 prg-scene-p1)
)(
(texture tx-scene-output rgb-f16 $vp-width $vp-height)
(framebuffer rt-scene (framebuffer rt-scene
(color tx-scene-output) (color tx-scene-output)
(color tx-scene-depth)) (color tx-scene-depth))
(program prg-scene-p1 "scene.f.glsl") (program prg-scene-p1 "scene.f.glsl")
(pipeline pl-scene-p1 prg-fullscreen prg-scene-p1) (pipeline pl-scene-p1 prg-fullscreen prg-scene-p1)
))
(odbg tx-scene-output hdr "Scene output") (odbg tx-scene-output hdr "Scene output")
(odbg tx-scene-depth depth "Scene depth") (odbg tx-scene-depth depth "Scene depth")
@ -100,8 +114,6 @@
(fn scene-render () (fn scene-render ()
(profiling "Scene render" (profiling "Scene render"
(uniforms prg-scene-p1 1 $vp-width $vp-height)
(uniforms prg-scene-p1 0 $time) (uniforms prg-scene-p1 0 $time)
(uniforms prg-scene-p1 2 (uniforms prg-scene-p1 2
(get-input camera-pos-x) (get-input camera-pos-x)
@ -130,9 +142,18 @@
(uniforms prg-scene-p1 7 (get-input fog)) (uniforms prg-scene-p1 7 (get-input fog))
(uniforms-i prg-scene-p1 8 (get-input raymarcher-correction)) (uniforms-i prg-scene-p1 8 (get-input raymarcher-correction))
(use-pipeline pl-scene-p1) (use-pipeline pl-scene-p1)
(if $use-compute (
(image 0 tx-scene-output 0)
(compute
(add 1 (div $vp-width $render-compute-size-x))
(add 1 (div $vp-height $render-compute-size-y))
1)
)(
(uniforms prg-scene-p1 1 $vp-width $vp-height)
(use-framebuffer rt-scene) (use-framebuffer rt-scene)
(viewport 0 0 $vp-width $vp-height) (viewport 0 0 $vp-width $vp-height)
(fullscreen) (fullscreen)
))
) )
) )
@ -414,8 +435,6 @@
(if $use-compute ( (if $use-compute (
(program prg-combine "combine.c.glsl") (program prg-combine "combine.c.glsl")
(pipeline pl-combine prg-combine) (pipeline pl-combine prg-combine)
(set combine-compute-size-x 8)
(set combine-compute-size-y 32)
)( )(
(framebuffer rt-combined tx-combined) (framebuffer rt-combined tx-combined)
(program prg-combine "combine.f.glsl") (program prg-combine "combine.f.glsl")

View file

@ -0,0 +1,22 @@
//! type chunk
layout(
local_size_x = 8 ,
local_size_y = 8
) in;
layout( location = 0 ) uniform float u_Time;
//layout( location = 1 ) uniform vec2 u_Resolution;
layout( location = 2 ) uniform vec3 u_CamPos;
layout( location = 3 ) uniform vec3 u_LookAt;
layout( location = 4 ) uniform vec3 u_CamUp;
layout( location = 5 ) uniform float u_NearPlane;
layout( location = 6 ) uniform vec4 u_Render;
layout( location = 7 ) uniform float u_FogAttenuation;
layout( location = 8 ) uniform int u_Correction;
//layout( location = 0 ) out vec3 o_Color;
//layout( location = 1 ) out float o_Z;
layout( binding = 0 , rgba16f ) writeonly uniform image2D u_Output;
//! include lib/raymarching.glsl

177
test/shaders/scene.c.glsl Normal file
View file

@ -0,0 +1,177 @@
#version 450 core
//! type compute
//! include chunks/raymarcher-cs.glsl
//! include lib/hg_sdf.glsl
//! include lib/shading-pbr.glsl
//! include lib/shading-blinnphong.glsl
//! include lib/fog.glsl
T_BPMaterial BPMaterials[1] = {
{ vec3( .95 , .8 , 1 ) * .2 , vec3( 10 ) , 800 , 0 }
};
T_PBRMaterial PBRMaterials[1] = {
{
// Albedo color
vec3( .9 , 1 , .9 ) ,
// Roughness , metallic , subsurface , anisotropy
.15 , .5 , .9 , .9 ,
// Specular strength / tint%
.5 , .5
}
};
vec3 Glow[1] = {
vec3( .95 , .8 , 1 ) * 5
};
void mapMaterial(
in int matIndex ,
out int type ,
out int tIndex ,
out int glowIndex )
{
if ( matIndex == 0 ) {
type = 1;
glowIndex = -1;
} else {
type = 0;
glowIndex = 0;
}
tIndex = 0;
}
vec2 RM_Map( vec3 pos )
{
vec3 q = pos;
HG_pMod2( q.xy , vec2( 8. ) );
HG_pR( q.xz , 1 );
return vec2( HG_fOpUnionStairs(
HG_fBox( q , vec3( 2.3 ) ) ,
HG_fSphere( q , 2.6 ) ,
1. , 4 ) ,
step( 0. , -HG_fSphere( pos , 3 ) ) );
}
vec3 pointlight(
in T_PBRMaterial material ,
in T_PBRPrecomputed pre ,
in vec3 hitPos ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightPos ,
in vec3 lightColor ,
in float lightIntensity )
{
vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .001 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
return vec3( 0 );
}
return lightColor * lPwr * PBR_Shade( material , pre ,
-rayDir , normal , normalize( lightRay ) );
}
vec3 pointlight(
in T_BPMaterial material ,
in vec3 hitPos ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightPos ,
in vec3 lightColor ,
in float lightIntensity )
{
vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .1 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
return vec3( 0 );
}
return lightColor * lPwr * BP_Shade(
material , -rayDir , normal , lightRay );
}
void main( )
{
vec2 res = imageSize( u_Output );
ivec2 coords = ivec2( gl_GlobalInvocationID.xy );
vec2 uv = ( vec2( coords ) / res ) * 2 - 1;
vec3 camDir = normalize( u_LookAt - u_CamPos );
vec3 side = normalize( cross( u_CamUp , camDir ) );
vec3 up = normalize( cross( camDir , side ) );
vec3 rayDir = normalize( camDir * u_NearPlane
+ uv.x * side * res.x / res.y
+ uv.y * up );
float tanPhi = RM_TanPhi( uv , u_Render.z );
vec3 r = RM_Advanced( u_CamPos , rayDir ,
int( u_Render.x ) , u_Render.y ,
tanPhi , .01 , u_Render.w );
const vec3 background = vec3( .005 );
vec3 bc = background;
vec4 outValue;
if ( r.y >= 0. ) {
const vec3 hitPos = RM_ReduceDiscontinuity(
u_CamPos , rayDir , r.x ,
tanPhi , u_Correction );
const vec3 normal = RM_GetNormal( hitPos );
const int midx = int( r.y );
const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) );
const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .05;
const vec3 pl1Pos = vec3( 3 , 3 , 5 );
const vec3 pl1Color = vec3( 1 , .7 , .8 );
const float pl1Power = 20;
const vec3 pl2Pos = vec3( -3 , -3 , 5 );
const vec3 pl2Color = vec3( .6 , 1 , .8 );
const float pl2Power = 20;
// Remap materials through mapMaterials
int mtype , mtidx , glowidx;
mapMaterial( midx , mtype , mtidx , glowidx );
if ( mtype == 0 ) {
T_BPMaterial mat = BPMaterials[ mtidx ];
bc = sunlightColor * BP_Shade( mat ,
-rayDir , normal , sunlightDir );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
} else {
T_PBRMaterial mat = PBRMaterials[ mtidx ];
T_PBRPrecomputed pre = PBR_Precompute(
mat , -rayDir , normal );
bc = sunlightColor * PBR_Shade( mat , pre ,
-rayDir , normal , sunlightDir );
bc += pointlight( mat , pre ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat , pre ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
}
if ( glowidx >= 0 ) {
bc += Glow[ glowidx ];
}
bc = FOG_Apply( bc , length( u_CamPos - hitPos ) ,
u_FogAttenuation , background );
outValue.w = r.x;
} else {
outValue.w = u_Render.w;
}
outValue.xyz = bc;
imageStore( u_Output , coords , outValue );
}