gfx-rs / naga

Universal shader translation in Rust
https://github.com/gfx-rs/wgpu/tree/trunk/naga
Other
1.54k stars 195 forks source link

[hlsl-out] D3DCompile error: incorrect number of arguments to numeric-type constructor #1238

Closed niklaskorz closed 3 years ago

niklaskorz commented 3 years ago

WGSL input: https://github.com/niklaskorz/linon/blob/ba70dec599c9718ba315916ff042f954c779dd14/src/main_view.wgsl

Logs:

[2021-08-19T13:34:43Z WARN  wgpu_hal::dx12::device] Naga generated shader for "main" at Compute:
    struct NagaConstants {
        int base_vertex;
        int base_instance;
        uint other;
    };
    ConstantBuffer<NagaConstants> _NagaConstants: register(b0);
    static const bool backface_culling = false;
    static const float3 light_color = float3(1.0, 1.0, 1.0);
    static const float ambient_strength = 0.01;
    static const float shininess = 64.0;
    static const float3 object_color = float3(0.5, 0.5, 0.5);
    static const bool linear_mode = false;
    static const bool use_lighting = true;
    static const float eps = 0.0000001;
    static const float PI = 3.141592653589793;
    static const float h = 0.005;

    struct Camera {
        float4 origin;
        float4 view_direction;
        float4 up;
        row_major float4x4 view_matrix;
    };

    struct Settings {
        float field_weight;
        float mouse_pos_x;
        float mouse_pos_y;
        int show_lyapunov_exponent;
        int central_difference_delta;
        float lyapunov_scaling;
    };

    struct Vertex {
        float x;
        float y;
        float z;
    };

    struct Face {
        uint a;
        uint b;
        uint c;
    };

    struct RaySample {
        float4 position;
        float4 color;
    };

    struct RaySamples {
        RaySample data[800];
    };

    struct NonlinearRayColorResult {
        float4 color;
        float4 mapping_point;
    };

    RWTexture2D<unorm float4> target : register(u1);
    RWTexture2D<float4> mapping : register(u2);
    cbuffer camera : register(b1) { Camera camera; }
    cbuffer settings : register(b2) { Settings settings; }
    ByteAddressBuffer vertices : register(t0);
    ByteAddressBuffer faces : register(t1);
    RWByteAddressBuffer ray_samples : register(u0);
    static float2 sample_positions[8] = float2(float2(-1.0, -1.0), float2(0.0, -1.0), float2(1.0, -1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0), float2(-1.0, 1.0), float2(-1.0, 0.0));
    static float3 sample_colors[8] = float3(float3(1.0, 0.0, 0.0), float3(0.5, 0.5, 0.0), float3(0.0, 1.0, 0.0), float3(0.5, 1.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.5, 0.5, 0.5), float3(0.0, 0.0, 1.0), float3(0.5, 0.0, 0.5));

    struct ComputeInput_main {
        uint3 gid1 : SV_DispatchThreadID;
    };

    float3 rotateX(float3 v, float phi)
    {
        return mul(v, float3x3(float3(1.0, 0.0, 0.0), float3(0.0, cos(phi), sin(phi)), float3(0.0, -sin(phi), cos(phi))));
    }

    float3 rotateY(float3 v1, float phi1)
    {
        return mul(v1, float3x3(float3(cos(phi1), 0.0, -sin(phi1)), float3(0.0, 1.0, 0.0), float3(sin(phi1), 0.0, cos(phi1))));
    }

    float3 rotateZ(float3 v2, float phi2)
    {
        return mul(v2, float3x3(float3(cos(phi2), sin(phi2), 0.0), float3(-sin(phi2), cos(phi2), 0.0), float3(0.0, 0.0, 0.0)));
    }

    float3 translate(float3 v3, float dx, float dy, float dz)
    {
        return float3((v3.x + dx), (v3.y + dy), (v3.z + dz));
    }

    float refraction_index(float t)
    {
        return (((0.0000104 * 101325.0) * (1.0 + ((101325.0 * (60.1 - (0.972 * t))) * pow(10.0, -10.0)))) / (1.0 + (0.00366 * t)));
    }

    float3 refraction(float t_in, float t_out, float3 v_in, float3 n)
    {
        float cosi = (float)0;
        float3 n_ref = (float3)0;

        const float _e20 = refraction_index(t_in);
        const float _e21 = refraction_index(t_out);
        cosi = clamp(-1.0, 1.0, dot(v_in, n));
        n_ref = n;
        float _expr28 = cosi;
        if ((_expr28 < 0.0)) {
            float _expr31 = cosi;
            cosi = -_expr31;
        } else {
            n_ref = -n;
        }
        float eta = (_e20 / _e21);
        float _expr38 = cosi;
        float _expr39 = cosi;
        float k = (1.0 - ((eta * eta) * (1.0 - (_expr38 * _expr39))));
        if ((k < 0.0)) {
            float3 _expr46 = n_ref;
            return reflect(v_in, _expr46);
        }
        float _expr49 = cosi;
        float3 _expr53 = n_ref;
        return ((eta * v_in) + (((eta * _expr49) - sqrt(k)) * _expr53));
    }

    float point_plane_distance(float3 p, float3 n1, float3 p0_)
    {
        float d = dot(p0_, n1);
        return abs((dot(p, n1) - d));
    }

    float3 field_function(float3 p_prev, float3 p1, float3 v0_, float3 v4, float t1)
    {
        float3 center = float3(-0.5, 0.5, -0.5);
        float3 center_dest = (p1 - center);
        float3 normal = normalize(center_dest);
        float dist_in = length((p_prev - center));
        float dist_out = length(center_dest);
        float part_in = clamp(0.0, 1.0, (dist_in / 0.25));
        float part_out = clamp(0.0, 1.0, (dist_out / 0.25));
        float t_in1 = ((part_in * 15.0) + ((1.0 - part_in) * 100.0));
        float t_out1 = ((part_out * 15.0) + ((1.0 - part_out) * 100.0));
        const float3 _e51 = refraction(t_in1, t_out1, v4, normal);
        return _e51;
    }

    float hit_triangle(float3 v5, float3 origin, float3 direction)
    {
        float3 edge1_ = (v5[1] - v5[0]);
        float3 edge2_ = (v5[2] - v5[0]);
        float3 h1 = cross(direction, edge2_);
        float a = dot(edge1_, h1);
        if (((backface_culling || (a > -eps)) && (a < eps))) {
            return -1.0;
        }
        float f = (1.0 / a);
        float3 s = (origin - v5[0]);
        float u = (f * dot(s, h1));
        if (((u < 0.0) || (u > 1.0))) {
            return -1.0;
        }
        float3 q = cross(s, edge1_);
        float v6 = (f * dot(direction, q));
        if (((v6 < 0.0) || ((u + v6) > 1.0))) {
            return -1.0;
        }
        float t5 = (f * dot(edge2_, q));
        if ((t5 > eps)) {
            return t5;
        }
        return -1.0;
    }

    uint NagaBufferLength(ByteAddressBuffer buffer)
    {
        uint ret;
        buffer.GetDimensions(ret);
        return ret;
    }

    float4 ray_color(float3 origin1, float3 direction1, float max_dist)
    {
        float t2 = -1.0;
        float t_new = (float)0;
        float3 d1_ = (float3)0;
        float3 d2_ = (float3)0;
        uint i = 0u;
        float intensity = (float)0;

        bool loop_init = true;
        while(true) {
            if (!loop_init) {
            uint _expr30 = i;
            i = (_expr30 + 1u);
            }
            loop_init = false;
            uint _expr26 = i;
            if ((_expr26 < ((NagaBufferLength(faces) - 0) / 12))) {
            } else {
                break;
            }
            uint _expr34 = i;
            Face face = {asuint(faces.Load(_expr34*12+0+0)), asuint(faces.Load(_expr34*12+0+4)), asuint(faces.Load(_expr34*12+0+8))};
            Vertex a = {asfloat(vertices.Load(face.a*12+0+0)), asfloat(vertices.Load(face.a*12+0+4)), asfloat(vertices.Load(face.a*12+0+8))};
            Vertex b = {asfloat(vertices.Load(face.b*12+0+0)), asfloat(vertices.Load(face.b*12+0+4)), asfloat(vertices.Load(face.b*12+0+8))};
            Vertex c = {asfloat(vertices.Load(face.c*12+0+0)), asfloat(vertices.Load(face.c*12+0+4)), asfloat(vertices.Load(face.c*12+0+8))};
            float3 triangle1[3] = { float3(a.x, a.y, a.z), float3(b.x, b.y, b.z), float3(c.x, c.y, c.z) };
            const float _e62 = hit_triangle(triangle1, origin1, direction1);
            t_new = _e62;
            float _expr63 = t_new;
            float _expr66 = t_new;
            float _expr69 = t2;
            float _expr72 = t_new;
            float _expr73 = t2;
            if ((((_expr63 > 0.0) && (_expr66 < max_dist)) && ((_expr69 < 0.0) || (_expr72 < _expr73)))) {
                float _expr77 = t_new;
                t2 = _expr77;
                d1_ = (triangle1[1] - triangle1[0]);
                d2_ = (triangle1[2] - triangle1[0]);
            }
        }
        float _expr88 = t2;
        float _expr91 = t2;
        if (((_expr88 > 0.0) && (_expr91 < max_dist))) {
            float3 _expr94 = d1_;
            float3 _expr95 = d2_;
            float3 normal = normalize(cross(_expr94, _expr95));
            if (!use_lighting) {
                float3 color1 = abs(normal);
                return float4(color1, 1.0);
            }
            float3 ambient = (ambient_strength * float3(1.0, 1.0, 1.0));
            intensity = max(dot(normal, -direction1), 0.0);
            float _expr108 = intensity;
            if ((_expr108 == 0.0)) {
                intensity = max(dot(-normal, -direction1), 0.0);
            }
            float diff = intensity;
            float3 diffuse = (diff * float3(1.0, 1.0, 1.0));
            float _expr118 = intensity;
            float spec = pow(_expr118, shininess);
            float3 specular = (spec * float3(1.0, 1.0, 1.0));
            float3 result1 = (((ambient + diffuse) + specular) * abs(normal));
            float _expr125 = t2;
            return float4(result1, _expr125);
        }
        return float4(0.0, 0.0, 0.0, 0.0);
    }

    NonlinearRayColorResult nonlinear_ray_color(float3 start_point, float3 start_dir)
    {
        NonlinearRayColorResult result = (NonlinearRayColorResult)0;
        bool has_color = false;
        bool has_mapping_point = false;
        float3 cur_point = (float3)0;
        float3 cur_dir = (float3)0;
        float t3 = 0.0;
        int i1 = 0;

        result.mapping_point = float4(0.0, 0.0, 0.0, 0.0);
        float field_weight = settings.field_weight;
        cur_point = start_point;
        cur_dir = start_dir;
        bool loop_init1 = true;
        while(true) {
            if (!loop_init1) {
            int _expr41 = i1;
            i1 = (_expr41 + 1);
            }
            loop_init1 = false;
            int _expr39 = i1;
            if ((_expr39 < 800)) {
            } else {
                break;
            }
            float3 _expr44 = cur_point;
            float3 _expr45 = cur_dir;
            float3 _expr48 = cur_point;
            float3 _expr49 = cur_dir;
            float _expr50 = t3;
            const float3 _e51 = field_function((_expr44 - (_expr45 * h)), _expr48, start_dir, _expr49, _expr50);
            float3 _expr52 = cur_point;
            float3 _expr53 = cur_point;
            float _expr58 = t3;
            const float3 _e62 = field_function(_expr52, (_expr53 + ((0.5 * h) * _e51)), start_dir, _e51, (_expr58 + (0.5 * h)));
            float3 _expr63 = cur_point;
            float3 _expr64 = cur_point;
            float _expr69 = t3;
            const float3 _e73 = field_function(_expr63, (_expr64 + ((0.5 * h) * _e62)), start_dir, _e62, (_expr69 + (0.5 * h)));
            float3 _expr74 = cur_point;
            float3 _expr75 = cur_point;
            float _expr78 = t3;
            const float3 _e80 = field_function(_expr74, (_expr75 + (h * _e73)), start_dir, _e73, (_expr78 + h));
            float3 v7 = ((((_e51 + (2.0 * _e62)) + (2.0 * _e73)) + _e80) / float3(6.0.xxx));
            float3 _expr93 = cur_dir;
            cur_dir = (((1.0 - field_weight) * _expr93) + (field_weight * v7));
            float3 _expr97 = cur_dir;
            float3 step_dir = (_expr97 * h);
            bool _expr99 = has_color;
            if (!_expr99) {
                float3 _expr102 = cur_point;
                const float4 _e105 = ray_color(_expr102, normalize(step_dir), length(step_dir));
                result.color = _e105;
                float4 _expr107 = result.color;
                has_color = (_expr107.w > 0.0);
            }
            float3 _expr111 = cur_point;
            cur_point = (_expr111 + step_dir);
            float _expr113 = t3;
            t3 = (_expr113 + h);
        }
        float3 _expr116 = cur_point;
        result.mapping_point = float4(_expr116, 1.0);
        result.color.w = 1.0;
        bool _expr122 = has_color;
        if (!_expr122) {
            result.color = float4(0.0, 0.0, 0.0, 1.0);
        }
        NonlinearRayColorResult _expr130 = result;
        const NonlinearRayColorResult nonlinearraycolorresult1 = _expr130;
        return nonlinearraycolorresult1;
    }

    void sample_rays(float3 start_point1, float3 start_dir1, int samples_index, float3 sample_color)
    {
        float3 cur_point1 = (float3)0;
        float3 cur_dir1 = (float3)0;
        float4 color = (float4)0;
        float t4 = 0.0;
        RaySample sample1 = (RaySample)0;
        int i2 = 0;

        float field_weight = settings.field_weight;
        cur_point1 = start_point1;
        cur_dir1 = start_dir1;
        sample1.color = float4(sample_color, 0.5);
        int sample_step_size = (800 / 100);
        bool loop_init2 = true;
        while(true) {
            if (!loop_init2) {
            int _expr39 = i2;
            i2 = (_expr39 + 1);
            }
            loop_init2 = false;
            int _expr37 = i2;
            if ((_expr37 < 800)) {
            } else {
                break;
            }
            float3 _expr42 = cur_point1;
            float3 _expr43 = cur_dir1;
            float3 _expr46 = cur_point1;
            float3 _expr47 = cur_dir1;
            float _expr48 = t4;
            const float3 _e49 = field_function((_expr42 - (_expr43 * h)), _expr46, start_dir1, _expr47, _expr48);
            float3 _expr50 = cur_point1;
            float3 _expr51 = cur_point1;
            float _expr56 = t4;
            const float3 _e60 = field_function(_expr50, (_expr51 + ((0.5 * h) * _e49)), start_dir1, _e49, (_expr56 + (0.5 * h)));
            float3 _expr61 = cur_point1;
            float3 _expr62 = cur_point1;
            float _expr67 = t4;
            const float3 _e71 = field_function(_expr61, (_expr62 + ((0.5 * h) * _e60)), start_dir1, _e60, (_expr67 + (0.5 * h)));
            float3 _expr72 = cur_point1;
            float3 _expr73 = cur_point1;
            float _expr76 = t4;
            const float3 _e78 = field_function(_expr72, (_expr73 + (h * _e71)), start_dir1, _e71, (_expr76 + h));
            float3 v8 = ((((_e49 + (2.0 * _e60)) + (2.0 * _e71)) + _e78) / float3(6.0.xxx));
            float3 _expr91 = cur_dir1;
            cur_dir1 = (((1.0 - field_weight) * _expr91) + (field_weight * v8));
            float3 _expr95 = cur_dir1;
            float3 step_dir = (_expr95 * h);
            int _expr97 = i2;
            if (((_expr97 % sample_step_size) == 0)) {
                float3 _expr102 = cur_point1;
                sample1.position = float4(_expr102, 1.0);
                int _expr106 = i2;
                int index = ((samples_index * 100) + (_expr106 / sample_step_size));
                RaySample _expr111 = sample1;
                {
                    RaySample _value4 = _expr111;
                    ray_samples.Store4(index*32+0+0, asuint(_value4.position));
                    ray_samples.Store4(index*32+0+16, asuint(_value4.color));
                }
            }
            float3 _expr112 = cur_point1;
            cur_point1 = (_expr112 + step_dir);
            float _expr114 = t4;
            t4 = (_expr114 + h);
        }
        return;
    }

    int2 NagaRWDimensions2D(RWTexture2D<unorm float4> texture)
    {
        uint4 ret;
        texture.GetDimensions(ret.x, ret.y);
        return ret.xy;
    }

    [numthreads(8, 8, 1)]
    void main(ComputeInput_main computeinput_main)
    {
        int2 size = NagaRWDimensions2D(target);
        int2 coords = int2(int(computeinput_main.gid1.x), ((size.y - int(computeinput_main.gid1.y)) - 1));
        if (((coords.x >= size.x) || (coords.y < 0))) {
            return;
        }
        float width = float(size.x);
        float height = float(size.y);
        float aspect_ratio = (width / height);
        float4 _expr43 = camera.origin;
        float3 origin2 = _expr43.xyz;
        float4 _expr46 = camera.view_direction;
        float3 view_direction = _expr46.xyz;
        float4 _expr49 = camera.up;
        float3 up = _expr49.xyz;
        float viewport_width = (aspect_ratio * 0.025);
        float3 w = -view_direction;
        float3 horizontal = cross(up, w);
        float3 vertical = cross(w, horizontal);
        float u = (((float(computeinput_main.gid1.x) / (width - 1.0)) * viewport_width) - (0.5 * viewport_width));
        float v9 = (((float(computeinput_main.gid1.y) / (height - 1.0)) * 0.025) - (0.5 * 0.025));
        float3 s = (((u * normalize(horizontal)) + (v9 * normalize(vertical))) + (0.035 * view_direction));
        float3 dir = normalize(s);
        if (linear_mode) {
            const float4 _e84 = ray_color(origin2, dir, 100.0);
            target[coords] = _e84;
        } else {
            const NonlinearRayColorResult _e85 = nonlinear_ray_color(origin2, dir);
            target[coords] = _e85.color;
            mapping[coords] = _e85.mapping_point;
        }
        if (((computeinput_main.gid1.x < 8u) && (computeinput_main.gid1.y == 0u))) {
            float _expr96 = settings.mouse_pos_x;
            float _expr98 = settings.mouse_pos_y;
            float2 mouse_pos = float2(_expr96, _expr98);
            float2 _expr104 = sample_positions[int(computeinput_main.gid1.x)];
            float2 pos = (mouse_pos + (0.01 * _expr104));
            float3 color2 = sample_colors[int(computeinput_main.gid1.x)];
            float u = ((pos.x * viewport_width) - (0.5 * viewport_width));
            float v10 = ((pos.y * 0.025) - (0.5 * 0.025));
            float3 s = (((u * normalize(horizontal)) + (v10 * normalize(vertical))) + (0.035 * view_direction));
            float3 dir = normalize(s);
            sample_rays(origin2, dir, coords.x, color2);
            return;
        } else {
            return;
        }
    }

[2021-08-19T13:34:43Z WARN  wgpu::backend::direct] Shader translation error for stage COMPUTE: D3DCompile error (0x80004005): C:\Users\korzn\Development\studium\linon\compute_shader(67,37-192): error X3014: incorrect number of arguments to numeric-type constructor

[2021-08-19T13:34:43Z WARN  wgpu::backend::direct] Please report it to https://github.com/gfx-rs/naga
[2021-08-19T13:34:43Z ERROR wgpu::backend::direct] wgpu error: Validation Error

    Caused by:
        In Device::create_compute_pipeline
          note: label = `compute_pipeline`
        Internal error: D3DCompile error (0x80004005): C:\Users\korzn\Development\studium\linon\compute_shader(67,37-192): error X3014: incorrect number of arguments to numeric-type constructor
cwfitzgerald commented 3 years ago

Ah, I see the issue. Fix incoming