stevensona / shader-toy

Shadertoy-like live preview for GLSL shaders in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=stevensona.shader-toy
MIT License
586 stars 51 forks source link

Extension causes high cpu load/ gets stuck loading the preview window #122

Open Hendiadyoin1 opened 3 years ago

Hendiadyoin1 commented 3 years ago

\

stevensona.shader-toy-unresponsive.cpuprofile.txt

Find more details here: https://github.com/microsoft/vscode/wiki/Explain-extension-causes-high-cpu-load </auto stuff>

So in short I was trying to open the preview window of one of my projects and the plugin refuses to start.
The other log says something like:

[2020-12-09 17:40:09.820] [renderer1] [error] Illegal argument: Error: Illegal argument
    at Object.t.illegalArgument (file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:50:46)
    at file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:2045:110
    at file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:1982:363
    at d.invokeFunction (file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:1724:588)
    at u._tryExecuteCommand (file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:4151:37)
    at file:///C:/Users/leon2/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:4150:774
[2020-12-09 17:40:24.350] [renderer1] [warning] UNRESPONSIVE extension host, 'stevensona.shader-toy' took 100% of 6428ms, saved PROFILE here: 'c:\Users\leon2\AppData\Local\Temp\exthost-b6b883.cpuprofile' [{"id":"gc","total":3000,"percentage":0},{"id":"program","total":8000,"percentage":0},{"id":"stevensona.shader-toy","total":6417000,"percentage":100}]
[2020-12-09 17:40:32.658] [renderer1] [warning] {}
[2020-12-09 17:41:12.936] [renderer1] [error] Failed opening file c:\Users\leon2\AppData\Roaming\Code\logs\20201209T173951\exthost1\exthost.log for writing: Permission denied: Error: Failed opening file c:\Users\leon2\AppData\Roaming\Code\logs\20201209T173951\exthost1\exthost.log for writing: Permission denied
    at createRotatingLogger (c:\Users\leon2\AppData\Local\Programs\Microsoft VS Code\resources\app\node_modules.asar\spdlog\index.js:34:9)
    at c:\Users\leon2\AppData\Local\Programs\Microsoft VS Code\resources\app\node_modules.asar\spdlog\index.js:27:7
    at c:\Users\leon2\AppData\Local\Programs\Microsoft VS Code\resources\app\node_modules.asar\mkdirp\index.js:48:26
    at FSReqCallback.oncomplete (fs.js:159:5)
[2020-12-09 17:41:41.556] [renderer1] [warning] UNRESPONSIVE extension host, 'stevensona.shader-toy' took 100% of 6625ms, saved PROFILE here: 'c:\Users\leon2\AppData\Local\Temp\exthost-33e175.cpuprofile' [{"id":"gc","total":7000,"percentage":0},{"id":"stevensona.shader-toy","total":6618000,"percentage":100}]

My File causing the Issue (sorta' long):

#iUniform float MinDist = 0.0005 in { 0.0001, 0.01 }
#iUniform float MaxDist = 15.0
#iUniform float reflectionThreshhold = 0.001 in {1e-5, 0.1}
#iUniform float zoom = 1.5 in {0, 10}
#iUniform float blend = 1 in {1, 10}
#iUniform float gamma = 1 in {0.1, 10}
#iChannel0 "self"

#define TWO_PI 6.28318530718
const vec3 sun = normalize(vec3(5,-10,10));

mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
    vec3 cw = normalize(ta-ro);
    vec3 cp = vec3(sin(cr), cos(cr),0.0);
    vec3 cu = normalize( cross(cw,cp) );
    vec3 cv = normalize( cross(cu,cw) );
    return mat3( cu, cv, cw );
}

struct Material{
    float roughness;
    float reflectance;
    float diffuse;
    vec3 color;
};

struct Map{
    float dist;
    Material material;
};

struct Ray{
    vec3 p;
    float dist;
    int steps;
    Material material;
    vec3 normal;
};

const Material NONE = Material(0.,0.,0.,vec3(0));
const Material Floor = Material(
    .05,
    .3,
    .5,
    vec3(.2,.9,.2)
);
const Material Sphere = Material(
    0.1,
    0.7,
    0.2,
    vec3(0.9)
);

vec2 hash23(vec3 p3)
{
    p3 = fract(p3 * vec3(443.897, 441.423, 437.195));
    p3 += dot(p3, p3.yzx+19.19);
    return fract((p3.xx+p3.yz)*p3.zy);
}
vec2 hash2( float n ) { return fract(sin(vec2(n,n+1.0))*vec2(43758.5453123,22578.1459123)); }

Map map(vec3 p){
    float dFloor = p.y;
    float dSphere = length(p-vec3(0,1,0))-1.0;

    if (dFloor<dSphere){
        return Map(dFloor,Floor);
    }else{
        return Map(dSphere,Sphere);
    }
}

vec3 calcNormal(vec3 p)
{
    const float h = 0.0001; // replace by an appropriate value
    const vec2 k = vec2(1,-1);
    return normalize( k.xyy*map( p + k.xyy*h ).dist + 
                      k.yyx*map( p + k.yyx*h ).dist + 
                      k.yxy*map( p + k.yxy*h ).dist + 
                      k.xxx*map( p + k.xxx*h ).dist );
}

Ray castRay(vec3 ro, vec3 rd){
    float dist = 0.;
    vec3 p = ro;
    int steps=0;
    for(;dist<MaxDist;steps++){
        Map map = map(p);
        if (map.dist<MinDist){
            return Ray(p,dist,steps,map.material,calcNormal(p));
        }
        dist += map.dist;
        p += rd * map.dist;
    }
    return Ray(p,dist,steps,NONE,vec3(0));
}
vec3 skyColor(vec3 rd){
    vec3 col = vec3(0.30, 0.36, 0.60) - (rd.y * 0.7);
    // sun glare    
    // float glare = clamp( dot(sun,rd), 0.0, 1.0 );
    // col += 0.25*vec3(1.0,0.4,0.2)*pow( glare, 4.0 );
    return col;
}

vec3 diffuseColor(Ray ray){
    vec3 lv = -sun;
    vec3 ro = ray.p+ ray.normal*2.f*MinDist;
    Ray r = castRay(ro,lv);
    float b = (dot(ray.normal,lv));

    if(r.dist<MaxDist) b*= 0.2;
    b = max(b,0.);
    vec3 sunC = vec3(0.9, 0.9, 0.8) *b;
    vec3 ambient = vec3(0.03, 0.04, 0.1);

    vec3 col = ray.material.color * (sunC+ambient);

    return col;
}

vec3 applyRoughness(Ray r){
    float alpha = r.material.roughness;

    vec2 rand = hash23(r.p);
    float epsilon1 = rand.x;
    float epsilon2 = rand.y;

    float thetam = atan(alpha*sqrt(epsilon1),sqrt(1.-epsilon1));
    float phim = TWO_PI * epsilon2 * alpha;

    float phin = atan(r.normal.y,r.normal.x);
    float thetan = acos(r.normal.z);

    float theta = thetan + thetam;
    float phi = phin + phim;

    vec3 m = vec3(
        sin(theta) * cos(phi),
        sin(theta) * sin(phi),
        cos(theta)
    );

    return normalize(m);
}

vec3 getRefl(in Ray r,vec3 rd){
    float factor = 1.f;
    vec3 colorFactor = normalize(vec3(1));
    vec3 col = vec3(0);
    while (length(colorFactor)>reflectionThreshhold){
        if(r.material == NONE){
            col += colorFactor * skyColor(rd);
            break;
        }
        vec3 normal = applyRoughness(r);
        // r.normal = normal;
        vec3 colDiff = diffuseColor(r);
        col += colDiff * colorFactor;

        colorFactor *= r.material.reflectance * r.material.color;

        float s = dot(normal,rd);
        // the new reflected rd
        rd = normalize(rd - 2. * s * normal);

        r = castRay(r.p+r.normal*MinDist*2.f,rd);
    }
    return col;
}

vec3 color(vec3 ro,vec3 rd, out Ray ray){
    ray = castRay(ro,rd);
    //Sky
    if (ray.material==NONE){
        return skyColor(rd);
    }
    // Material Based
    return getRefl(ray,rd);
}

void colorCorection(inout vec3 col){
    // gamma correction
    col = pow(col,vec3(gamma));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ){
    vec2 o = hash2( float(iFrame) ) - 0.5;

    vec2 uv = (-iResolution.xy + 2.0*(fragCoord+o))/ iResolution.y;

    float th1 = (iMouse.x-.5*iResolution.x)/iResolution.x*8.f;
    float th2 = ((-iResolution.y+2.*iMouse.y)/iResolution.y+0.5)*2.;

    // uv.y+= asin(th2);

    vec3 ro = vec3(0,2,-4);

    ro *=  mat3(
        vec3( cos(th1), 0 , sin(th1)),
        vec3(       0, 1 , 0      ),
        vec3(-sin(th1), 0 , cos(th1))
    );

    vec3 ta = vec3(0,th2,0);
    mat3 ca = setCamera(ro,ta,0.);

    vec3 rd = ca * normalize( vec3(uv.xy,zoom));

    Ray r;
    vec3 col = clamp(color(ro,rd,r),0.f,1.f); 

    colorCorection(col);

    mat4 oldCam = mat4( textureLod(iChannel0,vec2(0.5,0.5)/iResolution.xy, 0.0),
                        textureLod(iChannel0,vec2(1.5,0.5)/iResolution.xy, 0.0),
                        textureLod(iChannel0,vec2(2.5,0.5)/iResolution.xy, 0.0),
                        0.0, 0.0, 0.0, 1.0 );

    // world space
    vec4 wpos = vec4(ro + rd*r.dist,1.0);
    // camera space
    vec3 cpos = (wpos*oldCam).xyz; // note inverse multiply
    // ndc space
    vec2 npos = zoom * cpos.xy / cpos.z;
    // screen space
    vec2 spos = 0.5 + 0.5*npos*vec2(iResolution.y/iResolution.x,1.0);
    // undo dither
    spos -= o/iResolution.xy;
    // raster space
    vec2 rpos = spos * iResolution.xy;

    if( rpos.y<1.0 && rpos.x<3.0 )
    {
    }
    else
    {
        vec3 ocol = textureLod( iChannel0, spos, 0.0 ).xyz;
        if( iFrame==0 ) ocol = col;
        col = mix( ocol, col, clamp(exp(-blend),0.,1.) );
    }

    if( fragCoord.y<1.0 && fragCoord.x<3.0 )
    {
        if( abs(fragCoord.x-2.5)<0.5 ) fragColor = vec4( ca[2], -dot(ca[2],ro) );
        if( abs(fragCoord.x-1.5)<0.5 ) fragColor = vec4( ca[1], -dot(ca[1],ro) );
        if( abs(fragCoord.x-0.5)<0.5 ) fragColor = vec4( ca[0], -dot(ca[0],ro) );
    }
    else
    {   
        fragColor = vec4( col, 1.0 );
    }
}
Hendiadyoin1 commented 3 years ago

Other shaders seem to work, so propably my shader, but no error output is given

Malacath-92 commented 3 years ago

Thanks for reporting this, I'll take a look at it on the weekend. Probably getting stuck in the parser because of some corner case.