mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
modify copyBufferToTexture(), and add copyImageSourceToTexture()
This commit is contained in:
+8
-2
@@ -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 <HTMLCanvasElement>this._canvas;
|
||||
}
|
||||
|
||||
public get canvas2D(): HTMLCanvasElement {
|
||||
return <HTMLCanvasElement>this._canvas2D;
|
||||
}
|
||||
|
||||
public get queue(): GFXQueue {
|
||||
return <GFXQueue>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 = "";
|
||||
|
||||
@@ -114,6 +114,5 @@ export abstract class GFXInputAssembler {
|
||||
protected _vertexOffset: number = 0;
|
||||
protected _instanceCount: number = 0;
|
||||
protected _firstInstance: number = 0;
|
||||
|
||||
protected _isIndirect: boolean = false;
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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(<WebGLGFXDevice>this, bufferView, (<WebGLGFXTexture>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(<WebGLGFXDevice>this, bufferView, (<WebGLGFXTexture>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(<WebGLGFXDevice>this, bufferView, (<WebGLGFXTexture>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) {
|
||||
|
||||
@@ -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(<GFXFramebuffer>this._framebuffer, this._renderArea, this._clearColors, this._clearDepth, this._clearStencil);
|
||||
cmdBuff.bindPipelineState(<GFXPipelineState>this._pipelineState);
|
||||
cmdBuff.bindBindingLayout(<GFXBindingLayout>this._bindingLayout);
|
||||
cmdBuff.bindInputAssembler(<GFXInputAssembler>this._pipeline.quadIA);
|
||||
cmdBuff.draw(<GFXInputAssembler>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, <GFXBuffer>this._ubo);
|
||||
this._bindingLayout.bindTextureView(10, <GFXTextureView>this._texView);
|
||||
this._bindingLayout.bindSampler(10, <GFXSampler>this._sampler);
|
||||
|
||||
@@ -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[] = [];
|
||||
};
|
||||
Reference in New Issue
Block a user