WebGL æ¯ä¸ä¸ªå¤æç APIï¼é常æä»¬ä¸è½ææ¾çç¥éå®çæ¨èä½¿ç¨æ¹å¼ãè¯¥é¡µé¢æ¶µçäºåç§ä¸ä¸ç¥è¯ç建议ï¼ä¸ä» ä» æ¯å举åºä»ä¹è¯¥åï¼ä»ä¹ä¸è¯¥åï¼è¿æè¯¦ç»çè§£é为ä»ä¹è¦è¿æ ·åãä½ å¯ä»¥å°æ¬ææ¡£ä½ä¸ºæå¯¼ä½ éæ©çæ¹æ³ï¼ç¡®ä¿ä½ æ 论å¨ä½ç§æµè§å¨ä»¥å硬件ä¸é½ä½¿ç¨äºæ£ç¡®çæå·§ã
éè¦é¿å çäºæ#ifdef GL_ES
ï¼è½ç¶åè¾¹çä¸äºä¾å使ç¨äºè¿ä¸ªï¼è¿å¹¶æ å¿
è¦ï¼å 为è¿ä¸ªæ¡ä»¶å¤æå¨ WebGL shader ä¸å§ç»ä¸º trueãhighp
精度å°é»ç¢ä½ çå
å®¹å¨æäºæ§çç§»å¨è®¾å¤ä¸æ£ç¡®è¿è¡ãè¿éï¼ä½ å¯ä»¥ä½¿ç¨ mediump
ï¼ä½æ¯ï¼ä½ éè¦ç¥éï¼ç±äºå¨å¤§å¤ç§»å¨è®¾å¤ä¸ç精度丢失ï¼è¿ç»å¸¸å¯¼è´æ¸²æå¤±è´¥ï¼è¿å¨å
¸åç PC æºå䏿²¡æé®é¢ãé常æ¥è¯´ï¼å¨ vertex å fragment shader ä¸ä»
ä½¿ç¨ highp
ï¼é¤é shaders éè¿äºå大平å°çæµè¯ãä» Firefox 11 å¼å§ï¼WebGL ç getShaderPrecisionFormat()
彿°çå®ç°ï¼å
è®¸ä½ å¤æ highp
æ¯å¦å¾å°æ¯æï¼è¿èå
è®¸ä½ å¯ä»¥æ¥è¯¢å°å®é
ç精度ãgetParameter()
æ¹æ³ï¼è·åæ¤ç±»åæ°çèå´ï¼è¿äºæ°æ®åæ äºä½ ç客æ·ç«¯è½å¤æ¯æççå®åºç¨èå´ãä¾å¦ï¼ä½¿ç¨ webgl.getParameter(webgl.MAX_TEXTURE_SIZE)
å¯ä»¥æ¥è¯¢è®¾å¤ä¸æ¯æçæå¤§ç 2D 纹ç尺寸ãä» Firefox 10 å¼å§ï¼WebGL 屿§ webgl.min_capability_mode
åå¯ä»¥è¢«ç¨æ¥æµè¯æå°æ§è½æ¨¡å¼ä¸çå®é
表ç°ï¼ä»¥æµè¯å¯ç§»æ¤æ§ãwebgl.getParameter(webgl.MAX_VERTEX_TEXTURE_IMAGE_UNITS)
大äºé¶æ¶ï¼æè½ä½¿ç¨ vertex shaders ä¸ç纹çãç¶èï¼ç®åçç§»å¨ç¡¬ä»¶ä¸æ¯ä¸æ¯æç (è¯è
ï¼å¾
ç¡®è®¤ææ¡£æ¶ææ§)ãwebgl.disable-extensions
å
许模æååºåªäºæ©å±æ¯ä¸æ¯æçï¼ä»¥æµè¯å¯ç§»æ¤æ§ãOES_texture_float
æ©å±ï¼ä¹å¯è½ä¸æ¯ææ¸²ææµ®ç¹ç±»åæ°æ®ç纹çãé常ï¼è¿å¨å½åçç§»å¨ç¡¬ä»¶ä¸æ¯ä¸æ¯æç (è¯è
ï¼å¾
ç¡®è®¤ææ¡£æ¶ææ§)ãè¦æ£æ¥æ¯å¦æ¯ææµ®ç¹ç±»åæ°æ®ï¼å¿
é¡»è°ç¨ checkFramebufferStatus()
æ¥éªè¯ãAnything that requires syncing the CPU and GPU sides is potentially very slow, so if possible you should try to avoid doing that in your main rendering loops. This includes the following WebGL calls: getError()
, readPixels()
, and finish()
. WebGL getter calls such as getParameter()
and getUniformLocation()
should be considered slow too, so try to cache their results in a JavaScript variable.
Fewer, larger draw operations will improve performance. If you have 1000 sprites to paint, try to do it as a single drawArrays()
or drawElements()
call. You can draw degenerate (flat) triangles if you need to draw discontinuous objects as a single drawArrays()
call.
Fewer state changes will also improve performance. In particular, if you can pack multiple images into a single texture and select them by using the appropriate texture coordinates, that can help you do fewer texture binding changes, which improves performance.
Smaller textures perform better than larger ones. For this reason, mipmapping can be a performance win.
Simpler shaders perform better than complex ones. In particular, if you can remove an if
statement from a shader, that will make it run faster. Division and math functions like log()
should be considered expensive too.
normalize()
, dot()
and mix()
. The best advice in that regard is to use the built-in functions, rather than try to implement, for example, one's own version of a dot-product or linear interpolation, which may in fact compile to larger and less optimized machine code. Finally, it is important to keep in mind that GPUs are constructed to do complex mathematical calculations in hardware, and therefore, may support math functions, such as sin()
, cos()
and other, through dedicated machine instructions.Do as much as you can in the vertex shader, rather than in the fragment shader. Because, per rendering pass, fragment shaders run many more times than vertex shaders, any calculation that can be done on the vertices and then just interpolated among fragments is a performance boon (this interpolation is done "automagically" for you, through the fixed functionality rasterization phase of the OpenGL pipeline). For example, a simple animation of a textured surface can be achieved through a time-dependent transformation of texture coordinates (simplest case is to add a uniform vector to the texture coordinates attribute vector). If visually acceptable, one can transform the texture coordinates in the vertex shader rather than in the fragment shader, to get better performance.
Always have vertex attrib 0 array enabled. If you draw with vertex attrib 0 array disabled, you will force the browser to do complicated emulation when running on desktop OpenGL (e.g. on Mac OSX). This is because in desktop OpenGL, nothing gets drawn if vertex attrib 0 is not array-enabled. You can use bindAttribLocation()
to force a vertex attribute to use location 0
, and use enableVertexAttribArray()
to make it array-enabled.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4