Shader fixes for metal

This commit is contained in:
Lubos Lenco
2020-05-22 13:49:34 +02:00
parent 639738730f
commit 01a57c8206
3 changed files with 15 additions and 11 deletions
@@ -175,7 +175,7 @@ class Material {
con.add_elem("tang", "short4norm");
vert.add_uniform("mat3 N", "_normalMatrix");
vert.add_out("vec3 wtangent");
vert.write_attrib('wtangent = normalize(mul(tang, N));');
vert.write_attrib('wtangent = normalize(mul(tang.xyz, N));');
}
if (frag.vVecCam) {
vert.add_uniform("mat4 WV", "_worldViewMatrix");
@@ -465,7 +465,6 @@ class Material {
}
else if (node.type == "TEX_NOISE") {
curshader.add_function(MaterialFunctions.str_tex_noise);
curshader.add_uniform("sampler2D snoise256", "$noise256.k");
var co = getCoord(node);
var scale = parse_value_input(node.inputs[1]);
var res = 'vec3(tex_noise($co * $scale), tex_noise($co * $scale + 0.33), tex_noise($co * $scale + 0.66))';
@@ -482,10 +481,10 @@ class Material {
coloring = coloring.replace(" ", "_");
var res = "";
if (coloring == "INTENSITY") {
res = to_vec3('tex_voronoi($co * $scale).a');
res = to_vec3('tex_voronoi($co * $scale, texturePass(snoise256)).a');
}
else { // Cells
res = 'tex_voronoi($co * $scale).rgb';
res = 'tex_voronoi($co * $scale, texturePass(snoise256)).rgb';
}
if (sample_bump) write_bump(node, res);
return res;
@@ -528,7 +527,7 @@ class Material {
curshader.write('vec3 res1 = vec3(0.0, 0.0, 0.0);');
curshader.write('for (int i = -$steps; i <= $steps; ++i) {');
curshader.write('for (int j = -$steps; j <= $steps; ++j) {');
curshader.write('texCoordBlur = texCoord + vec2(i, j) / textureSize($texture, 0);');
curshader.write('texCoordBlur = texCoord + vec2(i, j) / vec2(textureSize($texture, 0));');
curshader.write('res1 += $out_col;');
curshader.write('}');
curshader.write('}');
@@ -1137,7 +1136,6 @@ class Material {
}
else if (node.type == "TEX_NOISE") {
curshader.add_function(MaterialFunctions.str_tex_noise);
curshader.add_uniform("sampler2D snoise256", "$noise256.k");
var co = getCoord(node);
var scale = parse_value_input(node.inputs[1]);
var res = 'tex_noise($co * $scale)';
@@ -1154,10 +1152,10 @@ class Material {
coloring = coloring.replace(" ", "_");
var res = "";
if (coloring == "INTENSITY") {
res = 'tex_voronoi($co * $scale).a';
res = 'tex_voronoi($co * $scale, texturePass(snoise256)).a';
}
else { // Cells
res = 'tex_voronoi($co * $scale).r';
res = 'tex_voronoi($co * $scale, texturePass(snoise256)).r';
}
if (sample_bump) write_bump(node, res);
return res;
@@ -1474,7 +1472,7 @@ class Material {
}
public static inline function to_vec3(s: String): String {
#if (kha_direct3d11 || kha_direct3d12 || kha_metal)
#if (kha_direct3d11 || kha_direct3d12)
return '($s).xxx';
#else
return 'vec3($s)';
@@ -24,7 +24,7 @@ float tex_checker_f(const vec3 co, const float scale) {
// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
public static var str_tex_voronoi = "
vec4 tex_voronoi(const vec3 x) {
vec4 tex_voronoi(const vec3 x, textureArg(snoise256)) {
vec3 p = floor(x);
vec3 f = fract(x);
float id = 0.0;
@@ -299,6 +299,8 @@ class MaterialShader {
#if (kha_direct3d11 || kha_direct3d12)
var s = '#define HLSL\n';
s += '#define textureArg(tex) Texture2D tex,SamplerState tex ## _sampler\n';
s += '#define texturePass(tex) tex,tex ## _sampler\n';
s += '#define sampler2D Texture2D\n';
s += '#define sampler3D Texture3D\n';
s += '#define texture(tex, coord) tex.Sample(tex ## _sampler, coord)\n';
@@ -484,6 +486,8 @@ class MaterialShader {
s += '#include <simd/simd.h>\n';
s += 'using namespace metal;\n';
s += '#define textureArg(tex) texture2d<float> tex,sampler tex ## _sampler\n';
s += '#define texturePass(tex) tex,tex ## _sampler\n';
s += '#define sampler2D texture2d<float>\n';
s += '#define sampler3D texture3d<float>\n';
s += '#define texture(tex, coord) tex.sample(tex ## _sampler, coord)\n';
@@ -493,7 +497,7 @@ class MaterialShader {
s += '#define texelFetch(tex, coord, lod) tex.read(uint2(coord), uint(lod))\n';
s += 'uint2 _getDimensions(texture2d<float> tex, uint lod) { return uint2(tex.get_width(lod), tex.get_height(lod)); }\n';
s += '#define textureSize _getDimensions\n';
s += '#define mod(a, b) (a % b)\n';
s += '#define mod(a, b) fmod(a, b)\n';
s += '#define vec2 float2\n';
s += '#define vec3 float3\n';
s += '#define vec4 float4\n';
@@ -718,6 +722,8 @@ class MaterialShader {
var s = '#version 330\n';
#end
s += '#define textureArg(tex) Sampler2D tex\n';
s += '#define texturePass(tex) tex\n';
s += '#define mul(a, b) b * a\n';
s += '#define textureShared texture\n';
s += '#define textureLodShared textureLod\n';