Merge branch 'master' of github.com:bkaradzic/bgfx

This commit is contained in:
Бранимир Караџић
2021-01-02 10:47:37 -08:00
58 changed files with 1906 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
SAMPLER2D(s_color, 0);
SAMPLER2D(s_albedo, 1);
void main()
{
vec2 texCoord = v_texcoord0;
vec3 lightColor = texture2D(s_color, texCoord).xyz;
vec3 albedo = texture2D(s_albedo, texCoord).xyz;
albedo = toLinear(albedo);
vec3 color = lightColor * albedo;
color = toGamma(color);
gl_FragColor = vec4(color, 1.0);
}

View File

@@ -0,0 +1,18 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
SAMPLER2D(s_color, 0);
void main()
{
vec2 texCoord = v_texcoord0;
vec4 color = texture2D(s_color, texCoord);
gl_FragColor = color;
}

View File

@@ -0,0 +1,61 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"
SAMPLER2D(s_color, 0);
SAMPLER2D(s_normal, 1);
float ShadertoyNoise (vec2 uv) {
return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
int ModHelper (float a, float b)
{
return int( a - (b*floor(a/b)));
}
void main()
{
vec2 texCoord = v_texcoord0;
// mess with result so there's something to denosie
float sn = 1.0;
if (1.5 < u_noiseType)
{
sn = ShadertoyNoise(gl_FragCoord.xy + vec2(314.0, 159.0)*u_frameIdx);
sn = (sn < 0.5) ? 0.0 : 1.0;
}
else if (0.5 < u_noiseType)
{
// having trouble compiling for gles when using % or mod :(
int modCoordX = ModHelper(gl_FragCoord.x, 2.0);
int modCoordY = ModHelper(gl_FragCoord.y, 2.0);
int frameSelect = modCoordY * 2 + modCoordX;
int frameMod4 = ModHelper(u_frameIdx, 4.0);
sn = (frameSelect == frameMod4) ? 1.0 : 0.0;
}
vec4 normalRoughness = texture2D(s_normal, texCoord).xyzw;
vec3 normal = NormalDecode(normalRoughness.xyz);
float roughness = 0.5;
// need to get a valid view vector for any microfacet stuff :(
float gloss = 1.0-roughness;
float specPower = 1022.0 * gloss + 2.0;
vec3 light = normalize(vec3(-0.2, 1.0, -0.2));
float NdotL = saturate(dot(normal, light));
float diff = NdotL*0.99 + 0.01;
float spec = 5.0 * pow(NdotL, specPower);
float lightAmt = (diff + spec) * sn;
gl_FragColor = vec4(vec3_splat(lightAmt), 1.0);
}

View File

@@ -0,0 +1,69 @@
$input v_normal, v_texcoord0, v_texcoord1, v_texcoord2, v_texcoord3
#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"
SAMPLER2D(s_albedo, 0);
SAMPLER2D(s_normal, 1);
// http://www.thetenthplanet.de/archives/1180
// "followup: normal mapping without precomputed tangents"
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
// solve the linear system
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
return mat3(T*invMax, B*invMax, N);
}
void main()
{
vec3 albedo = toLinear(texture2D(s_albedo, v_texcoord0).xyz);
// get vertex normal
vec3 normal = normalize(v_normal);
// get normal map normal, unpack, and calculate z
vec3 normalMap;
normalMap.xy = texture2D(s_normal, v_texcoord0).xy;
normalMap.xy = normalMap.xy * 2.0 - 1.0;
normalMap.z = sqrt(1.0 - dot(normalMap.xy, normalMap.xy));
// swap x and y, because the brick texture looks flipped, don't copy this...
normalMap.xy = normalMap.yx;
// perturb geometry normal by normal map
vec3 pos = v_texcoord2.xyz; // contains world space pos
mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
// need some proxy for roughness value w/o roughness texture
// assume horizontal (blue) normal map is smooth, and then
// modulate with albedo for some higher frequency detail
float roughness = normalMap.z * mix(0.9, 1.0, albedo.y);
roughness = roughness * 0.6 + 0.2;
// Calculate velocity as delta position from previous frame to this
vec2 previousNDC = v_texcoord1.xy * (1.0/v_texcoord1.w);
previousNDC.y *= -1.0;
previousNDC = previousNDC * 0.5 + 0.5;
vec2 velocity = gl_FragCoord.xy*u_viewTexel.xy - previousNDC;
vec3 bufferNormal = NormalEncode(bumpedNormal);
gl_FragData[0] = vec4(toGamma(albedo), 1.0);
gl_FragData[1] = vec4(bufferNormal, roughness); // Todo, better packing
gl_FragData[2] = vec4(velocity, 0.0, 0.0);
}

