devwaker / angleproject

Automatically exported from code.google.com/p/angleproject
Other
0 stars 0 forks source link

Clamping GLSL array indices to array bounds cannot reliably be translated to HLSL #399

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Some THREE.js lighting is broken when in Canary 26.0.1386.0 (Official Build 
177362) canary.

http://alteredqualia.com/tmp/tests/angle-issues/angle_shader_loop_test.html

This is because the expressions used to clamp array indices cannot be compiled 
by D3DX. Example translated HLSL for the THREE.js shader referenced:

// ...
{for(int _i = 0; (_i < 2); (_i++))
{
{
float3 _lVector = normalize(_vPointLight[_webgl_int_clamp(_i, 0, 1)].xyz); // 
<-- HERE
float _lDistance = _vPointLight[_webgl_int_clamp(_i, 0, 1)].w;
float _dotProduct = dot(_normal, _lVector);
float _pointDiffuseWeight = max(_dotProduct, 0.0);
(_pointDiffuse += (((_diffuse * ar_pointLightColor[_webgl_int_clamp(_i, 0, 1)]) 
* _pointDiffuseWeight) * _lDistance));
float3 _pointHalfVector = normalize((_lVector + _viewPosition));
float _pointDotNormalHalf = max(dot(_normal, _pointHalfVector), 0.0);
float _pointSpecularWeight = (_specularStrength * max(pow(_pointDotNormalHalf, 
_shininess), 0.0));
(_pointSpecular += ((((_specular * ar_pointLightColor[_webgl_int_clamp(_i, 0, 
1)]) * _pointSpecularWeight) * _pointDiffuseWeight) * _lDistance));
}
;}
// ...

_webgl_int_clamp(_i, 0, 1) is not a valid array index.

I will disable this pass on Windows in Chromium.

Original issue reported on code.google.com by apatr...@chromium.org on 18 Jan 2013 at 1:29

GoogleCodeExporter commented 9 years ago
The error is:
error X5533: Relative address register must be aL register (first source param).

That is with optimization level 0. It compiles without error with optimization 
levels 1, 2 and 3. I think D3DX is not unrolling the loop, which would 
eliminate the need for relative addressing.

It also compiles without error at all optimization levels using shader models 
4.0 and 5.0 using backwards compatibility mode.

Original comment by apatr...@chromium.org on 18 Jan 2013 at 2:06

GoogleCodeExporter commented 9 years ago
haixia@ added an alternate strategy for implementing array index clamping using 
the clamp intrinsic rather than a user-defined function. I added an API to 
allow switching between the two, and also re-incremented the optimization level 
to 1, which seems to allow this functionality to be turned on reliably on 
Windows:

https://code.google.com/p/angleproject/source/detail?r=1733
https://code.google.com/p/angleproject/source/detail?r=1734

Original comment by kbr@chromium.org on 25 Jan 2013 at 12:55

GoogleCodeExporter commented 9 years ago

Original comment by kbr@chromium.org on 25 Jan 2013 at 12:57

GoogleCodeExporter commented 9 years ago
Array index clamping has been re-enabled in Chromium in 
https://code.google.com/p/chromium/issues/detail?id=172323 . Closing as fixed.

Original comment by kbr@chromium.org on 12 Feb 2013 at 3:14