use base types in shaders
This commit is contained in:
parent
5952bd99e9
commit
426bfbefd0
@ -463,8 +463,9 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t);
|
|||||||
//~ Scalar types
|
//~ Scalar types
|
||||||
|
|
||||||
#if LanguageIsC
|
#if LanguageIsC
|
||||||
|
|
||||||
//- Cpu scalar types
|
//- Cpu scalar types
|
||||||
#include "stdint.h"
|
#include <stdint.h>
|
||||||
typedef int8_t i8;
|
typedef int8_t i8;
|
||||||
typedef int16_t i16;
|
typedef int16_t i16;
|
||||||
typedef int32_t i32;
|
typedef int32_t i32;
|
||||||
@ -477,12 +478,15 @@ typedef float f32;
|
|||||||
typedef double f64;
|
typedef double f64;
|
||||||
typedef i8 b8;
|
typedef i8 b8;
|
||||||
typedef u32 b32;
|
typedef u32 b32;
|
||||||
#else
|
|
||||||
|
#elif LanguageIsGpu
|
||||||
|
|
||||||
//- Gpu scalar types
|
//- Gpu scalar types
|
||||||
typedef int i32;
|
typedef int i32;
|
||||||
typedef uint u32;
|
typedef uint u32;
|
||||||
typedef float f32;
|
typedef float f32;
|
||||||
typedef uint b32;
|
typedef uint b32;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//- Min / max constants
|
//- Min / max constants
|
||||||
|
|||||||
@ -75,11 +75,11 @@ MaterialPS_Output PSDef(MaterialPS, MaterialPS_Input input)
|
|||||||
StructuredBuffer<MaterialGrid> grids = GpuResourceFromUrid(sig.grids_urid);
|
StructuredBuffer<MaterialGrid> grids = GpuResourceFromUrid(sig.grids_urid);
|
||||||
MaterialGrid grid = grids[input.grid_id];
|
MaterialGrid grid = grids[input.grid_id];
|
||||||
Vec2 grid_pos = input.SV_Position.xy + grid.offset;
|
Vec2 grid_pos = input.SV_Position.xy + grid.offset;
|
||||||
float half_thickness = grid.line_thickness / 2;
|
f32 half_thickness = grid.line_thickness / 2;
|
||||||
float spacing = grid.line_spacing;
|
f32 spacing = grid.line_spacing;
|
||||||
u32 color_srgb = grid.bg0_srgb;
|
u32 color_srgb = grid.bg0_srgb;
|
||||||
Vec2 v = abs(round(grid_pos / spacing) * spacing - grid_pos);
|
Vec2 v = abs(round(grid_pos / spacing) * spacing - grid_pos);
|
||||||
float dist = min(v.x, v.y);
|
f32 dist = min(v.x, v.y);
|
||||||
if (grid_pos.y <= half_thickness && grid_pos.y >= -half_thickness)
|
if (grid_pos.y <= half_thickness && grid_pos.y >= -half_thickness)
|
||||||
{
|
{
|
||||||
color_srgb = grid.x_srgb;
|
color_srgb = grid.x_srgb;
|
||||||
@ -127,23 +127,23 @@ MaterialPS_Output PSDef(MaterialPS, MaterialPS_Input input)
|
|||||||
//- Compute shader
|
//- Compute shader
|
||||||
|
|
||||||
[numthreads(8, 8, 1)]
|
[numthreads(8, 8, 1)]
|
||||||
void CSDef(FloodCS, Semantic(uint3, SV_DispatchThreadID))
|
void CSDef(FloodCS, Semantic(Vec3U32, SV_DispatchThreadID))
|
||||||
{
|
{
|
||||||
ConstantBuffer<FloodSig> sig = flood_sig;
|
ConstantBuffer<FloodSig> sig = flood_sig;
|
||||||
|
|
||||||
uint2 id = SV_DispatchThreadID.xy;
|
Vec2U32 id = SV_DispatchThreadID.xy;
|
||||||
uint2 tex_size = uint2(sig.tex_width, sig.tex_height);
|
Vec2U32 tex_size = Vec2U32(sig.tex_width, sig.tex_height);
|
||||||
if (id.x < tex_size.x && id.y < tex_size.y)
|
if (id.x < tex_size.x && id.y < tex_size.y)
|
||||||
{
|
{
|
||||||
Texture2D<Vec4> emittance_tex = GpuResourceFromUrid(sig.emittance_tex_urid);
|
Texture2D<Vec4> emittance_tex = GpuResourceFromUrid(sig.emittance_tex_urid);
|
||||||
RWTexture2D<uint2> read_flood_tex = GpuResourceFromUrid(sig.read_flood_tex_urid);
|
RWTexture2D<Vec2U32> read_flood_tex = GpuResourceFromUrid(sig.read_flood_tex_urid);
|
||||||
RWTexture2D<uint2> target_flood_tex = GpuResourceFromUrid(sig.target_flood_tex_urid);
|
RWTexture2D<Vec2U32> target_flood_tex = GpuResourceFromUrid(sig.target_flood_tex_urid);
|
||||||
int step_len = sig.step_len;
|
i32 step_len = sig.step_len;
|
||||||
if (step_len == -1)
|
if (step_len == -1)
|
||||||
{
|
{
|
||||||
/* Seed */
|
/* Seed */
|
||||||
Vec4 emittance = emittance_tex[id];
|
Vec4 emittance = emittance_tex[id];
|
||||||
uint2 seed = uint2(0xFFFF, 0xFFFF);
|
Vec2U32 seed = Vec2U32(0xFFFF, 0xFFFF);
|
||||||
if (emittance.a > 0)
|
if (emittance.a > 0)
|
||||||
{
|
{
|
||||||
seed = id;
|
seed = id;
|
||||||
@ -164,14 +164,14 @@ void CSDef(FloodCS, Semantic(uint3, SV_DispatchThreadID))
|
|||||||
(Vec2I32)id + Vec2I32(0, +step_len), /* bottom center */
|
(Vec2I32)id + Vec2I32(0, +step_len), /* bottom center */
|
||||||
(Vec2I32)id + Vec2I32(+step_len, +step_len) /* bottom right */
|
(Vec2I32)id + Vec2I32(+step_len, +step_len) /* bottom right */
|
||||||
};
|
};
|
||||||
uint2 closest_seed = uint2(0xFFFF, 0xFFFF);
|
Vec2U32 closest_seed = Vec2U32(0xFFFF, 0xFFFF);
|
||||||
u32 closest_seed_len_sq = 0xFFFFFFFF;
|
u32 closest_seed_len_sq = 0xFFFFFFFF;
|
||||||
for (int i = 0; i < 9; ++i)
|
for (i32 i = 0; i < 9; ++i)
|
||||||
{
|
{
|
||||||
Vec2I32 coord = read_coords[i];
|
Vec2I32 coord = read_coords[i];
|
||||||
if (coord.x >= 0 && coord.x < (int)tex_size.x && coord.y >= 0 && coord.y < (int)tex_size.y)
|
if (coord.x >= 0 && coord.x < (i32)tex_size.x && coord.y >= 0 && coord.y < (i32)tex_size.y)
|
||||||
{
|
{
|
||||||
uint2 seed = read_flood_tex[coord];
|
Vec2U32 seed = read_flood_tex[coord];
|
||||||
Vec2I32 dist_vec = (Vec2I32)id - (Vec2I32)seed;
|
Vec2I32 dist_vec = (Vec2I32)id - (Vec2I32)seed;
|
||||||
u32 dist_len_sq = dot(dist_vec, dist_vec);
|
u32 dist_len_sq = dot(dist_vec, dist_vec);
|
||||||
if (dist_len_sq < closest_seed_len_sq)
|
if (dist_len_sq < closest_seed_len_sq)
|
||||||
@ -193,7 +193,7 @@ void CSDef(FloodCS, Semantic(uint3, SV_DispatchThreadID))
|
|||||||
#define LightMarches 16
|
#define LightMarches 16
|
||||||
#define LightEdgeFalloff 100
|
#define LightEdgeFalloff 100
|
||||||
|
|
||||||
float RandAngle(uint2 pos, u32 ray_index)
|
f32 RandAngle(Vec2U32 pos, u32 ray_index)
|
||||||
{
|
{
|
||||||
ConstantBuffer<ShadeSig> sig = shade_sig;
|
ConstantBuffer<ShadeSig> sig = shade_sig;
|
||||||
Texture3D<u32> noise_tex = GpuResourceFromUrid(sig.noise_tex_urid);
|
Texture3D<u32> noise_tex = GpuResourceFromUrid(sig.noise_tex_urid);
|
||||||
@ -203,47 +203,38 @@ float RandAngle(uint2 pos, u32 ray_index)
|
|||||||
noise_coord.xyz += sig.frame_seed.xyz;
|
noise_coord.xyz += sig.frame_seed.xyz;
|
||||||
// noise_coord.xy -= sig.camera_offset;
|
// noise_coord.xy -= sig.camera_offset;
|
||||||
|
|
||||||
#if 0
|
u32 noise = noise_tex[noise_coord % Vec3U32(sig.noise_tex_width, sig.noise_tex_height, sig.noise_tex_depth)];
|
||||||
u32 noise = noise_tex[noise_coord % uint3(sig.noise_tex_width, sig.noise_tex_height, sig.noise_tex_depth)];
|
return ((f32)noise / (f32)0xFFFF) * Tau;
|
||||||
#else
|
|
||||||
u32 x = sig.noise_tex_width;
|
|
||||||
u32 y = sig.noise_tex_height;
|
|
||||||
u32 z = sig.noise_tex_depth;
|
|
||||||
noise_coord = noise_coord % Vec3U32(x, y, z);
|
|
||||||
u32 noise = noise_tex[noise_coord];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return ((float)noise / (float)0xFFFF) * Tau;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3 ColorFromDir(uint2 ray_start, Vec2 ray_dir)
|
Vec3 ColorFromDir(Vec2U32 ray_start, Vec2 ray_dir)
|
||||||
{
|
{
|
||||||
ConstantBuffer<ShadeSig> sig = shade_sig;
|
ConstantBuffer<ShadeSig> sig = shade_sig;
|
||||||
Texture2D<uint2> flood_tex = GpuResourceFromUrid(sig.emittance_flood_tex_urid);
|
Texture2D<Vec2U32> flood_tex = GpuResourceFromUrid(sig.emittance_flood_tex_urid);
|
||||||
Texture2D<Vec4> emittance_tex = GpuResourceFromUrid(sig.emittance_tex_urid);
|
Texture2D<Vec4> emittance_tex = GpuResourceFromUrid(sig.emittance_tex_urid);
|
||||||
|
|
||||||
Vec3 result = Vec3(0, 0, 0);
|
Vec3 result = Vec3(0, 0, 0);
|
||||||
Vec2 at_float = ray_start;
|
Vec2 at_f32 = ray_start;
|
||||||
uint2 at_uint = ray_start;
|
Vec2U32 at_u32 = ray_start;
|
||||||
for (u32 i = 0; i < LightMarches; ++i)
|
for (u32 i = 0; i < LightMarches; ++i)
|
||||||
{
|
{
|
||||||
uint2 flood = flood_tex[at_uint];
|
Vec2U32 flood = flood_tex[at_u32];
|
||||||
Vec2 dist_vec = at_float - (Vec2)flood;
|
Vec2 dist_vec = at_f32 - (Vec2)flood;
|
||||||
float dist = length(dist_vec);
|
f32 dist = length(dist_vec);
|
||||||
if (dist < 1)
|
if (dist < 1)
|
||||||
{
|
{
|
||||||
/* Scale light by distance from edge so that offscreen-lights fade in/out rather than popping in */
|
/* Scale light by distance from edge so that offscreen-lights fade in/out rather than popping in */
|
||||||
float dist_x = min(abs(sig.tex_width - at_float.x), at_float.x);
|
f32 dist_x = min(abs(sig.tex_width - at_f32.x), at_f32.x);
|
||||||
float dist_y = min(abs(sig.tex_height - at_float.y), at_float.y);
|
f32 dist_y = min(abs(sig.tex_height - at_f32.y), at_f32.y);
|
||||||
float dist_scale = min(min(dist_x, dist_y) / LightEdgeFalloff, 1);
|
f32 dist_scale = min(min(dist_x, dist_y) / LightEdgeFalloff, 1);
|
||||||
result = emittance_tex[flood].rgb * dist_scale;
|
result = emittance_tex[flood].rgb * dist_scale;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
at_float += ray_dir * dist;
|
at_f32 += ray_dir * dist;
|
||||||
at_uint = round(at_float);
|
at_u32 = round(at_f32);
|
||||||
if (at_uint.x < 0 || at_uint.x >= sig.tex_width || at_uint.y < 0 || at_uint.y >= sig.tex_height)
|
if (at_u32.x < 0 || at_u32.x >= sig.tex_width || at_u32.y < 0 || at_u32.y >= sig.tex_height)
|
||||||
{
|
{
|
||||||
/* Ray hit edge of screen */
|
/* Ray hit edge of screen */
|
||||||
break;
|
break;
|
||||||
@ -253,12 +244,12 @@ Vec3 ColorFromDir(uint2 ray_start, Vec2 ray_dir)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3 ColorFromPos(uint2 pos)
|
Vec3 ColorFromPos(Vec2U32 pos)
|
||||||
{
|
{
|
||||||
Vec3 result = 0;
|
Vec3 result = 0;
|
||||||
for (u32 i = 0; i < LightSamples; ++i)
|
for (u32 i = 0; i < LightSamples; ++i)
|
||||||
{
|
{
|
||||||
float angle = RandAngle(pos, i);
|
f32 angle = RandAngle(pos, i);
|
||||||
Vec2 dir = Vec2(cos(angle), sin(angle));
|
Vec2 dir = Vec2(cos(angle), sin(angle));
|
||||||
Vec3 light_in_dir = ColorFromDir(pos, dir);
|
Vec3 light_in_dir = ColorFromDir(pos, dir);
|
||||||
result += light_in_dir;
|
result += light_in_dir;
|
||||||
@ -270,11 +261,11 @@ Vec3 ColorFromPos(uint2 pos)
|
|||||||
//- Compute shader
|
//- Compute shader
|
||||||
|
|
||||||
[numthreads(8, 8, 1)]
|
[numthreads(8, 8, 1)]
|
||||||
void CSDef(ShadeCS, Semantic(uint3, SV_DispatchThreadID))
|
void CSDef(ShadeCS, Semantic(Vec3U32, SV_DispatchThreadID))
|
||||||
{
|
{
|
||||||
ConstantBuffer<ShadeSig> sig = shade_sig;
|
ConstantBuffer<ShadeSig> sig = shade_sig;
|
||||||
|
|
||||||
uint2 id = SV_DispatchThreadID.xy;
|
Vec2U32 id = SV_DispatchThreadID.xy;
|
||||||
if (id.x < sig.tex_width && id.y < sig.tex_height)
|
if (id.x < sig.tex_width && id.y < sig.tex_height)
|
||||||
{
|
{
|
||||||
Texture2D<Vec4> albedo_tex = GpuResourceFromUrid(sig.albedo_tex_urid);
|
Texture2D<Vec4> albedo_tex = GpuResourceFromUrid(sig.albedo_tex_urid);
|
||||||
@ -292,7 +283,7 @@ void CSDef(ShadeCS, Semantic(uint3, SV_DispatchThreadID))
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Apply temporal accumulation */
|
/* Apply temporal accumulation */
|
||||||
float hysterisis = 0;
|
f32 hysterisis = 0;
|
||||||
// hysterisis = 0.2;
|
// hysterisis = 0.2;
|
||||||
// hysterisis = 0.4;
|
// hysterisis = 0.4;
|
||||||
// hysterisis = 0.5;
|
// hysterisis = 0.5;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user