diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 2cb860cf9d..c7fc956cfa 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -7,12 +7,12 @@ export default [ { "name": "builtin-effect-particle-add|particle-vs-legacy:lpvs_main|particle-add-fs", "glsl3": { - "vert": "#version 300 es\nuniform Constants {\n vec2 frameTile;\n vec2 mainTiling;\n vec2 mainOffset;\n float velocityScale;\n float lengthScale;\n};\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", - "frag": "#version 300 es\n uniform sampler2D mainTexture;\n uniform FragConstants {\n vec4 tintColor;\n };\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n" + "vert": "precision mediump float;\nuniform Constants {\n vec2 frameTile;\n vec2 mainTiling;\n vec2 mainOffset;\n float velocityScale;\n float lengthScale;\n};\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nin vec3 a_position; \nin vec3 a_texCoord; \nin vec2 a_texCoord1; \nin vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nin vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", + "frag": "\n precision mediump float;\n uniform sampler2D mainTexture;\n uniform FragConstants {\n vec4 tintColor;\n };\n in vec2 uv;\n in vec4 color;\n out vec4 fragColor;\n void main () {\n \n fragColor = 2.0 * color * tintColor * texture(mainTexture, uv);\n }\n" }, "glsl1": { - "vert": "\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", - "frag": "\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n" + "vert": "precision mediump float;\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", + "frag": "\n precision mediump float;\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n" }, "defines": [ {"name":"USE_BILLBOARD", "type":"boolean", "defines":[]}, @@ -47,12 +47,12 @@ export default [ { "name": "builtin-effect-skybox|sky-vs|sky-fs", "glsl3": { - "vert": "#version 300 es\n attribute vec3 a_position;\n \nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", - "frag": "#version 300 es\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n" + "vert": "\n precision mediump float;\n in vec3 a_position;\n \nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\n out vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", + "frag": "\n precision mediump float;\n in vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n out vec4 fragColor;\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(texture(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n fragColor = vec4(c, 1.0);\n #else\n fragColor = texture(cubeMap, viewDir);\n #endif\n }\n" }, "glsl1": { - "vert": "\n attribute vec3 a_position;\n \n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", - "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n" + "vert": "\n precision mediump float;\n attribute vec3 a_position;\n \n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", + "frag": "\n precision mediump float;\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n" }, "defines": [ {"name":"USE_RGBE_CUBEMAP", "type":"boolean", "defines":[]} @@ -72,12 +72,12 @@ export default [ { "name": "builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "glsl3": { - "vert": "#version 300 es\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\nuniform ui {\n mat4 cc_matViewProj;\n};\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "#version 300 es\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + "vert": "\nprecision mediump float;\nin vec3 a_position;\nin vec2 a_texCoord;\nin vec4 a_color;\nout vec2 uv0;\nout vec4 color;\nuniform ui {\n mat4 cc_matViewProj;\n};\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nprecision mediump float;\nuniform sampler2D mainTexture;\nin vec2 uv0;\nin vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture(mainTexture, uv0);\n o *= color;\n return o;\n}\nout vec4 fragColor;\nvoid main() { fragColor = frag(); }\n" }, "glsl1": { - "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\n uniform mat4 cc_matViewProj;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + "vert": "\nprecision mediump float;\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\n uniform mat4 cc_matViewProj;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nprecision mediump float;\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" }, "defines": [], "blocks": [ @@ -99,12 +99,12 @@ export default [ { "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "glsl3": { - "vert": "#version 300 es\nattribute vec3 a_position;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform TexCoords {\n vec2 mainTiling;\n vec2 mainOffset;\n };\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform CCSkinning {\n mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "#version 300 es\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform Constant {\n vec4 color;\n };\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + "vert": "\nprecision mediump float;\nin vec3 a_position;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n#if USE_TEXTURE\n in vec2 a_texCoord;\n uniform TexCoords {\n vec2 mainTiling;\n vec2 mainOffset;\n };\n out vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nin vec4 a_weights;\nin vec4 a_joints;\nuniform CCSkinning {\n mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nprecision mediump float;\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n in vec2 uv0;\n#endif\n#if USE_COLOR\n uniform Constant {\n vec4 color;\n };\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nout vec4 fragColor;\nvoid main() { fragColor = frag(); }\n" }, "glsl1": { - "vert": "\nattribute vec3 a_position;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + "vert": "\nprecision mediump float;\nattribute vec3 a_position;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nprecision mediump float;\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" }, "defines": [ {"name":"USE_TEXTURE", "type":"boolean", "defines":[]}, diff --git a/cocos/3d/framework/model-component.ts b/cocos/3d/framework/model-component.ts index 139af8c67c..8ce8e79754 100644 --- a/cocos/3d/framework/model-component.ts +++ b/cocos/3d/framework/model-component.ts @@ -192,7 +192,7 @@ export class ModelComponent extends RenderableComponent { return; } - if (this._model) { + if (this._model && this._model.inited) { this._model.destroy(); } else { this._createModel(); diff --git a/cocos/core/game.js b/cocos/core/game.js index bb0e0262ae..5fdd044d06 100644 --- a/cocos/core/game.js +++ b/cocos/core/game.js @@ -766,6 +766,7 @@ var game = { { // todo, adjust opts here this._gfxDevice = new cc.WebGLGFXDevice; + /* let gl2 = localCanvas.getContext('webgl2'); if (!gl2) { diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 85c0151065..036a2256c6 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -176,7 +176,7 @@ export class Root { for (let i = 0; i < this._windows.length; ++i) { if (this._windows[i] === window) { window.destroy(); - this._windows.splice(i); + this._windows.splice(i, 1); return; } } @@ -200,7 +200,7 @@ export class Root { for (let i = 0; i < this._scenes.length; ++i) { if (this._scenes[i] === scene) { scene.destroy(); - this._scenes.splice(i); + this._scenes.splice(i, 1); return; } } @@ -230,7 +230,7 @@ export class Root { public destroyView (view: RenderView) { for (let i = 0; i < this._views.length; ++i) { if (this._views[i] === view) { - this._views.splice(i); + this._views.splice(i, 1); return; } } diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index edeb01452e..37e50dfda0 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -21,6 +21,7 @@ export enum WebGLEXT { export const GFX_MAX_VERTEX_ATTRIBUTES: number = 16; export const GFX_MAX_TEXTURE_UNITS: number = 16; export const GFX_MAX_ATTACHMENTS: number = 4; +export const GFX_MAX_BUFFER_BINDINGS: number = 32; export enum GFXObjectType { UNKNOWN, diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index ddad601dd7..ef6090d668 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -2,7 +2,7 @@ import { GFXBindingLayout, IGFXBindingLayoutInfo } from './binding-layout'; import { GFXBuffer, IGFXBufferInfo } from './buffer'; import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from './command-allocator'; import { GFXCommandBuffer, IGFXCommandBufferInfo } from './command-buffer'; -import { GFXBufferTextureCopy, GFXFormat } from './define'; +import { GFXBufferTextureCopy, GFXFormat, GFX_MAX_BUFFER_BINDINGS } from './define'; import { GFXFramebuffer, IGFXFramebufferInfo } from './framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from './input-assembler'; import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from './pipeline-layout'; @@ -97,6 +97,14 @@ export abstract class GFXDevice { return this._maxVertexTextureUnits; } + public get maxUniformBufferBindings (): number { + return this._maxUniformBufferBindings; + } + + public get maxUniformBlockSize (): number { + return this._maxUniformBlockSize; + } + public get depthBits (): number { return this._depthBits; } @@ -131,6 +139,8 @@ export abstract class GFXDevice { protected _maxFragmentUniformVectors: number = 0; protected _maxTextureUnits: number = 0; protected _maxVertexTextureUnits: number = 0; + protected _maxUniformBufferBindings: number = GFX_MAX_BUFFER_BINDINGS; + protected _maxUniformBlockSize: number = 0; protected _depthBits: number = 0; protected _stencilBits: number = 0; protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; diff --git a/cocos/gfx/webgl/webgl-buffer.ts b/cocos/gfx/webgl/webgl-buffer.ts index 3f60598413..3005328b1a 100644 --- a/cocos/gfx/webgl/webgl-buffer.ts +++ b/cocos/gfx/webgl/webgl-buffer.ts @@ -45,7 +45,6 @@ export class WebGLGFXBuffer extends GFXBuffer { stride: info.stride ? info.stride : 1, buffer: null, vf32: null, - uniforms: [], indirects: [], glTarget: 0, glBuffer: 0, diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 54ee945f48..2f41a8fa2d 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -562,6 +562,7 @@ export class WebGLCmdPackage { export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { const gl = device.gl; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { @@ -575,9 +576,7 @@ export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: Web device.stateCache.glArrayBuffer = gpuBuffer.glBuffer; } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, null); device.stateCache.glArrayBuffer = 0; } @@ -593,9 +592,7 @@ export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: Web device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, null); device.stateCache.glElementArrayBuffer = 0; } @@ -628,6 +625,7 @@ export function WebGLCmdFuncDestroyBuffer (device: WebGLGFXDevice, gpuBuffer: We export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { const gl = device.gl; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { @@ -635,9 +633,7 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: Web gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, null); device.stateCache.glArrayBuffer = 0; } else if (gpuBuffer.usage & GFXBufferUsageBit.INDEX) { @@ -646,9 +642,7 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: Web gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, null); device.stateCache.glElementArrayBuffer = 0; } else if (gpuBuffer.usage & GFXBufferUsageBit.UNIFORM) { @@ -682,13 +676,6 @@ export function WebGLCmdFuncUpdateBuffer (device: WebGLGFXDevice, gpuBuffer: Web gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glArrayBuffer = gpuBuffer.glBuffer; } - - if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); - } else { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); - } - break; } case WebGLRenderingContext.ELEMENT_ARRAY_BUFFER: { @@ -696,19 +683,19 @@ export function WebGLCmdFuncUpdateBuffer (device: WebGLGFXDevice, gpuBuffer: Web gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - - if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); - } else { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); - } - break; } default: { console.error('Unsupported GFXBufferType, update buffer failed.'); + return; } } + + if (size === buff.byteLength) { + gl.bufferSubData(gpuBuffer.glTarget, offset, buff); + } else { + gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); + } } } diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 5f7cff5931..76d4cfca4a 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -9,11 +9,8 @@ import { GFXFormatInfos, GFXFormatSize, GFXQueueType, - GFXTextureFlagBit, - GFXTextureType, - GFXTextureViewType, } from '../define'; -import { GFXDevice, GFXFeature, IGFXDeviceInfo, GFXAPI } from '../device'; +import { GFXAPI, GFXDevice, GFXFeature, IGFXDeviceInfo } from '../device'; import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler'; import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from '../pipeline-layout'; diff --git a/cocos/gfx/webgl/webgl-gpu-objects.ts b/cocos/gfx/webgl/webgl-gpu-objects.ts index a385f26b5b..d99f79e9e2 100644 --- a/cocos/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/gfx/webgl/webgl-gpu-objects.ts @@ -40,7 +40,6 @@ export class WebGLGPUBuffer { public glBuffer: WebGLBuffer = 0; public buffer: ArrayBuffer | null = null; public vf32: Float32Array | null = null; - public uniforms: IWebGLGPUUniformInfo[] = []; public indirects: IGFXDrawInfo[] = []; } diff --git a/cocos/gfx/webgl2/webgl2-buffer.ts b/cocos/gfx/webgl2/webgl2-buffer.ts index 22f006b9c0..b8a216964f 100644 --- a/cocos/gfx/webgl2/webgl2-buffer.ts +++ b/cocos/gfx/webgl2/webgl2-buffer.ts @@ -45,7 +45,6 @@ export class WebGL2GFXBuffer extends GFXBuffer { stride: info.stride ? info.stride : 1, buffer: null, vf32: null, - uniforms: [], indirects: [], glTarget: 0, glBuffer: 0, diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index ae9229d8d3..d1b49a6e4e 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -47,6 +47,7 @@ import { WebGL2GPUUniformSampler, } from './webgl2-gpu-objects'; import { IWebGL2TexUnit } from './webgl2-state-cache'; +import { GFXUniformBlock } from '../shader'; const _uniformValues = new Array(1024 * 4); @@ -740,9 +741,13 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.glBuffer); device.stateCache.glUniformBuffer = gpuBuffer.glBuffer; } - } - gpuBuffer.glTarget = WebGL2RenderingContext.NONE; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGL2RenderingContext.STATIC_DRAW : WebGL2RenderingContext.DYNAMIC_DRAW; + gl.bufferData(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.size, glUsage); + + gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, null); + device.stateCache.glUniformBuffer = 0; + } } else if (gpuBuffer.usage & GFXBufferUsageBit.INDIRECT) { gpuBuffer.glTarget = WebGL2RenderingContext.NONE; } else if (gpuBuffer.usage & GFXBufferUsageBit.TRANSFER_DST) { @@ -765,6 +770,7 @@ export function WebGL2CmdFuncDestroyBuffer (device: WebGL2GFXDevice, gpuBuffer: export function WebGL2CmdFuncResizeBuffer (device: WebGL2GFXDevice, gpuBuffer: WebGL2GPUBuffer) { const gl = device.gl; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGL2RenderingContext.STATIC_DRAW : WebGL2RenderingContext.DYNAMIC_DRAW; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { @@ -772,9 +778,7 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGL2RenderingContext.STATIC_DRAW : WebGL2RenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGL2RenderingContext.ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGL2RenderingContext.ARRAY_BUFFER, null); device.stateCache.glArrayBuffer = 0; } else if (gpuBuffer.usage & GFXBufferUsageBit.INDEX) { @@ -783,9 +787,7 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGL2RenderingContext.STATIC_DRAW : WebGL2RenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER, null); device.stateCache.glElementArrayBuffer = 0; } else if (gpuBuffer.usage & GFXBufferUsageBit.UNIFORM) { @@ -793,9 +795,7 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.glBuffer); } - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.DEVICE ? WebGL2RenderingContext.STATIC_DRAW : WebGL2RenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.size, glUsage); - gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, null); device.stateCache.glUniformBuffer = 0; } else if ((gpuBuffer.usage & GFXBufferUsageBit.INDIRECT) || @@ -822,13 +822,6 @@ export function WebGL2CmdFuncUpdateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glArrayBuffer = gpuBuffer.glBuffer; } - - if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); - } else { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); - } - break; } case WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER: { @@ -836,33 +829,26 @@ export function WebGL2CmdFuncUpdateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gl.bindBuffer(WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - - if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); - } else { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); - } - break; } case WebGL2RenderingContext.UNIFORM_BUFFER: { if (device.stateCache.glUniformBuffer !== gpuBuffer.glBuffer) { gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.glBuffer); - device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; + device.stateCache.glUniformBuffer = gpuBuffer.glBuffer; } - - if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); - } else { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); - } - break; } default: { console.error('Unsupported GFXBufferType, update buffer failed.'); + return; } } + + if (size === buff.byteLength) { + gl.bufferSubData(gpuBuffer.glTarget, offset, buff); + } else { + gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); + } } } @@ -1165,75 +1151,96 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W } // create uniform blocks - if (gpuShader.blocks.length > 0) { - gpuShader.glBlocks = new Array(gpuShader.blocks.length); - for (let i = 0; i < gpuShader.blocks.length; ++i) { - const block = gpuShader.blocks[i]; + const activeBlockCount = gl.getProgramParameter(gpuShader.glProgram, gl.ACTIVE_UNIFORM_BLOCKS); + let blockName: string; + // let blockIdx: number; + let blockSize: number; + let uBlock: GFXUniformBlock | null; - const glBlock: WebGL2GPUUniformBlock = { - binding: block.binding, - name: block.name, - size: 0, - glUniforms: new Array(block.members.length), - glActiveUniforms: [], - isUniformPackage: true, - }; + let blockUniformCount: number; + let uIndices: Uint32Array; + let indices: number[]; + let glUniformTypes: GLenum[]; + let glUniformSizes: GLenum[]; + let glUniformOffsets: GLenum[]; + let glUniformInfo: WebGLActiveInfo | null; - gpuShader.glBlocks[i] = glBlock; + if (activeBlockCount) { + gpuShader.glBlocks = new Array(activeBlockCount); - for (let u = 0; u < block.members.length; ++u) { - const uniform = block.members[u]; - const glType = GFXTypeToWebGLType(uniform.type); - const stride = WebGLGetTypeSize(glType); - const size = stride * uniform.count; - const begin = glBlock.size / 4; - const count = size / 4; - const array = new Array(count); - array.fill(0); + for (let i = 0; i < activeBlockCount; ++i) { - glBlock.glUniforms[u] = { - binding: -1, - name: uniform.name, - type: uniform.type, - stride, - count: uniform.count, - size, - offset: glBlock.size, - - glType, - glLoc: -1, - array, - begin, - isFirst: true, - }; - - glBlock.size += size; + blockName = gl.getActiveUniformBlockName(gpuShader.glProgram, i)!; + const nameOffset = blockName.indexOf('['); + if (nameOffset !== -1) { + blockName = blockName.substr(0, nameOffset); } - /* - glBlock.buffer = new ArrayBuffer(glBlock.size); + // blockIdx = gl.getUniformBlockIndex(gpuShader.glProgram, blockName); - for (const glUniform of glBlock.glUniforms) { - switch (glUniform.glType) { - case gl.BOOL: - case gl.BOOL_VEC2: - case gl.BOOL_VEC3: - case gl.BOOL_VEC4: - case gl.INT: - case gl.INT_VEC2: - case gl.INT_VEC3: - case gl.INT_VEC4: - case gl.SAMPLER_2D: - case gl.SAMPLER_CUBE: { - glUniform.vi32 = new Int32Array(glBlock.buffer); - break; - } - default: { - glUniform.vf32 = new Float32Array(glBlock.buffer); + uBlock = null; + for (const block of gpuShader.blocks) { + if (block.name === blockName) { + uBlock = block; + break; + } + } + + if (uBlock) { + gl.uniformBlockBinding(gpuShader.glProgram, i, uBlock.binding); + + blockSize = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_DATA_SIZE); + blockUniformCount = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_ACTIVE_UNIFORMS); + + const glBlock: WebGL2GPUUniformBlock = { + binding: uBlock.binding, + idx: i, + name: blockName, + size: blockSize, + glUniforms: new Array(blockUniformCount), + glActiveUniforms: [], + isUniformPackage: false, + }; + + gpuShader.glBlocks[i] = glBlock; + + uIndices = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES); + indices = new Array(uIndices.length); + for (let n = 0; n < uIndices.length; ++n) { + indices[n] = uIndices[n]; + } + + glUniformTypes = gl.getActiveUniforms(gpuShader.glProgram, indices, gl.UNIFORM_TYPE); + glUniformSizes = gl.getActiveUniforms(gpuShader.glProgram, indices, gl.UNIFORM_SIZE); + glUniformOffsets = gl.getActiveUniforms(gpuShader.glProgram, indices, gl.UNIFORM_OFFSET); + + for (let u = 0; u < blockUniformCount; ++u) { + glUniformInfo = gl.getActiveUniform(gpuShader.glProgram, uIndices[u]); + if (glUniformInfo) { + const stride = WebGLGetTypeSize(glUniformInfo.type); + const size = glUniformSizes[u] * stride; + const begin = glUniformOffsets[u] / 4; + const count = size / 4; + const array = new Array(count); + array.fill(0); + + glBlock.glUniforms[u] = { + binding: -1, + name: glUniformInfo.name, + type: WebGLTypeToGFXType(glUniformInfo.type), + stride, + count: glUniformInfo.size, + size, + offset: glUniformOffsets[u], + glType: glUniformInfo.type, + glLoc: -1, + array, + begin, + isFirst: true, + }; } } } - */ } } @@ -1276,25 +1283,7 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W const isSampler = (uniformInfo.type === WebGL2RenderingContext.SAMPLER_2D) || (uniformInfo.type === WebGL2RenderingContext.SAMPLER_CUBE); - if (!isSampler) { - // let stride = WebGLGetTypeSize(info.type); - - // build uniform block mapping - for (const glBlock of gpuShader.glBlocks) { - - for (const glUniform of glBlock.glUniforms) { - if (glUniform.name === varName) { - // let varSize = stride * info.size; - - glUniform.glLoc = glLoc; - glBlock.glActiveUniforms.push(glUniform); - - break; - } - } - } // for - } else { - + if (isSampler) { for (const glSampler of gpuShader.glSamplers) { if (glSampler.name === varName) { // let varSize = stride * uniformInfo.size; @@ -1826,11 +1815,11 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W case GFXBindingType.UNIFORM_BUFFER: { if (gpuBinding.gpuBuffer) { - for (const block of gpuShader.glBlocks) { - if (block.binding === gpuBinding.binding) { - if (device.stateCache.glBindUBO !== gpuBinding.gpuBuffer.glBuffer) { - gl.bindBufferBase(WebGL2RenderingContext.UNIFORM_BUFFER, block.binding, gpuBinding.gpuBuffer.glBuffer); - device.stateCache.glBindUBO = gpuBinding.gpuBuffer.glBuffer; + for (const glBlock of gpuShader.glBlocks) { + if (glBlock.binding === gpuBinding.binding) { + if (device.stateCache.glBindUBOs[glBlock.binding] !== gpuBinding.gpuBuffer.glBuffer) { + gl.bindBufferBase(WebGL2RenderingContext.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer); + device.stateCache.glBindUBOs[glBlock.binding] = gpuBinding.gpuBuffer.glBuffer; } break; diff --git a/cocos/gfx/webgl2/webgl2-device.ts b/cocos/gfx/webgl2/webgl2-device.ts index 7b02efc3d4..09c57ec8cb 100644 --- a/cocos/gfx/webgl2/webgl2-device.ts +++ b/cocos/gfx/webgl2/webgl2-device.ts @@ -1,9 +1,10 @@ +import * as WebGLDeveloperTools from 'webgl-debug'; import { GFXBindingLayout, IGFXBindingLayoutInfo } from '../binding-layout'; import { GFXBuffer, IGFXBufferInfo } from '../buffer'; import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator'; import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer'; import { GFXBufferTextureCopy, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType } from '../define'; -import { GFXDevice, GFXFeature, IGFXDeviceInfo, GFXAPI } from '../device'; +import { GFXAPI, GFXDevice, GFXFeature, IGFXDeviceInfo } from '../device'; import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler'; import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from '../pipeline-layout'; @@ -112,8 +113,8 @@ export class WebGL2GFXDevice extends GFXDevice { this._webGL2RC = this._canvas.getContext('webgl2', webGLCtxAttribs); /* - if (this._webGLRC && info.debug) { - this._webGLRC = WebGLDeveloperTools.makeDebugContext(this._webGLRC, this._onWebGLError.bind(this)); + if (this._webGL2RC && info.debug) { + this._webGL2RC = WebGLDeveloperTools.makeDebugContext(this._webGL2RC, this._onWebGLError.bind(this)); } */ } catch (err) { @@ -141,18 +142,20 @@ export class WebGL2GFXDevice extends GFXDevice { this._renderer = gl.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_RENDERER_WEBGL); this._vendor = gl.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_VENDOR_WEBGL); } else { - this._renderer = gl.getParameter(WebGLRenderingContext.RENDERER); - this._vendor = gl.getParameter(WebGLRenderingContext.VENDOR); + this._renderer = gl.getParameter(WebGL2RenderingContext.RENDERER); + this._vendor = gl.getParameter(WebGL2RenderingContext.VENDOR); } - this._version = gl.getParameter(WebGLRenderingContext.VERSION); - this._maxVertexAttributes = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_ATTRIBS); - this._maxVertexUniformVectors = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_UNIFORM_VECTORS); - this._maxFragmentUniformVectors = gl.getParameter(WebGLRenderingContext.MAX_FRAGMENT_UNIFORM_VECTORS); - this._maxTextureUnits = gl.getParameter(WebGLRenderingContext.MAX_TEXTURE_IMAGE_UNITS); - this._maxVertexTextureUnits = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_TEXTURE_IMAGE_UNITS); - this._depthBits = gl.getParameter(WebGLRenderingContext.DEPTH_BITS); - this._stencilBits = gl.getParameter(WebGLRenderingContext.STENCIL_BITS); + this._version = gl.getParameter(WebGL2RenderingContext.VERSION); + this._maxVertexAttributes = gl.getParameter(WebGL2RenderingContext.MAX_VERTEX_ATTRIBS); + this._maxVertexUniformVectors = gl.getParameter(WebGL2RenderingContext.MAX_VERTEX_UNIFORM_VECTORS); + this._maxFragmentUniformVectors = gl.getParameter(WebGL2RenderingContext.MAX_FRAGMENT_UNIFORM_VECTORS); + this._maxTextureUnits = gl.getParameter(WebGL2RenderingContext.MAX_TEXTURE_IMAGE_UNITS); + this._maxVertexTextureUnits = gl.getParameter(WebGL2RenderingContext.MAX_VERTEX_TEXTURE_IMAGE_UNITS); + this._maxUniformBufferBindings = gl.getParameter(WebGL2RenderingContext.MAX_UNIFORM_BUFFER_BINDINGS); + this._maxUniformBlockSize = gl.getParameter(WebGL2RenderingContext.MAX_UNIFORM_BLOCK_SIZE); + this._depthBits = gl.getParameter(WebGL2RenderingContext.DEPTH_BITS); + // let uboOffsetAlignment = gl.getParameter(WebGL2RenderingContext.UNIFORM_BUFFER_OFFSET_ALIGNMENT); this._width = this._canvas.width; this._height = this._canvas.height; @@ -190,8 +193,11 @@ export class WebGL2GFXDevice extends GFXDevice { console.info('MAX_FRAGMENT_UNIFORM_VECTORS: ' + this._maxFragmentUniformVectors); console.info('MAX_TEXTURE_IMAGE_UNITS: ' + this._maxTextureUnits); console.info('MAX_VERTEX_TEXTURE_IMAGE_UNITS: ' + this._maxVertexTextureUnits); + console.info('MAX_UNIFORM_BUFFER_BINDINGS: ' + this._maxUniformBufferBindings); + console.info('MAX_UNIFORM_BLOCK_SIZE: ' + this._maxUniformBlockSize); console.info('DEPTH_BITS: ' + this._depthBits); console.info('STENCIL_BITS: ' + this._stencilBits); + // console.info('UNIFORM_BUFFER_OFFSET_ALIGNMENT: ' + uboOffsetAlignment); this._extensions = gl.getSupportedExtensions(); /* @@ -467,4 +473,8 @@ export class WebGL2GFXDevice extends GFXDevice { gl.colorMask(true, true, true, true); gl.blendColor(0.0, 0.0, 0.0, 0.0); } + + private _onWebGLError (error: GLenum, functionName: string, ...args: any[]) { + throw new Error(`${WebGLDeveloperTools.glEnumToString(error)} was caused by call to ${functionName}`); + } } diff --git a/cocos/gfx/webgl2/webgl2-gpu-objects.ts b/cocos/gfx/webgl2/webgl2-gpu-objects.ts index 03f27a89e3..a62038bb78 100644 --- a/cocos/gfx/webgl2/webgl2-gpu-objects.ts +++ b/cocos/gfx/webgl2/webgl2-gpu-objects.ts @@ -40,7 +40,6 @@ export class WebGL2GPUBuffer { public glBuffer: WebGLBuffer = 0; public buffer: ArrayBuffer | null = null; public vf32: Float32Array | null = null; - public uniforms: IWebGL2GPUUniformInfo[] = []; public indirects: IGFXDrawInfo[] = []; } @@ -139,6 +138,7 @@ export interface IWebGL2GPUUniform { export class WebGL2GPUUniformBlock { public binding: number = -1; + public idx: number = 0; public name: string = ''; public size: number = 0; public glUniforms: IWebGL2GPUUniform[] = []; diff --git a/cocos/gfx/webgl2/webgl2-state-cache.ts b/cocos/gfx/webgl2/webgl2-state-cache.ts index fbdada9fc1..d92cc66fcc 100644 --- a/cocos/gfx/webgl2/webgl2-state-cache.ts +++ b/cocos/gfx/webgl2/webgl2-state-cache.ts @@ -1,4 +1,4 @@ -import { GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUTES, IGFXRect, IGFXViewport } from '../define'; +import { GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUTES, IGFXRect, IGFXViewport, GFX_MAX_BUFFER_BINDINGS } from '../define'; import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state'; export interface IWebGL2TexUnit { @@ -9,7 +9,7 @@ export class WebGL2StateCache { public glArrayBuffer: WebGLBuffer = 0; public glElementArrayBuffer: WebGLBuffer = 0; public glUniformBuffer: WebGLBuffer = 0; - public glBindUBO: WebGLBuffer = 0; + public glBindUBOs: WebGLBuffer[]; public texUnit: number = 0; public glTex2DUnits: IWebGL2TexUnit[]; public glTexCubeUnits: IWebGL2TexUnit[]; @@ -24,6 +24,9 @@ export class WebGL2StateCache { public glCurrentAttribLocs: boolean[]; constructor () { + this.glBindUBOs = new Array(GFX_MAX_BUFFER_BINDINGS); + this.glBindUBOs.fill(0); + this.glTex2DUnits = new Array(GFX_MAX_TEXTURE_UNITS); this.glTexCubeUnits = new Array(GFX_MAX_TEXTURE_UNITS); this.viewport = { left: 0.0, top: 0.0, width: 0.0, height: 0.0, minDepth: 0.0, maxDepth: 0.0 }; diff --git a/cocos/pipeline/forward/forward-flow.ts b/cocos/pipeline/forward/forward-flow.ts index fff7ab07d8..fa59ebbf41 100644 --- a/cocos/pipeline/forward/forward-flow.ts +++ b/cocos/pipeline/forward/forward-flow.ts @@ -1,7 +1,6 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; // import { TestStage } from "./test-stage"; -import { TestModelStage } from '../testcase/test-model-stage'; import { ForwardStage } from './forward-stage'; export enum ForwardStagePriority { diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 5a523a3a96..f69437a328 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -4,10 +4,10 @@ import { IBlockInfo, IBlockMember, IDefineInfo, ISamplerInfo, IShaderInfo } from import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define'; import { GFXAPI, GFXDevice } from '../../gfx/device'; import { GFXShader, GFXUniform, GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader'; +import { UBOForwardLights } from '../../pipeline/forward/forward-pipeline'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; import { SkinningUBO } from '../models/model-uniforms'; import { IDefineMap } from './effect'; -import { UBOForwardLights } from '../../pipeline/forward/forward-pipeline'; function _generateDefines ( device: GFXDevice, @@ -40,12 +40,10 @@ interface IProgramInfo extends IShaderInfo { } class ProgramLib { - protected _precision: string; protected _templates: Record; protected _cache: Record; constructor () { - this._precision = `precision highp float;\n`; this._templates = {}; this._cache = {}; } @@ -130,11 +128,15 @@ class ProgramLib { const tmpl = this._templates[name]; const customDef = _generateDefines(device, defines, tmpl.defines, tmpl.dependencies) + '\n'; - // const source = tmpl.glsl1; // temporary measure - const source = (device.gfxAPI === GFXAPI.WEBGL2 ? tmpl.glsl3 : tmpl.glsl1); - - const vert = customDef + this._precision + source.vert; - const frag = customDef + this._precision + source.frag; + let vert: string = ''; + let frag: string = ''; + if (device.gfxAPI === GFXAPI.WEBGL2) { + vert = `#version 300 es\n${customDef}\n${tmpl.glsl3.vert}`; + frag = `#version 300 es\n${customDef}\n${tmpl.glsl3.frag}`; + } else { + vert = `#version 100\n${customDef}\n${tmpl.glsl1.vert}`; + frag = `#version 100\n${customDef}\n${tmpl.glsl1.frag}`; + } const instanceName = Object.keys(defines).reduce((acc, cur) => defines[cur] ? `${acc}|${cur}` : acc, name); program = device.createShader({ diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 96ea742157..a11d428692 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -26,6 +26,78 @@ const _subMeshPool = new Pool(() => { */ export class Model { + set scene (scene: RenderScene) { + this._scene = scene; + this._id = this._scene.generateModelId(); + } + + get scene () { + return this._scene; + } + + get id () { + return this._id; + } + + get subModelNum () { + return this._subModels.length; + } + + get inited (): boolean { + return this._inited; + } + + set enabled (val) { + this._enabled = val; + } + + get enabled () { + return this._enabled; + } + + /** + * Get the hosting node of this camera + * @returns the hosting node + */ + get node () { + return this._node; + } + + /** + * Set the hosting node of this model + * @param {Node} node the hosting node + */ + set node (node: Node) { + this._node = node; + } + + get worldBounds () { + return this._worldBounds; + } + get modelBounds () { + return this._modelBounds; + } + + get viewID () { + return this._viewID; + } + + /** + * Set the user key + * @param {number} key + */ + set userKey (key: number) { + this._userKey = key; + } + + get uboLocal () { + return this._uboLocal; + } + + get localUBO () { + return this._localUBO; + } + protected _type: string = 'default'; protected _device: GFXDevice; private _scene: RenderScene; @@ -42,6 +114,7 @@ export class Model { private _matRefCount: Map; private _uboLocal: UBOLocal; private _localUBO: GFXBuffer | null; + private _inited: boolean; /** * Setup a default empty model @@ -56,6 +129,7 @@ export class Model { this._matRefCount = new Map(); this._uboLocal = new UBOLocal(); this._localUBO = null; + this._inited = false; } public destroy () { @@ -72,23 +146,7 @@ export class Model { this._subModels.splice(0); this._matPSORecord.clear(); this._matRefCount.clear(); - } - - set scene (scene: RenderScene) { - this._scene = scene; - this._id = this._scene.generateModelId(); - } - - get scene () { - return this._scene; - } - - get id () { - return this._id; - } - - get subModelNum () { - return this._subModels.length; + this._inited = false; } public getSubModel (idx: number) { @@ -141,41 +199,6 @@ export class Model { this._worldBounds = aabb.clone(this._modelBounds); } - set enabled (val) { - this._enabled = val; - } - - get enabled () { - return this._enabled; - } - - /** - * Get the hosting node of this camera - * @returns the hosting node - */ - get node () { - return this._node; - } - - /** - * Set the hosting node of this model - * @param {Node} node the hosting node - */ - set node (node: Node) { - this._node = node; - } - - get worldBounds () { - return this._worldBounds; - } - get modelBounds () { - return this._modelBounds; - } - - get viewID () { - return this._viewID; - } - public initSubModel (idx: number, subMeshData: IRenderingSubmesh, mat: Material) { if (this._subModels[idx] == null) { this._subModels[idx] = _subMeshPool.alloc(); @@ -186,6 +209,7 @@ export class Model { } this.allocatePSO(mat); this._subModels[idx].initialize(subMeshData, mat, this._matPSORecord.get(mat)!); + this._inited = true; } public setSubModelMesh (idx: number, subMeshData: IRenderingSubmesh) { @@ -240,6 +264,10 @@ export class Model { } } + protected _onCreatePSO (pso: GFXPipelineState) { + pso.pipelineLayout.layouts[0].bindBuffer(UBOLocal.BLOCK.binding, this.localUBO!); + } + private allocatePSO (mat: Material) { if (this._matRefCount.get(mat) == null) { this._matRefCount.set(mat, 1); @@ -257,24 +285,4 @@ export class Model { this._matRefCount.delete(mat); } } - - protected _onCreatePSO (pso: GFXPipelineState) { - pso.pipelineLayout.layouts[0].bindBuffer(UBOLocal.BLOCK.binding, this.localUBO!); - } - - /** - * Set the user key - * @param {number} key - */ - set userKey (key: number) { - this._userKey = key; - } - - get uboLocal () { - return this._uboLocal; - } - - get localUBO () { - return this._localUBO; - } } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 26c441c2c3..6851cba2a8 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -5,7 +5,7 @@ import { Root } from '../../core/root'; import { Mat4, Vec3 } from '../../core/value-types'; import { mat4, vec3 } from '../../core/vmath'; import { GFXPrimitiveMode } from '../../gfx/define'; -import { UBOGlobal } from '../../pipeline/render-pipeline'; +import { UBOForwardLights } from '../../pipeline/forward/forward-pipeline'; import { Layers } from '../../scene-graph/layers'; import { Node } from '../../scene-graph/node'; import { Camera, ICameraInfo } from './camera'; @@ -111,7 +111,7 @@ export class RenderScene { } public createPointLight (name: string, node: Node): Light | null { - if (this._pointLights.length >= UBOGlobal.MAX_POINT_LIGHTS) { return null; } + if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; } const light = new PointLight(this, node, name); this._pointLights.push(light); return light; @@ -127,7 +127,7 @@ export class RenderScene { } public createDirectionalLight (name: string, node: Node): Light | null { - if (this._directionalLights.length >= UBOGlobal.MAX_DIR_LIGHTS) { return null; } + if (this._directionalLights.length >= UBOForwardLights.MAX_DIR_LIGHTS) { return null; } const light = new DirectionalLight(this, node, name); this._directionalLights.push(light); return light; @@ -143,7 +143,7 @@ export class RenderScene { } public createSpotLight (name: string, node: Node): Light | null { - if (this._spotLights.length >= UBOGlobal.MAX_SPOT_LIGHTS) { return null; } + if (this._spotLights.length >= UBOForwardLights.MAX_SPOT_LIGHTS) { return null; } const light = new SpotLight(this, node, name); this._spotLights.push(light); return light; diff --git a/globals.d.ts b/globals.d.ts index 558d0b281c..4ae241f9d6 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -51,5 +51,10 @@ declare module "webgl-debug" { throwOnGLError: (err: GLenum, funcName: string, ...args: any[]) => void ): WebGLRenderingContext; + export function makeDebugContext( + webGL2RenderingContext: WebGL2RenderingContext, + throwOnGLError: (err: GLenum, funcName: string, ...args: any[]) => void + ): WebGL2RenderingContext; + export function glEnumToString(glEnum: GLenum): string; } \ No newline at end of file