View File

@@ -0,0 +1,12 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
// don't use 5x5 sample pattern for spatial denoise, use 3x3 instead
#define USE_SPATIAL_5X5 0
// includes main function to implement spatial pattern
#include "fs_denoise_spatial_implementation.sh"

View File

@@ -0,0 +1,12 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
// use 5x5 sample pattern for spatial denoise
#define USE_SPATIAL_5X5 1
// includes main function to implement spatial pattern
#include "fs_denoise_spatial_implementation.sh"

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef FS_DENOISE_SPATIAL_IMPLEMENTATION_SH
#define FS_DENOISE_SPATIAL_IMPLEMENTATION_SH
#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"
SAMPLER2D(s_color, 0); // input color, signal to be denoised
SAMPLER2D(s_normal, 1); // scene's gbuffer normal, used for edge stopping function
SAMPLER2D(s_depth, 2); // scene's depth, used for edge stopping function
void main()
{
vec2 texCoord = v_texcoord0;
// read center pixel
vec4 color = texture2D(s_color, texCoord);
vec3 normal = NormalDecode(texture2D(s_normal, texCoord).xyz); // * 2.0 - 1.0;
float depth = texture2D(s_depth, texCoord).x;
// want depth gradient for edge stopping function
float depthGradient = abs(dFdx(depth)) + abs(dFdy(depth));
float du = u_texCoordStep * u_viewTexel.x;
float dv = u_texCoordStep * u_viewTexel.y;
#if USE_SPATIAL_5X5
float gaussianWeights[5];
gaussianWeights[0] = 1.0/16.0;
gaussianWeights[1] = 4.0/16.0;
gaussianWeights[2] = 6.0/16.0;
gaussianWeights[3] = 4.0/16.0;
gaussianWeights[4] = 1.0/16.0;
float initialWeight = (gaussianWeights[2]*gaussianWeights[2]);
int centerIdx = 2;
vec4 accumulateColor = color * initialWeight;
float accumulateWeight = initialWeight;
for (int yy = 0; yy < 5; ++yy)
{
for (int xx = 0; xx < 5; ++xx)
{
#else
float gaussianWeights[3];
gaussianWeights[0] = 1.0/4.0;
gaussianWeights[1] = 2.0/4.0;
gaussianWeights[2] = 1.0/4.0;
float initialWeight = (gaussianWeights[1]*gaussianWeights[1]);
int centerIdx = 1;
vec4 accumulateColor = color * initialWeight;
float accumulateWeight = initialWeight;
for (int yy = 0; yy < 3; ++yy)
{
for (int xx = 0; xx < 3; ++xx)
{
#endif // USE_SPATIAL_5X5
if ((centerIdx == xx) && (centerIdx == yy)) {
continue;
}
float xOffset = float(xx) - float(centerIdx);
float yOffset = float(yy) - float(centerIdx);
vec2 sampleTexCoord = texCoord;
sampleTexCoord.x += xOffset * du;
sampleTexCoord.y += yOffset * dv;
vec4 sampleColor = texture2D(s_color, sampleTexCoord);
vec3 sampleNormal = NormalDecode(texture2D(s_normal, sampleTexCoord).xyz);
float normalWeight = pow(saturate(dot(normal, sampleNormal)), u_sigmaNormal);
float sampleDepth = texture2D(s_depth, sampleTexCoord).x;
float depthDelta = depth - sampleDepth;
float depthWeight = exp(-abs(depthDelta) / max(1e-5, u_sigmaDepth*u_sigmaDepth));
float weight = depthWeight * normalWeight;
// apply gaussian
weight *= (gaussianWeights[xx]*gaussianWeights[yy]);
accumulateColor += sampleColor * weight;
accumulateWeight += weight;
}
}
accumulateColor /= max(accumulateWeight, 1e-5);
gl_FragColor = accumulateColor;
}
#endif // FS_DENOISE_SPATIAL_IMPLEMENTATION_SH

View File

@@ -0,0 +1,96 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"
#include "shared_functions.sh"
SAMPLER2D(s_color, 0);
SAMPLER2D(s_normal, 1);
SAMPLER2D(s_velocity, 2);
SAMPLER2D(s_previousColor, 3); // previous color
SAMPLER2D(s_previousNormal, 4); // previous normal
#define COS_PI_OVER_4 0.70710678118
void main()
{
vec2 texCoord = v_texcoord0;
// read center pixel
vec4 color = texture2D(s_color, texCoord);
vec3 normal = NormalDecode(texture2D(s_normal, texCoord).xyz);
// offset to last pixel
vec2 velocity = texture2D(s_velocity, texCoord).xy;
vec2 texCoordPrev = GetTexCoordPreviousNoJitter(texCoord, velocity);
// SVGF approach suggests sampling and test/rejecting 4 contributing
// samples individually and then doing custom bilinear filter of result
// multiply texCoordPrev by dimensions to get nearest pixels, produces (X.5, Y.5) coordinate
// under no motion, so subtract half here to get correct weights for bilinear filter.
// not thrilled by this, feels like something is wrong.
vec2 screenPixelPrev = texCoordPrev * u_viewRect.zw - vec2_splat(0.5);
vec2 screenPixelMin = floor(screenPixelPrev);
vec2 screenPixelMix = fract(screenPixelPrev);
float x0 = 1.0 - screenPixelMix.x;
float x1 = screenPixelMix.x;
float y0 = 1.0 - screenPixelMix.y;
float y1 = screenPixelMix.y;
float coordWeights[4];
coordWeights[0] = x0*y0;
coordWeights[1] = x1*y0;
coordWeights[2] = x0*y1;
coordWeights[3] = x1*y1;
// adding a half texel here to correct the modification above, in addition to pixel offset
// to grab adjacent pixels for bilinear filter. not thrilled by this, feels like something is wrong.
vec2 coords[4];
coords[0] = (screenPixelMin + vec2(0.5, 0.5)) * u_viewTexel.xy;
coords[1] = (screenPixelMin + vec2(1.5, 0.5)) * u_viewTexel.xy;
coords[2] = (screenPixelMin + vec2(0.5, 1.5)) * u_viewTexel.xy;
coords[3] = (screenPixelMin + vec2(1.5, 1.5)) * u_viewTexel.xy;
// SVGF paper mentions comparing depths and normals to establish
// whether samples are similar enough to contribute, but does not
// describe how. References the following paper, which uses threshold
// of cos(PI/4) to accept/reject.
// https://software.intel.com/content/www/us/en/develop/articles/streaming-g-buffer-compression-for-multi-sample-anti-aliasing.html
// this paper also discusses using depth derivatives to estimate overlapping depth range
vec4 accumulatedColor = vec4_splat(0.0);
float accumulatedWeight = 0.0;
for (int i = 0; i < 4; ++i)
{
vec3 sampleNormal = NormalDecode(texture2D(s_previousNormal, coords[i]).xyz);
float normalSimilarity = dot(normal, sampleNormal);
float weight = (normalSimilarity < COS_PI_OVER_4) ? 0.0 : 1.0;
vec4 sampleColor = texture2D(s_previousColor, coords[i]);
weight *= coordWeights[i];
accumulatedColor += sampleColor * weight;
accumulatedWeight += weight;
}
if (0.0 < accumulatedWeight)
{
accumulatedColor *= (1.0 / accumulatedWeight);
color = mix(color, accumulatedColor, 0.8);
}
else
{
// debug colorize
//color.xyz *= vec3(0.5, 0.01, 0.65);
}
gl_FragColor = color;
}

View File

@@ -0,0 +1,208 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
#include "shared_functions.sh"
#define APPLY_TXAA_IN_LINEAR 0
#define DEBUG_HALF_SCREEN 0
SAMPLER2D(s_color, 0); // this frame's shaded color
SAMPLER2D(s_previousColor, 1); // previous frame's shaded color
SAMPLER2D(s_velocity, 2); // screenspace delta from previous to current frame
SAMPLER2D(s_depth, 3); // depth buffer
vec3 FindNearestDepth(sampler2D _depthSampler, vec2 _texCoord) {
vec2 du = vec2(u_viewTexel.x, 0.0);
vec2 dv = vec2(u_viewTexel.y, 0.0);
vec2 coord = _texCoord - du - dv;
vec3 tcd0 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord - dv;
vec3 tcd1 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord + du - dv;
vec3 tcd2 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord + du;
vec3 tcd3 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord;
vec3 tcd4 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord - du;
vec3 tcd5 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord - du + dv;
vec3 tcd6 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord + dv;
vec3 tcd7 = vec3(coord, texture2D(_depthSampler, coord).x);
coord = _texCoord + du + dv;
vec3 tcd8 = vec3(coord, texture2D(_depthSampler, coord).x);
vec3 minTcd = tcd0;
if (tcd1.z < minTcd.z) minTcd = tcd1;
if (tcd2.z < minTcd.z) minTcd = tcd2;
if (tcd3.z < minTcd.z) minTcd = tcd3;
if (tcd4.z < minTcd.z) minTcd = tcd4;
if (tcd5.z < minTcd.z) minTcd = tcd5;
if (tcd6.z < minTcd.z) minTcd = tcd6;
if (tcd7.z < minTcd.z) minTcd = tcd7;
if (tcd8.z < minTcd.z) minTcd = tcd8;
return minTcd;
}
float Mitchell (float _b, float _c, float _x) {
float v = 0.0;
float x = abs(_x);
float x2 = x*x;
float x3 = x2*x;
if (x < 1.0) {
v = (12.0-9.0*_b-6.0*_c)*x3 + (-18.0+12.0*_b+6.0*_c)*x2 + (6.0-2.0*_b);
}
else if (x < 2.0) {
v = (-_b-6.0*_c)*x3 + (6.0*_b+30.0*_c)*x2 + (-12.0*_b-48.0*_c)*x + (8.0*_b+24.0*_c);
}
return v*(1.0/6.0);
}
void main()
{
vec2 texCoord = v_texcoord0;
vec3 colorCurr = texture2D(s_color, texCoord).xyz;
#if DEBUG_HALF_SCREEN
if (texCoord.x > 0.5) {
#endif
vec3 nearestCoordAndDepth = FindNearestDepth(s_depth, texCoord);
vec2 velocity = texture2D(s_velocity, nearestCoordAndDepth.xy).xy;
vec2 texCoordPrev = GetTexCoordPrevious(texCoord, velocity);
vec3 colorPrev = texture2D(s_previousColor, texCoordPrev).xyz;
// Sample local neighborhood for variance clipping
vec2 du = vec2(u_viewTexel.x, 0.0);
vec2 dv = vec2(0.0, u_viewTexel.y);
vec3 colorUL = texture2D(s_color, texCoord - du - dv).xyz;
vec3 colorUp = texture2D(s_color, texCoord - dv).xyz;
vec3 colorUR = texture2D(s_color, texCoord + du - dv).xyz;
vec3 colorRi = texture2D(s_color, texCoord + du ).xyz;
vec3 colorLe = texture2D(s_color, texCoord - du ).xyz;
vec3 colorDL = texture2D(s_color, texCoord - du + dv).xyz;
vec3 colorDo = texture2D(s_color, texCoord + dv).xyz;
vec3 colorDR = texture2D(s_color, texCoord + du + dv).xyz;
// in an ideal world, lighting and such is in linear space,
// would possibly want to convert to gamma and apply txaa
// there. but this sample isn't storing intermediate results
// in linear space (or doing any reasonable lighting) so
// would possibly want to do the opposite.
#if APPLY_TXAA_IN_LINEAR
colorCurr = toLinear(colorCurr);
colorPrev = toLinear(colorPrev);
colorUL = toLinear(colorUL);
colorUp = toLinear(colorUp);
colorUR = toLinear(colorUR);
colorLe = toLinear(colorLe);
colorRi = toLinear(colorRi);
colorDL = toLinear(colorDL);
colorDo = toLinear(colorDo);
colorDR = toLinear(colorDR);
#endif
// Compute variance box on color neighborhood, clip to box
float outVal = 0.0;
{
vec3 m1 = vec3_splat(0.0);
vec3 m2 = vec3_splat(0.0);
m1 += colorUL; m2 += colorUL*colorUL;
m1 += colorUp; m2 += colorUp*colorUp;
m1 += colorUR; m2 += colorUR*colorUR;
m1 += colorLe; m2 += colorLe*colorLe;
m1 += colorCurr; m2 += colorCurr*colorCurr;
m1 += colorRi; m2 += colorRi*colorRi;
m1 += colorDL; m2 += colorDL*colorDL;
m1 += colorDo; m2 += colorDo*colorDo;
m1 += colorDR; m2 += colorDR*colorDR;
m1 *= (1.0/9.0);
m2 *= (1.0/9.0);
vec3 var = max(vec3_splat(0.0), m2 - m1*m1);
vec3 sigma = sqrt(var);
outVal = max(sigma.x, max(sigma.y, sigma.z));
sigma *= 1.4;
vec3 colorMin = m1 - sigma;
vec3 colorMax = m1 + sigma;
vec3 displacement = colorPrev - m1;
vec3 units = abs(displacement / sigma);
float maxUnit = max(max(units.x, units.y), max(units.z, 1.0));
colorPrev = m1 + displacement * (1.0/maxUnit);
}
float lumaCurr = dot(colorCurr, vec3(0.3, 0.6, 0.1));
float lumaPrev = dot(colorPrev, vec3(0.3, 0.6, 0.1));
// adjust feedback/blend amount depending on color difference
float r = abs(lumaCurr-lumaPrev) / max(max(lumaCurr, lumaPrev), 0.2);
r = 1.0-r;
r = r*r;
float feedback = mix(u_feedbackMin, u_feedbackMax, r);
vec3 colorOut = mix(colorCurr, colorPrev, feedback);
// optionally blur current color, since we've already taken
// the samples to build the variance window. could use more
// blur when feedback is lower, to replace temporal accumulation
// with spatial accumulation. or could use filter to sharpen.
if (u_applyMitchellFilter > 0.0)
{
// adjust filter coefficients depending on color difference
float b = mix(3.0/2.0, 1.0/3.0, r);
float c = mix(-1.0/4.0, 1.0/3.0, r);
float m0 = Mitchell(b, c, 0.0);
float m1 = Mitchell(b, c, 1.0);
float m2 = Mitchell(b, c, sqrt(2.0));
vec3 colorFilter = m0 * colorCurr;
colorFilter += m1 * colorLe;
colorFilter += m1 * colorRi;
colorFilter += m1 * colorUp;
colorFilter += m1 * colorDo;
colorFilter += m2 * colorUL;
colorFilter += m2 * colorUR;
colorFilter += m2 * colorDL;
colorFilter += m2 * colorDR;
colorFilter *= 1.0/(m0 + 4.0*m1 + 4.0*m2);
colorOut = mix(colorFilter, colorPrev, feedback);
}
// in an ideal world, lighting and such is in linear space,
// would possibly want to convert to gamma and apply txaa
// there. but this sample isn't storing intermediate results
// in linear space (or doing any reasonable lighting) so
// would possibly want to do the opposite.
#if APPLY_TXAA_IN_LINEAR
colorCurr = toGamma(colorOut);
#else
colorCurr = colorOut;
#endif
#if DEBUG_HALF_SCREEN
}
#endif
gl_FragColor = vec4(colorCurr, 1.0);
}

View File

@@ -0,0 +1,10 @@
#
# Copyright 2011-2019 Branimir Karadzic. All rights reserved.
# License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
#
BGFX_DIR=../..
RUNTIME_DIR=$(BGFX_DIR)/examples/runtime
BUILD_DIR=../../.build
include $(BGFX_DIR)/scripts/shader.mk

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef NORMAL_ENCODING_SH
#define NORMAL_ENCODING_SH
#define NE_USE_OCTAHEDRAL_REPRESENTATION 0
// From "A Survey of Efficient Representations for Independent Unit Vectors"
// http://jcgt.org/published/0003/02/01/paper.pdf
// Convert an oct24 (2x12bit normal) to an rgb8 value for storing in texture
vec3 snorm12x2_to_unorm8x3 (vec2 f) {
f = clamp(f, -1.0, 1.0);//min(max(f, vec2(-1.0)), vec2(1.0));
vec2 u = floor(f * 2047.0 + 2047.5);
float t = floor(u.y / 256.0);
// "This code assumes that rounding will occur during storage."
// -- Not certain but this appears to mainly apply to the x channel.
// From paper: x = u.x / 16.0 - 0.5
// Instead round by +0.5 and floor.
return vec3(floor(u.x / 16.0), fract(u.x / 16.0) * 256.0 + t, u.y - t * 256.0) / 255.0;
}
// Unpack oct24 (2x12bit normal) from an rgb8 value stored in texture (normal spec)
vec2 unorm8x3_to_snorm12x2 (vec3 u) {
u *= 255.0;
u.y *= (1.0 / 16.0);
vec2 s = vec2(u.x * 16.0 + floor(u.y), fract(u.y) * (16.0 * 256.0) + u.z);
s = s * (1.0 / 2047.0) - 1.0;
return min(max(s, -1.0), 1.0);
}
// Built in sign test could return 0, don't want that
vec2 signNotZero (vec2 v) {
return vec2((v.x >= 0.0) ? 1.0 : -1.0, (v.y >= 0.0) ? 1.0 : -1.0);
}
// Assume normalized input. Output is (-1, 1) for each component
vec2 float32x3_to_oct(vec3 v) {
// Project the sphere onto the octahedron, and then onto the xy plane
vec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z)));
// Reflect the folds of the lower hemisphere over the diagonals
return (v.z <= 0.0) ? ((1.0 - abs(p.yx)) * signNotZero(p)) : p;
}
// Get a float3 normal from an oct representation
vec3 oct_to_float32x3 (vec2 e) {
vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0.0) {
v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy);
}
return normalize(v);
}
vec3 SignedNormalEncodeToOct (vec3 normal) {
return snorm12x2_to_unorm8x3(float32x3_to_oct(normal));
}
vec3 SignedNormalDecodeFromOct (vec3 normal) {
return oct_to_float32x3(unorm8x3_to_snorm12x2(normal));
}
vec3 NormalEncode (vec3 normal)
{
#if NE_USE_OCTAHEDRAL_REPRESENTATION
return SignedNormalEncodeToOct(normal);
#else
return normal * 0.5 + 0.5;
#endif
}
vec3 NormalDecode (vec3 normal)
{
#if NE_USE_OCTAHEDRAL_REPRESENTATION
return SignedNormalDecodeFromOct(normal);
#else
return normal * 2.0 - 1.0;
#endif
}
#endif // NORMAL_ENCODING_SH

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef PARAMETERS_SH
#define PARAMETERS_SH
uniform vec4 u_params[13];
#define u_jitterCurr (u_params[0].xy)
#define u_jitterPrev (u_params[0].zw)
#define u_feedbackMin (u_params[1].x)
#define u_feedbackMax (u_params[1].y)
#define u_applyMitchellFilter (u_params[2].y)
#define u_worldToViewPrev0 (u_params[3])
#define u_worldToViewPrev1 (u_params[4])
#define u_worldToViewPrev2 (u_params[5])
#define u_worldToViewPrev3 (u_params[6])
#define u_viewToProjPrev0 (u_params[7])
#define u_viewToProjPrev1 (u_params[8])
#define u_viewToProjPrev2 (u_params[9])
#define u_viewToProjPrev3 (u_params[10])
#define u_frameIdx (u_params[11].x)
#define u_noiseType (u_params[11].y) // 0=none, 1=dither, 2=random
#define u_texCoordStep (u_params[12].x)
#define u_sigmaDepth (u_params[12].y)
#define u_sigmaNormal (u_params[12].z)
#endif // PARAMETERS_SH

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef SHARED_FUNCTIONS_SH
#define SHARED_FUNCTIONS_SH
vec2 GetTexCoordPreviousNoJitter(vec2 texCoord, vec2 velocity)
{
vec2 texCoordPrev = texCoord - velocity;
return texCoordPrev;
}
vec2 GetTexCoordPrevious(vec2 texCoord, vec2 velocity)
{
vec2 texCoordPrev = texCoord - velocity;
vec2 jitterDelta = (u_jitterCurr-u_jitterPrev);
texCoordPrev += jitterDelta * u_viewTexel.xy;
return texCoordPrev;
}
#endif // SHARED_FUNCTIONS_SH

