Skip to content

Commit

Permalink
Performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Aug 7, 2019
1 parent 55b9d99 commit 76b5d33
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 89 deletions.
4 changes: 3 additions & 1 deletion game_folder/shaders/compute/distance_estimators.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ uniform vec3 iMarblePos;
uniform float iMarbleRad;
uniform float iFlagScale;
uniform vec3 iFlagPos;
uniform int FRACTAL_ITER;
#define FRACTAL_ITER 16

///Original MM distance estimators

Expand Down Expand Up @@ -144,6 +144,7 @@ vec4 col_flag(vec4 p)

float de_scene(vec3 pos)
{
DE_count = DE_count+1;
vec4 p = vec4(pos,1.f);
float d = de_fractal(p);
d = min(d, de_marble(p));
Expand All @@ -153,6 +154,7 @@ float de_scene(vec3 pos)

vec4 col_scene(vec3 pos)
{
DE_count = DE_count+1;
vec4 p = vec4(pos,1.f);
vec4 col = col_fractal(p);
vec4 col_f = col_flag(p);
Expand Down
104 changes: 55 additions & 49 deletions game_folder/shaders/compute/main.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#version 430
//4*4 ray bundle
#define bundle_size 4
#define bundle_length 16
#define bundle_center 8
#define group_size 8
#define block_size 64
#define bundle_size 1
#define bundle_length 1
#define bundle_center 0
#define group_size 4
#define block_size 16
#define MAX_MARCHES 128
#define RBM 0
#include<camera.glsl>

//4*4 bundle of ray bundles(oh lol)
Expand All @@ -14,9 +15,10 @@ layout(rgba8, binding = 0) uniform image2D img_output;

//make all the local distance estimator spheres shared
shared vec4 de_sph[block_size];

float DE_count = 0;
#include<ray_marching.glsl>


//each shader invocation will march a ray bundle,
//all invocations will have all the DE data from neighbore
//ray bundles to improve marching efficiency
Expand All @@ -26,52 +28,56 @@ shared vec4 de_sph[block_size];
void main() {
ivec2 global_pos = ivec2(gl_GlobalInvocationID.x,gl_GlobalInvocationID.y);
int local_indx = int(gl_LocalInvocationIndex);

//ray bundle array
vec4 pos[bundle_length];
vec4 dir[bundle_length];
vec4 var[bundle_length];

//initialize the ray bundle
for(int i = 0; i < bundle_length; i++)
{
int index = bundle_size*local_indx + i;
ivec2 pix = bundle_size*global_pos + getLPos(i);
ray rr = get_ray(vec2(pix)/vec2(imageSize(img_output)));
pos[i] = vec4(rr.pos,0);
dir[i] = vec4(rr.dir,0);
var[i] = vec4(0);
}

de_sph[local_indx] = pos[bundle_center];

memoryBarrierShared();

ray_bundle_marching1(pos, dir, var, local_indx);

/*for(int i = 0; i < bundle_length; i++)
{
ray_march(pos[i], dir[i], var[i]);
}*/

// output to the specified image block
for(int i = 0; i < bundle_length; i++)
ivec2 block_pos = bundle_size*global_pos;
vec2 img_size = vec2(imageSize(img_output));
if(block_pos.x < img_size.x && block_pos.y < img_size.y)
{
ivec2 pix = bundle_size*global_pos + getLPos(i);
ray rr = get_ray(vec2(pix)/vec2(imageSize(img_output)));

/* pos[i] = vec4(rr.pos,0);
dir[i] = vec4(rr.dir,0);
ray_march(pos[i], dir[i]);*/
//ray bundle array
vec4 pos[bundle_length];
vec4 dir[bundle_length];
vec4 var[bundle_length];

float ao = (1 - var[i].x/MAX_MARCHES);
float td = (1 - dir[i].w*0.06f);
vec3 depth = vec3(td,td,td);
if(isinf(td))
{
depth = vec3(td,td,ao);
//initialize the ray bundle
ivec2 pix = block_pos;
#pragma unroll
for(int i = 0; i < bundle_length; pix = block_pos + getLPos(++i))
{
ray rr = get_ray(vec2(pix)/img_size);
pos[i] = vec4(rr.pos,0);
dir[i] = vec4(rr.dir,0);
var[i] = vec4(0);
}

imageStore(img_output, pix, vec4(depth, 1));
//de_sph[local_indx] = pos[bundle_center];

memoryBarrierShared();

#if RBM
ray_bundle_marching1(pos, dir, var, local_indx);
#else
#pragma unroll
for(int i = 0; i < bundle_length; i++)
{
ray_march(pos[i], dir[i], var[i], fovray);
}
#endif

// output to the specified image block

pix = block_pos;
#pragma unroll
for(int i = 0; i < bundle_length; pix = block_pos + getLPos(++i))
{
//ray rr = get_ray(vec2(pix)/vec2(imageSize(img_output)));

/* pos[i] = vec4(rr.pos,0);
dir[i] = vec4(rr.dir,0);
ray_march(pos[i], dir[i]);*/
float DE_per_pix = DE_count/float(bundle_length*MAX_MARCHES);
float ao = (1 - var[i].x/MAX_MARCHES);
float td = (1 - dir[i].w*0.06f);
//memoryBarrierImage();
imageStore(img_output, pix, vec4(DE_per_pix,DE_per_pix,DE_per_pix,1));
}
}
}
4 changes: 2 additions & 2 deletions game_folder/shaders/compute/pipeline.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#shader name
main.glsl
#global size
width/32
height/32
width/4
height/4
#texture resolution
width
height
Expand Down
71 changes: 34 additions & 37 deletions game_folder/shaders/compute/ray_marching.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,41 @@
#define MIN_DIST 0.001


void ray_march(inout vec4 pos, inout vec4 dir, inout vec4 var, float fov = fovray)
void ray_march(inout vec4 pos, inout vec4 dir, inout vec4 var, float fov)
{
//March the ray
pos.w = DE(pos.xyz);
float s = 0;
for (; s < 5; s += 1.0) {
for (; s < MAX_MARCHES; s += 1.0) {
pos.w = DE(pos.xyz);
//if the distance from the surface is less than the distance per pixel we stop
float min_dist = max(fov*dir.w, MIN_DIST);
if(dir.w > MAX_DIST || pos.w < min_dist)
if(dir.w > MAX_DIST || pos.w < max(fov*dir.w, MIN_DIST))
{
break;
}
dir.w += pos.w;
pos.xyz += pos.w*dir.xyz;
pos.w = DE(pos.xyz);
}
var.x = s;
}


float sphere_intersection(vec3 r, vec3 p, vec4 sphere)
{
if(sphere.w > MIN_DIST && sphere.w < MAX_DIST)
p = sphere.xyz - p;
if(p == vec3(0)) return sphere.w;

r.x = dot(p, r);
r.y = sphere.w*sphere.w - dot(r,r);
r.z = r.x*r.x + r.y;

if((r.z <= 0) || (r.y <= 0)) //if no intersection
{
vec3 dp = sphere.xyz - p;
/*if(dp == vec3(0))
{
return sphere.w;
}*/

float b = dot(dp, r);
float c = sphere.w*sphere.w - dot(dp,dp);
float D = b*b + c;

if((D <= 0) || (c <= 0)) //if no intersection
{
return 0;
}
else
{
return sqrt(D) + b; //use furthest solution in the direction of the ray
}
return 0;
}
else
{
return sqrt(r.z) + r.x; //use furthest solution in the direction of the ray
}
return 0;
}


Expand Down Expand Up @@ -199,31 +190,37 @@ void ray_bundle_marching(inout vec4 pos[bundle_length], inout vec4 dir[bundle_le
}
}
*/

float divergence(in vec4 dir[bundle_length])
{
return sqrt(1-pow(dot(dir[0], dir[bundle_length-1]),2));
}

void ray_bundle_marching1(inout vec4 pos[bundle_length], inout vec4 dir[bundle_length], inout vec4 var[bundle_length], int id)
{
bool marching = true;
float d = 0.f;

//march central ray while safe to do so(ray bundle within ray cone)
ray_march(pos[bundle_center], dir[bundle_center], var[bundle_center], fovray*bundle_size*4);
ray_march(pos[bundle_center], dir[bundle_center], var[bundle_center], divergence(dir));

pos[bundle_center].w = 0;
for(int i = bundle_center+1; i < bundle_length+bundle_center; i++)
#pragma unroll
for(int i = bundle_center+1, ii = i; i < bundle_length+bundle_center; ii = ++i%bundle_length)
{
int ii = i%bundle_length;
pos[ii].xyz += dir[ii].xyz*dir[bundle_center].w;
dir[ii].w = dir[bundle_center].w;
var[ii] = var[bundle_center];
}

vec4 sphere = vec4(0);
for(int m = 0; (m < MAX_MARCHES) && marching; m++)
for(int m = 0; (m < MAX_MARCHES -var[bundle_center].x ) && marching; m++)
{
marching = false;
sphere.w = 0;
for(int i = 0; i < bundle_length; i++)
{
if((dir[i].w > MAX_DIST || (pos[i].w < fovray*dir[i].w && pos[i].w > 0)) && dir[i].w > 0)
if((dir[i].w > MAX_DIST || (pos[i].w < fovray*dir[i].w && pos[i].w > 0)) && (dir[i].w > 0))
{
continue;
}
Expand All @@ -233,14 +230,14 @@ void ray_bundle_marching1(inout vec4 pos[bundle_length], inout vec4 dir[bundle_l
}


pos[i].xyz += dir[i].xyz*abs(pos[i].w);
dir[i].w += abs(pos[i].w);
pos[i].xyz += dir[i].xyz*pos[i].w;
dir[i].w += pos[i].w;
var[i].x += 1.f;

memoryBarrierShared();
//memoryBarrierShared();
d = sphere_intersection(dir[i].xyz, pos[i].xyz, sphere);

if(d == 0)
if(d < fovray*dir[i].w)
{
pos[i].w = DE(pos[i].xyz);
if(sphere.w < pos[i].w)
Expand All @@ -250,7 +247,7 @@ void ray_bundle_marching1(inout vec4 pos[bundle_length], inout vec4 dir[bundle_l
}
else //if found a usable DE
{
pos[i].w = -d;
pos[i].w = d;
}
}
}
Expand Down

0 comments on commit 76b5d33

Please sign in to comment.