Metal fixes

This commit is contained in:
Lubos Lenco 2020-05-11 09:03:13 +02:00
parent a05ab6d855
commit e30a8c7f46
27 changed files with 126 additions and 62 deletions

View File

@ -1,14 +1,14 @@
{
"contexts": [
{
"name": "clear_pass",
"name": "clear_color_depth_pass",
"depth_write": true,
"compare_mode": "always",
"cull_mode": "none",
"links": [],
"texture_params": [],
"vertex_shader": "../include/pass.vert.glsl",
"fragment_shader": "clear_pass.frag.glsl",
"fragment_shader": "clear_color_depth_pass.frag.glsl",
"color_attachments": ["_HDR"]
}
]

View File

@ -0,0 +1,8 @@
#version 450
in vec2 texCoord;
out vec4 fragColor;
void main() {
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
}

View File

@ -0,0 +1,15 @@
{
"contexts": [
{
"name": "clear_color_pass",
"depth_write": false,
"compare_mode": "always",
"cull_mode": "none",
"links": [],
"texture_params": [],
"vertex_shader": "../include/pass.vert.glsl",
"fragment_shader": "clear_color_pass.frag.glsl",
"color_attachments": ["_HDR"]
}
]
}

View File

@ -0,0 +1,8 @@
#version 450
in vec2 texCoord;
out vec4 fragColor;
void main() {
gl_FragDepth = 1.0;
}

View File

@ -0,0 +1,19 @@
{
"contexts": [
{
"name": "clear_depth_pass",
"depth_write": true,
"color_write_red": false,
"color_write_green": false,
"color_write_blue": false,
"color_write_alpha": false,
"compare_mode": "always",
"cull_mode": "none",
"links": [],
"texture_params": [],
"vertex_shader": "../include/pass.vert.glsl",
"fragment_shader": "clear_depth_pass.frag.glsl",
"color_attachments": ["_HDR"]
}
]
}

View File

@ -14,7 +14,7 @@ void main() {
// Scale vertex attribute to [0-1] range
const vec2 madd = vec2(0.5, 0.5);
texCoord = pos.xy * madd + madd;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif

View File

@ -13,14 +13,14 @@ void main() {
// Scale vertex attribute to [0-1] range
const vec2 madd = vec2(0.5, 0.5);
texCoord = pos.xy * madd + madd;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif
gl_Position = vec4(pos.xy, 0.0, 1.0);
// NDC (at the back of cube)
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0);
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0);
v = vec4(invP * v);
viewRay = vec3(v.xy / v.z, 1.0);
}

View File

@ -18,7 +18,7 @@ in vec3 viewRay;
out vec4 fragColor;
vec2 getVelocity(vec2 coord, float depth) {
#ifdef HLSL
#ifdef _InvY
coord.y = 1.0 - coord.y;
#endif
vec4 currentPos = vec4(coord.xy * 2.0 - 1.0, depth, 1.0);
@ -26,7 +26,7 @@ vec2 getVelocity(vec2 coord, float depth) {
vec4 previousPos = prevVP * worldPos;
previousPos /= previousPos.w;
vec2 velocity = (currentPos - previousPos).xy / 40.0;
#ifdef HLSL
#ifdef _InvY
velocity.y = -velocity.y;
#endif
return velocity;
@ -34,7 +34,7 @@ vec2 getVelocity(vec2 coord, float depth) {
void main() {
fragColor.rgb = textureLod(tex, texCoord, 0.0).rgb;
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
if (depth == 1.0) {
return;
@ -42,7 +42,7 @@ void main() {
float blurScale = motionBlurIntensity * frameScale;
vec2 velocity = getVelocity(texCoord, depth) * blurScale;
vec2 offset = texCoord;
int processed = 1;
for(int i = 0; i < 8; ++i) {

View File

@ -14,11 +14,11 @@ out vec4 fragColor;
void main() {
vec2 velocity = textureLod(sveloc, texCoord, 0.0).rg * motionBlurIntensity * frameScale;
#ifdef HLSL
#ifdef _InvY
velocity.y = -velocity.y;
#endif
fragColor.rgb = textureLod(tex, texCoord, 0.0).rgb;
// float speed = length(velocity / texStep);

View File

@ -17,7 +17,7 @@ out vec4 fragColor;
void main() {
vec2 texCoord = wvpposition.xy / wvpposition.w;
texCoord = texCoord * 0.5 + 0.5;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif
@ -46,7 +46,7 @@ void main() {
vec3 v = wp - eye;
vec3 r = reflect(v, n);
#ifdef HLSL
#ifdef _InvY
r.y = -r.y;
#endif
float intensity = clamp((1.0 - roughness) * dot(wp - probep, n), 0.0, 1.0);

View File

@ -17,7 +17,7 @@ out vec4 fragColor;
void main() {
vec2 texCoord = wvpposition.xy / wvpposition.w;
texCoord = texCoord * 0.5 + 0.5;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif
@ -39,7 +39,7 @@ void main() {
vec3 wp = getPos2(invVP, depth, texCoord);
vec4 pp = probeVP * vec4(wp.xyz, 1.0);
vec2 tc = (pp.xy / pp.w) * 0.5 + 0.5;
#ifdef HLSL
#ifdef _InvY
tc.y = 1.0 - tc.y;
#endif

View File

@ -1,5 +1,6 @@
#version 450
#include "compiled.inc"
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
#define SMAA_AREATEX_MAX_DISTANCE 16
#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
@ -33,7 +34,7 @@ out vec4 fragColor;
vec2 cdw_end;
vec4 textureLodA(sampler2D tex, vec2 coord, float lod) {
#ifdef HLSL
#ifdef _InvY
coord.y = 1.0 - coord.y;
#endif
return textureLod(tex, coord, lod);
@ -104,7 +105,7 @@ vec2 SMAASearchDiag2(vec2 texcoord, vec2 dir) {
return coord.zw;
}
/**
/**
* Similar to SMAAArea, this calculates the area corresponding to a certain
* diagonal distance and crossing edges 'e'.
*/
@ -147,7 +148,7 @@ vec2 SMAACalculateDiagWeights(vec2 texcoord, vec2 e, vec4 subsampleIndices) {
// Fetch the crossing edges:
vec4 coords = mad(vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), screenSizeInv.xyxy, texcoord.xyxy);
vec4 c;
c.xy = SMAASampleLevelZeroOffset(edgesTex, coords.xy, ivec2(-1, 0)).rg;
c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, ivec2( 1, 0)).rg;
c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw);
@ -172,7 +173,7 @@ vec2 SMAACalculateDiagWeights(vec2 texcoord, vec2 e, vec4 subsampleIndices) {
d.yw = SMAASearchDiag2(texcoord, vec2(1.0, 1.0)/*, cdw_end*/);
float dadd = cdw_end.y > 0.9 ? 1.0 : 0.0;
d.y += dadd;
}
}
else {
d.yw = vec2(0.0, 0.0);
}
@ -207,7 +208,7 @@ vec2 SMAACalculateDiagWeights(vec2 texcoord, vec2 e, vec4 subsampleIndices) {
/**
* This allows to determine how much length should we add in the last step
* of the searches. It takes the bilinearly interpolated edge (see
* of the searches. It takes the bilinearly interpolated edge (see
* @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
* crossing edges are active.
*/
@ -244,7 +245,7 @@ float SMAASearchXLeft(vec2 texcoord, float end) {
* which edges are active from the four fetched ones.
*/
vec2 e = vec2(0.0, 1.0);
while (texcoord.x > end &&
while (texcoord.x > end &&
e.g > 0.8281 && // Is there some edge not activated?
e.r == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLodA(edgesTex, texcoord, 0.0).rg;
@ -257,20 +258,20 @@ float SMAASearchXLeft(vec2 texcoord, float end) {
float SMAASearchXRight(vec2 texcoord, float end) {
vec2 e = vec2(0.0, 1.0);
while (texcoord.x < end &&
while (texcoord.x < end &&
e.g > 0.8281 && // Is there some edge not activated?
e.r == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLodA(edgesTex, texcoord, 0.0).rg;
texcoord = mad(vec2(2.0, 0.0), screenSizeInv.xy, texcoord);
}
float offset = mad(-(255.0 / 127.0), SMAASearchLength(e, 0.5), 3.25);
return mad(-screenSizeInv.x, offset, texcoord.x);
}
float SMAASearchYUp(vec2 texcoord, float end) {
vec2 e = vec2(1.0, 0.0);
while (texcoord.y > end &&
while (texcoord.y > end &&
e.r > 0.8281 && // Is there some edge not activated?
e.g == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLodA(edgesTex, texcoord, 0.0).rg;
@ -282,7 +283,7 @@ float SMAASearchYUp(vec2 texcoord, float end) {
float SMAASearchYDown(vec2 texcoord, float end) {
vec2 e = vec2(1.0, 0.0);
while (texcoord.y < end &&
while (texcoord.y < end &&
e.r > 0.8281 && // Is there some edge not activated?
e.g == 0.0) { // Or is there a crossing edge that breaks the line?
e = textureLodA(edgesTex, texcoord, 0.0).rg;
@ -292,14 +293,14 @@ float SMAASearchYDown(vec2 texcoord, float end) {
return mad(-screenSizeInv.y, offset, texcoord.y);
}
/**
/**
* Ok, we have the distance and both crossing edges. So, what are the areas
* at each side of current edge?
*/
vec2 SMAAArea(vec2 dist, float e1, float e2, float offset) {
// Rounding prevents precision errors of bilinear filtering:
vec2 texcoord = mad(vec2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * vec2(e1, e2)), dist);
// We do a scale and bias for mapping to texel space:
texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
@ -363,7 +364,7 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord,
// one of the boundaries is enough.
weights.rg = SMAACalculateDiagWeights(texcoord, e, subsampleIndices);
// We give priority to diagonals, so if we find a diagonal we skip
// We give priority to diagonals, so if we find a diagonal we skip
// horizontal/vertical processing.
//SMAA_BRANCH
if (weights.r == -weights.g) { // weights.r + weights.g == 0.0
@ -433,7 +434,7 @@ vec4 SMAABlendingWeightCalculationPS(vec2 texcoord, vec2 pixcoord,
// We want the distances to be in pixel units:
d = abs(round(mad(screenSize.yy, d, -pixcoord.yy)));
// SMAAArea below needs a sqrt, as the areas texture is compressed
// SMAAArea below needs a sqrt, as the areas texture is compressed
// quadratically:
vec2 sqrt_d = sqrt(d);

View File

@ -11,7 +11,7 @@ out vec4 offset0;
out vec4 offset1;
out vec4 offset2;
#ifdef HLSL
#ifdef _InvY
#define V_DIR(v) -(v)
#else
#define V_DIR(v) v
@ -21,7 +21,7 @@ void main() {
// Scale vertex attribute to [0-1] range
const vec2 madd = vec2(0.5, 0.5);
texCoord = pos.xy * madd + madd;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif

View File

@ -18,7 +18,7 @@ out vec4 fragColor;
// Neighborhood Blending Pixel Shader (Third Pass)
vec4 textureLodA(sampler2D tex, vec2 coords, float lod) {
#ifdef HLSL
#ifdef _InvY
coords.y = 1.0 - coords.y;
#endif
return textureLod(tex, coords, lod);
@ -49,7 +49,7 @@ vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset) {
// Calculate the blending offsets:
vec4 blendingOffset = vec4(0.0, a.y, 0.0, a.w);
vec2 blendingWeight = a.yw;
if (h) {
blendingOffset.x = a.x;
blendingOffset.y = 0.0;
@ -58,11 +58,11 @@ vec4 SMAANeighborhoodBlendingPS(vec2 texcoord, vec4 offset) {
blendingWeight.x = a.x;
blendingWeight.y = a.z;
}
blendingWeight /= dot(blendingWeight, vec2(1.0, 1.0));
// Calculate the texture coordinates:
#ifdef HLSL
#ifdef _InvY
vec2 tc = vec2(texcoord.x, 1.0 - texcoord.y);
#else
vec2 tc = texcoord;

View File

@ -9,7 +9,7 @@ uniform vec2 screenSizeInv;
out vec2 texCoord;
out vec4 offset;
#ifdef HLSL
#ifdef _InvY
#define V_DIR(v) -(v)
#else
#define V_DIR(v) v
@ -19,7 +19,7 @@ void main() {
// Scale vertex attribute to [0-1] range
const vec2 madd = vec2(0.5, 0.5);
texCoord = pos.xy * madd + madd;
#ifdef HLSL
#ifdef _InvY
texCoord.y = 1.0 - texCoord.y;
#endif

View File

@ -37,7 +37,7 @@ vec2 getProjectedCoord(vec3 hitCoord) {
vec4 projectedCoord = P * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
#ifdef HLSL
#ifdef _InvY
projectedCoord.y = 1.0 - projectedCoord.y;
#endif
return projectedCoord.xy;

View File

@ -29,13 +29,13 @@ vec2 getProjectedCoord(const vec3 hit) {
vec4 projectedCoord = P * vec4(hit, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
#ifdef HLSL
#ifdef _InvY
projectedCoord.y = 1.0 - projectedCoord.y;
#endif
return projectedCoord.xy;
}
float getDeltaDepth(const vec3 hit) {
float getDeltaDepth(const vec3 hit) {
depth = textureLod(gbufferD, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
vec3 viewPos = getPosView(viewRay, depth, cameraProj);
return viewPos.z - hit.z;
@ -79,7 +79,7 @@ void main() {
float spec = fract(textureLod(gbuffer1, texCoord, 0.0).a);
if (spec == 0.0) { fragColor.rgb = vec3(0.0); return; }
float d = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
if (d == 1.0) { fragColor.rgb = vec3(0.0); return; }
@ -88,18 +88,18 @@ void main() {
n.z = 1.0 - abs(enc.x) - abs(enc.y);
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
n = normalize(n);
vec3 viewNormal = V3 * n;
vec3 viewPos = getPosView(viewRay, d, cameraProj);
vec3 reflected = normalize(reflect(viewPos, viewNormal));
hitCoord = viewPos;
#ifdef _CPostprocess
vec3 dir = reflected * (1.0 - rand(texCoord) * PPComp10.y * roughness) * 2.0;
#else
vec3 dir = reflected * (1.0 - rand(texCoord) * ssrJitter * roughness) * 2.0;
#endif
// * max(ssrMinRayStep, -viewPos.z)
vec4 coords = rayCast(dir);

View File

@ -19,7 +19,7 @@ vec3 getPosView(const vec3 viewRay, const float depth, const vec2 cameraProj) {
return viewRay * linearDepth;
}
vec3 getPos(const vec3 eye, const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
vec3 getPos(const vec3 eye, const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
// eyeLook, viewRay should be normalized
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
float viewZDist = dot(eyeLook, viewRay);
@ -27,7 +27,7 @@ vec3 getPos(const vec3 eye, const vec3 eyeLook, const vec3 viewRay, const float
return wposition;
}
vec3 getPosNoEye(const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
vec3 getPosNoEye(const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
// eyeLook, viewRay should be normalized
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
float viewZDist = dot(eyeLook, viewRay);
@ -35,7 +35,7 @@ vec3 getPosNoEye(const vec3 eyeLook, const vec3 viewRay, const float depth, cons
return wposition;
}
#ifdef HLSL
#if defined(HLSL) || defined(METAL)
vec3 getPos2(const mat4 invVP, const float depth, vec2 coord) {
coord.y = 1.0 - coord.y;
#else
@ -47,7 +47,7 @@ vec3 getPos2(const mat4 invVP, const float depth, const vec2 coord) {
return pos.xyz;
}
#ifdef HLSL
#if defined(HLSL) || defined(METAL)
vec3 getPosView2(const mat4 invP, const float depth, vec2 coord) {
coord.y = 1.0 - coord.y;
#else
@ -59,7 +59,7 @@ vec3 getPosView2(const mat4 invP, const float depth, const vec2 coord) {
return pos.xyz;
}
#ifdef HLSL
#if defined(HLSL) || defined(METAL)
vec3 getPos2NoEye(const vec3 eye, const mat4 invVP, const float depth, vec2 coord) {
coord.y = 1.0 - coord.y;
#else

View File

@ -35,7 +35,7 @@ float PCFCube(samplerCubeShadow shadowMapCube, const vec3 lp, vec3 ml, const flo
const float s = shadowmapCubePcfSize; // TODO: incorrect...
float compare = lpToDepth(lp, lightProj) - bias * 1.5;
ml = ml + n * bias * 20;
#ifdef HLSL
#ifdef _InvY
ml.y = -ml.y;
#endif
float result = texture(shadowMapCube, vec4(ml, compare));
@ -105,7 +105,7 @@ float shadowTestCascade(sampler2DShadow shadowMap, const vec3 eye, const vec3 p,
int casi;
int casIndex;
mat4 LWVP = getCascadeMat(d, casi, casIndex);
vec4 lPos = LWVP * vec4(p, 1.0);
lPos.xyz /= lPos.w;

View File

@ -9,7 +9,7 @@ vec2 getProjectedCoord(vec3 hitCoord) {
vec4 projectedCoord = VP * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
#ifdef HLSL
#if defined(HLSL) || defined(METAL)
projectedCoord.y = 1.0 - projectedCoord.y;
#endif
return projectedCoord.xy;

View File

@ -15,12 +15,12 @@ const float SMAA_REPROJECTION_WEIGHT_SCALE = 30.0;
void main() {
vec4 current = textureLod(tex, texCoord, 0.0);
#ifdef _Veloc
// Velocity is assumed to be calculated for motion blur, so we need to inverse it for reprojection
vec2 velocity = -textureLod(sveloc, texCoord, 0.0).rg;
#ifdef HLSL
#ifdef _InvY
velocity.y = -velocity.y;
#endif

View File

@ -37,6 +37,15 @@ class RenderPathDeferred {
path = _path;
#if kha_metal
{
path.loadShader("shader_datas/clear_color_depth_pass/clear_color_depth_pass");
path.loadShader("shader_datas/clear_color_pass/clear_color_pass");
path.loadShader("shader_datas/clear_depth_pass/clear_depth_pass");
path.clearShader = "shader_datas/clear_color_depth_pass/clear_color_depth_pass";
}
#end
#if (rp_background == "World")
{
path.loadShader("shader_datas/world_pass/world_pass");

View File

@ -64,8 +64,10 @@ class RenderPathForward {
#if kha_metal
{
path.loadShader("shader_datas/clear_pass/clear_pass");
path.clearShader = "shader_datas/clear_pass/clear_pass";
path.loadShader("shader_datas/clear_color_depth_pass/clear_color_depth_pass");
path.loadShader("shader_datas/clear_color_pass/clear_color_pass");
path.loadShader("shader_datas/clear_depth_pass/clear_depth_pass");
path.clearShader = "shader_datas/clear_color_depth_pass/clear_color_depth_pass";
}
#end

View File

@ -128,7 +128,9 @@ def build():
assets.add_khafile_def('rp_shadowmap_cube={0}'.format(rpdat.rp_shadowmap_cube))
if arm.utils.get_gapi() == 'metal':
assets.add_shader_pass('clear_pass')
assets.add_shader_pass('clear_color_depth_pass')
assets.add_shader_pass('clear_color_pass')
assets.add_shader_pass('clear_depth_pass')
assets.add_khafile_def('rp_background={0}'.format(rpdat.rp_background))
if rpdat.rp_background == 'World':

View File

@ -30,7 +30,7 @@ def make(context_id):
vert.write('wnormal = N * vec3(0.0, 0.0, 1.0);')
vert.write('wvpposition = WVP * vec4(pos.xyz, 1.0);')
vert.write('gl_Position = wvpposition;')
frag.add_include('compiled.inc')
frag.add_include('std/gbuffer.glsl')
frag.ins = vert.outs
@ -43,11 +43,11 @@ def make(context_id):
frag.write_attrib(' vec2 screenPosition = wvpposition.xy / wvpposition.w;')
frag.write_attrib(' vec2 depthCoord = screenPosition * 0.5 + 0.5;')
frag.write_attrib('#ifdef HLSL')
frag.write_attrib('#ifdef _InvY')
frag.write_attrib(' depthCoord.y = 1.0 - depthCoord.y;')
frag.write_attrib('#endif')
frag.write_attrib(' float depth = texture(gbufferD, depthCoord).r * 2.0 - 1.0;')
frag.write_attrib(' vec3 wpos = getPos2(invVP, depth, depthCoord);')
frag.write_attrib(' vec4 mpos = invW * vec4(wpos, 1.0);')
frag.write_attrib(' if (abs(mpos.x) > 1.0) discard;')

View File

@ -413,7 +413,7 @@ def make_forward_mobile(con_mesh):
frag.add_uniform('samplerCubeShadow shadowMapPoint[1]')
frag.write('const float s = shadowmapCubePcfSize;') # TODO: incorrect...
frag.write('float compare = lpToDepth(ld, lightProj) - pointBias * 1.5;')
frag.write('#ifdef HLSL')
frag.write('#ifdef _InvY')
frag.write('l.y = -l.y;')
frag.write('#endif')
if '_Legacy' in wrd.world_defs: