From fb126fca025867f95bc3390a4c5dda3d4290ba05 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 25 Feb 2019 10:41:11 +0800 Subject: [PATCH] ubo can work correctly in webgl2 --- cocos/3d/builtin/effects.js | 17 ++++---- cocos/core/game.js | 4 +- cocos/gfx/define.ts | 2 +- cocos/gfx/device.ts | 5 +++ cocos/gfx/webgl/webgl-commands.ts | 6 ++- cocos/gfx/webgl2/webgl2-commands.ts | 47 +++++++++++++--------- cocos/gfx/webgl2/webgl2-device.ts | 5 +++ cocos/pipeline/forward/forward-pipeline.ts | 2 +- cocos/pipeline/render-pipeline.ts | 26 ++++++------ cocos/renderer/core/pass.ts | 3 +- cocos/renderer/ui/ui.ts | 6 +-- 11 files changed, 73 insertions(+), 50 deletions(-) diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 363901517f..3c545cf855 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -72,17 +72,17 @@ export default [ { "name": "builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "glsl3": { - "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`, + "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 u_matViewProj;\n};\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = u_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 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "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`, + "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 u_matViewProj;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = u_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": [ - {"name": "ui", "size": 64, "defines": [], "binding": 0, "members": [ - {"name":"cc_matViewProj", "type":26, "count":1, "size":64} + {"name": "UI", "size": 64, "defines": [], "binding": 0, "members": [ + {"name":"u_matViewProj", "type":26, "count":1, "size":64} ]} ], "samplers": [ @@ -94,16 +94,16 @@ export default [ }, { "name": "builtin-effect-unlit", - "techniques": [{"passes":[{"program":"builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "properties":{"color":{"type":17, "value":[1, 1, 1, 1]}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "mainTexture":{"type":29, "value":"grey"}}}]}], + "techniques": [{"passes":[{"program":"builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "properties":{"color":{"type":17, "value":[1, 1, 1, 1]}, "tilingAndOffset":{"type":16, "value":[1, 1, 0, 0]}, "mainTexture":{"type":29, "value":"grey"}}}]}], "shaders": [ { "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "glsl3": { - "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_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\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`, + "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_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\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 vec4 tilingAndOffset;\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 * tilingAndOffset.xy + tilingAndOffset.zw;\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 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "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_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\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`, + "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_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\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 vec4 tilingAndOffset;\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 * tilingAndOffset.xy + tilingAndOffset.zw;\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": [ @@ -114,8 +114,7 @@ export default [ ], "blocks": [ {"name": "TexCoords", "size": 16, "defines": ["USE_TEXTURE"], "binding": 0, "members": [ - {"name":"mainTiling", "type":14, "count":1, "size":8}, - {"name":"mainOffset", "type":14, "count":1, "size":8} + {"name":"tilingAndOffset", "type":16, "count":1, "size":16} ]}, {"name": "Constant", "size": 16, "defines": ["USE_COLOR"], "binding": 1, "members": [ {"name":"color", "type":16, "count":1, "size":16} diff --git a/cocos/core/game.js b/cocos/core/game.js index 3638d11d2c..753e2bebaf 100644 --- a/cocos/core/game.js +++ b/cocos/core/game.js @@ -765,16 +765,14 @@ var game = { { // todo, adjust opts here - this._gfxDevice = new cc.WebGLGFXDevice; + // this._gfxDevice = new cc.WebGLGFXDevice; - /* let gl2 = localCanvas.getContext('webgl2'); if (!gl2) { this._gfxDevice = new cc.WebGLGFXDevice; } else { this._gfxDevice = new cc.WebGL2GFXDevice; } - */ this._gfxDevice.initialize({ canvasElm : localCanvas, diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index 37e50dfda0..6c580647cb 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -21,7 +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 const GFX_MAX_BUFFER_BINDINGS: number = 24; export enum GFXObjectType { UNKNOWN, diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index 63a3052f8a..86985b102b 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -105,6 +105,10 @@ export abstract class GFXDevice { return this._maxUniformBlockSize; } + public get maxCombinedUniformBlocks (): number { + return this._maxCombinedUniformBlocks; + } + public get depthBits (): number { return this._depthBits; } @@ -141,6 +145,7 @@ export abstract class GFXDevice { protected _maxVertexTextureUnits: number = 0; protected _maxUniformBufferBindings: number = GFX_MAX_BUFFER_BINDINGS; protected _maxUniformBlockSize: number = 0; + protected _maxCombinedUniformBlocks: number = GFX_MAX_BUFFER_BINDINGS; protected _depthBits: number = 0; protected _stencilBits: number = 0; protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 9f9072f15c..1db91a306c 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -663,7 +663,11 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: Web export function WebGLCmdFuncUpdateBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer, buffer: GFXBufferSource, offset: number, size: number) { if (gpuBuffer.usage & GFXBufferUsageBit.UNIFORM) { - gpuBuffer.vf32!.set(buffer as Float32Array, offset); + if (buffer instanceof Float32Array) { + gpuBuffer.vf32!.set(buffer, offset); + } else { + gpuBuffer.vf32!.set(new Float32Array(buffer as ArrayBuffer), offset); + } } else if (gpuBuffer.usage & GFXBufferUsageBit.INDIRECT) { gpuBuffer.indirects = (buffer as IGFXIndirectBuffer).drawInfos; } else { diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index fd2ec8d68f..b52d9c900f 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -847,8 +847,15 @@ export function WebGL2CmdFuncUpdateBuffer (device: WebGL2GFXDevice, gpuBuffer: W device.stateCache.glUniformBuffer = gpuBuffer.glBuffer; } + let buf: Float32Array; + if (buffer instanceof Float32Array) { + buf = buffer; + } else { + buf = new Float32Array(buffer as ArrayBuffer); + } + if (size === buff.byteLength) { - gl.bufferSubData(gpuBuffer.glTarget, offset, buff); + gl.bufferSubData(gpuBuffer.glTarget, offset, buf); } else { gl.bufferSubData(gpuBuffer.glTarget, offset, buff.slice(0, size)); } @@ -1163,48 +1170,50 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W // create uniform blocks const activeBlockCount = gl.getProgramParameter(gpuShader.glProgram, gl.ACTIVE_UNIFORM_BLOCKS); let blockName: string; - // let blockIdx: number; + let blockIdx: number; let blockSize: number; - let uBlock: GFXUniformBlock | null; + let blockBinding: number; let blockUniformCount: number; let uIndices: Uint32Array; let indices: number[]; - let glUniformTypes: GLenum[]; + // let glUniformTypes: GLenum[]; let glUniformSizes: GLenum[]; let glUniformOffsets: GLenum[]; + // let glUniformArrayStride: GLint[]; let glUniformInfo: WebGLActiveInfo | null; if (activeBlockCount) { gpuShader.glBlocks = new Array(activeBlockCount); - for (let i = 0; i < activeBlockCount; ++i) { + for (let b = 0; b < activeBlockCount; ++b) { - blockName = gl.getActiveUniformBlockName(gpuShader.glProgram, i)!; + blockName = gl.getActiveUniformBlockName(gpuShader.glProgram, b)!; const nameOffset = blockName.indexOf('['); if (nameOffset !== -1) { blockName = blockName.substr(0, nameOffset); } // blockIdx = gl.getUniformBlockIndex(gpuShader.glProgram, blockName); - - uBlock = null; + blockBinding = -1; for (const block of gpuShader.blocks) { if (block.name === blockName) { - uBlock = block; + blockBinding = block.binding; break; } } - if (uBlock) { - gl.uniformBlockBinding(gpuShader.glProgram, i, uBlock.binding); + if (blockBinding >= 0) { + // blockIdx = gl.getUniformBlockIndex(gpuShader.glProgram, blockName); + blockIdx = b; + blockSize = gl.getActiveUniformBlockParameter(gpuShader.glProgram, blockIdx, gl.UNIFORM_BLOCK_DATA_SIZE); + blockUniformCount = gl.getActiveUniformBlockParameter(gpuShader.glProgram, blockIdx, gl.UNIFORM_BLOCK_ACTIVE_UNIFORMS); - blockSize = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_DATA_SIZE); - blockUniformCount = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_ACTIVE_UNIFORMS); + gl.uniformBlockBinding(gpuShader.glProgram, blockIdx, blockBinding); const glBlock: WebGL2GPUUniformBlock = { - binding: uBlock.binding, - idx: i, + binding: blockBinding, + idx: blockIdx, name: blockName, size: blockSize, glUniforms: new Array(blockUniformCount), @@ -1212,17 +1221,18 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W isUniformPackage: false, }; - gpuShader.glBlocks[i] = glBlock; + gpuShader.glBlocks[b] = glBlock; - uIndices = gl.getActiveUniformBlockParameter(gpuShader.glProgram, i, gl.UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES); + uIndices = gl.getActiveUniformBlockParameter(gpuShader.glProgram, blockIdx, 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); + // 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); + // glUniformArrayStride = gl.getActiveUniforms(gpuShader.glProgram, indices, gl.UNIFORM_ARRAY_STRIDE); for (let u = 0; u < blockUniformCount; ++u) { glUniformInfo = gl.getActiveUniform(gpuShader.glProgram, uIndices[u]); @@ -1829,6 +1839,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W if (glBlock.binding === gpuBinding.binding) { if (device.stateCache.glBindUBOs[glBlock.binding] !== gpuBinding.gpuBuffer.glBuffer) { gl.bindBufferBase(WebGL2RenderingContext.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer); + // gl.bindBufferRange(WebGL2RenderingContext.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer, 0, gpuBinding.gpuBuffer.size); device.stateCache.glBindUBOs[glBlock.binding] = gpuBinding.gpuBuffer.glBuffer; } diff --git a/cocos/gfx/webgl2/webgl2-device.ts b/cocos/gfx/webgl2/webgl2-device.ts index 09c57ec8cb..41ad4a4f93 100644 --- a/cocos/gfx/webgl2/webgl2-device.ts +++ b/cocos/gfx/webgl2/webgl2-device.ts @@ -154,7 +154,11 @@ export class WebGL2GFXDevice extends GFXDevice { 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._maxCombinedUniformBlocks = gl.getParameter(WebGL2RenderingContext.MAX_COMBINED_UNIFORM_BLOCKS); this._depthBits = gl.getParameter(WebGL2RenderingContext.DEPTH_BITS); + this._stencilBits = gl.getParameter(WebGL2RenderingContext.STENCIL_BITS); + // let maxVertexUniformBlocks = gl.getParameter(WebGL2RenderingContext.MAX_VERTEX_UNIFORM_BLOCKS); + // let maxFragmentUniformBlocks = gl.getParameter(WebGL2RenderingContext.MAX_FRAGMENT_UNIFORM_BLOCKS); // let uboOffsetAlignment = gl.getParameter(WebGL2RenderingContext.UNIFORM_BUFFER_OFFSET_ALIGNMENT); this._width = this._canvas.width; @@ -195,6 +199,7 @@ export class WebGL2GFXDevice extends GFXDevice { 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('MAX_COMBINED_UNIFORM_BLOCKS: ' + this._maxCombinedUniformBlocks); console.info('DEPTH_BITS: ' + this._depthBits); console.info('STENCIL_BITS: ' + this._stencilBits); // console.info('UNIFORM_BUFFER_OFFSET_ALIGNMENT: ' + uboOffsetAlignment); diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index f0de635561..4f1c35eb26 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -29,7 +29,7 @@ export class UBOForwardLights { public static SIZE: number = UBOForwardLights.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 29, name: 'CCForwardLights', members: [ + binding: 22, name: 'CCForwardLights', members: [ { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS }, diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index f4db81fa41..d613430b2f 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -16,20 +16,20 @@ import { RenderView } from './render-view'; export class UBOGlobal { public static TIME_OFFSET: number = 0; - public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4; - public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4; - public static MAT_VIEW_OFFSET: number = UBOGlobal.SCREEN_SCALE_OFFSET + 4; - public static MAT_VIEW_INV_OFFSET: number = UBOGlobal.MAT_VIEW_OFFSET + 16; - public static MAT_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16; - public static MAT_PROJ_INV_OFFSET: number = UBOGlobal.MAT_PROJ_OFFSET + 16; - public static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16; - public static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16; - public static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16; - public static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4; + public static SCREEN_SIZE_OFFSET: number = 4; + public static SCREEN_SCALE_OFFSET: number = 8; + public static MAT_VIEW_OFFSET: number = 12; + public static MAT_VIEW_INV_OFFSET: number = 28; + public static MAT_PROJ_OFFSET: number = 44; + public static MAT_PROJ_INV_OFFSET: number = 60; + public static MAT_VIEW_PROJ_OFFSET: number = 76; + public static MAT_VIEW_PROJ_INV_OFFSET: number = 92; + public static CAMERA_POS_OFFSET: number = 108; + public static COUNT: number = 112; public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 30, name: 'CCGlobal', members: [ + binding: 20, name: 'CCGlobal', members: [ // constants { name: 'cc_time', type: GFXType.FLOAT4, count: 1 }, { name: 'cc_screenSize', type: GFXType.FLOAT4, count: 1 }, @@ -55,7 +55,7 @@ export class UBOLocal { public static SIZE: number = UBOLocal.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 31, name: 'CCLocal', members: [ + binding: 21, name: 'CCLocal', members: [ { name: 'cc_matWorld', type: GFXType.MAT4, count: 1 }, { name: 'cc_matWorldIT', type: GFXType.MAT4, count: 1 }, ], @@ -338,7 +338,7 @@ export abstract class RenderPipeline { this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); // update ubos - this._globalUBO!.update(this._uboGlobal.view); + this._globalUBO!.update(this._uboGlobal.view.buffer); } protected sceneCulling (view: RenderView) { diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 31a319ba2e..1a8fec24a4 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -157,7 +157,8 @@ export class Pass { else { console.error('create buffer failed.'); return; } // buffer data processing system const block: IBlock = this._blocks[u.binding] = { - buffer: buffer.buffer as ArrayBuffer, + // buffer: buffer.buffer as ArrayBuffer, + buffer: new ArrayBuffer(u.size), dirty: false, views: [], }; diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index c3ddb2dead..2c75d96a6b 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -36,12 +36,12 @@ import { UIMaterial } from './ui-material'; export class UBOUI { public static MAT_VIEW_PROJ_OFFSET: number = 0; - public static COUNT: number = UBOUI.MAT_VIEW_PROJ_OFFSET + 16; + public static COUNT: number = 16; public static SIZE: number = UBOUI.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 26, name: 'CCUI', members: [ - { name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 }, + binding: 26, name: 'UI', members: [ + { name: 'u_matViewProj', type: GFXType.MAT4, count: 1 }, ], };