View File

@@ -0,0 +1,9 @@
vec4 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;
vec3 a_normal : NORMAL;
vec2 v_texcoord0 : TEXCOORD0;
vec4 v_texcoord1 : TEXCOORD1;
vec4 v_texcoord2 : TEXCOORD2;
vec4 v_texcoord3 : TEXCOORD3;
vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0);

View File

@@ -0,0 +1,54 @@
$input a_position, a_normal, a_texcoord0
$output v_normal, v_texcoord0, v_texcoord1, v_texcoord2, v_texcoord3
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
void main()
{
// Calculate vertex position
vec3 pos = a_position.xyz;
gl_Position = mul(u_modelViewProj, vec4(pos, 1.0));
// Calculate previous frame's position
mat4 worldToViewPrev = mat4(
u_worldToViewPrev0,
u_worldToViewPrev1,
u_worldToViewPrev2,
u_worldToViewPrev3
);
mat4 viewToProjPrev = mat4(
u_viewToProjPrev0,
u_viewToProjPrev1,
u_viewToProjPrev2,
u_viewToProjPrev3
);
vec3 wsPos = mul(u_model[0], vec4(pos, 1.0)).xyz;
vec3 vspPos = instMul(worldToViewPrev, vec4(wsPos, 1.0)).xyz;
vec4 pspPos = instMul(viewToProjPrev, vec4(vspPos, 1.0));
// Calculate normal, unpack
vec3 osNormal = a_normal.xyz * 2.0 - 1.0;
// Transform normal into world space
vec3 wsNormal = mul(u_model[0], vec4(osNormal, 0.0)).xyz;
v_normal.xyz = normalize(wsNormal);
v_texcoord0 = a_texcoord0;
// Store previous frame projection space position in extra texCoord attribute
v_texcoord1 = pspPos;
// Store world space view vector in extra texCoord attribute
vec3 wsCamPos = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
vec3 view = normalize(wsCamPos - wsPos);
v_texcoord2 = vec4(wsPos, 1.0);
v_texcoord3 = vec4(wsCamPos, 1.0);
}

View File

@@ -0,0 +1,10 @@
$input a_position, a_texcoord0
$output v_texcoord0
#include "../common/common.sh"
void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position.xyz, 1.0));
v_texcoord0 = a_texcoord0;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -581,6 +581,7 @@ or _OPTIONS["with-combined-examples"] then
, "40-svt"
, "41-tess"
, "42-bunnylod"
, "43-denoise"
)
-- 17-drawstress requires multithreading, does not compile for singlethreaded wasm