From 911f1d9af890ba3b4c2cf28a96b2d83257b2fa87 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 7 Jan 2019 19:07:58 +0800 Subject: [PATCH] modify copyBufferToTexture(), and add copyImageSourceToTexture() --- cocos/gfx/device.ts | 10 ++++-- cocos/gfx/input-assembler.ts | 1 - cocos/gfx/webgl/webgl-commands.ts | 4 +-- cocos/gfx/webgl/webgl-device.ts | 54 +++++++++++++--------------- cocos/pipeline/forward/test-stage.ts | 27 +++++++++++--- cocos/pipeline/render-queue.ts | 51 ++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 38 deletions(-) create mode 100644 cocos/pipeline/render-queue.ts diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index d1e3f93686..e4a1fa5c62 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -13,7 +13,7 @@ import { GFXCommandBufferInfo, GFXCommandBuffer } from './command-buffer'; import { GFXCommandAllocatorInfo, GFXCommandAllocator } from './command-allocator'; import { GFXWindowInfo, GFXWindow } from './window'; import { GFXBindingLayoutInfo, GFXBindingLayout } from './binding-layout'; -import { GFXRect } from './define'; +import { GFXRect, GFXBufferTextureCopy } from './define'; export enum GFXFeature { }; @@ -45,12 +45,17 @@ export abstract class GFXDevice { public abstract createWindow(info: GFXWindowInfo): GFXWindow | null; public abstract present(); - public abstract copyBufferToTexture2D(buffer: ArrayBuffer, texture: GFXTexture, rect?: GFXRect); + public abstract copyBufferToTexture(buffer: ArrayBuffer, texture: GFXTexture, regions: GFXBufferTextureCopy[]); + public abstract copyImageSourceToTexture(source: CanvasImageSource, texture: GFXTexture, regions: GFXBufferTextureCopy[]); public get canvas(): HTMLCanvasElement { return this._canvas; } + public get canvas2D(): HTMLCanvasElement { + return this._canvas2D; + } + public get queue(): GFXQueue { return this._queue; } @@ -112,6 +117,7 @@ export abstract class GFXDevice { } protected _canvas: HTMLCanvasElement | null = null; + protected _canvas2D: HTMLCanvasElement | null = null; protected _deviceName: string = ""; protected _renderer: string = ""; protected _vendor: string = ""; diff --git a/cocos/gfx/input-assembler.ts b/cocos/gfx/input-assembler.ts index fe18052dd4..9bac9ff01f 100644 --- a/cocos/gfx/input-assembler.ts +++ b/cocos/gfx/input-assembler.ts @@ -114,6 +114,5 @@ export abstract class GFXInputAssembler { protected _vertexOffset: number = 0; protected _instanceCount: number = 0; protected _firstInstance: number = 0; - protected _isIndirect: boolean = false; }; diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 09748aedb5..b8cc6ef947 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -1987,7 +1987,7 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLCmd.COPY_BUFFER_TO_TEXTURE: { let cmd7 = cmdPackage.copyBufferToTextureCmds[cmdId]; if (cmd7.gpuBuffer && cmd7.gpuBuffer.viewUI8 && cmd7.gpuTexture) { - WebGLCmdFuncCopyBufferToTexture2D(device, cmd7.gpuBuffer.viewUI8, cmd7.gpuTexture, cmd7.regions); + WebGLCmdFuncCopyBufferToTexture(device, cmd7.gpuBuffer.viewUI8, cmd7.gpuTexture, cmd7.regions); } break; @@ -1996,7 +1996,7 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG } // for } -export function WebGLCmdFuncCopyBufferToTexture2D( +export function WebGLCmdFuncCopyBufferToTexture( device: WebGLGFXDevice, bufferView: Uint8Array, gpuTexture: WebGLGPUTexture, diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 5e28863b76..b418eec772 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -4,7 +4,7 @@ import { WebGLGFXBuffer } from './webgl-buffer'; import { WebGLGFXQueue } from './webgl-queue'; import { WebGLStateCache } from './webgl-state-cache'; import { WebGLGPUBuffer, WebGLGPUObjectType, WebGLGPUTexture, WebGLGPURenderPass, WebGLGPUFramebuffer, WebGLGPUTextureView, WebGLGPUShader, WebGLGPUShaderStage, WebGLGPUSampler, WebGLGPUInputAssembler, WebGLGPUPipelineState, WebGLGPUPipelineLayout, WebGLGPUBindingLayout as WebGLGPUBindingLayout, WebGLGPUBinding } from './webgl-gpu-objects'; -import { WebGLCmdFuncUpdateBuffer, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncCreateBuffer, WebGLCmdFuncDestroyTexture, WebGLCmdFuncCreateTexture, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncCreateShader, WebGLCmdFuncDestroyShader, WebGLCmdFuncCreateInputAssember, WebGLCmdFuncCopyBufferToTexture2D } from './webgl-commands'; +import { WebGLCmdFuncUpdateBuffer, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncCreateBuffer, WebGLCmdFuncDestroyTexture, WebGLCmdFuncCreateTexture, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncCreateShader, WebGLCmdFuncDestroyShader, WebGLCmdFuncCreateInputAssember, WebGLCmdFuncCopyBufferToTexture } from './webgl-commands'; import { GFXTexture, GFXTextureInfo } from '../texture'; import { GFXTextureViewInfo, GFXTextureView } from '../texture-view'; import { GFXRenderPassInfo, GFXRenderPass } from '../render-pass'; @@ -31,7 +31,7 @@ import { WebGLGFXFramebuffer } from './webgl-framebuffer'; import { WebGLGFXInputAssembler } from './webgl-input-assembler'; import { GFXWindow, GFXWindowInfo } from '../window'; import { WebGLGFXWindow } from './webgl-window'; -import { GFXBindingType, GFXFilter, GFXAddress, GFXTextureType, GFXTextureFlagBit, GFXTextureViewType, GFXBufferUsageBit, GFXQueueType, GFXFormat, GFXBufferTextureCopy, GFXMemoryUsageBit, GFXTextureLayout, GFXTextureSubres, GFXRect } from '../define'; +import { GFXBindingType, GFXFilter, GFXAddress, GFXTextureType, GFXTextureFlagBit, GFXTextureViewType, GFXBufferUsageBit, GFXQueueType, GFXFormat, GFXBufferTextureCopy, GFXMemoryUsageBit, GFXTextureLayout, GFXTextureSubres, GFXRect, GFXFormatInfos } from '../define'; import { WebGLGFXBindingLayout } from './webgl-binding-layout'; const WebGLPrimitives: GLenum[] = [ @@ -103,6 +103,8 @@ export class WebGLGFXDevice extends GFXDevice { return false; } + this._canvas2D = document.createElement('canvas'); + console.info('WebGL device initialized.'); this._deviceName = "WebGL"; @@ -382,34 +384,28 @@ export class WebGLGFXDevice extends GFXDevice { public present() { } - public copyBufferToTexture2D(buffer: ArrayBuffer, texture: GFXTexture, rect?: GFXRect) { - + public copyBufferToTexture(buffer: ArrayBuffer, texture: GFXTexture, regions: GFXBufferTextureCopy[]) { + let bufferView = new Uint8Array(buffer); + WebGLCmdFuncCopyBufferToTexture(this, bufferView, (texture).gpuTexture, regions); + } - let x, y, w, h; + public copyImageSourceToTexture(source: CanvasImageSource, texture: GFXTexture, regions: GFXBufferTextureCopy[]) { - if(rect !== undefined) { - x = rect.x; - y = rect.y; - w = rect.width; - h = rect.height; - } else { - x = 0; - y = 0; - w = texture.width; - h = texture.height; + if (this._canvas2D) { + let context = this._canvas2D.getContext('2d'); + + if (context) { + this._canvas2D.width = texture.width; + this._canvas2D.height = texture.height; + context.drawImage(source, 0, 0); + + let imgData = context.getImageData(0, 0, this._canvas2D.width, this._canvas2D.height); + let bufferView = new Uint8Array(imgData.data); + + WebGLCmdFuncCopyBufferToTexture(this, bufferView, (texture).gpuTexture, regions); + } } - - let region: GFXBufferTextureCopy = { - buffOffset: 0, - buffStride: 0, - buffTexHeight: 0, - texOffset: { x: x, y: y, z: 0 }, - texExtent: { width: w, height: h, depth: 1 }, - texSubres: new GFXTextureSubres, - }; - - WebGLCmdFuncCopyBufferToTexture2D(this, bufferView, (texture).gpuTexture, [region]); } public get gl(): WebGLRenderingContext { @@ -638,9 +634,9 @@ export class WebGLGFXDevice extends GFXDevice { let glMinFilter = WebGLRenderingContext.NONE; let glMagFilter = WebGLRenderingContext.NONE; - let minFilter = (info.minFilter !== undefined? info.minFilter : GFXFilter.LINEAR); - let magFilter = (info.magFilter !== undefined? info.magFilter : GFXFilter.LINEAR); - let mipFilter = (info.mipFilter !== undefined? info.mipFilter : GFXFilter.NONE); + let minFilter = (info.minFilter !== undefined ? info.minFilter : GFXFilter.LINEAR); + let magFilter = (info.magFilter !== undefined ? info.magFilter : GFXFilter.LINEAR); + let mipFilter = (info.mipFilter !== undefined ? info.mipFilter : GFXFilter.NONE); if (minFilter === GFXFilter.LINEAR || minFilter === GFXFilter.ANISOTROPIC) { if (mipFilter === GFXFilter.LINEAR || mipFilter === GFXFilter.ANISOTROPIC) { diff --git a/cocos/pipeline/forward/test-stage.ts b/cocos/pipeline/forward/test-stage.ts index 8a4f67b2e3..414c8fd43d 100644 --- a/cocos/pipeline/forward/test-stage.ts +++ b/cocos/pipeline/forward/test-stage.ts @@ -28,10 +28,13 @@ let _shader_vs = `#version 100 attribute vec2 a_position; attribute vec2 a_texCoord; +//uniform mat4 u_matWorldViewProj; + varying vec2 v_texCoord; void main() { gl_Position = vec4(a_position, 0.0, 1.0); + //gl_Position = u_matWorldViewProj * vec4(a_position, 0.0, 1.0); v_texCoord = a_texCoord; }`; @@ -87,6 +90,10 @@ export class TestStage extends RenderStage { name: "test", stages: [vsStage, fsStage], blocks: [{ + binding: 0, name: "Global", uniforms: [ + { name: "u_matWorldViewProj", type: GFXType.MAT4, count: 1 }, + ] + }, { binding: 5, name: "UBO", uniforms: [ { name: "u_color", type: GFXType.FLOAT4, count: 1 }, ] @@ -224,8 +231,8 @@ export class TestStage extends RenderStage { cmdBuff.beginRenderPass(this._framebuffer, this._renderArea, this._clearColors, this._clearDepth, this._clearStencil); cmdBuff.bindPipelineState(this._pipelineState); cmdBuff.bindBindingLayout(this._bindingLayout); - cmdBuff.bindInputAssembler(this._pipeline.quadIA); - cmdBuff.draw(this._pipeline.quadIA); + cmdBuff.bindInputAssembler(this._pipeline.quadIA); + cmdBuff.draw(this._pipeline.quadIA); cmdBuff.endRenderPass(); cmdBuff.end(); @@ -234,6 +241,7 @@ export class TestStage extends RenderStage { private loadTexture(image: HTMLImageElement): boolean { //let canvas2d = this._device.canvas.getContext("2d"); + /* let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); if (!context) { @@ -246,7 +254,7 @@ export class TestStage extends RenderStage { let imgData = context.getImageData(0, 0, image.width, image.height); let imgBuffer: ArrayBuffer = imgData.data; - //var buffer = image.currentSrc;//arraybuffer object + */ this._texture = this._device.createTexture({ type: GFXTextureType.TEX2D, @@ -271,7 +279,17 @@ export class TestStage extends RenderStage { return false; } - this._device.copyBufferToTexture2D(imgBuffer, this._texture); + let region: GFXBufferTextureCopy = { + buffOffset: 0, + buffStride: 0, + buffTexHeight: 0, + texOffset: { x: 0, y: 0, z: 0 }, + texExtent: { width: image.width, height: image.height, depth: 1 }, + texSubres: { baseMipLevel: 0, levelCount: 1, baseArrayLayer: 0, layerCount: 1 }, + }; + + this._device.copyImageSourceToTexture(image, this._texture, [region]); + //this._device.copyBufferToTexture(imgBuffer, this._texture, [region]); this._sampler = this._device.createSampler({ minFilter: GFXFilter.LINEAR, @@ -290,6 +308,7 @@ export class TestStage extends RenderStage { return false; } + this._bindingLayout.bindBuffer(0, this.pipeline.globalUBO); this._bindingLayout.bindBuffer(5, this._ubo); this._bindingLayout.bindTextureView(10, this._texView); this._bindingLayout.bindSampler(10, this._sampler); diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts new file mode 100644 index 0000000000..d33721abb1 --- /dev/null +++ b/cocos/pipeline/render-queue.ts @@ -0,0 +1,51 @@ +import { Pass } from "../renderer/core/pass"; +import Model from "../renderer/scene/model"; + +export class RenderItem { + hash: number; + model: Model; + pass: Pass; + + constructor(hash: number, model: Model, pass: Pass) { + this.hash = hash; + this.model = model; + this.pass = pass; + } +}; + +export class RenderQueue { + + constructor() { + } + + public add(model: Model, pass: Pass) { + let pso = pass.pipelineState; + + let isTransparent = pso.blendState.targets[0].blend; + + let hash: number = 0; + + if(!isTransparent) { + // Opaque objects are sorted by depth front to back, then by shader id. + hash = (0 << 63) | (pso.shader.id << 13); + + this._opaques.push({hash, model, pass}); + } else { + // Transparent objects are sorted by depth back to front, then by by shader id. + hash = (1 << 63) | (pso.shader.id << 13); + + this._transparents.push({hash, model, pass}); + } + } + + public get opaques(): RenderItem[] { + return this._opaques; + } + + public get transparents(): RenderItem[] { + return this._transparents; + } + + private _opaques: RenderItem[] = []; + private _transparents: RenderItem[] = []; +};