From d48482ef273e0fc7544e76b1c2597d459efda50e Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Fri, 28 Dec 2018 10:49:04 +0800 Subject: [PATCH] fix gfx crash --- cocos/renderer/gfx/gfx-define.ts | 2 + cocos/renderer/gfx/gfx-device.ts | 9 +- cocos/renderer/gfx/index.js | 32 ++- cocos/renderer/gfx/webgl/webgl-commands.ts | 9 +- cocos/renderer/gfx/webgl/webgl-gfx-device.ts | 186 ++++++++++++++++-- cocos/renderer/gfx/webgl/webgl-gfx-window.ts | 8 + cocos/renderer/gfx/webgl/webgl-state-cache.ts | 31 ++- tsconfig.json | 2 +- 8 files changed, 249 insertions(+), 30 deletions(-) diff --git a/cocos/renderer/gfx/gfx-define.ts b/cocos/renderer/gfx/gfx-define.ts index 9599049256..b19ff66039 100644 --- a/cocos/renderer/gfx/gfx-define.ts +++ b/cocos/renderer/gfx/gfx-define.ts @@ -123,6 +123,7 @@ export const enum GFXFormat { // Depth-Stencil Format D16, + D16S8, D24, D24S8, D32F, @@ -253,6 +254,7 @@ export const GFXFormatInfos : GFXFormatInfo[] = [ { name : "RGB9E5", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, { name : "D16", size : 2, count : 1, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, + { name : "D16S8", size : 3, count : 2, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, { name : "D24", size : 3, count : 1, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, { name : "D24S8", size : 4, count : 2, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, { name : "D32F", size : 4, count : 1, isFloating : true, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, diff --git a/cocos/renderer/gfx/gfx-device.ts b/cocos/renderer/gfx/gfx-device.ts index 22f4284486..274f8f0830 100644 --- a/cocos/renderer/gfx/gfx-device.ts +++ b/cocos/renderer/gfx/gfx-device.ts @@ -13,8 +13,13 @@ import { GFXCommandBufferInfo, GFXCommandBuffer } from './gfx-command-buffer'; import { GFXCommandAllocatorInfo, GFXCommandAllocator } from './gfx-command-allocator'; import { GFXWindowInfo, GFXWindow } from './gfx-window'; -export class GFXDeviceInfo { - canvasElm: HTMLElement | null = null; +export const enum GFXFeature { +}; + +export interface GFXDeviceInfo { + canvasElm: HTMLElement; + isAntialias?: boolean; + isPremultipliedAlpha?: boolean; }; export abstract class GFXDevice { diff --git a/cocos/renderer/gfx/index.js b/cocos/renderer/gfx/index.js index 61fcaa8754..4cd2f5e244 100644 --- a/cocos/renderer/gfx/index.js +++ b/cocos/renderer/gfx/index.js @@ -15,7 +15,21 @@ import TextureCube from './texture-cube'; import RenderBuffer from './render-buffer'; import FrameBuffer from './frame-buffer'; import Device from './device'; -import { WebGLGFXDevice } from '../../../dist/webgl/webgl-gfx-device'; + +import { GFXDevice } from '../../../dist/cocos/renderer/gfx/gfx-device'; +import { GFXBuffer } from '../../../dist/cocos/renderer/gfx/gfx-buffer'; +import { GFXTexture } from '../../../dist/cocos/renderer/gfx/gfx-texture'; +import { GFXTextureView } from '../../../dist/cocos/renderer/gfx/gfx-texture-view'; +import { GFXSampler } from '../../../dist/cocos/renderer/gfx/gfx-sampler'; +import { GFXShader } from '../../../dist/cocos/renderer/gfx/gfx-shader'; +import { GFXInputAssembler } from '../../../dist/cocos/renderer/gfx/gfx-input-assembler'; +import { GFXRenderPass } from '../../../dist/cocos/renderer/gfx/gfx-render-pass'; +import { GFXFramebuffer } from '../../../dist/cocos/renderer/gfx/gfx-framebuffer'; +import { GFXPipelineLayout } from '../../../dist/cocos/renderer/gfx/gfx-pipeline-layout'; +import { GFXPipelineState } from '../../../dist/cocos/renderer/gfx/gfx-pipeline-state'; +import { GFXCommandBuffer } from '../../../dist/cocos/renderer/gfx/gfx-command-buffer'; +import { GFXQueue } from '../../../dist/cocos/renderer/gfx/gfx-queue'; +import { WebGLGFXDevice } from '../../../dist/cocos/renderer/gfx/webgl/webgl-gfx-device'; let gfx = { // classes @@ -29,7 +43,6 @@ let gfx = { RenderBuffer, FrameBuffer, Device, - WebGLGFXDevice, // functions attrTypeBytes, @@ -40,3 +53,18 @@ Object.assign(gfx, enums); export default gfx; cc.gfx = gfx; + +cc.GFXDevice = GFXDevice; +cc.GFXBuffer = GFXBuffer; +cc.GFXTexture = GFXTexture; +cc.GFXTextureView = GFXTextureView; +cc.GFXSampler = GFXSampler; +cc.GFXShader = GFXShader; +cc.GFXInputAssembler = GFXInputAssembler; +cc.GFXRenderPass = GFXRenderPass; +cc.GFXFramebuffer = GFXFramebuffer; +cc.GFXPipelineLayout = GFXPipelineLayout; +cc.GFXPipelineState = GFXPipelineState; +cc.GFXCommandBuffer = GFXCommandBuffer; +cc.GFXQueue = GFXQueue; +cc.WebGLGFXDevice = WebGLGFXDevice; diff --git a/cocos/renderer/gfx/webgl/webgl-commands.ts b/cocos/renderer/gfx/webgl/webgl-commands.ts index cecff14aae..83445367cd 100644 --- a/cocos/renderer/gfx/webgl/webgl-commands.ts +++ b/cocos/renderer/gfx/webgl/webgl-commands.ts @@ -12,7 +12,7 @@ import { WebGLTexUnit } from "./webgl-state-cache"; import { GFXBufferTextureCopy } from "../gfx-command-buffer"; import { GFXTextureFlagBit } from "../gfx-texture"; -function GFXFormatToWebGLType(format: GFXFormat): GLenum { +function GFXFormatToWebGLType(format: GFXFormat, device : WebGLGFXDevice): GLenum { switch (format) { case GFXFormat.R8: return WebGLRenderingContext.UNSIGNED_BYTE; case GFXFormat.R8SN: return WebGLRenderingContext.BYTE; @@ -69,8 +69,9 @@ function GFXFormatToWebGLType(format: GFXFormat): GLenum { case GFXFormat.RGB9E5: return WebGLRenderingContext.UNSIGNED_BYTE; case GFXFormat.D16: return WebGLRenderingContext.UNSIGNED_SHORT; + case GFXFormat.D16S8: return WebGLRenderingContext.UNSIGNED_SHORT; case GFXFormat.D24: return WebGLRenderingContext.UNSIGNED_INT; - case GFXFormat.D24S8: return WebGLRenderingContext.UNSIGNED_INT; + case GFXFormat.D24S8: return device.WEBGL_depth_texture? device.WEBGL_depth_texture.UNSIGNED_INT_24_8_WEBGL : WebGLRenderingContext.UNSIGNED_INT; case GFXFormat.D32F: return WebGLRenderingContext.FLOAT; case GFXFormat.D32F_S8: return WebGLRenderingContext.FLOAT; @@ -125,6 +126,7 @@ function GFXFormatToWebGLInternalFormat(format: GFXFormat): GLenum { case GFXFormat.RGB5A1: return WebGLRenderingContext.RGB5_A1; case GFXFormat.RGBA4: return WebGLRenderingContext.RGBA4; case GFXFormat.D16: return WebGLRenderingContext.DEPTH_COMPONENT16; + case GFXFormat.D16S8: return WebGLRenderingContext.DEPTH_STENCIL; case GFXFormat.D24: return WebGLRenderingContext.DEPTH_COMPONENT; case GFXFormat.D24S8: return WebGLRenderingContext.DEPTH_STENCIL; case GFXFormat.D32F: return WebGLRenderingContext.DEPTH_COMPONENT; @@ -164,6 +166,7 @@ function GFXFormatToWebGLFormat(format: GFXFormat): GLenum { case GFXFormat.RGB5A1: return WebGLRenderingContext.RGBA; case GFXFormat.RGBA4: return WebGLRenderingContext.RGBA; case GFXFormat.D16: return WebGLRenderingContext.DEPTH_COMPONENT; + case GFXFormat.D16S8: return WebGLRenderingContext.DEPTH_STENCIL; case GFXFormat.D24: return WebGLRenderingContext.DEPTH_COMPONENT; case GFXFormat.D24S8: return WebGLRenderingContext.DEPTH_STENCIL; case GFXFormat.D32F: return WebGLRenderingContext.DEPTH_COMPONENT; @@ -590,7 +593,7 @@ export function WebGLCmdFuncCreateTexture(device: WebGLGFXDevice, gpuTexture: We gpuTexture.glInternelFmt = GFXFormatToWebGLInternalFormat(gpuTexture.format); gpuTexture.glFormat = GFXFormatToWebGLFormat(gpuTexture.format); - gpuTexture.glType = GFXFormatToWebGLType(gpuTexture.format); + gpuTexture.glType = GFXFormatToWebGLType(gpuTexture.format, device); switch (gpuTexture.viewType) { case GFXTextureViewType.TV2D: { diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-device.ts b/cocos/renderer/gfx/webgl/webgl-gfx-device.ts index c4ff704be8..4e135ad191 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-device.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-device.ts @@ -62,8 +62,7 @@ export class WebGLGFXDevice extends GFXDevice { stateCache: WebGLStateCache = new WebGLStateCache; //webGLCmdAllocator: WebGLGFXCommandAllocator; - constructor(canvasEL: HTMLElement) { - + constructor() { super(); //this.webGLCmdAllocator = new WebGLGFXCommandAllocator(this); @@ -71,21 +70,29 @@ export class WebGLGFXDevice extends GFXDevice { public initialize(info: GFXDeviceInfo): boolean { - this._canvasElm = info.canvasElm; + let canvasElm = info.canvasElm; + + if (info.isAntialias) { + this._isAntialias = info.isAntialias; + } + + if (info.isPremultipliedAlpha) { + this._isPremultipliedAlpha = info.isPremultipliedAlpha; + } try { let webGLCtxAttribs: WebGLContextAttributes = { alpha: true, - antialias: true, + antialias: this._isAntialias, depth: true, stencil: true, - premultipliedAlpha: true, + premultipliedAlpha: this._isPremultipliedAlpha, preserveDrawingBuffer: false, powerPreference: "default", failIfMajorPerformanceCaveat: false, }; - this._webGLRC = this._canvasElm.getContext('webgl', webGLCtxAttribs); + this._webGLRC = canvasElm.getContext('webgl', webGLCtxAttribs); } catch (err) { console.error(err); return false; @@ -95,16 +102,58 @@ export class WebGLGFXDevice extends GFXDevice { // Tested through ios baidu browser 4.14.1 if (!this._webGLRC) { console.error('This device does not support WebGL.'); + return false; } + console.info('WebGL device initialization successed.'); + this._deviceName = "WebGL"; + this._WEBGL_debug_renderer_info = this._webGLRC.getExtension("WEBGL_debug_renderer_info"); + if (this._WEBGL_debug_renderer_info) { + let renderer = this._webGLRC.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_RENDERER_WEBGL); + console.info("RENDERER: " + renderer); + + let vendor = this._webGLRC.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_VENDOR_WEBGL); + console.info("VENDOR: " + vendor); + } + + this._extensions = this._webGLRC.getSupportedExtensions(); + /* + if (this._extensions) { + for (let i = 0; i < this._extensions.length; ++i) { + console.info(this._extensions[i]); + } + } + */ + + this._EXT_texture_filter_anisotropic = this._webGLRC.getExtension("EXT_texture_filter_anisotropic"); + this._EXT_frag_depth = this._webGLRC.getExtension("EXT_frag_depth"); + this._EXT_shader_texture_lod = this._webGLRC.getExtension("EXT_shader_texture_lod"); + this._EXT_sRGB = this._webGLRC.getExtension("EXT_sRGB"); + this._OES_vertex_array_object = this._webGLRC.getExtension("OES_vertex_array_object"); + this._WEBGL_color_buffer_float = this._webGLRC.getExtension("WEBGL_color_buffer_float"); + this._WEBGL_compressed_texture_astc = this._webGLRC.getExtension("WEBGL_compressed_texture_astc"); + this._WEBGL_compressed_texture_s3tc = this._webGLRC.getExtension("WEBGL_compressed_texture_s3tc"); + this._WEBGL_compressed_texture_s3tc_srgb = this._webGLRC.getExtension("WEBGL_compressed_texture_s3tc_srgb"); + this._WEBGL_debug_shaders = this._webGLRC.getExtension("WEBGL_debug_shaders"); + this._WEBGL_draw_buffers = this._webGLRC.getExtension("WEBGL_draw_buffers"); + this._WEBGL_lose_context = this._webGLRC.getExtension("WEBGL_lose_context"); + this._WEBGL_depth_texture = this._webGLRC.getExtension("WEBGL_depth_texture"); + this._OES_texture_half_float = this._webGLRC.getExtension("OES_texture_half_float"); + this._OES_texture_half_float_linear = this._webGLRC.getExtension("OES_texture_half_float_linear"); + this._OES_texture_float = this._webGLRC.getExtension("OES_texture_float"); + this._OES_standard_derivatives = this._webGLRC.getExtension("OES_standard_derivatives"); + this._OES_element_index_uint = this._webGLRC.getExtension("OES_element_index_uint"); + this._ANGLE_instanced_arrays = this._webGLRC.getExtension("ANGLE_instanced_arrays"); + + // create primary window this._primaryWindow = this.createWindow({ - title: this._canvasElm.title, - left: this._canvasElm.offsetLeft, - top: this._canvasElm.offsetTop, - width: this._canvasElm.width, - height: this._canvasElm.height, + title: this._webGLRC.canvas.title, + left: this._webGLRC.canvas.offsetLeft, + top: this._webGLRC.canvas.offsetTop, + width: this._webGLRC.drawingBufferWidth, + height: this._webGLRC.drawingBufferHeight, }); return true; @@ -117,7 +166,7 @@ export class WebGLGFXDevice extends GFXDevice { this._primaryWindow = null; } - this._canvasElm = null; + this._webGLRC = null; } public createBuffer(info: GFXBufferInfo): GFXBuffer | null { @@ -697,6 +746,117 @@ export class WebGLGFXDevice extends GFXDevice { // WebGLCmdFuncDestroyInputAssembler(this, gpuInputAssembler); } + public get isAntialias(): boolean { + return this._isAntialias; + } + + public get isPremultipliedAlpha(): boolean { + return this._isPremultipliedAlpha; + } + + public get EXT_texture_filter_anisotropic(): EXT_texture_filter_anisotropic | null { + return this._EXT_texture_filter_anisotropic; + } + + public get EXT_frag_depth(): EXT_frag_depth | null { + return this._EXT_frag_depth; + } + + public get EXT_shader_texture_lod(): EXT_shader_texture_lod | null { + return this._EXT_shader_texture_lod; + } + + public get EXT_sRGB(): EXT_sRGB | null { + return this._EXT_sRGB; + } + + public get OES_vertex_array_object(): OES_vertex_array_object | null { + return this._OES_vertex_array_object; + } + + public get WEBGL_color_buffer_float(): WEBGL_color_buffer_float | null { + return this._WEBGL_color_buffer_float; + } + + public get WEBGL_compressed_texture_astc(): WEBGL_compressed_texture_astc | null { + return this._WEBGL_compressed_texture_astc; + } + + public get WEBGL_compressed_texture_s3tc(): WEBGL_compressed_texture_s3tc | null { + return this._WEBGL_compressed_texture_s3tc; + } + + public get WEBGL_compressed_texture_s3tc_srgb(): WEBGL_compressed_texture_s3tc_srgb | null { + return this._WEBGL_compressed_texture_s3tc_srgb; + } + + public get WEBGL_debug_shaders(): WEBGL_debug_shaders | null { + return this._WEBGL_debug_shaders; + } + + public get WEBGL_draw_buffers(): WEBGL_draw_buffers | null { + return this._WEBGL_draw_buffers; + } + + public get WEBGL_lose_context(): WEBGL_lose_context | null { + return this._WEBGL_lose_context; + } + + public get WEBGL_depth_texture(): WEBGL_depth_texture | null { + return this._WEBGL_depth_texture; + } + + public get WEBGL_debug_renderer_info(): WEBGL_debug_renderer_info | null { + return this._WEBGL_debug_renderer_info; + } + + public get OES_texture_half_float(): OES_texture_half_float | null { + return this._OES_texture_half_float; + } + + public get OES_texture_half_float_linear(): OES_texture_half_float_linear | null { + return this._OES_texture_half_float_linear; + } + + public get OES_texture_float(): OES_texture_float | null { + return this._OES_texture_float; + } + + public get OES_standard_derivatives(): OES_standard_derivatives | null { + return this._OES_standard_derivatives; + } + + public get OES_element_index_uint(): OES_element_index_uint | null { + return this._OES_element_index_uint; + } + + public get ANGLE_instanced_arrays(): ANGLE_instanced_arrays | null { + return this._ANGLE_instanced_arrays; + } + private _webGLRC: WebGLRenderingContext | null = null; - private _canvasElm: HTMLCanvasElement | null = null; + private _isAntialias: boolean = true; + private _isPremultipliedAlpha: boolean = false; + + private _extensions: string[] | null = null; + private _EXT_texture_filter_anisotropic: EXT_texture_filter_anisotropic | null = null; + private _EXT_frag_depth: EXT_frag_depth | null = null; + private _EXT_shader_texture_lod: EXT_shader_texture_lod | null = null; + private _EXT_sRGB: EXT_sRGB | null = null; + private _OES_vertex_array_object: OES_vertex_array_object | null = null; + private _WEBGL_color_buffer_float: WEBGL_color_buffer_float | null = null; + private _WEBGL_compressed_texture_astc: WEBGL_compressed_texture_astc | null = null; + private _WEBGL_compressed_texture_s3tc: WEBGL_compressed_texture_s3tc | null = null; + private _WEBGL_compressed_texture_s3tc_srgb: WEBGL_compressed_texture_s3tc_srgb | null = null; + private _WEBGL_debug_shaders: WEBGL_debug_shaders | null = null; + private _WEBGL_draw_buffers: WEBGL_draw_buffers | null = null; + private _WEBGL_lose_context: WEBGL_lose_context | null = null; + private _WEBGL_depth_texture: WEBGL_depth_texture | null = null; + private _WEBGL_debug_renderer_info: WEBGL_debug_renderer_info | null = null; + private _OES_texture_half_float: OES_texture_half_float | null = null; + private _OES_texture_half_float_linear: OES_texture_half_float_linear | null = null; + private _OES_texture_float: OES_texture_float | null = null; + private _OES_standard_derivatives: OES_standard_derivatives | null = null; + private _OES_element_index_uint: OES_element_index_uint | null = null; + private _ANGLE_instanced_arrays: ANGLE_instanced_arrays | null = null; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-window.ts b/cocos/renderer/gfx/webgl/webgl-gfx-window.ts index 52a70146a4..a1d837a028 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-window.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-window.ts @@ -5,6 +5,7 @@ import { GFXFramebufferInfo } from '../gfx-framebuffer'; import { GFXRenderPassInfo, GFXColorAttachment, GFXLoadOp, GFXStoreOp, GFXTextureLayout, GFXPipelineBindPoint, GFXDepthStencilAttachment } from '../gfx-render-pass'; import { GFXTextureType, GFXTextureUsageBit, GFXTextureFlagBit } from '../gfx-texture'; import { GFXTextureViewType } from '../gfx-texture-view'; +import { GFXFormat } from '../gfx-define'; export class WebGLGFXWindow extends GFXWindow { @@ -28,6 +29,13 @@ export class WebGLGFXWindow extends GFXWindow { this._width = info.width; this._height = info.height; + this._colorFmt = GFXFormat.RGBA8; + + if(this.webGLDevice.WEBGL_depth_texture) { + this._depthStencilFmt = GFXFormat.D24S8; + } else { + this._depthStencilFmt = GFXFormat.D16S8; + } this._renderPass = this._device.createRenderPass({ colorAttachment: [{ diff --git a/cocos/renderer/gfx/webgl/webgl-state-cache.ts b/cocos/renderer/gfx/webgl/webgl-state-cache.ts index 353bec8c35..23f65a6f32 100644 --- a/cocos/renderer/gfx/webgl/webgl-state-cache.ts +++ b/cocos/renderer/gfx/webgl/webgl-state-cache.ts @@ -1,16 +1,16 @@ import { GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUTES } from "../gfx-define"; import { GFXRasterizerState, GFXDepthStencilState, GFXBlendState } from "../gfx-pipeline-state"; -export class WebGLTexUnit +export interface WebGLTexUnit { - glTexture : WebGLTexture = 0; - minFilter : GLenum = WebGLRenderingContext.NONE; - magFilter : GLenum = WebGLRenderingContext.NONE; - wrapS : GLenum = WebGLRenderingContext.NONE; - wrapT : GLenum = WebGLRenderingContext.NONE; - wrapR : GLenum = WebGLRenderingContext.NONE; - baseLevel : number = 0; - maxLevel : number = 1000; + glTexture : WebGLTexture; + minFilter : GLenum; + magFilter : GLenum; + wrapS : GLenum; + wrapT : GLenum; + wrapR : GLenum; + baseLevel : number; + maxLevel : number; }; export class WebGLStateCache { @@ -38,6 +38,19 @@ export class WebGLStateCache { this.glEnabledAttribLocs = new Array(GFX_MAX_VERTEX_ATTRIBUTES); this.glCurrentAttribLocs = new Array(GFX_MAX_VERTEX_ATTRIBUTES); + for(let i = 0; i < GFX_MAX_TEXTURE_UNITS; ++i) { + this.glTex2DUnits[i] = { + glTexture : 0, + minFilter : WebGLRenderingContext.NONE, + magFilter : WebGLRenderingContext.NONE, + wrapS : WebGLRenderingContext.NONE, + wrapT : WebGLRenderingContext.NONE, + wrapR : WebGLRenderingContext.NONE, + baseLevel : 0, + maxLevel : 1000, + } + } + for(let i = 0; i < GFX_MAX_VERTEX_ATTRIBUTES; ++i) { this.glEnabledAttribLocs[i] = false; this.glCurrentAttribLocs[i] = false; diff --git a/tsconfig.json b/tsconfig.json index 6e5dea8439..fa152bcee8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist", /* Redirect output structure to the directory. */ - // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */