Runtime shader fixes

This commit is contained in:
luboslenco 2019-02-27 21:30:48 +01:00
parent 5942ee72ea
commit e6d4fe43da
3 changed files with 47 additions and 23 deletions

View file

@ -978,7 +978,7 @@ class Cycles {
curshader.write('${sample_bump_res}_fh2 *= ($strength) * 3.0;');
curshader.write('vec3 ${sample_bump_res}_a = normalize(vec3(1.0, 0.0, ${sample_bump_res}_fh1));');
curshader.write('vec3 ${sample_bump_res}_b = normalize(vec3(0.0, 1.0, ${sample_bump_res}_fh2));');
res = 'normalize(mat3(${sample_bump_res}_a, ${sample_bump_res}_b, normalize(vec3(${sample_bump_res}_fh1, ${sample_bump_res}_fh2, 2.0))) * n)';
res = 'normalize(mul(n, mat3(${sample_bump_res}_a, ${sample_bump_res}_b, normalize(vec3(${sample_bump_res}_fh1, ${sample_bump_res}_fh2, 2.0)))))';
sample_bump_res = '';
}
else {
@ -992,10 +992,13 @@ class Cycles {
curshader.add_function(armory.system.CyclesFunctions.str_cotangentFrame);
if (!curshader.invTBN) {
curshader.invTBN = true;
curshader.write('mat3 invTBN = inverse(cotangentFrame(n, -vVec, texCoord));');
#if kha_direct3d11
curshader.write('mat3 invTBN = transpose(cotangentFrame(n, vVec, texCoord));');
#else
curshader.write('mat3 invTBN = transpose(cotangentFrame(n, -vVec, texCoord));');
#end
}
res = '(normalize(invTBN * normalize($res)) * 0.5 + 0.5)';
res = '(normalize(mul($res, invTBN)) * 0.5 + 0.5)';
return res;
}
@ -1119,14 +1122,19 @@ class Cycles {
frag.add_function(CyclesFunctions.str_cotangentFrame);
}
frag.n = true;
#if kha_direct3d11
frag.write('mat3 TBN = cotangentFrame(n, vVec, texCoord);');
#else
frag.write('mat3 TBN = cotangentFrame(n, -vVec, texCoord);');
frag.write('n = TBN * normalize(texn);');
#end
frag.write('n = mul(normalize(texn), TBN);');
}
// else:
// frag.write('vec3 n = ({0}) * 2.0 - 1.0;'.format(out_normaltan))
// if (strength != '1.0') frag.write('n.xy *= $strength;');
// # frag.write('n = normalize(TBN * normalize(n));')
// frag.write('n = TBN * normalize(n);')
// # frag.write('n = normalize(mul(normalize(n), TBN));')
// frag.write('n = mul(normalize(n), TBN);')
// con.add_elem('tang', 'short4norm')
parse_teximage_vector = true;
@ -1185,9 +1193,13 @@ class Cycles {
else if (node.type == 'CAMERA') {
// View Z Depth
if (socket == node.outputs[1]) {
// curshader.add_include('std/math.glsl');
curshader.add_uniform('vec2 cameraProj', '_cameraPlaneProj');
#if kha_direct3d11
curshader.wvpposition = true;
return '(cameraProj.y / ((wvpposition.z / wvpposition.w) - cameraProj.x))';
#else
return '(cameraProj.y / (gl_FragCoord.z - cameraProj.x))';
#end
}
// View Distance
else {
@ -1206,7 +1218,11 @@ class Cycles {
else if (node.type == 'NEW_GEOMETRY') {
if (socket == node.outputs[6]) // Backfacing
#if kha_direct3d11
return '0.0'; // SV_IsFrontFace
#else
return '(1.0 - float(gl_FrontFacing))';
#end
else if (socket == node.outputs[7]) // Pointiness
return '0.0';
}

View file

@ -98,9 +98,9 @@ vec3 random3(const vec3 c) {
float tex_musgrave_f(const vec3 p) {
const float F3 = 0.3333333;
const float G3 = 0.1666667;
vec3 s = floor(p + dot(p, vec3(F3)));
vec3 x = p - s + dot(s, vec3(G3));
vec3 e = step(vec3(0.0), x - x.yzx);
vec3 s = floor(p + dot(p, vec3(F3, F3, F3)));
vec3 x = p - s + dot(s, vec3(G3, G3, G3));
vec3 e = step(vec3(0.0, 0.0, 0.0), x - x.yzx);
vec3 i1 = e*(1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy*(1.0 - e);
vec3 x1 = x - i1 + G3;
@ -119,7 +119,7 @@ float tex_musgrave_f(const vec3 p) {
w *= w;
w *= w;
d *= w;
return clamp(dot(d, vec4(52.0)), 0.0, 1.0);
return clamp(dot(d, vec4(52.0, 52.0, 52.0, 52.0)), 0.0, 1.0);
}
";
@ -214,7 +214,7 @@ float traceConeAO(sampler3D voxels, const vec3 origin, vec3 dir, const float ape
while (sampleCol < 1.0 && dist < maxDist) {
samplePos = dir * dist + origin;
float mip = max(log2(diam * voxelgiResolution.x), 0);
float mipSample = textureLod(voxels, samplePos * 0.5 + vec3(0.5), mip).r;
float mipSample = textureLod(voxels, samplePos * 0.5 + vec3(0.5, 0.5, 0.5), mip).r;
sampleCol += (1 - sampleCol) * mipSample;
dist += max(diam / 2, VOXEL_SIZE);
diam = dist * aperture;
@ -227,10 +227,9 @@ vec3 tangent(const vec3 n) {
if (length(t1) > length(t2)) return normalize(t1);
else return normalize(t2);
}
float traceAO(const vec3 origin, const vec3 normal, sampler3D voxels, float vrange, float voffset) {
const float TAN_22_5 = 0.55785173935;
float traceAO(const vec3 origin, const vec3 normal, const float vrange, const float voffset) {
const float angleMix = 0.5f;
const float aperture = TAN_22_5;
const float aperture = 0.55785173935;
vec3 o1 = normalize(tangent(normal));
vec3 o2 = normalize(cross(o1, normal));
vec3 c1 = 0.5f * (o1 + o2);
@ -249,8 +248,7 @@ float traceAO(const vec3 origin, const vec3 normal, sampler3D voxels, float vran
col += traceConeAO(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, offset);
const float blendFac = 1.0;
return col / (9.0 * blendFac);
return col / 9.0;
}
";

View file

@ -282,11 +282,11 @@ class CyclesShader {
}
#if kha_direct3d11
var s = 'SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };\n';
s += 'SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };\n';
var s = '#define HLSL\n';
s += '#define sampler2D Texture2D\n';
s += '#define sampler3D Texture3D\n';
s += '#define texture(tex, coord) tex.Sample(LinearSampler, coord)\n';
s += '#define texture(tex, coord) tex.Sample(tex ## _sampler, coord)\n';
s += '#define textureLod(tex, coord, lod) tex.SampleLevel(tex ## _sampler, coord, lod)\n';
s += '#define texelFetch(tex, coord, lod) tex.Load(float3(coord.xy, lod))\n';
s += '#define mod(a, b) (a % b)\n';
s += '#define vec2 float2\n';
@ -302,6 +302,7 @@ class CyclesShader {
s += '#define dFdy ddy\n';
s += '#define inversesqrt rsqrt\n';
s += '#define fract frac\n';
s += '#define mix lerp\n';
s += header;
@ -357,10 +358,18 @@ class CyclesShader {
s += '};\n';
}
for (a in uniforms)
for (a in uniforms) {
s += 'uniform ' + a + ';\n';
for (f in functions)
#if kha_direct3d11
if (StringTools.startsWith(a, 'sampler')) {
s += 'SamplerState ' + a.split(' ')[1] + '_sampler;';
}
#end
}
for (f in functions) {
s += f + '\n';
}
// Begin main
if (outs.length > 0 || shader_type == 'vert') {
@ -409,6 +418,7 @@ class CyclesShader {
// Write output structure
if (shader_type == 'vert') {
s += 'SPIRV_Cross_Output stage_output;\n';
s += 'gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n';
s += 'stage_output.svpos = gl_Position;\n';
for (a in outs) {
var b = a.substring(5); // Remove type 'vec4 '