English 中文(简体)
如果阴影永远不会改变, 是否有办法缓存慢速 WebGL 阴影编译?
原标题:Is there a way to cache slow WebGL shader compilation if the shader never changes?
  • 时间:2012-05-23 14:05:30
  •  标签:
  • shader
  • webgl

我有一个WebGL 阴影器, 当我有这个功能时, 它会立即编集感知( Windows 7 上的 Chronome) :

void main(void) {
    if (antialias_level == 1)
        gl_FragColor = NewtonIteration((gl_FragCoord.xy + offset) / zoom);
    else if (antialias_level == 2)
        gl_FragColor = (NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.25, -0.25)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.25, 0.25)) / zoom)) * 0.5;
}

但编纂这篇文章需要很长的时间(~ 10秒) :

void main(void) {
    if (antialias_level == 1)
        gl_FragColor = NewtonIteration((gl_FragCoord.xy + offset) / zoom);
    else if (antialias_level == 2)
        gl_FragColor = (NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.25, -0.25)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.25, 0.25)) / zoom)) * 0.5;
    else if (antialias_level == 4)
        gl_FragColor = (NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.25, -0.25)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.25, 0.25)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.25, -0.25)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.25, 0.25)) / zoom)) * 0.25;
    else if (antialias_level == 9)
        gl_FragColor = (NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.33, -0.33)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.0, -0.33)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.33, -0.33)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.33, 0.0)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.0, 0.0)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.33, 0.0)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.33, 0.33)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.0, 0.33)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.33, 0.33)) / zoom)) * 0.111111111;
    else if (antialias_level == 16)
        gl_FragColor = (NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.375, -0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.125, -0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.125, -0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.375, -0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.375, -0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.125, -0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.125, -0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.375, -0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.375, 0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.125, 0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.125, 0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.375, 0.125)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.375, 0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(-0.125, 0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.125, 0.375)) / zoom) + NewtonIteration((gl_FragCoord.xy + offset + vec2(0.375, 0.375)) / zoom)) * 0.0625;
}

是否有办法将 WebGL 编译结果缓存到二进制中, 并加载到二进制中, 或者没有帮助? 我假设长时间的延迟与将遮罩从 OpenGL 转换为 DirectX 有关 。

(""http://jnm2.com/newton/"rel="nofollow">这里有一个活生生的例子 。)

最佳回答

我并不知道为WebGL预设隐形阴影的任何办法;提供这种特征可能会有许多可移动性和安全问题。

antialias_level 是整个场景常态的用户选择参数吗? 您可能最好为当前所选的级别而不是所有可能的级别编集一个遮光镜; 这在运行时会更有效,而且能更快地编集。

在将您的阴影源与您的 JS 代码分隔开来的同时,一个自定义遮罩器的简单方法就是在编译前先将 #define ANTIALIAS_level " + level 改为 < defaine ANTIALIAS_develment " + level , 在您的遮罩源中使用 来选择合适的情况。 但是,由于您的 main () 代码非常系统化, 可能值得按逻辑生成它。

问题回答

不,不可能先于遮阴器,那会是一个巨大的安全洞

另一方面,两种浏览器都有可能 在未来会开始为您在引擎盖下 开始刮焦阴影

Here s the issue for Chrome http://code.google.com/p/chromium/issues/detail?id=88572

我不确定Firefox或其他浏览器是否有类似的浏览器。





相关问题
IronPython: Trouble building a WPF ShaderEffect

I m trying to build an extensible program where users, among other things, can build their own shader effects. Google searching got me this far; class Test(ShaderEffect): inputProperty = ...

Making Photoshop-like drop shadows in a game

I m making a game where the game s size varies, so I want to make my own shadows. The api i m using can fill rectangles, make ellipses, horizontal lines etc. And supports rgba. Given this, how could I ...

How to change a GLSL shader parameter in Processing

I m playing with shaders in openGL using Processing. I m pretty noob at this and a bit lost. I found this thread that has an example on how to use GLSL shaders in Processing. I m just trying to the ...

Shader limitations

I ve been tuning my game s renderer for my laptop, which has a Radeon HD 3850. This chip has a decent amount of processing power, but rather limited memory bandwidth, so I ve been trying to move more ...

XNA .Fbx textures

I m using the standard .fbx importer with custom shaders in XNA. The .fbx model is UV wrapped properly and is textured appropriately when I use BasicEffect. However when I use my custom effect I have ...

Is it possible to use a pixel shader inside a sprite?

Is it possible to use a pixel shader inside a sprite? I have create a simple pixel shader, that just writes red color, for testing. I have surrounded my Sprite.DrawImage(tex,...) call by the effect....

WPF ShaderEffect with PS 3.0 shaders

在什么地方,我可以找到用作WPF 4的ShaderEffects的PS 3.0 棚户区的例子? I m 寻找这方面的辅导、介绍和任何资源。

热门标签