diff --git a/examples/index.ts b/examples/index.ts index 1a4d9d4454609a1f6ec1f06496656cc81d6a0f97..2887de25a4492158719963e1658f86b8de59ac2d 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -10,7 +10,8 @@ const files = { "sample8", ], "test":[ - "fractalCube" + "fractalCube", + "RenderObjectChanges", ], "regl-examples": [ "basic", diff --git a/examples/src/WebGL2Samples/buffer_copy.ts b/examples/src/WebGL2Samples/buffer_copy.ts index 541e38656c63433576502d49e79ce741a0d5940e..2929efc93830d504ddb10f2349cd647128405e4f 100644 --- a/examples/src/WebGL2Samples/buffer_copy.ts +++ b/examples/src/WebGL2Samples/buffer_copy.ts @@ -1,5 +1,5 @@ -import { ICopyBufferToBuffer, IRenderPass, IRenderPipeline, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, IGLVertexBuffer, WebGL, getIGLBuffer } from "@feng3d/webgl"; +import { GBuffer, CanvasContext, CopyBufferToBuffer, RenderPass, RenderPipeline, VertexAttributes } from "@feng3d/render-api"; +import { WebGL, getIGLBuffer } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; (function () @@ -12,13 +12,12 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // -- Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init Program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list" }, }; // -- Init Buffer @@ -30,31 +29,34 @@ import { getShaderSource } from "./utility"; -1.0, 1.0, -1.0, -1.0 ]); - const vertexPosBufferSrc: IGLVertexBuffer = { target: "ARRAY_BUFFER", size: vertices.byteLength, data: vertices, usage: "STATIC_DRAW" }; + const vertexPosBufferSrc: GBuffer = { target: "ARRAY_BUFFER", size: vertices.byteLength, data: vertices, usage: "STATIC_DRAW" }; const vertexPosBufferDst = new Float32Array(vertices.length); - const cb: ICopyBufferToBuffer = { - __type: "CopyBufferToBuffer", + const cb: CopyBufferToBuffer = { + __type__: "CopyBufferToBuffer", source: vertexPosBufferSrc, destination: getIGLBuffer(vertexPosBufferDst, "ARRAY_BUFFER"), sourceOffset: 0, destinationOffset: 0, size: vertices.length * Float32Array.BYTES_PER_ELEMENT }; // -- Init Vertex Array - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { pos: { data: vertexPosBufferDst, format: "float32x2" }, } }; // -- Render - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [{ pipeline: program, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6 }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + }, }] }; diff --git a/examples/src/WebGL2Samples/buffer_uniform.ts b/examples/src/WebGL2Samples/buffer_uniform.ts index 4928f9c1ba054ec0063ef7afe2a47918d7e627cb..c5e19cd8ba12da4bbac7fddd866e953306402892 100644 --- a/examples/src/WebGL2Samples/buffer_uniform.ts +++ b/examples/src/WebGL2Samples/buffer_uniform.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISubmit, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Submit, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; (function () @@ -12,11 +12,11 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // --Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init Program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -73,7 +73,7 @@ import { getShaderSource } from "./utility"; }; // -- Init Vertex Array - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: vertices, format: "float32x3", arrayStride: 40, offset: 0 }, normal: { data: vertices, format: "float32x3", arrayStride: 40, offset: 12 }, @@ -81,24 +81,26 @@ import { getShaderSource } from "./utility"; }, }; - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - indices: elementData, - uniforms: { + bindingResources: { PerDraw: transforms, PerPass: lightPos, PerScene: material, }, - drawIndexed: { indexCount: 6, firstIndex: 0 } + geometry:{ + vertices: vertexArray.vertices, + indices: elementData, + draw: { __type__: "DrawIndexed", indexCount: 6, firstIndex: 0 } + }, }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro], }; - const submit: ISubmit = { commandEncoders: [{ passEncoders: [rp] }] }; + const submit: Submit = { commandEncoders: [{ passEncoders: [rp] }] }; let uTime = 0; function render() diff --git a/examples/src/WebGL2Samples/draw_image_space.ts b/examples/src/WebGL2Samples/draw_image_space.ts index 9751293912288b687f4713a83ccff23b6d245d9e..c97064a04cc646aae81feb4728ffbabb6cd73d48 100644 --- a/examples/src/WebGL2Samples/draw_image_space.ts +++ b/examples/src/WebGL2Samples/draw_image_space.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPipeline, ISampler, ITexture } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPipeline, Sampler, Texture } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,23 +8,22 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); loadImage("../../assets/img/Di-3d.png", (img) => { - const texture: ITexture = { + const texture: Texture = { size: [img.width, img.height], sources: [{ image: img, flipY: false }], format: "rgba8unorm", }; - const sampler: ISampler = { + const sampler: Sampler = { minFilter: "linear", magFilter: "linear", }; - const program: IRenderPipeline = { - primitive: { topology: "triangle-list" }, + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, @@ -34,12 +33,15 @@ loadImage("../../assets/img/Di-3d.png", (img) => } }; - const renderObject: IRenderObject = { - uniforms: { + const renderObject: RenderObject = { + bindingResources: { diffuse: { texture, sampler }, u_imageSize: [canvas.width / 2, canvas.height / 2], }, - drawVertex: { firstVertex: 0, vertexCount: 3 }, + geometry: { + primitive: { topology: "triangle-list" }, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 3 }, + }, pipeline: program }; diff --git a/examples/src/WebGL2Samples/draw_instanced.ts b/examples/src/WebGL2Samples/draw_instanced.ts index b6f822eae92e0e58084806606b188293134860b9..dc8d4e9b3696d8dcf8b6f8f895d7d66d7722bd7f 100644 --- a/examples/src/WebGL2Samples/draw_instanced.ts +++ b/examples/src/WebGL2Samples/draw_instanced.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPipeline, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPipeline, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); const vertexPosBuffer = new Float32Array([-0.3, -0.5, @@ -18,23 +18,25 @@ const vertexColorBuffer = new Float32Array([ 1.0, 0.5, 0.0, 0.0, 0.5, 1.0]); -const program: IRenderPipeline = { - primitive: { topology: "triangle-list" }, +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs"), targets: [{ blend: {} }] } }; -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { pos: { data: vertexPosBuffer, format: "float32x2" }, color: { data: vertexColorBuffer, format: "float32x3", stepMode: "instance" }, }, }; -const renderObject: IRenderObject = { - vertices: vertexArray.vertices, - uniforms: {}, - drawVertex: { vertexCount: 3, instanceCount: 2 }, +const renderObject: RenderObject = { + bindingResources: {}, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 3, instanceCount: 2 }, + }, pipeline: program }; diff --git a/examples/src/WebGL2Samples/draw_instanced_ubo.ts b/examples/src/WebGL2Samples/draw_instanced_ubo.ts index 0f730efdbda6b5f8dbbf100cd81ffe3c224d7f24..6b37457c8b1db5827c58f9089e7e9d41e08e8c60 100644 --- a/examples/src/WebGL2Samples/draw_instanced_ubo.ts +++ b/examples/src/WebGL2Samples/draw_instanced_ubo.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPipeline } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPipeline } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,11 +8,11 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program -const program: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; // -- Init Buffer const vertices = new Float32Array([ @@ -46,18 +46,20 @@ const materials = { }; // -- Render -const rp: IRenderPass = { +const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0, 0, 0, 1], loadOp: "clear" }] }, renderObjects: [{ pipeline: program, - vertices: { - pos: { data: vertices, format: "float32x2" }, - }, - uniforms: { + bindingResources: { Transform: transforms, Material: materials, }, - drawVertex: { vertexCount: 3, instanceCount: 2 }, + geometry: { + vertices: { + pos: { data: vertices, format: "float32x2" }, + }, + draw: { __type__: "DrawVertex", vertexCount: 3, instanceCount: 2 }, + } }] }; diff --git a/examples/src/WebGL2Samples/draw_primitive_restart.ts b/examples/src/WebGL2Samples/draw_primitive_restart.ts index 7a2e4e52ce3915b94d1b909b1c12158673dd9eb8..e321c6eb49c7b99c26b26492bd5292c3ef2a716d 100644 --- a/examples/src/WebGL2Samples/draw_primitive_restart.ts +++ b/examples/src/WebGL2Samples/draw_primitive_restart.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPipeline, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPipeline, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.18 @@ -22,8 +22,7 @@ const vertexPosBuffer = new Float32Array([ 1.0, 1.0, ]); -const program: IRenderPipeline = { - primitive: { topology: "triangle-strip" }, +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, @@ -37,17 +36,20 @@ const indices = new Uint16Array([ 0, 1, 2, MAX_UNSIGNED_SHORT, 2, 3, 1 ]); -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { pos: { data: vertexPosBuffer, format: "float32x2" }, }, }; -const renderObject: IRenderObject = { - vertices: vertexArray.vertices, - indices, - uniforms: {}, - drawIndexed: { indexCount: 7, instanceCount: 2 }, +const renderObject: RenderObject = { + bindingResources: {}, + geometry: { + primitive: { topology: "triangle-strip" }, + vertices: vertexArray.vertices, + indices, + draw: { __type__: "DrawIndexed", indexCount: 7, instanceCount: 2 }, + }, pipeline: program, }; diff --git a/examples/src/WebGL2Samples/draw_range_arrays.ts b/examples/src/WebGL2Samples/draw_range_arrays.ts index 5cf681bb216e734956601aba46ad14739ff8a89a..114fdf8da14bb3fe986bde6c4e9dcf5033e1a464 100644 --- a/examples/src/WebGL2Samples/draw_range_arrays.ts +++ b/examples/src/WebGL2Samples/draw_range_arrays.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); const vertexPosBuffer = new Float32Array([ @@ -26,8 +26,7 @@ const vertexPosBuffer = new Float32Array([ -0.5, -0.5, ]); -const pipeline: IRenderPipeline = { - primitive: { topology: "triangle-strip" }, +const pipeline: RenderPipeline = { vertex: { code: getShaderSource("vs") }, @@ -37,19 +36,23 @@ const pipeline: IRenderPipeline = { } }; -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: vertexPosBuffer, format: "float32x2" }, } }; const vertexCount = 12; -const renderObject: IRenderObject = { - vertices: vertexArray.vertices, - pipeline, +const renderObject: RenderObject = { + pipeline: pipeline, + geometry: { + primitive: { topology: "triangle-strip" }, + vertices: vertexArray.vertices, + draw: undefined, + } }; -const rp: IRenderPass = { +const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -60,12 +63,18 @@ const rp: IRenderPass = { { ...renderObject, viewport: { x: 0, y: 0, width: canvas.width / 2, height: canvas.height }, - drawVertex: { firstVertex: 0, vertexCount: vertexCount / 2 }, + geometry: { + ...renderObject.geometry, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: vertexCount / 2 }, + } }, { ...renderObject, viewport: { x: canvas.width / 2, y: 0, width: canvas.width / 2, height: canvas.height }, - drawVertex: { firstVertex: 6, vertexCount: vertexCount / 2 }, + geometry: { + ...renderObject.geometry, + draw: { __type__: "DrawVertex", firstVertex: 6, vertexCount: vertexCount / 2 }, + }, }, ], }; diff --git a/examples/src/WebGL2Samples/fbo_blit.ts b/examples/src/WebGL2Samples/fbo_blit.ts index 394d776a7f7687122037b40f1b6234451bcf585c..87b8080c8dfb48ea4df3388d442886d1630f7986 100644 --- a/examples/src/WebGL2Samples/fbo_blit.ts +++ b/examples/src/WebGL2Samples/fbo_blit.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassDescriptor, IRenderPipeline, ISampler, ITexture, ITextureView, IVertexAttributes } from "@feng3d/render-api"; -import { IGLBlitFramebuffer, IGLBlitFramebufferItem, IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, TextureView, VertexAttributes } from "@feng3d/render-api"; +import { BlitFramebuffer, BlitFramebufferItem, WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,11 +8,10 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); -const program: IRenderPipeline = { - primitive: { topology: "triangle-list" }, +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, @@ -39,7 +38,7 @@ const vertexTexBuffer = new Float32Array([ 0.0, 1.0 ]); -const vertices: IVertexAttributes = { +const vertices: VertexAttributes = { position: { data: vertexPosBuffer, format: "float32x2" }, texcoord: { data: vertexTexBuffer, format: "float32x2" }, }; @@ -51,39 +50,38 @@ loadImage("../../assets/img/Di-3d.png", (image) => y: image.height }; - const textureDiffuse: ITexture = { + const textureDiffuse: Texture = { size: [image.width, image.height], format: "rgba8unorm", sources: [{ image, flipY: true }], }; - const samplerDiffuse: ISampler = { + const samplerDiffuse: Sampler = { minFilter: "linear", magFilter: "linear", }; - const textureColorBuffer: ITexture = { + const textureColorBuffer: Texture = { format: "rgba8unorm", size: [FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y], }; - const samplerColorBuffer: ISampler = { + const samplerColorBuffer: Sampler = { minFilter: "linear", magFilter: "linear", }; // 此处 Renderbuffer 直接使用 IGLTextureView 替代。 - const colorRenderbuffer: ITextureView = { texture: { format: "rgba8unorm", size: [FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y] } }; + const colorRenderbuffer: TextureView = { texture: { format: "rgba8unorm", size: [FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y] } }; - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices, }; - const renderObject: IRenderObject = { + const renderObject: RenderObject = { viewport: { x: 0, y: 0, width: FRAMEBUFFER_SIZE.x, height: FRAMEBUFFER_SIZE.y }, pipeline: program, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { MVP: new Float32Array([ 0.8, 0.0, 0.0, 0.0, 0.0, 0.8, 0.0, 0.0, @@ -92,11 +90,15 @@ loadImage("../../assets/img/Di-3d.png", (image) => ]), diffuse: { texture: textureDiffuse, sampler: samplerDiffuse }, }, - drawVertex: { firstVertex: 0, vertexCount: 6 } + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 6 } + } }; // Render FBO - const fboRenderPass: IRenderPass = { + const fboRenderPass: RenderPass = { descriptor: { colorAttachments: [{ view: colorRenderbuffer, @@ -106,7 +108,7 @@ loadImage("../../assets/img/Di-3d.png", (image) => renderObjects: [renderObject], }; - const framebufferResolve: IRenderPassDescriptor = { + const framebufferResolve: RenderPassDescriptor = { colorAttachments: [{ view: { texture: textureColorBuffer, baseMipLevel: 0 }, clearValue: [0.7, 0.0, 0.0, 1.0] @@ -114,11 +116,11 @@ loadImage("../../assets/img/Di-3d.png", (image) => }; // - const renderPassResolve: IRenderPass = { + const renderPassResolve: RenderPass = { descriptor: framebufferResolve, }; - const blitFramebuffers: IGLBlitFramebufferItem[] = []; + const blitFramebuffers: BlitFramebufferItem[] = []; const TILE = 4; const BORDER = 2; for (let j = 0; j < TILE; j++) @@ -141,17 +143,16 @@ loadImage("../../assets/img/Di-3d.png", (image) => } } - const blitFramebuffer: IGLBlitFramebuffer = { - __type: "BlitFramebuffer", + const blitFramebuffer: BlitFramebuffer = { + __type__: "BlitFramebuffer", read: fboRenderPass.descriptor, draw: renderPassResolve.descriptor, blitFramebuffers, }; - const renderObject2: IRenderObject = { + const renderObject2: RenderObject = { viewport: { x: 0, y: 0, width: canvas.width, height: canvas.height }, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { MVP: new Float32Array([ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, @@ -160,11 +161,14 @@ loadImage("../../assets/img/Di-3d.png", (image) => ]), diffuse: { texture: textureColorBuffer, sampler: samplerColorBuffer }, }, - drawVertex: { firstVertex: 0, vertexCount: 6 }, + geometry: { + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 6 }, + }, pipeline: program, }; - const renderPass2: IRenderPass = { + const renderPass2: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], diff --git a/examples/src/WebGL2Samples/fbo_multisample.ts b/examples/src/WebGL2Samples/fbo_multisample.ts index 00a8f2ff43edd9f4d64ff3133e0991e8735476cc..5f7eb6c1e5d913396dc38a0eb0cbfef1f00cb2d4 100644 --- a/examples/src/WebGL2Samples/fbo_multisample.ts +++ b/examples/src/WebGL2Samples/fbo_multisample.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassDescriptor, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { getShaderSource } from "./utility"; @@ -9,7 +9,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(renderingContext); // -- Init program @@ -19,16 +19,14 @@ const PROGRAM = { MAX: 2 }; -const programs: IRenderPipeline[] = [ +const programs: RenderPipeline[] = [ { vertex: { code: getShaderSource("vs-render") }, fragment: { code: getShaderSource("fs-render") }, - primitive: { topology: "LINE_LOOP" }, }, { vertex: { code: getShaderSource("vs-splash") }, fragment: { code: getShaderSource("fs-splash") }, - primitive: { topology: "triangle-list" }, }, ]; @@ -70,20 +68,20 @@ const FRAMEBUFFER_SIZE = { x: canvas.width, y: canvas.height }; -const texture: ITexture = { +const texture: Texture = { format: "rgba8unorm", size: [FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y] }; -const sampler: ISampler = { minFilter: "nearest", magFilter: "nearest" }; +const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest" }; // -- Init Frame Buffers -const framebuffer: IRenderPassDescriptor = { +const framebuffer: RenderPassDescriptor = { colorAttachments: [{ view: { texture, baseMipLevel: 0 }, clearValue: [0.0, 0.0, 0.0, 1.0] }], sampleCount: 4 // 多重采样 }; // -- Init VertexArray -const vertexArrays: { vertices?: IVertexAttributes }[] = [ +const vertexArrays: { vertices?: VertexAttributes }[] = [ { vertices: { position: { data, format: "float32x2" } } }, @@ -100,13 +98,16 @@ const IDENTITY = mat4.create(); // -- Render // Pass 1 -const renderPass1: IRenderPass = { +const renderPass1: RenderPass = { descriptor: framebuffer, renderObjects: [{ pipeline: programs[PROGRAM.TEXTURE], - vertices: vertexArrays[PROGRAM.TEXTURE].vertices, - uniforms: { MVP: IDENTITY }, - drawVertex: { vertexCount }, + bindingResources: { MVP: IDENTITY }, + geometry: { + primitive: { topology: "LINE_LOOP" }, + vertices: vertexArrays[PROGRAM.TEXTURE].vertices, + draw: { __type__: "DrawVertex", vertexCount }, + } }] }; @@ -117,14 +118,17 @@ vec3.set(scaleVector3, 8.0, 8.0, 8.0); const mvp = mat4.create(); mat4.scale(mvp, IDENTITY, scaleVector3); -const renderPass2: IRenderPass = { +const renderPass2: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ { pipeline: programs[PROGRAM.SPLASH], - vertices: vertexArrays[PROGRAM.SPLASH].vertices, - uniforms: { diffuse: { texture, sampler }, MVP: mvp }, - drawVertex: { vertexCount: 6 }, + bindingResources: { diffuse: { texture, sampler }, MVP: mvp }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArrays[PROGRAM.SPLASH].vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } } ], }; diff --git a/examples/src/WebGL2Samples/fbo_new_blend_equation.ts b/examples/src/WebGL2Samples/fbo_new_blend_equation.ts index 1180ada0cac52210dcf02727493b954bed986172..ab0c3c8f1ec881c736dfdd8718f37ebba0829252 100644 --- a/examples/src/WebGL2Samples/fbo_new_blend_equation.ts +++ b/examples/src/WebGL2Samples/fbo_new_blend_equation.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes, IViewport } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes, Viewport } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -9,7 +9,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(renderingContext); // -- Divide viewport @@ -59,7 +59,7 @@ viewport[Corners.TOP_LEFT] = { // -- Initialize program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs"), targets: [{ blend: {} }] }, }; @@ -84,7 +84,7 @@ const texcoords = new Float32Array([ ]); // -- Initilize vertex array -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -92,12 +92,12 @@ const vertexArray: { vertices?: IVertexAttributes } = { }; // -- Load texture then render -const sampler: ISampler = { +const sampler: Sampler = { minFilter: "linear", magFilter: "linear" }; const imageUrl = "../../assets/img/Di-3d.png"; -let texture: ITexture; +let texture: Texture; loadImage(imageUrl, function (image) { texture = { @@ -119,22 +119,24 @@ function render() 0.0, 0.0, 0.0, 1.0 ]); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - uniforms: { mvp: matrix, diffuse: { texture, sampler } }, - drawVertex: { vertexCount: 6 }, + bindingResources: { mvp: matrix, diffuse: { texture, sampler } }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; - const renderObjects: IRenderPassObject[] = []; - const renderPass: IRenderPass = { + const renderObjects: RenderPassObject[] = []; + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.5, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects, }; for (let i = 0; i < Corners.MAX; ++i) { - const viewport0: IViewport = { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }; + const viewport0: Viewport = { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }; if (i === Corners.TOP_LEFT) { diff --git a/examples/src/WebGL2Samples/fbo_read_pixels.ts b/examples/src/WebGL2Samples/fbo_read_pixels.ts index 21e86a53211cb3ba90d1d35863202f173089d10d..2c863bec045d43c8ffe1c47469845d28df36cb94 100644 --- a/examples/src/WebGL2Samples/fbo_read_pixels.ts +++ b/examples/src/WebGL2Samples/fbo_read_pixels.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassDescriptor, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(renderingContext); // -- Divide viewport @@ -51,15 +51,13 @@ viewport[Textures.BLUE] = { // -- Initialize program // Multiple out shaders -const multipleOutputProgram: IRenderPipeline = { +const multipleOutputProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-multiple-output") }, fragment: { code: getShaderSource("fs-multiple-output") }, - primitive: { topology: "triangle-list" }, }; // Layer shaders -const layerProgram: IRenderPipeline = { +const layerProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-layer") }, fragment: { code: getShaderSource("fs-layer") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -84,13 +82,13 @@ const texcoords = new Float32Array([ // -- Initialize vertex array -const multipleOutputVertexArray: { vertices?: IVertexAttributes } = { +const multipleOutputVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, } }; -const layerVertexArray: { vertices?: IVertexAttributes } = { +const layerVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -102,16 +100,16 @@ const layerVertexArray: { vertices?: IVertexAttributes } = { const w = 16; const h = 16; -const texture: ITexture = { +const texture: Texture = { dimension: "2d-array", size: [w, h, 3], format: "rgba8unorm", }; -const sampler: ISampler = { lodMinClamp: 0, lodMaxClamp: 0, minFilter: "nearest", magFilter: "nearest" }; +const sampler: Sampler = { lodMinClamp: 0, lodMaxClamp: 0, minFilter: "nearest", magFilter: "nearest" }; // -- Initialize frame buffer -const frameBuffer: IRenderPassDescriptor = { +const frameBuffer: RenderPassDescriptor = { colorAttachments: [ { view: { texture, baseMipLevel: 0, baseArrayLayer: Textures.RED } }, { view: { texture, baseMipLevel: 0, baseArrayLayer: Textures.GREEN } }, @@ -128,34 +126,39 @@ const matrix = new Float32Array([ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]); -const rp1: IRenderPass = { +const rp1: RenderPass = { descriptor: frameBuffer, renderObjects: [ { viewport: { x: 0, y: 0, width: w, height: h }, pipeline: multipleOutputProgram, - uniforms: { mvp: matrix }, - vertices: multipleOutputVertexArray.vertices, - drawVertex: { vertexCount: 6 }, + bindingResources: { mvp: matrix }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: multipleOutputVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }], }; -const renderObjects: IRenderPassObject[] = []; +const renderObjects: RenderPassObject[] = []; // Pass 2 -const rp: IRenderPass = { +const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects }; -const ro: IRenderObject = { +const ro: RenderObject = { pipeline: layerProgram, - uniforms: { + bindingResources: { mvp: matrix, diffuse: { texture, sampler }, layer: 0, }, - vertices: layerVertexArray.vertices, - drawVertex: { vertexCount: 6 }, + geometry: { + vertices: layerVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; for (let i = 0; i < Textures.MAX; ++i) @@ -164,7 +167,7 @@ for (let i = 0; i < Textures.MAX; ++i) { ...ro, viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, - uniforms: { ...ro.uniforms, layer: i }, + bindingResources: { ...ro.bindingResources, layer: i }, }); } @@ -172,18 +175,24 @@ webgl.submit({ commandEncoders: [{ passEncoders: [rp1, rp] }] }); const data = new Uint8Array(w * h * 4 * 3); -webgl.runReadPixels({ - frameBuffer, attachmentPoint: "COLOR_ATTACHMENT0", - x: 0, y: 0, width: w, height: h, format: "RGBA", type: "UNSIGNED_BYTE", dstData: data, dstOffset: 0 +let result = webgl.readPixels({ + textureView: frameBuffer.colorAttachments[0].view, + origin: [0, 0], + copySize: [w, h], }); -webgl.runReadPixels({ - frameBuffer, attachmentPoint: "COLOR_ATTACHMENT1", - x: 0, y: 0, width: w, height: h, format: "RGBA", type: "UNSIGNED_BYTE", dstData: data, dstOffset: w * h * 4 +data.set(new Uint8Array(result.buffer), 0); +result = webgl.readPixels({ + textureView: frameBuffer.colorAttachments[1].view, + origin: [0, 0], + copySize: [w, h], }); -webgl.runReadPixels({ - frameBuffer, attachmentPoint: "COLOR_ATTACHMENT1", - x: 0, y: 0, width: w, height: h, format: "RGBA", type: "UNSIGNED_BYTE", dstData: data, dstOffset: w * h * 4 * 2 +data.set(new Uint8Array(result.buffer), w * h * 4); +result = webgl.readPixels({ + textureView: frameBuffer.colorAttachments[2].view, + origin: [0, 0], + copySize: [w, h], }); +data.set(new Uint8Array(result.buffer), w * h * 4 * 2); console.log(data); diff --git a/examples/src/WebGL2Samples/fbo_rtt_depth_texture.ts b/examples/src/WebGL2Samples/fbo_rtt_depth_texture.ts index 75f628d64205c80c488df3338bcad26c7e61904e..21b471e3364c9bbeb7e091eb6a862b15ba28fd2e 100644 --- a/examples/src/WebGL2Samples/fbo_rtt_depth_texture.ts +++ b/examples/src/WebGL2Samples/fbo_rtt_depth_texture.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassDescriptor, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; +const renderingContext: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false } }; const webgl = new WebGL(renderingContext); const windowSize = { @@ -19,16 +19,14 @@ const windowSize = { // -- Initialize program // Depth shaders -const depthProgram: IRenderPipeline = { +const depthProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-depth") }, fragment: { code: getShaderSource("fs-depth") }, depthStencil: {}, - primitive: { topology: "triangle-list" }, }; // Draw shaders -const drawProgram: IRenderPipeline = { +const drawProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-draw") }, fragment: { code: getShaderSource("fs-draw") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -59,13 +57,13 @@ const quadTexcoords = new Float32Array([ // -- Initialize vertex array -const triVertexArray: { vertices?: IVertexAttributes } = { +const triVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: triPositions, format: "float32x3" }, } }; -const quadVertexArray: { vertices?: IVertexAttributes } = { +const quadVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: quadPositions, format: "float32x2" }, textureCoordinates: { data: quadTexcoords, format: "float32x2" }, @@ -76,15 +74,15 @@ const quadVertexArray: { vertices?: IVertexAttributes } = { // the proper texture format combination can be found here // https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml -const depthTexture: ITexture = { +const depthTexture: Texture = { size: [windowSize.x, windowSize.y], format: "depth16unorm", }; -const depthSampler: ISampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; +const depthSampler: Sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; // -- Initialize frame buffer -const frameBuffer: IRenderPassDescriptor = { +const frameBuffer: RenderPassDescriptor = { colorAttachments: [], depthStencilAttachment: { view: { texture: depthTexture, baseMipLevel: 0 }, depthLoadOp: "clear" }, }; @@ -92,26 +90,32 @@ const frameBuffer: IRenderPassDescriptor = { // -- Render // Pass 1: Depth -const renderPass: IRenderPass = { +const renderPass: RenderPass = { descriptor: frameBuffer, renderObjects: [{ pipeline: depthProgram, - vertices: triVertexArray.vertices, - drawVertex: { vertexCount: 3 }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: triVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 3 }, + } }], }; // Pass 2: Draw -const rp2: IRenderPass = { +const rp2: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], }, renderObjects: [{ pipeline: drawProgram, - uniforms: { depthMap: { texture: depthTexture, sampler: depthSampler } }, - vertices: quadVertexArray.vertices, - drawVertex: { vertexCount: 6 }, + bindingResources: { depthMap: { texture: depthTexture, sampler: depthSampler } }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: quadVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }], }; diff --git a/examples/src/WebGL2Samples/fbo_rtt_draw_buffers.ts b/examples/src/WebGL2Samples/fbo_rtt_draw_buffers.ts index 70b60f8c4cce1b0a7e84ae34e69f6da0b15996b7..233ad8c8329df7dadad6537f005ae8dbd2f6ace2 100644 --- a/examples/src/WebGL2Samples/fbo_rtt_draw_buffers.ts +++ b/examples/src/WebGL2Samples/fbo_rtt_draw_buffers.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassDescriptor, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); const windowSize = { @@ -19,17 +19,15 @@ const windowSize = { // -- Initialize program // Draw buffer shaders -const drawBufferProgram: IRenderPipeline = { +const drawBufferProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-draw-buffer") }, fragment: { code: getShaderSource("fs-draw-buffer") }, - primitive: { topology: "triangle-list" }, }; // Draw shaders -const drawProgram: IRenderPipeline = { +const drawProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-draw") }, fragment: { code: getShaderSource("fs-draw") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -60,13 +58,13 @@ const quadTexcoords = new Float32Array([ // -- Initialize vertex array -const triVertexArray: { vertices?: IVertexAttributes } = { +const triVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: triPositions, format: "float32x3" } } }; -const quadVertexArray: { vertices?: IVertexAttributes } = { +const quadVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: quadPositions, format: "float32x2" }, textureCoordinates: { data: quadTexcoords, format: "float32x2" }, @@ -75,21 +73,21 @@ const quadVertexArray: { vertices?: IVertexAttributes } = { // -- Initialize texture targets -const color1Texture: ITexture = { +const color1Texture: Texture = { format: "rgba8unorm", size: [windowSize.x, windowSize.y], }; -const color1Sampler: ISampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; +const color1Sampler: Sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; -const color2Texture: ITexture = { +const color2Texture: Texture = { format: "rgba8unorm", size: [windowSize.x, windowSize.y], }; -const color2Sampler: ISampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; +const color2Sampler: Sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "nearest", magFilter: "nearest" }; // -- Initialize frame buffer -const frameBuffer: IRenderPassDescriptor = { +const frameBuffer: RenderPassDescriptor = { colorAttachments: [ { view: { texture: color1Texture, baseMipLevel: 0 } }, { view: { texture: color2Texture, baseMipLevel: 0 } }, @@ -98,26 +96,32 @@ const frameBuffer: IRenderPassDescriptor = { // -- Render -const renderPass: IRenderPass = { +const renderPass: RenderPass = { descriptor: frameBuffer, renderObjects: [{ pipeline: drawBufferProgram, - vertices: triVertexArray.vertices, - drawVertex: { vertexCount: 3 }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: triVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 3 }, + } }], }; // Pass 2: Draw to screen -const renderPass2: IRenderPass = { +const renderPass2: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [{ pipeline: drawProgram, - uniforms: { + bindingResources: { color1Map: { texture: color1Texture, sampler: color1Sampler }, color2Map: { texture: color2Texture, sampler: color2Sampler }, }, - vertices: quadVertexArray.vertices, - drawVertex: { vertexCount: 6 }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: quadVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }], }; diff --git a/examples/src/WebGL2Samples/fbo_rtt_texture_array.ts b/examples/src/WebGL2Samples/fbo_rtt_texture_array.ts index d18b0806c1d27a8af146ba359eb94edc81bbf3f5..ddf9ca2996544d386aec55357bf51fc3252a7062 100644 --- a/examples/src/WebGL2Samples/fbo_rtt_texture_array.ts +++ b/examples/src/WebGL2Samples/fbo_rtt_texture_array.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassDescriptor, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPassDescriptor, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const renderingContext: IGLCanvasContext = { canvasId: "glcanvas" }; +const renderingContext: CanvasContext = { canvasId: "glcanvas" }; const webgl = new WebGL(renderingContext); // -- Divide viewport @@ -51,15 +51,13 @@ viewport[Textures.BLUE] = { // -- Initialize program // Multiple out shaders -const multipleOutputProgram: IRenderPipeline = { +const multipleOutputProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-multiple-output") }, fragment: { code: getShaderSource("fs-multiple-output") }, - primitive: { topology: "triangle-list" }, }; // Layer shaders -const layerProgram: IRenderPipeline = { +const layerProgram: RenderPipeline = { vertex: { code: getShaderSource("vs-layer") }, fragment: { code: getShaderSource("fs-layer") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -84,13 +82,13 @@ const texcoords = new Float32Array([ // -- Initialize vertex array -const multipleOutputVertexArray: { vertices?: IVertexAttributes } = { +const multipleOutputVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, } }; -const layerVertexArray: { vertices?: IVertexAttributes } = { +const layerVertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -102,16 +100,16 @@ const layerVertexArray: { vertices?: IVertexAttributes } = { const w = 16; const h = 16; -const texture: ITexture = { +const texture: Texture = { dimension: "2d-array", format: "rgba8unorm", size: [w, h, 3], }; -const sampler: ISampler = { minFilter: "nearest", magFilter: "nearest", lodMinClamp: 0, lodMaxClamp: 0 }; +const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest", lodMinClamp: 0, lodMaxClamp: 0 }; // -- Initialize frame buffer -const frameBuffer: IRenderPassDescriptor = { +const frameBuffer: RenderPassDescriptor = { colorAttachments: [ { view: { texture, baseMipLevel: 0, baseArrayLayer: Textures.RED } }, { view: { texture, baseMipLevel: 0, baseArrayLayer: Textures.GREEN } }, @@ -130,32 +128,38 @@ const matrix = new Float32Array([ 0.0, 0.0, 0.0, 1.0 ]); -const renderPass1: IRenderPass = { +const renderPass1: RenderPass = { descriptor: frameBuffer, renderObjects: [ { viewport: { x: 0, y: 0, width: w, height: h }, pipeline: multipleOutputProgram, - uniforms: { mvp: matrix }, - vertices: multipleOutputVertexArray.vertices, - drawVertex: { vertexCount: 6 }, + bindingResources: { mvp: matrix }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: multipleOutputVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }] }; // Pass 2 -const renderObjects: IRenderPassObject[] = []; -const renderPass: IRenderPass = { +const renderObjects: RenderPassObject[] = []; +const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], }, renderObjects, }; -const renderObject: IRenderObject = { +const renderObject: RenderObject = { pipeline: layerProgram, - uniforms: { mvp: matrix, diffuse: { texture, sampler } }, - vertices: layerVertexArray.vertices, - + bindingResources: { mvp: matrix, diffuse: { texture, sampler } }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: layerVertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; // @@ -165,8 +169,11 @@ for (let i = 0; i < Textures.MAX; ++i) { viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, ...renderObject, - uniforms: { ...renderObject.uniforms, layer: i }, - drawVertex: { vertexCount: 6 }, + bindingResources: { ...renderObject.bindingResources, layer: i }, + geometry: { + ...renderObject.geometry, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } } ); } diff --git a/examples/src/WebGL2Samples/geo_vertex_format.ts b/examples/src/WebGL2Samples/geo_vertex_format.ts index 8b590d7e3d2d680edc833628ad677de1652e2dc4..8e419901ec5a6decc14af0a178a761da40227d5e 100644 --- a/examples/src/WebGL2Samples/geo_vertex_format.ts +++ b/examples/src/WebGL2Samples/geo_vertex_format.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { HalfFloat } from "./third-party/HalfFloatUtility"; import { getShaderSource, loadImage } from "./utility"; @@ -12,13 +12,12 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false } }; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list", cullFace: "back" }, depthStencil: {}, }; @@ -150,7 +149,7 @@ import { getShaderSource, loadImage } from "./utility"; // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { a_position: { data: positions, format: "float32x3" }, a_normal: { data: normals, format: "float16x4", arrayStride: 6 }, // 由于不支持类型 "float16x3",则需要设置 arrayStride 为6,表示每次间隔3个半浮点数。 @@ -161,8 +160,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Init Texture const imageUrl = "../../assets/img/Di-3d.png"; - let texture: ITexture; - let sampler: ISampler; + let texture: Texture; + let sampler: Sampler; loadImage(imageUrl, function (image) { // -- Init 2D Texture @@ -201,20 +200,23 @@ import { getShaderSource, loadImage } from "./utility"; const lightPosition = [0.0, 0.0, 5.0]; - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - indices: new Uint16Array(cubeVertexIndices), - uniforms: { + bindingResources: { u_model: modelMatrix, u_modelInvTrans: modelInvTrans, u_lightPosition: lightPosition, u_ambient: 0.1, }, - drawIndexed: { indexCount: 36 }, + geometry: { + primitive: { topology: "triangle-list", cullFace: "back" }, + vertices: vertexArray.vertices, + indices: new Uint16Array(cubeVertexIndices), + draw: { __type__: "DrawIndexed", indexCount: 36 }, + } }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro], }; @@ -232,8 +234,8 @@ import { getShaderSource, loadImage } from "./utility"; mat4.multiply(viewProj, perspectiveMatrix, viewMatrix); // - ro.uniforms.u_viewProj = viewProj; - ro.uniforms.s_tex2D = { texture, sampler }; + ro.bindingResources.u_viewProj = viewProj; + ro.bindingResources.s_tex2D = { texture, sampler }; webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/glsl_centroid.ts b/examples/src/WebGL2Samples/glsl_centroid.ts index de9255040b3d0cd58ee08f886200960ecf0ba018..aec18b7ed4b27e066526ab2bca84beab88c74495 100644 --- a/examples/src/WebGL2Samples/glsl_centroid.ts +++ b/examples/src/WebGL2Samples/glsl_centroid.ts @@ -1,5 +1,5 @@ -import { IPassEncoder, IRenderObject, IRenderPass, IRenderPassDescriptor, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes, IViewport } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, PassEncoder, RenderObject, RenderPass, RenderPassDescriptor, RenderPassObject, RenderPipeline, Sampler, Texture, VertexAttributes, Viewport } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { getShaderSource } from "./utility"; @@ -9,7 +9,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Divide viewport @@ -24,7 +24,7 @@ const VIEWPORTS = { MAX: 2 }; -const viewport: IViewport[] = new Array(VIEWPORTS.MAX); +const viewport: Viewport[] = new Array(VIEWPORTS.MAX); viewport[VIEWPORTS.LEFT] = { x: 0, @@ -48,18 +48,15 @@ const PROGRAM = { MAX: 3 }; -const programs: IRenderPipeline[] = [ +const programs: RenderPipeline[] = [ { vertex: { code: getShaderSource("vs-render") }, fragment: { code: getShaderSource("fs-render") }, - primitive: { topology: "triangle-list" }, }, { vertex: { code: getShaderSource("vs-render-centroid") }, fragment: { code: getShaderSource("fs-render-centroid") }, - primitive: { topology: "triangle-list" }, }, { vertex: { code: getShaderSource("vs-splash") }, fragment: { code: getShaderSource("fs-splash") }, - primitive: { topology: "triangle-list" }, } ]; @@ -104,8 +101,8 @@ const FRAMEBUFFER_SIZE = { x: canvas.width, y: canvas.height }; -const textures: ITexture[] = []; -const samplers: ISampler[] = []; +const textures: Texture[] = []; +const samplers: Sampler[] = []; for (let i = 0; i < VIEWPORTS.MAX; ++i) { @@ -125,13 +122,13 @@ const FRAMEBUFFER = { COLORBUFFER_CENTROID: 3 }; -const framebuffers: IRenderPassDescriptor[] = [ +const framebuffers: RenderPassDescriptor[] = [ { colorAttachments: [{ view: { texture: textures[0], baseMipLevel: 0 }, clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], sampleCount: 4 }, { colorAttachments: [{ view: { texture: textures[1], baseMipLevel: 0 }, clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], sampleCount: 4 }, ]; // -- Init VertexArray -const vertexArrays: { vertices?: IVertexAttributes }[] = [ +const vertexArrays: { vertices?: VertexAttributes }[] = [ { vertices: { position: { data: positions, format: "float32x2" }, @@ -153,33 +150,40 @@ const vertexArrays: { vertices?: IVertexAttributes }[] = [ ]; // -- Render -const passEncoders: IPassEncoder[] = []; +const passEncoders: PassEncoder[] = []; // Pass 1 const IDENTITY = mat4.create(); for (let i = 0; i < VIEWPORTS.MAX; ++i) { // render buffers - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: framebuffers[i], renderObjects: [{ pipeline: programs[i], - vertices: vertexArrays[i].vertices, - uniforms: { MVP: IDENTITY }, - drawVertex: { vertexCount }, + bindingResources: { MVP: IDENTITY }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArrays[i].vertices, + draw: { __type__: "DrawVertex", vertexCount }, + } }] }; passEncoders.push(rp); } -const renderObjects: IRenderPassObject[] = []; +const renderObjects: RenderPassObject[] = []; // Pass 2 -const rp2: IRenderPass = { +const rp2: RenderPass = { renderObjects, }; -const ro: IRenderObject = { +const ro: RenderObject = { pipeline: programs[PROGRAM.SPLASH], - vertices: vertexArrays[PROGRAM.SPLASH].vertices, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArrays[PROGRAM.SPLASH].vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + }, }; const scaleVector3 = vec3.create(); @@ -194,11 +198,13 @@ for (let i = 0; i < VIEWPORTS.MAX; ++i) { ...ro, viewport: viewport[i], - uniforms: { + bindingResources: { MVP: mvp, diffuse: { texture: textures[i], sampler: samplers[i] }, }, - drawVertex: { vertexCount: 6 }, + geometry: { + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } } ); } diff --git a/examples/src/WebGL2Samples/glsl_flat_smooth_interpolators.ts b/examples/src/WebGL2Samples/glsl_flat_smooth_interpolators.ts index 769e1354748f264a0d60d08ec8331059828d764a..acae1e1e3283691ec64b6a25e9abcefd3dce113b 100644 --- a/examples/src/WebGL2Samples/glsl_flat_smooth_interpolators.ts +++ b/examples/src/WebGL2Samples/glsl_flat_smooth_interpolators.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderPass, IRenderPassObject, IRenderPipeline, IVertexAttributes, IViewport } from "@feng3d/render-api"; -import { getIVertexFormat, IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, RenderPassObject, RenderPass, RenderPipeline, VertexAttributes, VertexFormat, Viewport } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { GlTFLoader, Primitive } from "./third-party/gltf-loader"; import { getShaderSource } from "./utility"; @@ -10,7 +10,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Divide viewport @@ -25,7 +25,7 @@ const VIEWPORTS = { MAX: 2 }; -const viewport: IViewport[] = new Array(VIEWPORTS.MAX); +const viewport: Viewport[] = new Array(VIEWPORTS.MAX); viewport[VIEWPORTS.LEFT] = { x: 0, @@ -42,15 +42,13 @@ viewport[VIEWPORTS.RIGHT] = { }; // -- Initialize program -const programs: IRenderPipeline[] = [ +const programs: RenderPipeline[] = [ { vertex: { code: getShaderSource("vs-flat") }, fragment: { code: getShaderSource("fs-flat") }, - primitive: { topology: "triangle-list" }, depthStencil: { depthCompare: "less-equal" }, }, { vertex: { code: getShaderSource("vs-smooth") }, fragment: { code: getShaderSource("fs-smooth") }, - primitive: { topology: "triangle-list" }, depthStencil: { depthCompare: "less-equal" }, } ]; @@ -64,7 +62,7 @@ glTFLoader.loadGLTF(gltfUrl, function (glTF) // -- Initialize vertex array const vertexArrayMaps: { - [key: string]: { vertexArray: { vertices?: IVertexAttributes }, indices: IIndicesDataTypes }[] + [key: string]: { vertexArray: { vertices?: VertexAttributes }, indices: IIndicesDataTypes }[] } = {}; // var in loop @@ -73,7 +71,7 @@ glTFLoader.loadGLTF(gltfUrl, function (glTF) }; let primitive: Primitive; // { matrix: mat4, attributes: { [key: string]: { size: number, type: number, stride: number, offset: number } }, vertexBuffer, indices }; - let vertexArray: { vertices?: IVertexAttributes }; + let vertexArray: { vertices?: VertexAttributes }; let i: number; let len: number; @@ -102,11 +100,11 @@ glTFLoader.loadGLTF(gltfUrl, function (glTF) vertexArray = { vertices: { position: { - data: vertices, format: getIVertexFormat(positionInfo.size), + data: vertices, format: (["float32", "float32x2", "float32x3", "float32x4"] as VertexFormat[])[positionInfo.size], arrayStride: positionInfo.stride, offset: positionInfo.offset }, normal: { - data: vertices, format: getIVertexFormat(normalInfo.size), + data: vertices, format: (["float32", "float32x2", "float32x3", "float32x4"] as VertexFormat[])[normalInfo.size], arrayStride: normalInfo.stride, offset: normalInfo.offset }, }, @@ -137,8 +135,8 @@ glTFLoader.loadGLTF(gltfUrl, function (glTF) // -- Render loop (function render() { - const renderObjects: IRenderPassObject[] = []; - const rp: IRenderPass = { + const renderObjects: RenderPassObject[] = []; + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], depthStencilAttachment: { depthLoadOp: "clear" } @@ -171,13 +169,16 @@ glTFLoader.loadGLTF(gltfUrl, function (glTF) { viewport: viewport[i], pipeline: programs[i], - vertices: vertexArray.vertices, - indices, - uniforms: { + bindingResources: { mvp: localMVP, mvNormal: localMVNormal, }, - drawIndexed: { indexCount: primitive.indices.length, firstIndex: 0 }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + indices, + draw: { __type__: "DrawIndexed", indexCount: primitive.indices.length, firstIndex: 0 }, + }, }); } } diff --git a/examples/src/WebGL2Samples/glsl_non_square_matrix.ts b/examples/src/WebGL2Samples/glsl_non_square_matrix.ts index 87f68bd9e9945d9115ecf1718163a0514b9fce08..2cdb8692fd848806acdd04f0b0d04f887e24105c 100644 --- a/examples/src/WebGL2Samples/glsl_non_square_matrix.ts +++ b/examples/src/WebGL2Samples/glsl_non_square_matrix.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,13 +8,12 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list" }, }; // -- Init buffers: vec2 Position, vec2 Texcoord @@ -37,7 +36,7 @@ const texCoords = new Float32Array([ ]); // -- Init VertexArray -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -47,14 +46,14 @@ const vertexArray: { vertices?: IVertexAttributes } = { loadImage("../../assets/img/Di-3d.png", function (image) { // -- Init Texture - const texture: ITexture = { + const texture: Texture = { size: [image.width, image.height], format: "rgba8unorm", sources: [{ image, flipY: false, }] }; - const sampler: ISampler = { minFilter: "nearest", magFilter: "nearest" }; + const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest" }; // -- Render const matrix = new Float32Array([ @@ -64,13 +63,16 @@ loadImage("../../assets/img/Di-3d.png", function (image) 0.2, -0.2, 0.0 //translation ]); - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [{ pipeline: program, - uniforms: { MVP: matrix, diffuse: { texture, sampler } }, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6 }, + bindingResources: { MVP: matrix, diffuse: { texture, sampler } }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }] }; diff --git a/examples/src/WebGL2Samples/query_occlusion.ts b/examples/src/WebGL2Samples/query_occlusion.ts index d15d4446f200cdbb4c13f76bed982be90a22b6dc..48bbc9de0e40e63b8c7955d8c28a65ff402ba266 100644 --- a/examples/src/WebGL2Samples/query_occlusion.ts +++ b/examples/src/WebGL2Samples/query_occlusion.ts @@ -1,7 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, IGLOcclusionQuery, WebGL } from "@feng3d/webgl"; - -import { watcher } from "@feng3d/watcher"; +import { CanvasContext, OcclusionQuery, RenderObject, RenderPass, RenderPassObject, RenderPipeline, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; @@ -13,14 +11,13 @@ canvas.height = window.innerHeight; document.body.appendChild(canvas); // -- Init WebGL Context -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init Program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, depthStencil: {}, - primitive: { topology: "triangle-list" }, }; // -- Init Buffer @@ -35,15 +32,15 @@ const vertices = new Float32Array([ ]); // -- Init Vertex Array -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { pos: { data: vertices, format: "float32x3", arrayStride: 0, offset: 0 }, } }; -const renderObjects: IRenderPassObject[] = []; +const renderObjects: RenderPassObject[] = []; // -- Render -const rp: IRenderPass = { +const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], depthStencilAttachment: { depthLoadOp: "clear" }, @@ -51,29 +48,33 @@ const rp: IRenderPass = { renderObjects, }; -const ro: IRenderObject = { - vertices: vertexArray.vertices, +const ro: RenderObject = { pipeline: program, - drawVertex: { firstVertex: 0, vertexCount: 3 }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 3 }, + } }; renderObjects.push(ro); -const occlusionQuery: IGLOcclusionQuery = { - __type: "OcclusionQuery", +const occlusionQuery: OcclusionQuery = { + __type__: "OcclusionQuery", renderObjects: [{ ...ro, - drawVertex: { firstVertex: 3, vertexCount: 3 }, - }] + geometry: { + ...ro.geometry, + draw: { __type__: "DrawVertex", firstVertex: 3, vertexCount: 3 }, + } + }], + onQuery(result: number) { + document.getElementById("samplesPassed").innerHTML = `Any samples passed: ${Number(result)}`; + } }; renderObjects.push(occlusionQuery); webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); -watcher.watch(occlusionQuery, "result", () => -{ - document.getElementById("samplesPassed").innerHTML = `Any samples passed: ${Number(occlusionQuery.result.result)}`; -}); - // -- Delete WebGL resources webgl.deleteProgram(program); diff --git a/examples/src/WebGL2Samples/sampler_filter.ts b/examples/src/WebGL2Samples/sampler_filter.ts index 482de0fb757af4bcf0ed2cc14a82fc94310534b2..f3db340d31fed3eeb7c18b78ec85e07e640e512e 100644 --- a/examples/src/WebGL2Samples/sampler_filter.ts +++ b/examples/src/WebGL2Samples/sampler_filter.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Divide viewport @@ -58,9 +58,8 @@ viewport[Corners.TOP_LEFT] = { // -- Initialize program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -85,7 +84,7 @@ const texcoords = new Float32Array([ // -- Initialize vertex array -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -94,7 +93,7 @@ const vertexArray: { vertices?: IVertexAttributes } = { // -- Initialize samplers -const samplers: ISampler[] = new Array(Corners.MAX); +const samplers: Sampler[] = new Array(Corners.MAX); for (let i = 0; i < Corners.MAX; ++i) { samplers[i] = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", addressModeW: "clamp-to-edge" }; @@ -119,7 +118,7 @@ samplers[Corners.BOTTOM_LEFT].mipmapFilter = "linear"; // -- Load texture then render const imageUrl = "../../assets/img/Di-3d.png"; -let texture: ITexture; +let texture: Texture; loadImage(imageUrl, function (image) { texture = { @@ -142,17 +141,20 @@ function render() 0.0, 0.0, 0.0, 1.0 ]); - const renderObjects: IRenderPassObject[] = []; - const rp: IRenderPass = { + const renderObjects: RenderPassObject[] = []; + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects }; - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - uniforms: { mvp: matrix }, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6, instanceCount: 1 }, + bindingResources: { mvp: matrix }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6, instanceCount: 1 }, + } }; // Bind samplers @@ -162,8 +164,8 @@ function render() { ...ro, viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, - uniforms: { - ...ro.uniforms, + bindingResources: { + ...ro.bindingResources, diffuse: { texture, sampler: samplers[i] } diff --git a/examples/src/WebGL2Samples/sampler_object.ts b/examples/src/WebGL2Samples/sampler_object.ts index 79f995ff57fd62d55145a486d824a891b26cc91a..79041763dbb190a4ea23447cc7fa66e925f3f8dc 100644 --- a/examples/src/WebGL2Samples/sampler_object.ts +++ b/examples/src/WebGL2Samples/sampler_object.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -9,14 +9,13 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Initialize program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -41,7 +40,7 @@ const texcoords = new Float32Array([ // -- Initialize vertex array -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -50,12 +49,12 @@ const vertexArray: { vertices?: IVertexAttributes } = { // -- Initialize samplers -const samplerA: ISampler = { +const samplerA: Sampler = { minFilter: "nearest", magFilter: "nearest", mipmapFilter: "nearest", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", addressModeW: "clamp-to-edge", lodMinClamp: -1000.0, lodMaxClamp: 1000.0, }; -const samplerB: ISampler = { +const samplerB: Sampler = { minFilter: "linear", magFilter: "linear", mipmapFilter: "linear", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", addressModeW: "clamp-to-edge", lodMinClamp: -1000.0, lodMaxClamp: 1000.0, @@ -64,7 +63,7 @@ const samplerB: ISampler = { // -- Load texture then render const imageUrl = "../../assets/img/Di-3d.png"; -let texture: ITexture; +let texture: Texture; loadImage(imageUrl, function (image) { texture = { @@ -86,17 +85,20 @@ function render() 0.0, 0.0, 0.0, 1.0 ]); - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [{ pipeline: program, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { mvp: matrix, materialDiffuse0: { texture, sampler: samplerA }, materialDiffuse1: { texture, sampler: samplerB }, }, - drawVertex: { vertexCount: 6, instanceCount: 1 }, + geometry:{ + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6, instanceCount: 1 }, + } }], }; diff --git a/examples/src/WebGL2Samples/sampler_wrap.ts b/examples/src/WebGL2Samples/sampler_wrap.ts index 91fbd8f348fb060ed92e9c62ffd2c6c33595a9ef..e1703b24b0442ac4bee36f290a10096983b77e76 100644 --- a/examples/src/WebGL2Samples/sampler_wrap.ts +++ b/examples/src/WebGL2Samples/sampler_wrap.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; const canvas = document.createElement("canvas"); @@ -8,7 +8,7 @@ canvas.width = Math.min(window.innerWidth, window.innerHeight); canvas.height = canvas.width; document.body.appendChild(canvas); -const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; +const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Divide viewport @@ -58,9 +58,8 @@ viewport[Corners.TOP_LEFT] = { // -- Initialize program -const program: IRenderPipeline = { +const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, - primitive: { topology: "triangle-list" }, }; // -- Initialize buffer @@ -85,7 +84,7 @@ const texcoords = new Float32Array([ // -- Initilize vertex array -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -94,7 +93,7 @@ const vertexArray: { vertices?: IVertexAttributes } = { // -- Initialize samplers -const samplers: ISampler[] = new Array(Corners.MAX); +const samplers: Sampler[] = new Array(Corners.MAX); for (let i = 0; i < Corners.MAX; ++i) { @@ -118,7 +117,7 @@ samplers[Corners.BOTTOM_LEFT].addressModeV = "clamp-to-edge"; // -- Load texture then render const imageUrl = "../../assets/img/Di-3d.png"; -let texture: ITexture; +let texture: Texture; loadImage(imageUrl, function (image) { texture = { @@ -132,9 +131,9 @@ loadImage(imageUrl, function (image) function render() { - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // Clear color buffer - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects, }; @@ -146,10 +145,14 @@ function render() 0.0, 0.0, 0.0, 1.0 ]); - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - uniforms: { mvp: matrix }, - vertices: vertexArray.vertices, + bindingResources: { mvp: matrix }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6, instanceCount: 1 }, + } }; for (let i = 0; i < Corners.MAX; ++i) @@ -158,11 +161,14 @@ function render() { ...ro, viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, - uniforms: { - ...ro.uniforms, + bindingResources: { + ...ro.bindingResources, diffuse: { texture, sampler: samplers[i] }, }, - drawVertex: { vertexCount: 6, instanceCount: 1 }, + geometry: { + ...ro.geometry, + draw: { __type__: "DrawVertex", vertexCount: 6, instanceCount: 1 }, + } }); } diff --git a/examples/src/WebGL2Samples/texture_2d_array.ts b/examples/src/WebGL2Samples/texture_2d_array.ts index c69e0cb035d9b4ba068d6c9dbc99a90138264715..c4e0cd36503174203b4c920bd3f09e0ad6fcdc72 100644 --- a/examples/src/WebGL2Samples/texture_2d_array.ts +++ b/examples/src/WebGL2Samples/texture_2d_array.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; (function () @@ -10,11 +10,11 @@ import { getShaderSource, loadImage } from "./utility"; canvas.width = canvas.height * 960 / 540; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -38,15 +38,15 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, } }; - let texture: ITexture; - let sampler: ISampler; + let texture: Texture; + let sampler: Sampler; loadImage("../../assets/img/di-animation-array.jpg", function (image) { const NUM_IMAGES = 3; @@ -68,7 +68,7 @@ import { getShaderSource, loadImage } from "./utility"; size: [IMAGE_SIZE.width, IMAGE_SIZE.height, NUM_IMAGES], dimension: "2d-array", format: "rgba8unorm", - sources: [{ __type: "TextureDataSource", size: [IMAGE_SIZE.width, IMAGE_SIZE.height, NUM_IMAGES], data: pixels }], + sources: [{ __type__: "TextureDataSource", size: [IMAGE_SIZE.width, IMAGE_SIZE.height, NUM_IMAGES], data: pixels }], }; sampler = { minFilter: "linear", @@ -82,17 +82,19 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [1.0, 1.0, 1.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro], }; @@ -101,7 +103,7 @@ import { getShaderSource, loadImage } from "./utility"; (function render() { // -- Render - ro.uniforms.layer = frame; + ro.bindingResources.layer = frame; webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_3d.ts b/examples/src/WebGL2Samples/texture_3d.ts index 48b1e93845a3138a86f79ad99638c82e0cf753b3..563bbfbcda684f8ebcfbd0641d2c24d457d1d677 100644 --- a/examples/src/WebGL2Samples/texture_3d.ts +++ b/examples/src/WebGL2Samples/texture_3d.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { snoise } from "./third-party/noise3D"; import { getShaderSource } from "./utility"; @@ -11,7 +11,7 @@ import { getShaderSource } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Divide viewport @@ -79,14 +79,14 @@ import { getShaderSource } from "./utility"; } } - const texture: ITexture = { + const texture: Texture = { size: [SIZE, SIZE, SIZE], dimension: "3d", format: "r8unorm", generateMipmap: true, - sources: [{ __type: "TextureDataSource", mipLevel: 0, size: [SIZE, SIZE, SIZE], data }], + sources: [{ __type__: "TextureDataSource", mipLevel: 0, size: [SIZE, SIZE, SIZE], data }], }; - const sampler: ISampler = { + const sampler: Sampler = { lodMinClamp: 0, lodMaxClamp: Math.log2(SIZE), minFilter: "linear", @@ -95,7 +95,7 @@ import { getShaderSource } from "./utility"; }; // -- Initialize program - const program: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; // -- Initialize buffer const positions = new Float32Array([ @@ -118,7 +118,7 @@ import { getShaderSource } from "./utility"; // -- Initilize vertex array - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, in_texcoord: { data: texCoords, format: "float32x2" }, @@ -157,16 +157,18 @@ import { getShaderSource } from "./utility"; ]; } - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - uniforms: { + bindingResources: { diffuse: { texture, sampler }, }, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6 } + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 } + } }; - const renderPassObjects: IRenderObject[] = []; + const renderPassObjects: RenderObject[] = []; for (let i = 0; i < Corners.MAX; ++i) { renderPassObjects.push({ @@ -175,7 +177,7 @@ import { getShaderSource } from "./utility"; }); } - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: renderPassObjects, }; @@ -194,7 +196,7 @@ import { getShaderSource } from "./utility"; for (let i = 0; i < Corners.MAX; ++i) { - renderPassObjects[i].uniforms.orientation = matrices[i]; + renderPassObjects[i].bindingResources.orientation = matrices[i]; } webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_derivative.ts b/examples/src/WebGL2Samples/texture_derivative.ts index 5e5762d981222e435db3f233ad94c091ff487cf3..c10953c520a20311625f6122d715657ecb9c9dab 100644 --- a/examples/src/WebGL2Samples/texture_derivative.ts +++ b/examples/src/WebGL2Samples/texture_derivative.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { getShaderSource, loadImage } from "./utility"; @@ -12,18 +12,17 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs"), targets: [{ }] }, depthStencil: {}, - primitive: { topology: "triangle-list", cullFace: "back" } }; // -- Init buffers @@ -116,7 +115,7 @@ import { getShaderSource, loadImage } from "./utility"; ]; // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x3" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -126,8 +125,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Init Texture const imageUrl = "../../assets/img/Di-3d.png"; - let texture: ITexture; - let sampler: ISampler; + let texture: Texture; + let sampler: Sampler; loadImage(imageUrl, function (image) { // -- Init 2D Texture @@ -206,15 +205,18 @@ import { getShaderSource, loadImage } from "./utility"; lastMouseY = newY; }; - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - indices: new Uint16Array(cubeVertexIndices), - uniforms: {}, - drawIndexed: { indexCount: 36 }, + bindingResources: {}, + geometry:{ + primitive: { topology: "triangle-list", cullFace: "back" }, + vertices: vertexArray.vertices, + indices: new Uint16Array(cubeVertexIndices), + draw: { __type__: "DrawIndexed", indexCount: 36 }, + } }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro] }; @@ -230,9 +232,9 @@ import { getShaderSource, loadImage } from "./utility"; mat4.rotateY(mvMatrix, mvMatrix, orientation[1] * Math.PI); mat4.rotateZ(mvMatrix, mvMatrix, orientation[2] * Math.PI); - ro.uniforms.mvMatrix = mvMatrix; - ro.uniforms.pMatrix = perspectiveMatrix; - ro.uniforms.diffuse = { texture, sampler }; + ro.bindingResources.mvMatrix = mvMatrix; + ro.bindingResources.pMatrix = perspectiveMatrix; + ro.bindingResources.diffuse = { texture, sampler }; webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_fetch.ts b/examples/src/WebGL2Samples/texture_fetch.ts index 4283d049198d71ec71ebb6f81ab71ee4e8055b2e..331ffdb28750ce841ccb0a8d1a8b6a39dc4d07f5 100644 --- a/examples/src/WebGL2Samples/texture_fetch.ts +++ b/examples/src/WebGL2Samples/texture_fetch.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -11,11 +11,11 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; // -- Init buffers: vec2 Position, vec2 Texcoord const positions = new Float32Array([ @@ -37,7 +37,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -47,14 +47,14 @@ import { getShaderSource, loadImage } from "./utility"; loadImage("../../assets/img/Di-3d.png", function (image) { // -- Init Texture - const texture: ITexture = { + const texture: Texture = { size: [image.width, image.height], format: "rgba8unorm", sources: [{ mipLevel: 0, image, flipY: false, }], }; - const sampler: ISampler = { + const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest", }; @@ -68,17 +68,19 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ { pipeline: program, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } } ], }; diff --git a/examples/src/WebGL2Samples/texture_format.ts b/examples/src/WebGL2Samples/texture_format.ts index 30b0bade2ee06f33d4031ec039c4ff3c6d104aaa..945c2be554b406ad202b14569236c7195a640a1c 100644 --- a/examples/src/WebGL2Samples/texture_format.ts +++ b/examples/src/WebGL2Samples/texture_format.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, ITextureFormat, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderPass, RenderPipeline, Sampler, Texture, TextureFormat, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -11,7 +11,7 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Viewport @@ -49,9 +49,9 @@ import { getShaderSource, loadImage } from "./utility"; } // -- Init program - const programUint: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-uint") } }; + const programUint: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-uint") } }; - const programNormalized: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-normalized") } }; + const programNormalized: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-normalized") } }; // -- Init buffers: vec2 Position, vec2 Texcoord const positions = new Float32Array([ @@ -73,7 +73,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -95,7 +95,7 @@ import { getShaderSource, loadImage } from "./utility"; MAX: 9 }; - const textureFormats: { format: ITextureFormat }[] = new Array(TextureTypes.MAX); + const textureFormats: { format: TextureFormat }[] = new Array(TextureTypes.MAX); textureFormats[TextureTypes.RGB] = { format: "rgba8unorm", @@ -135,8 +135,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Init Texture - const textures: ITexture[] = new Array(TextureTypes.MAX); - const samplers: ISampler[] = new Array(TextureTypes.MAX); + const textures: Texture[] = new Array(TextureTypes.MAX); + const samplers: Sampler[] = new Array(TextureTypes.MAX); let i = 0; for (i = 0; i < TextureTypes.MAX; ++i) { @@ -163,8 +163,8 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const renderObjects: IRenderPassObject[] = []; - const rp: IRenderPass = { + const renderObjects: RenderPassObject[] = []; + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects }; @@ -174,13 +174,15 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push( { viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, - vertices: vertexArray.vertices, pipeline: programNormalized, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture: textures[i], sampler: samplers[i] }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); } @@ -190,13 +192,15 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push( { viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, - vertices: vertexArray.vertices, pipeline: programUint, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture: textures[i], sampler: samplers[i] }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); } diff --git a/examples/src/WebGL2Samples/texture_grad.ts b/examples/src/WebGL2Samples/texture_grad.ts index 7f38b7dc71792f94c2259fdea25a210b38a8c44e..39b30c337a113d6734d6c60bf16afd7d12696f41 100644 --- a/examples/src/WebGL2Samples/texture_grad.ts +++ b/examples/src/WebGL2Samples/texture_grad.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { getShaderSource, loadImage } from "./utility"; @@ -12,14 +12,13 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, depthStencil: {}, - primitive: { cullFace: "back" }, }; // -- Init buffers @@ -111,7 +110,7 @@ import { getShaderSource, loadImage } from "./utility"; ]; // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x3" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -121,8 +120,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Init Texture const imageUrl = "../../assets/img/Di-3d.png"; - let texture: ITexture; - let sampler: ISampler; + let texture: Texture; + let sampler: Sampler; loadImage(imageUrl, function (image) { // -- Init 2D Texture @@ -197,15 +196,18 @@ import { getShaderSource, loadImage } from "./utility"; lastMouseY = newY; }; - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - indices: new Uint16Array(cubeVertexIndices), - uniforms: {}, - drawIndexed: { indexCount: 36 }, + bindingResources: {}, + geometry:{ + primitive: { cullFace: "back" }, + vertices: vertexArray.vertices, + indices: new Uint16Array(cubeVertexIndices), + draw: { __type__: "DrawIndexed", indexCount: 36 }, + } }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro], }; @@ -220,9 +222,9 @@ import { getShaderSource, loadImage } from "./utility"; mat4.rotateY(mvMatrix, mvMatrix, orientation[1] * Math.PI); mat4.rotateZ(mvMatrix, mvMatrix, orientation[2] * Math.PI); - ro.uniforms.mvMatrix = mvMatrix; - ro.uniforms.pMatrix = perspectiveMatrix; - ro.uniforms.diffuse = { texture, sampler }; + ro.bindingResources.mvMatrix = mvMatrix; + ro.bindingResources.pMatrix = perspectiveMatrix; + ro.bindingResources.diffuse = { texture, sampler }; webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_immutable.ts b/examples/src/WebGL2Samples/texture_immutable.ts index 9ad3d4db2bcca073e299fdd28dc91bbe5a4b570e..d9626605e0853b45249f8f2e16ee904678b86453 100644 --- a/examples/src/WebGL2Samples/texture_immutable.ts +++ b/examples/src/WebGL2Samples/texture_immutable.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { snoise } from "./third-party/noise3D"; import { getShaderSource, loadImage } from "./utility"; @@ -12,7 +12,7 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(rc); const Corners = { @@ -38,9 +38,9 @@ import { getShaderSource, loadImage } from "./utility"; }; // -- Init program - const program: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") } }; - const program3D: IRenderPipeline = { vertex: { code: getShaderSource("vs-3d") }, fragment: { code: getShaderSource("fs-3d") } }; + const program3D: RenderPipeline = { vertex: { code: getShaderSource("vs-3d") }, fragment: { code: getShaderSource("fs-3d") } }; // -- Init buffers: vec2 Position, vec2 Texcoord const positions = new Float32Array([ @@ -62,7 +62,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, in_texcoord: { data: texCoords, format: "float32x2" }, @@ -83,7 +83,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init 2D Texture - const texture2D: ITexture = { + const texture2D: Texture = { format: "rgba8unorm", mipLevelCount: 1, size: [512, 512], @@ -91,7 +91,7 @@ import { getShaderSource, loadImage } from "./utility"; image, flipY: false, }], }; - const sampler2D: ISampler = { + const sampler2D: Sampler = { minFilter: "nearest", magFilter: "linear", addressModeU: "clamp-to-edge", @@ -99,17 +99,19 @@ import { getShaderSource, loadImage } from "./utility"; }; // -- Render - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - vertices: vertexArray.vertices, - uniforms: { + bindingResources: { MVP: matrix, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; - const renderObjects: IRenderPassObject[] = []; - const rp: IRenderPass = { + const renderObjects: RenderPassObject[] = []; + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects }; @@ -119,8 +121,8 @@ import { getShaderSource, loadImage } from "./utility"; viewport: { x: viewports[Corners.LEFT].x, y: viewports[Corners.LEFT].y, width: viewports[Corners.LEFT].z, height: viewports[Corners.LEFT].w }, ...ro, pipeline: program, - uniforms: { - ...ro.uniforms, + bindingResources: { + ...ro.bindingResources, diffuse: { texture: texture2D, sampler: sampler2D }, }, }); @@ -131,8 +133,8 @@ import { getShaderSource, loadImage } from "./utility"; viewport: { x: viewports[Corners.RIGHT].x, y: viewports[Corners.RIGHT].y, width: viewports[Corners.RIGHT].z, height: viewports[Corners.RIGHT].w }, ...ro, pipeline: program3D, - uniforms: { - ...ro.uniforms, + bindingResources: { + ...ro.bindingResources, diffuse: { texture: texture3D, sampler: sampler3D }, }, }); @@ -166,15 +168,15 @@ import { getShaderSource, loadImage } from "./utility"; } } - const texture3D: ITexture = { + const texture3D: Texture = { dimension: "3d", format: "r8uint", generateMipmap: true, mipLevelCount: Math.log2(SIZE), size: [SIZE, SIZE, SIZE], - sources: [{ __type: "TextureDataSource", size: [SIZE, SIZE, SIZE], data }], + sources: [{ __type__: "TextureDataSource", size: [SIZE, SIZE, SIZE], data }], }; - const sampler3D: ISampler = { + const sampler3D: Sampler = { lodMinClamp: 0, lodMaxClamp: Math.log2(SIZE), minFilter: "linear", diff --git a/examples/src/WebGL2Samples/texture_integer.ts b/examples/src/WebGL2Samples/texture_integer.ts index 5fe9048bf2fde6139ae9fe3370be46d7728345bb..a771824deaa79f7f7462fbb92c9ea42b2bac73cb 100644 --- a/examples/src/WebGL2Samples/texture_integer.ts +++ b/examples/src/WebGL2Samples/texture_integer.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -11,11 +11,11 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -39,7 +39,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -49,14 +49,14 @@ import { getShaderSource, loadImage } from "./utility"; loadImage("../../assets/img/Di-3d.png", function (image) { // -- Init Texture - const texture: ITexture = { + const texture: Texture = { size: [image.width, image.height], format: "rgba8uint", sources: [{ mipLevel: 0, image, flipY: false, }], }; - const sampler: ISampler = { + const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest", }; @@ -69,17 +69,19 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, }, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects: [ro], }; diff --git a/examples/src/WebGL2Samples/texture_lod.ts b/examples/src/WebGL2Samples/texture_lod.ts index a2c740a4988aed9d54f32b8c199eebec86d5cd6e..7e7d43a29cad4fe3638a22477ec8b6b6e789ddd9 100644 --- a/examples/src/WebGL2Samples/texture_lod.ts +++ b/examples/src/WebGL2Samples/texture_lod.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -11,7 +11,7 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Mouse Behaviour @@ -87,7 +87,7 @@ import { getShaderSource, loadImage } from "./utility"; }; // -- Initialize program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -111,7 +111,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Initialize vertex array - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, textureCoordinates: { data: texcoords, format: "float32x2" }, @@ -120,8 +120,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Load texture then render const imageUrl = "../../assets/img/Di-3d.png"; - const textures: ITexture[] = new Array(Corners.MAX); - const samplers: ISampler[] = new Array(Corners.MAX); + const textures: Texture[] = new Array(Corners.MAX); + const samplers: Sampler[] = new Array(Corners.MAX); loadImage(imageUrl, function (image) { textures[Corners.TOP_LEFT] = { @@ -183,9 +183,9 @@ import { getShaderSource, loadImage } from "./utility"; function render() { - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // Clear color buffer - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects, }; @@ -197,13 +197,15 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const ro: IRenderObject = { + const ro: RenderObject = { pipeline: program, - uniforms: { + bindingResources: { mvp: matrix, }, - vertices: vertexArray.vertices, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }; const lodBiasArray = [0.0, 0.0, 0.0, 0.0]; @@ -215,7 +217,7 @@ import { getShaderSource, loadImage } from "./utility"; { viewport: { x: viewport[i].x, y: viewport[i].y, width: viewport[i].z, height: viewport[i].w }, ...ro, - uniforms: { + bindingResources: { mvp: matrix, lodBias: lodBiasArray[i], diffuse: { texture: textures[i], sampler: samplers[i] }, diff --git a/examples/src/WebGL2Samples/texture_offset.ts b/examples/src/WebGL2Samples/texture_offset.ts index b423614a07d28cf66cb5a6bedcef608ad37f266b..17d708629e31c46311512950c50bcd8e824527c1 100644 --- a/examples/src/WebGL2Samples/texture_offset.ts +++ b/examples/src/WebGL2Samples/texture_offset.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; (function () @@ -10,7 +10,7 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); const Corners = { @@ -36,9 +36,9 @@ import { getShaderSource, loadImage } from "./utility"; }; // -- Init program - const programBicubic: IRenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-bicubic") } }; + const programBicubic: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-bicubic") } }; - const programOffsetBicubic: IRenderPipeline = { + const programOffsetBicubic: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs-offset-bicubic") }, }; @@ -62,7 +62,7 @@ import { getShaderSource, loadImage } from "./utility"; ]); // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes } = { + const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: positions, format: "float32x2" }, texcoord: { data: texCoords, format: "float32x2" }, @@ -72,21 +72,21 @@ import { getShaderSource, loadImage } from "./utility"; loadImage("../../assets/img/Di-3d.png", function (image) { // -- Init Texture - const texture: ITexture = { + const texture: Texture = { size: [image.width, image.height], format: "rgba8unorm", sources: [{ mipLevel: 0, image, flipY: false }], }; - const sampler: ISampler = { + const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", }; - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // -- Render - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects }; @@ -102,13 +102,15 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push( { viewport: { x: viewports[Corners.RIGHT].x, y: viewports[Corners.RIGHT].y, width: viewports[Corners.RIGHT].z, height: viewports[Corners.RIGHT].w }, - vertices: vertexArray.vertices, pipeline: programBicubic, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, }, - drawVertex: { vertexCount: 6 }, + geometry: { + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); // Offset @@ -117,14 +119,16 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push( { viewport: { x: viewports[Corners.LEFT].x, y: viewports[Corners.LEFT].y, width: viewports[Corners.LEFT].z, height: viewports[Corners.LEFT].w }, - vertices: vertexArray.vertices, pipeline: programOffsetBicubic, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, offset, }, - drawVertex: { vertexCount: 6 }, + geometry: { + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_pixel_store.ts b/examples/src/WebGL2Samples/texture_pixel_store.ts index 89d05f524a67c3f59c16fa0d073a7b919d702871..957cb1c0e0fe1b65dc8c86ad50f31787511f0f07 100644 --- a/examples/src/WebGL2Samples/texture_pixel_store.ts +++ b/examples/src/WebGL2Samples/texture_pixel_store.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { getIGLBuffer, IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, RenderPassObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes, VertexDataTypes } from "@feng3d/render-api"; +import { getIGLBuffer, WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; (function () @@ -10,11 +10,11 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -27,7 +27,7 @@ import { getShaderSource, loadImage } from "./utility"; -1.0, 1.0, -1.0, -1.0 ]); - const vertexPosBuffer: IVertexDataTypes = positions; + const vertexPosBuffer: VertexDataTypes = positions; const texCoords = new Float32Array([ 0.0, 1.0, @@ -37,10 +37,10 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const vertexTexBuffer: IVertexDataTypes = texCoords; + const vertexTexBuffer: VertexDataTypes = texCoords; // -- Init VertexArray - const vertexArray: { vertices?: IVertexAttributes, indices?: IIndicesDataTypes } = { + const vertexArray: { vertices?: VertexAttributes, indices?: IIndicesDataTypes } = { vertices: { position: { data: vertexPosBuffer, format: "float32x2" }, texcoord: { data: vertexTexBuffer, format: "float32x2" }, @@ -59,11 +59,11 @@ import { getShaderSource, loadImage } from "./utility"; const pixels = new Uint8Array(imageData.data.buffer); // -- Init Texture - const texture: ITexture = { + const texture: Texture = { size: [image.width / 2, image.height / 2], format: "rgba8unorm", sources: [{ - __type: "TextureDataSource", + __type__: "TextureDataSource", mipLevel: 0, size: [image.width / 2, image.height / 2], data: pixels, @@ -71,14 +71,14 @@ import { getShaderSource, loadImage } from "./utility"; dataImageOrigin: [image.width / 4, image.width / 4], }] }; - const sampler: ISampler = { + const sampler: Sampler = { minFilter: "nearest", magFilter: "nearest", }; - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // -- Render - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects, }; @@ -92,13 +92,15 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push({ pipeline: program, - vertices: vertexArray.vertices, - indices: vertexArray.indices, - uniforms: { + bindingResources: { MVP: matrix, diffuse: { texture, sampler }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices: vertexArray.vertices, + indices: vertexArray.indices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_srgb.ts b/examples/src/WebGL2Samples/texture_srgb.ts index f5a947ec462371ea13b3f7482babc77afe399883..649e08b5a3a9d8ff12e9e64871f38414c2495733 100644 --- a/examples/src/WebGL2Samples/texture_srgb.ts +++ b/examples/src/WebGL2Samples/texture_srgb.ts @@ -1,5 +1,5 @@ -import { IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { getIGLBuffer, IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderPassObject, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes, VertexDataTypes } from "@feng3d/render-api"; +import { getIGLBuffer, WebGL } from "@feng3d/webgl"; import { getShaderSource, loadImage } from "./utility"; @@ -11,12 +11,12 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Initialize program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, }; @@ -30,7 +30,7 @@ import { getShaderSource, loadImage } from "./utility"; -1.0, 1.0, -1.0, -1.0 ]); - const vertexPosBuffer: IVertexDataTypes = positions; + const vertexPosBuffer: VertexDataTypes = positions; const texcoords = new Float32Array([ 0.0, 1.0, @@ -40,11 +40,11 @@ import { getShaderSource, loadImage } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const vertexTexBuffer: IVertexDataTypes = texcoords; + const vertexTexBuffer: VertexDataTypes = texcoords; // -- Initialize vertex array - const vertices: IVertexAttributes = { + const vertices: VertexAttributes = { position: { data: vertexPosBuffer, format: "float32x2" }, textureCoordinates: { data: vertexTexBuffer, format: "float32x2" }, }; @@ -52,8 +52,8 @@ import { getShaderSource, loadImage } from "./utility"; // -- Load texture then render const imageUrl = "../../assets/img/Di-3d.png"; - let texture: ITexture; - let sampler: ISampler; + let texture: Texture; + let sampler: Sampler; loadImage(imageUrl, function (image) { texture = { @@ -68,9 +68,9 @@ import { getShaderSource, loadImage } from "./utility"; function render() { - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // Clear color buffer - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }] }, renderObjects, }; @@ -83,12 +83,14 @@ import { getShaderSource, loadImage } from "./utility"; ]); renderObjects.push({ pipeline: program, - vertices, - uniforms: { + bindingResources: { mvp: matrix, materialDiffuse: { texture, sampler }, }, - drawVertex: { vertexCount: 6 }, + geometry:{ + vertices, + draw: { __type__: "DrawVertex", vertexCount: 6 }, + } }); webgl.submit({ commandEncoders: [{ passEncoders: [rp] }] }); diff --git a/examples/src/WebGL2Samples/texture_vertex.ts b/examples/src/WebGL2Samples/texture_vertex.ts index 79edd93e89a2873300f94e80799655f3f6516cbb..e209feac6f9a1cb50f049270d5a94da79050b373 100644 --- a/examples/src/WebGL2Samples/texture_vertex.ts +++ b/examples/src/WebGL2Samples/texture_vertex.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IPrimitiveTopology, IRenderObject, IRenderPass, IRenderPassObject, IRenderPipeline, ISampler, ITexture, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { getIVertexFormat, IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, GLVertexAttributeTypes, IIndicesDataTypes, RenderPassObject, PrimitiveTopology, RenderPass, RenderPipeline, Sampler, Texture, VertexAttributes, VertexDataTypes, VertexFormat, vertexFormatMap } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4, vec3 } from "gl-matrix"; import { GlTFLoader, Primitive } from "./third-party/gltf-loader"; @@ -7,7 +7,7 @@ import { getShaderSource, loadImage } from "./utility"; (function () { - const IDrawMode2Name: { [key: string]: IPrimitiveTopology } = { + const IDrawMode2Name: { [key: string]: PrimitiveTopology } = { 0: "point-list", 3: "line-strip", 2: "LINE_LOOP", @@ -36,29 +36,25 @@ import { getShaderSource, loadImage } from "./utility"; canvas.height = canvas.width; document.body.appendChild(canvas); - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Init program - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs") }, fragment: { code: getShaderSource("fs") }, depthStencil: { depthCompare: "less" }, }; - const vertexArrayMaps: { [key: string]: { vertices?: IVertexAttributes, indices: IIndicesDataTypes }[] } = {}; + const vertexArrayMaps: { [key: string]: { vertices?: VertexAttributes, indices: IIndicesDataTypes }[] } = {}; // var in loop let mesh; let primitive: Primitive; - let vertexBuffer: IVertexDataTypes; + let vertexBuffer: VertexDataTypes; let indicesBuffer: IIndicesDataTypes; - let texture: ITexture; - let sampler: ISampler; - - const ro: IRenderObject = { - pipeline: program, - }; + let texture: Texture; + let sampler: Sampler; // -- Load model then render const glTFLoader = new GlTFLoader(); @@ -185,9 +181,9 @@ import { getShaderSource, loadImage } from "./utility"; const localMV = mat4.create(); function render() { - const renderObjects: IRenderPassObject[] = []; + const renderObjects: RenderPassObject[] = []; // -- Render - const rp: IRenderPass = { + const rp: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], loadOp: "clear" }], depthStencilAttachment: { depthLoadOp: "clear" } @@ -217,17 +213,19 @@ import { getShaderSource, loadImage } from "./utility"; renderObjects.push({ pipeline: { ...program, - primitive: { topology: IDrawMode2Name[primitive.mode] } }, - vertices: vertexArrayMaps[mid][i].vertices, - indices: vertexArrayMaps[mid][i].indices, - uniforms: { + bindingResources: { mvMatrix: localMV, pMatrix: perspectiveMatrix, displacementMap: { texture, sampler }, diffuse: { texture, sampler }, }, - drawIndexed: { indexCount: primitive.indices.length } + geometry:{ + primitive: { topology: IDrawMode2Name[primitive.mode] }, + vertices: vertexArrayMaps[mid][i].vertices, + indices: vertexArrayMaps[mid][i].indices, + draw: { __type__: "DrawIndexed", indexCount: primitive.indices.length } + } }); } } @@ -236,4 +234,24 @@ import { getShaderSource, loadImage } from "./utility"; requestAnimationFrame(render); } -})(); \ No newline at end of file +})(); + +function getIVertexFormat(numComponents: 1 | 2 | 3 | 4, type: GLVertexAttributeTypes = "FLOAT", normalized = false): VertexFormat +{ + for (const key in vertexFormatMap) + { + const element = vertexFormatMap[key]; + if ( + element.numComponents === numComponents + && element.type === type + && !element.normalized === !normalized + ) + { + return key as VertexFormat; + } + } + + console.error(`没有找到与 ${JSON.stringify({ numComponents, type, normalized })} 对应的顶点数据格式!`); + + return undefined; +} \ No newline at end of file diff --git a/examples/src/WebGL2Samples/transform_feedback_instanced.ts b/examples/src/WebGL2Samples/transform_feedback_instanced.ts index 0fd0b220367e8204721eb6fbbfe3fda6994bc190..fd3872927c85479a53199628f4c2623e9b88265d 100644 --- a/examples/src/WebGL2Samples/transform_feedback_instanced.ts +++ b/examples/src/WebGL2Samples/transform_feedback_instanced.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderObject, IRenderPipeline, ISubmit, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { IGLCanvasContext, IGLTransformFeedback, IGLTransformFeedbackObject, IGLTransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, RenderObject, RenderPipeline, Submit, VertexAttributes, VertexDataTypes } from "@feng3d/render-api"; +import { TransformFeedback, TransformFeedbackObject, TransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; (function () @@ -12,7 +12,7 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // -- Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); canvas.addEventListener("webglcontextlost", function (event) @@ -65,12 +65,12 @@ import { getShaderSource } from "./utility"; const COLOR_LOCATION = 3; const NUM_LOCATIONS = 4; - const vertexArrays: { vertices?: IVertexAttributes, indices?: IIndicesDataTypes }[][] = []; + const vertexArrays: { vertices?: VertexAttributes, indices?: IIndicesDataTypes }[][] = []; // Transform feedback objects track output buffer state - const transformFeedbacks: IGLTransformFeedback[] = []; + const transformFeedbacks: TransformFeedback[] = []; - const vertexBuffers: IVertexDataTypes[][] = new Array(vertexArrays.length); + const vertexBuffers: VertexDataTypes[][] = new Array(vertexArrays.length); for (let i = 0; i < 2; ++i) { @@ -107,13 +107,13 @@ import { getShaderSource } from "./utility"; function initPrograms() { - const programTransform: IGLTransformFeedbackPipeline = { + const programTransform: TransformFeedbackPipeline = { vertex: { code: getShaderSource("vs-emit") }, transformFeedbackVaryings: { varyings: ["v_offset", "v_rotation"], bufferMode: "SEPARATE_ATTRIBS" }, }; // Setup program for draw shader - const programDraw: IRenderPipeline = { + const programDraw: RenderPipeline = { vertex: { code: getShaderSource("vs-draw") }, fragment: { code: getShaderSource("fs-draw"), targets: [{ @@ -123,34 +123,36 @@ import { getShaderSource } from "./utility"; } }] }, - primitive: { topology: "triangle-list" }, }; - const programs: [IGLTransformFeedbackPipeline, IRenderPipeline] = [programTransform, programDraw]; + const programs: [TransformFeedbackPipeline, RenderPipeline] = [programTransform, programDraw]; return programs; } - const transformRO: IGLTransformFeedbackObject = { + const transformRO: TransformFeedbackObject = { pipeline: programs[PROGRAM_TRANSFORM], vertices: null, transformFeedback: null, uniforms: {}, - drawVertex: { vertexCount: NUM_INSTANCES }, + draw: { __type__: "DrawVertex", vertexCount: NUM_INSTANCES }, }; - const renderRO: IRenderObject = { + const renderRO: RenderObject = { viewport: { x: 0, y: 0, width: canvas.width, height: canvas.height - 10 }, pipeline: programs[PROGRAM_DRAW], - uniforms: {}, - drawVertex: { vertexCount: 3, instanceCount: NUM_INSTANCES }, + bindingResources: {}, + geometry:{ + primitive: { topology: "triangle-list" }, + draw: { __type__: "DrawVertex", vertexCount: 3, instanceCount: NUM_INSTANCES }, + } }; - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ { - __type: "TransformFeedbackPass", + __type__: "TransformFeedbackPass", transformFeedbackObjects: [transformRO], }, { @@ -181,8 +183,8 @@ import { getShaderSource } from "./utility"; // Rotate triangles transform(); - renderRO.vertices = vertexArrays[currentSourceIdx][1].vertices; - renderRO.indices = vertexArrays[currentSourceIdx][1].indices; + renderRO.geometry.vertices = vertexArrays[currentSourceIdx][1].vertices; + renderRO.geometry.indices = vertexArrays[currentSourceIdx][1].indices; webgl.submit(submit); diff --git a/examples/src/WebGL2Samples/transform_feedback_interleaved.ts b/examples/src/WebGL2Samples/transform_feedback_interleaved.ts index 769a2c3b5b63af0ce3d1fb13098081f663ee11f5..5c8d459158b4dba01953a550e6d14188c4ff5fbf 100644 --- a/examples/src/WebGL2Samples/transform_feedback_interleaved.ts +++ b/examples/src/WebGL2Samples/transform_feedback_interleaved.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderPipeline, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { getIGLVertexBuffer, IGLCanvasContext, IGLTransformFeedback, IGLTransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, RenderPipeline, VertexAttributes, VertexDataTypes } from "@feng3d/render-api"; +import { getIGLBuffer, TransformFeedback, TransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; (function () @@ -12,7 +12,7 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // -- Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Init Program @@ -21,7 +21,7 @@ import { getShaderSource } from "./utility"; const programTransform = (function (vertexShaderSourceTransform, fragmentShaderSourceTransform) { - const programTransform: IGLTransformFeedbackPipeline = { + const programTransform: TransformFeedbackPipeline = { vertex: { code: vertexShaderSourceTransform }, transformFeedbackVaryings: { varyings: ["gl_Position", "v_color"], bufferMode: "INTERLEAVED_ATTRIBS" }, }; @@ -29,7 +29,7 @@ import { getShaderSource } from "./utility"; return programTransform; })(getShaderSource("vs-transform"), getShaderSource("fs-transform")); - const programFeedback: IRenderPipeline = { + const programFeedback: RenderPipeline = { vertex: { code: getShaderSource("vs-feedback") }, fragment: { code: getShaderSource("fs-feedback") }, }; @@ -45,7 +45,7 @@ import { getShaderSource } from "./utility"; -1.0, -1.0, 0.0, 1.0 ]); - const buffers: IVertexDataTypes[] = [ + const buffers: VertexDataTypes[] = [ // Transform buffer vertices, // Feedback empty buffer @@ -53,7 +53,7 @@ import { getShaderSource } from "./utility"; ]; // -- Init Vertex Array - const vertexArrays: { vertices?: IVertexAttributes, indices?: IIndicesDataTypes }[] = [ + const vertexArrays: { vertices?: VertexAttributes, indices?: IIndicesDataTypes }[] = [ { vertices: { position: { data: buffers[PROGRAM_TRANSFORM], format: "float32x4" }, @@ -68,7 +68,7 @@ import { getShaderSource } from "./utility"; ]; // -- Init TransformFeedback - const transformFeedback: IGLTransformFeedback = { + const transformFeedback: TransformFeedback = { bindBuffers: [ { index: 0, data: buffers[PROGRAM_FEEDBACK] } ] @@ -88,14 +88,14 @@ import { getShaderSource } from "./utility"; commandEncoders: [{ passEncoders: [ { - __type: "TransformFeedbackPass", + __type__: "TransformFeedbackPass", transformFeedbackObjects: [ { pipeline: programTransform, vertices: vertexArrays[PROGRAM_TRANSFORM].vertices, uniforms: { MVP: matrix }, transformFeedback, - drawVertex: { vertexCount: VERTEX_COUNT }, + draw: { __type__: "DrawVertex", vertexCount: VERTEX_COUNT }, } ], }, @@ -105,9 +105,11 @@ import { getShaderSource } from "./utility"; // Second draw, reuse captured attributes { pipeline: programFeedback, - vertices: vertexArrays[PROGRAM_FEEDBACK].vertices, - indices: vertexArrays[PROGRAM_FEEDBACK].indices, - drawVertex: { vertexCount: VERTEX_COUNT }, + geometry:{ + vertices: vertexArrays[PROGRAM_FEEDBACK].vertices, + indices: vertexArrays[PROGRAM_FEEDBACK].indices, + draw: { __type__: "DrawVertex", vertexCount: VERTEX_COUNT }, + }, } ], }] @@ -116,8 +118,8 @@ import { getShaderSource } from "./utility"; // -- Delete WebGL resources webgl.deleteTransformFeedback(transformFeedback); - webgl.deleteBuffer(getIGLVertexBuffer(buffers[PROGRAM_TRANSFORM])); - webgl.deleteBuffer(getIGLVertexBuffer(buffers[PROGRAM_FEEDBACK])); + webgl.deleteBuffer(getIGLBuffer(buffers[PROGRAM_TRANSFORM])); + webgl.deleteBuffer(getIGLBuffer(buffers[PROGRAM_FEEDBACK])); webgl.deleteProgram(programTransform); webgl.deleteProgram(programFeedback); })(); diff --git a/examples/src/WebGL2Samples/transform_feedback_separated.ts b/examples/src/WebGL2Samples/transform_feedback_separated.ts index d6126ec2c17303e4fec79b5d3f2366c0d44632bf..ea4ba53d13cec7b0426e7ecb867e310921d54fd8 100644 --- a/examples/src/WebGL2Samples/transform_feedback_separated.ts +++ b/examples/src/WebGL2Samples/transform_feedback_separated.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderPass, IRenderPassObject, IRenderPipeline, ISubmit, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { IGLCanvasContext, IGLTransformFeedback, IGLTransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, VertexDataTypes, RenderPipeline, Submit, VertexAttributes } from "@feng3d/render-api"; +import { TransformFeedback, TransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; @@ -13,13 +13,13 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // -- Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false }}; const webgl = new WebGL(rc); // -- Init Program const programTransform = (function (vertexShaderSourceTransform, fragmentShaderSourceTransform) { - const transformFeedbackPipeline: IGLTransformFeedbackPipeline = { + const transformFeedbackPipeline: TransformFeedbackPipeline = { vertex: { code: vertexShaderSourceTransform }, transformFeedbackVaryings: { varyings: ["gl_Position", "v_color"], bufferMode: "SEPARATE_ATTRIBS" }, }; @@ -27,7 +27,7 @@ import { getShaderSource } from "./utility"; return transformFeedbackPipeline; })(getShaderSource("vs-transform"), getShaderSource("fs-transform")); - const programFeedback: IRenderPipeline = { + const programFeedback: RenderPipeline = { vertex: { code: getShaderSource("vs-feedback") }, fragment: { code: getShaderSource("fs-feedback") }, }; @@ -54,7 +54,7 @@ import { getShaderSource } from "./utility"; MAX: 3 }; - const buffers: IVertexDataTypes[] = [ + const buffers: VertexDataTypes[] = [ // Transform buffer positions, // Feedback empty buffers @@ -63,7 +63,7 @@ import { getShaderSource } from "./utility"; ]; // -- Init Transform Vertex Array - const vertexArrays: { vertices?: IVertexAttributes, indices?: IIndicesDataTypes }[] = [ + const vertexArrays: { vertices?: VertexAttributes, indices?: IIndicesDataTypes }[] = [ { vertices: { position: { data: buffers[BufferType.VERTEX], format: "float32x4" }, @@ -78,7 +78,7 @@ import { getShaderSource } from "./utility"; ]; // -- Init TransformFeedback - const transformFeedback: IGLTransformFeedback = { + const transformFeedback: TransformFeedback = { bindBuffers: [ { index: 0, data: buffers[BufferType.POSITION] }, { index: 1, data: buffers[BufferType.COLOR] }, @@ -95,18 +95,18 @@ import { getShaderSource } from "./utility"; 0.0, 0.0, 0.0, 1.0 ]); - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ { - __type: "TransformFeedbackPass", + __type__: "TransformFeedbackPass", transformFeedbackObjects: [ { pipeline: programTransform, vertices: vertexArrays[PROGRAM_TRANSFORM].vertices, uniforms: { MVP: matrix }, transformFeedback, - drawVertex: { vertexCount: VERTEX_COUNT }, + draw: { __type__: "DrawVertex", vertexCount: VERTEX_COUNT }, } ] }, @@ -116,9 +116,11 @@ import { getShaderSource } from "./utility"; // Second draw, reuse captured attributes { pipeline: programs[PROGRAM_FEEDBACK], - vertices: vertexArrays[PROGRAM_FEEDBACK].vertices, - indices: vertexArrays[PROGRAM_FEEDBACK].indices, - drawVertex: { vertexCount: VERTEX_COUNT }, + geometry: { + vertices: vertexArrays[PROGRAM_FEEDBACK].vertices, + indices: vertexArrays[PROGRAM_FEEDBACK].indices, + draw: { __type__: "DrawVertex", vertexCount: VERTEX_COUNT }, + } } ], } diff --git a/examples/src/WebGL2Samples/transform_feedback_separated_2.ts b/examples/src/WebGL2Samples/transform_feedback_separated_2.ts index 055ade8dc2882ab523b1d720b6b64deb6dc0c27d..f48d3fc74b566c1ce6a7330c3b4256003ae2daa3 100644 --- a/examples/src/WebGL2Samples/transform_feedback_separated_2.ts +++ b/examples/src/WebGL2Samples/transform_feedback_separated_2.ts @@ -1,5 +1,5 @@ -import { IIndicesDataTypes, IRenderObject, IRenderPipeline, ISubmit, IVertexAttributes, IVertexDataTypes } from "@feng3d/render-api"; -import { IGLCanvasContext, IGLTransformFeedback, IGLTransformFeedbackObject, IGLTransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; +import { CanvasContext, IIndicesDataTypes, RenderObject, RenderPipeline, Submit, VertexAttributes, VertexDataTypes } from "@feng3d/render-api"; +import { TransformFeedback, TransformFeedbackObject, TransformFeedbackPipeline, WebGL } from "@feng3d/webgl"; import { getShaderSource } from "./utility"; @@ -13,7 +13,7 @@ import { getShaderSource } from "./utility"; document.body.appendChild(canvas); // -- Init WebGL Context - const rc: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2", antialias: false }; + const rc: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2", webGLContextAttributes: { antialias: false } }; const webgl = new WebGL(rc); canvas.addEventListener("webglcontextlost", function (event) @@ -58,12 +58,12 @@ import { getShaderSource } from "./utility"; const NUM_LOCATIONS = 5; // -- Init Vertex Arrays and Buffers - const vertexArrays: { vertices?: IVertexAttributes, indices?: IIndicesDataTypes }[][] = []; + const vertexArrays: { vertices?: VertexAttributes, indices?: IIndicesDataTypes }[][] = []; // Transform feedback objects track output buffer state - const transformFeedbacks: IGLTransformFeedback[] = []; + const transformFeedbacks: TransformFeedback[] = []; - const vertexBuffers: IVertexDataTypes[][] = new Array(vertexArrays.length); + const vertexBuffers: VertexDataTypes[][] = new Array(vertexArrays.length); for (let i = 0; i < 2; ++i) { @@ -108,14 +108,14 @@ import { getShaderSource } from "./utility"; }; } - function initProgram(): [IGLTransformFeedbackPipeline, IRenderPipeline] + function initProgram(): [TransformFeedbackPipeline, RenderPipeline] { - const transformFeedbackPipeline: IGLTransformFeedbackPipeline = { + const transformFeedbackPipeline: TransformFeedbackPipeline = { vertex: { code: getShaderSource("vs-emit") }, transformFeedbackVaryings: { varyings: ["v_position", "v_velocity", "v_spawntime", "v_lifetime"], bufferMode: "SEPARATE_ATTRIBS" }, }; - const program: IRenderPipeline = { + const program: RenderPipeline = { vertex: { code: getShaderSource("vs-draw") }, fragment: { code: getShaderSource("fs-draw"), @@ -126,36 +126,38 @@ import { getShaderSource } from "./utility"; } }] }, - primitive: { topology: "point-list" }, }; return [transformFeedbackPipeline, program]; } - const transformRO: IGLTransformFeedbackObject = { + const transformRO: TransformFeedbackObject = { pipeline: transformFeedbackPipeline, vertices: null, transformFeedback: null, uniforms: { u_acceleration: [0.0, ACCELERATION], }, - drawVertex: { vertexCount: NUM_PARTICLES }, + draw: { __type__: "DrawVertex", vertexCount: NUM_PARTICLES }, }; - const renderRO: IRenderObject = { + const renderRO: RenderObject = { viewport: { x: 0, y: 0, width: canvas.width, height: canvas.height - 10 }, pipeline: program, - uniforms: { + bindingResources: { u_color: [0.0, 1.0, 1.0, 1.0], }, - drawVertex: { vertexCount: NUM_PARTICLES }, + geometry: { + draw: { __type__: "DrawVertex", vertexCount: NUM_PARTICLES }, + primitive: { topology: "point-list" }, + } }; - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ { - __type: "TransformFeedbackPass", + __type__: "TransformFeedbackPass", transformFeedbackObjects: [transformRO], }, { @@ -186,8 +188,8 @@ import { getShaderSource } from "./utility"; transform(); // - renderRO.vertices = vertexArrays[currentSourceIdx][1].vertices; - renderRO.indices = vertexArrays[currentSourceIdx][1].indices; + renderRO.geometry.vertices = vertexArrays[currentSourceIdx][1].vertices; + renderRO.geometry.indices = vertexArrays[currentSourceIdx][1].indices; webgl.submit(submit); diff --git a/examples/src/regl-examples/basic.ts b/examples/src/regl-examples/basic.ts index 0e8551cba0c613571be2edd537c685353d0ccc5e..bff99361ff9f630aab2a0ecc6d565a31509f5dc4 100644 --- a/examples/src/regl-examples/basic.ts +++ b/examples/src/regl-examples/basic.ts @@ -1,4 +1,4 @@ -import { IRenderObject } from "@feng3d/render-api"; +import { RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; /** @@ -16,18 +16,21 @@ document.body.appendChild(canvas); const webgl = new WebGL({ canvasId: "glcanvas" }); -const renderObject: IRenderObject = { - vertices: { - position: { - data: new Float32Array([ - -1, 0, - 0, -1, - 1, 1 - ]), - format: "float32x2", +const renderObject: RenderObject = { + geometry: { + vertices: { + position: { + data: new Float32Array([ + -1, 0, + 0, -1, + 1, 1 + ]), + format: "float32x2", + }, }, + draw: { __type__: "DrawVertex", vertexCount: 3 }, }, - uniforms: { color: [1, 0, 0, 1] }, + bindingResources: { color: [1, 0, 0, 1] }, pipeline: { vertex: { code: ` @@ -49,7 +52,6 @@ const renderObject: IRenderObject = { }, depthStencil: {}, }, - drawVertex: { vertexCount: 3 }, }; function draw() diff --git a/examples/src/regl-examples/batch.ts b/examples/src/regl-examples/batch.ts index aa974ad8aacd6154dedd252a3cc0e73c1bdb5d6c..f46defb1cb80a3a34e35b9dceb145636105f2274 100644 --- a/examples/src/regl-examples/batch.ts +++ b/examples/src/regl-examples/batch.ts @@ -1,4 +1,4 @@ -import { IRenderObject, IRenderPipeline, ISubmit, IVertexAttributes } from "@feng3d/render-api"; +import { RenderObject, RenderPipeline, Submit, VertexAttributes } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; const canvas = document.createElement("canvas"); @@ -21,7 +21,7 @@ const offsets = [{ offset: [-1, -1] }, { offset: [1, 0] }, { offset: [1, 1] }]; -const pipeline: IRenderPipeline = { +const pipeline: RenderPipeline = { vertex: { code: `precision mediump float; attribute vec2 position; @@ -41,7 +41,7 @@ const pipeline: IRenderPipeline = { depthStencil: { depthWriteEnabled: false }, }; -const vertexArray: { vertices?: IVertexAttributes } = { +const vertexArray: { vertices?: VertexAttributes } = { vertices: { position: { data: new Float32Array([ @@ -56,25 +56,27 @@ const vertexArray: { vertices?: IVertexAttributes } = { function getRenderObject(batchId: number) { - const renderObject: IRenderObject = { - vertices: vertexArray.vertices, - uniforms: { + const renderObject: RenderObject = { + geometry: { + vertices: vertexArray.vertices, + draw: { __type__: "DrawVertex", vertexCount: 3 } + }, + bindingResources: { offset: offsets[batchId].offset, }, - pipeline, - drawVertex: { vertexCount: 3 } + pipeline: pipeline, }; return renderObject; } -const renderObjects: IRenderObject[] = []; +const renderObjects: RenderObject[] = []; for (let i = 0; i < offsets.length; i++) { renderObjects.push(getRenderObject(i)); } -const submit: ISubmit = { +const submit: Submit = { commandEncoders: [{ passEncoders: [ { @@ -97,12 +99,12 @@ function draw() batchId = i; // const ro = renderObjects[i]; - ro.uniforms.color = [ + ro.bindingResources.color = [ Math.sin(0.02 * ((0.1 + Math.sin(batchId)) * tick + 3.0 * batchId)), Math.cos(0.02 * (0.02 * tick + 0.1 * batchId)), Math.sin(0.02 * ((0.3 + Math.cos(2.0 * batchId)) * tick + 0.8 * batchId)), 1]; - ro.uniforms.angle = 0.01 * tick; + ro.bindingResources.angle = 0.01 * tick; } webgl.submit(submit); diff --git a/examples/src/regl-examples/bunny.ts b/examples/src/regl-examples/bunny.ts index 388a1960add266a3624b8221e383d8c65198caa7..04e3a590dab1c168f1659d45b2d9ec8ac7c02b41 100644 --- a/examples/src/regl-examples/bunny.ts +++ b/examples/src/regl-examples/bunny.ts @@ -1,4 +1,4 @@ -import { IRenderObject, ISubmit } from "@feng3d/render-api"; +import { Submit, RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import * as bunny from "./mikolalysenko/bunny"; @@ -10,7 +10,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const webgl = new WebGL({ canvasId: "glcanvas", antialias: true }); +const webgl = new WebGL({ canvasId: "glcanvas", webGLContextAttributes: { antialias: true } }); const positions = bunny.positions.reduce((pv: number[], cv: number[]) => { @@ -30,13 +30,15 @@ let tick = 0; let viewportWidth = canvas.clientWidth; let viewportHeight = canvas.clientHeight; -const renderObject: IRenderObject = { - vertices: { - position: { data: new Float32Array(positions), format: "float32x3" }, +const renderObject: RenderObject = { + geometry: { + vertices: { + position: { data: new Float32Array(positions), format: "float32x3" }, + }, + indices: new Uint16Array(indices), + draw: { __type__: "DrawIndexed", indexCount: indices.length }, }, - indices: new Uint16Array(indices), - drawIndexed: { indexCount: indices.length }, - uniforms: { + bindingResources: { model: mat4.identity([]), }, pipeline: { @@ -58,7 +60,7 @@ const renderObject: IRenderObject = { } }; -const submit: ISubmit = { +const submit: Submit = { commandEncoders: [{ passEncoders: [ { @@ -77,12 +79,12 @@ function draw() tick++; const t = 0.01 * tick; - renderObject.uniforms.view = mat4.lookAt([], + renderObject.bindingResources.view = mat4.lookAt([], [30 * Math.cos(t), 2.5, 30 * Math.sin(t)], [0, 2.5, 0], [0, 1, 0]); - renderObject.uniforms.projection + renderObject.bindingResources.projection = mat4.perspective([], Math.PI / 4, viewportWidth / viewportHeight, diff --git a/examples/src/regl-examples/camera.ts b/examples/src/regl-examples/camera.ts index ae5f99ab83847731f9b63e55dc09e4d7ebdb5b6a..da3e3b02456f00c561588f2fccbe6759b1d4753b 100644 --- a/examples/src/regl-examples/camera.ts +++ b/examples/src/regl-examples/camera.ts @@ -1,4 +1,4 @@ -import { IRenderObject } from "@feng3d/render-api"; +import { RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { angleNormals } from "./mikolalysenko/angle-normals"; @@ -11,7 +11,7 @@ canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas); -const webgl = new WebGL({ canvasId: "glcanvas", antialias: true }); +const webgl = new WebGL({ canvasId: "glcanvas", webGLContextAttributes: { antialias: true } }); const camera = createCamera({ center: [0, 2.5, 0] @@ -21,14 +21,16 @@ const positions = bunny.positions.flat(); const indices = bunny.cells.flat(); const normals = angleNormals(bunny.cells, bunny.positions).flat(); -const renderObject: IRenderObject = { - vertices: { - position: { data: new Float32Array(positions), format: "float32x3" }, - normal: { data: new Float32Array(normals), format: "float32x3" }, +const renderObject: RenderObject = { + geometry: { + vertices: { + position: { data: new Float32Array(positions), format: "float32x3" }, + normal: { data: new Float32Array(normals), format: "float32x3" }, + }, + indices: new Uint16Array(indices), + draw: { __type__: "DrawIndexed", indexCount: indices.length }, }, - indices: new Uint16Array(indices), - drawIndexed: { indexCount: indices.length }, - uniforms: {}, + bindingResources: {}, pipeline: { vertex: { code: `precision mediump float; diff --git a/examples/src/regl-examples/cloth.ts b/examples/src/regl-examples/cloth.ts index b67600e519e0fe23a8847c9abc61a1f338a90d10..7682ac080e73056db2653d96c3e40f80ba764551 100644 --- a/examples/src/regl-examples/cloth.ts +++ b/examples/src/regl-examples/cloth.ts @@ -1,5 +1,5 @@ -import { IRenderObject, ISubmit } from "@feng3d/render-api"; -import { getIGLVertexBuffer, IGLSamplerTexture, WebGL } from "@feng3d/webgl"; +import { RenderObject, Submit } from "@feng3d/render-api"; +import { getIGLBuffer, SamplerTexture, WebGL } from "@feng3d/webgl"; import { fit } from "./hughsk/canvas-fit"; import { attachCamera } from "./hughsk/canvas-orbit-camera"; @@ -164,18 +164,20 @@ import * as vec3 from "./stackgl/gl-vec3"; let viewportWidth = 1; let viewportHeight = 1; - const renderObject: IRenderObject = { - vertices: { - position: { data: new Float32Array(positions), format: "float32x3" }, - normal: { data: new Float32Array(normals), format: "float32x3" }, - uv: { data: new Float32Array(uvs), format: "float32x2" }, + const renderObject: RenderObject = { + geometry:{ + vertices: { + position: { data: new Float32Array(positions), format: "float32x3" }, + normal: { data: new Float32Array(normals), format: "float32x3" }, + uv: { data: new Float32Array(uvs), format: "float32x2" }, + }, + indices: new Uint16Array(indices), + draw: { __type__: "DrawIndexed", indexCount: indices.length }, }, - indices: new Uint16Array(indices), - drawIndexed: { indexCount: indices.length }, - uniforms: {}, + bindingResources: {}, pipeline: { vertex: { - code: `precision mediump float; + code: /* wgsl */`precision mediump float; attribute vec3 position; attribute vec3 normal; @@ -192,7 +194,7 @@ import * as vec3 from "./stackgl/gl-vec3"; gl_Position = projection * view * vec4(position, 1); }` }, fragment: { - code: `precision mediump float; + code: /* wgsl */`precision mediump float; varying vec2 vUv; varying vec3 vNormal; @@ -221,7 +223,7 @@ import * as vec3 from "./stackgl/gl-vec3"; } }; - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ { @@ -359,8 +361,8 @@ import * as vec3 from "./stackgl/gl-vec3"; return pv; }, []); - getIGLVertexBuffer(renderObject.vertices.position.data).data = new Float32Array(positions); - getIGLVertexBuffer(renderObject.vertices.normal.data).data = new Float32Array(normals); + getIGLBuffer(renderObject.geometry.vertices.position.data).data = new Float32Array(positions); + getIGLBuffer(renderObject.geometry.vertices.normal.data).data = new Float32Array(normals); tick++; @@ -369,8 +371,8 @@ import * as vec3 from "./stackgl/gl-vec3"; camera.tick(); - renderObject.uniforms.view = camera.view(); - renderObject.uniforms.projection + renderObject.bindingResources.view = camera.view(); + renderObject.bindingResources.projection = mat4.perspective([], Math.PI / 4, viewportWidth / viewportHeight, @@ -386,14 +388,14 @@ import * as vec3 from "./stackgl/gl-vec3"; img.src = "../../assets/cloth.png"; await img.decode(); - const diffuse: IGLSamplerTexture = { + const diffuse: SamplerTexture = { texture: { size: [img.width, img.height], generateMipmap: true, sources: [{ image: img }] }, sampler: { minFilter: "linear", mipmapFilter: "linear", addressModeU: "repeat", addressModeV: "repeat" } }; - renderObject.uniforms.texture = diffuse; + renderObject.bindingResources.texture = diffuse; draw(); })(); \ No newline at end of file diff --git a/examples/src/regl-examples/cube.ts b/examples/src/regl-examples/cube.ts index f1dfad842bb3d02f8dfd370a4ec7ed48466f6910..1b1e80c67483470e6ef44298826f7e9cdfb157c4 100644 --- a/examples/src/regl-examples/cube.ts +++ b/examples/src/regl-examples/cube.ts @@ -1,5 +1,5 @@ -import { IRenderObject, ISubmit } from "@feng3d/render-api"; -import { IGLSamplerTexture, WebGL } from "@feng3d/webgl"; +import { RenderObject, Submit } from "@feng3d/render-api"; +import { SamplerTexture, WebGL } from "@feng3d/webgl"; import * as mat4 from "./stackgl/gl-mat4"; (async () => @@ -64,14 +64,16 @@ import * as mat4 from "./stackgl/gl-mat4"; let viewportWidth = 1; let viewportHeight = 1; - const renderObject: IRenderObject = { - vertices: { - position: { data: new Float32Array(positions), format: "float32x3" }, - uv: { data: new Float32Array(uvs), format: "float32x2" }, + const renderObject: RenderObject = { + geometry:{ + vertices: { + position: { data: new Float32Array(positions), format: "float32x3" }, + uv: { data: new Float32Array(uvs), format: "float32x2" }, + }, + indices: new Uint16Array(indices), + draw: { __type__: "DrawIndexed", indexCount: indices.length }, }, - indices: new Uint16Array(indices), - drawIndexed: { indexCount: indices.length }, - uniforms: {}, + bindingResources: {}, pipeline: { vertex: { code: `precision mediump float; @@ -96,7 +98,7 @@ import * as mat4 from "./stackgl/gl-mat4"; } }; - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ { @@ -114,11 +116,11 @@ import * as mat4 from "./stackgl/gl-mat4"; viewportHeight = canvas.height = canvas.clientHeight; const t = 0.01 * tick; - renderObject.uniforms.view = mat4.lookAt([], + renderObject.bindingResources.view = mat4.lookAt([], [5 * Math.cos(t), 2.5 * Math.sin(t), 5 * Math.sin(t)], [0, 0.0, 0], [0, 1, 0]); - renderObject.uniforms.projection + renderObject.bindingResources.projection = mat4.perspective([], Math.PI / 4, viewportWidth / viewportHeight, @@ -134,13 +136,13 @@ import * as mat4 from "./stackgl/gl-mat4"; img.src = "../../assets/peppers.png"; await img.decode(); - const diffuse: IGLSamplerTexture = { + const diffuse: SamplerTexture = { texture: { size: [img.width, img.height], sources: [{ image: img }] }, sampler: { minFilter: "linear" } }; - renderObject.uniforms.tex = diffuse; + renderObject.bindingResources.tex = diffuse; draw(); })(); \ No newline at end of file diff --git a/examples/src/regl-examples/util/camera.ts b/examples/src/regl-examples/util/camera.ts index c477f796089b728274880217bf1091f4c339c5e4..9564a6a8b104c1016cf016062e1842a104114fdc 100644 --- a/examples/src/regl-examples/util/camera.ts +++ b/examples/src/regl-examples/util/camera.ts @@ -1,4 +1,4 @@ -import { IRenderObject } from "@feng3d/render-api"; +import { RenderObject } from "@feng3d/render-api"; import { mouseListen as mouseChange } from "../mikolalysenko/mouse-change"; import { mouseWheelListen as mouseWheel } from "../mikolalysenko/mouse-wheel"; @@ -101,21 +101,21 @@ export function createCamera(props) lookAt(cameraState.view, eye, center, up); } - const injectContext = (renderObject: IRenderObject, viewportWidth: number, viewportHeight: number) => + const injectContext = (renderObject: RenderObject, viewportWidth: number, viewportHeight: number) => { Object.keys(cameraState).forEach(function (name) { - renderObject.uniforms[name] = setupCamera[name]; + renderObject.bindingResources[name] = setupCamera[name]; }); - renderObject.uniforms["projection"] = perspective(cameraState.projection, + renderObject.bindingResources["projection"] = perspective(cameraState.projection, Math.PI / 4.0, viewportWidth / viewportHeight, 0.01, 1000.0); }; - function setupCamera(renderObject: IRenderObject, viewportWidth: number, viewportHeight: number) + function setupCamera(renderObject: RenderObject, viewportWidth: number, viewportHeight: number) { updateCamera(); injectContext(renderObject, viewportWidth, viewportHeight); diff --git a/examples/src/test/RenderObjectChanges.html b/examples/src/test/RenderObjectChanges.html new file mode 100644 index 0000000000000000000000000000000000000000..a9750b638e93186b93bdb30ed0f23809786ff735 --- /dev/null +++ b/examples/src/test/RenderObjectChanges.html @@ -0,0 +1,17 @@ + + + +
+ ++ 测试渲染对象数据变化。 +
+ + + + \ No newline at end of file diff --git a/examples/src/test/RenderObjectChanges.ts b/examples/src/test/RenderObjectChanges.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9da5e74f1ef6c912626d8f21740c7a30c477830 --- /dev/null +++ b/examples/src/test/RenderObjectChanges.ts @@ -0,0 +1,105 @@ +import { Submit, RenderObject } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; + +const init = async (canvas: HTMLCanvasElement) => +{ + const devicePixelRatio = window.devicePixelRatio || 1; + canvas.width = canvas.clientWidth * devicePixelRatio; + canvas.height = canvas.clientHeight * devicePixelRatio; + + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // 初始化WebGL + + const renderObject: RenderObject = { // 渲染对象 + pipeline: { // 渲染管线 + vertex: { // 顶点着色器 + code: ` + attribute vec4 position; + + void main() { + gl_Position = position; + } + ` }, + fragment: { // 片段着色器 + code: ` + precision highp float; + uniform vec4 color; + void main() { + gl_FragColor = color; + } + ` }, + }, + geometry: { + vertices: { + position: { data: new Float32Array([0.0, 0.5, -0.5, -0.5, 0.5, -0.5]), format: "float32x2" }, // 顶点坐标数据 + }, + indices: new Uint16Array([0, 1, 2]), // 顶点索引数据 + draw: { __type__: "DrawIndexed", indexCount: 3 }, // 绘制命令 + }, + bindingResources: { color: [1, 0, 0, 1] }, // Uniform 颜色值。 + }; + + const submit: Submit = { // 一次GPU提交 + commandEncoders: [ // 命令编码列表 + { + passEncoders: [ // 通道编码列表 + { // 渲染通道 + descriptor: { // 渲染通道描述 + colorAttachments: [{ // 颜色附件 + clearValue: [0.0, 0.0, 0.0, 1.0], // 渲染前填充颜色 + }], + }, + renderObjects: [renderObject] + }, + ] + } + ], + }; + + function render() + { + webgl.submit(submit); // 提交GPU执行 + requestAnimationFrame(render); + } + + render(); + + window.onclick = () => + { + // 修改顶点着色器代码 + renderObject.pipeline.vertex.code = ` + attribute vec4 position; + + void main() { + vec4 pos = position; + pos.x = pos.x + 0.5; + gl_Position = pos; + } + `; + + // 修改片段着色器代码 + renderObject.pipeline.fragment.code = ` + precision highp float; + uniform vec4 color; + void main() { + vec4 col = color; + col.x = 0.5; + col.y = 0.6; + col.z = 0.7; + gl_FragColor = col; + } + `; + // + // renderObject.uniforms.color = [0, 1, 0, 1]; + }; +}; + +let webglCanvas = document.querySelector("#glcanvas") as HTMLCanvasElement; +if (!webglCanvas) +{ + webglCanvas = document.createElement("canvas"); + webglCanvas.id = "webgpu"; + webglCanvas.style.width = "400px"; + webglCanvas.style.height = "300px"; + document.body.appendChild(webglCanvas); +} +init(webglCanvas); \ No newline at end of file diff --git a/examples/src/test/fractalCube.ts b/examples/src/test/fractalCube.ts index 35e98edbdc6fb244b64830bca35876f532b0c5a9..499f776451e3bd331a2c20794a1964b0a5175d73 100644 --- a/examples/src/test/fractalCube.ts +++ b/examples/src/test/fractalCube.ts @@ -1,5 +1,5 @@ -import { IRenderObject, ISampler, ISubmit, ITexture } from "@feng3d/render-api"; -import { IGLCanvasContext, WebGL } from "@feng3d/webgl"; +import { CanvasContext, RenderObject, Sampler, Submit, Texture } from "@feng3d/render-api"; +import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; let cubeRotation = 0.0; @@ -13,7 +13,7 @@ async function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const renderingContext: IGLCanvasContext = { canvasId: "glcanvas", contextId: "webgl2" }; + const renderingContext: CanvasContext = { canvasId: "glcanvas", webGLcontextId: "webgl2" }; const webgl = new WebGL(renderingContext); @@ -22,13 +22,12 @@ async function main() const buffers = initBuffers(); const texture: { - texture: ITexture; - sampler: ISampler; + texture: Texture; + sampler: Sampler; } = { texture: { size: [canvas.width, canvas.height] }, sampler: {} }; - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-list" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -62,22 +61,25 @@ async function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x3", - data: buffers.position, - }, - aTextureCoord: { - format: "float32x2", - data: buffers.textureCoord, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: { + aVertexPosition: { + format: "float32x3", + data: buffers.position, + }, + aTextureCoord: { + format: "float32x2", + data: buffers.textureCoord, + }, }, + indices: buffers.indices, + draw: { __type__: "DrawIndexed", firstIndex: 0, indexCount: 36 }, }, - indices: buffers.indices, - uniforms: { uSampler: texture }, - drawIndexed: { firstIndex: 0, indexCount: 36 }, + bindingResources: { uSampler: texture }, }; - const submit: ISubmit = { + const submit: Submit = { commandEncoders: [{ passEncoders: [ // 绘制 @@ -90,7 +92,7 @@ async function main() }, // 从画布中拷贝到纹理。 { - __type: "CopyTextureToTexture", + __type__: "CopyTextureToTexture", source: { texture: null }, // 当值设置为 null或者undefined时表示当前画布。 destination: { texture: texture.texture }, copySize: [canvas.width, canvas.height], @@ -111,8 +113,8 @@ async function main() const { projectionMatrix, modelViewMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; webgl.submit(submit); diff --git a/examples/src/webgl-examples/sample1.ts b/examples/src/webgl-examples/sample1.ts index 9f334e70c24128ea28840ceffd10087c4f2db113..4f95e13458451448e0dc5fa3c411f66d1fd58bdb 100644 --- a/examples/src/webgl-examples/sample1.ts +++ b/examples/src/webgl-examples/sample1.ts @@ -1,4 +1,4 @@ -import { ISubmit } from "@feng3d/render-api"; +import { Submit } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; const init = async (canvas: HTMLCanvasElement) => @@ -7,9 +7,9 @@ const init = async (canvas: HTMLCanvasElement) => canvas.width = canvas.clientWidth * devicePixelRatio; canvas.height = canvas.clientHeight * devicePixelRatio; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); // 初始化WebGL + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // 初始化WebGL - const submit: ISubmit = { // 一次GPU提交 + const submit: Submit = { // 一次GPU提交 commandEncoders: [ // 命令编码列表 { passEncoders: [ // 通道编码列表 @@ -39,12 +39,14 @@ const init = async (canvas: HTMLCanvasElement) => } ` }, }, - vertices: { - position: { data: new Float32Array([0.0, 0.5, -0.5, -0.5, 0.5, -0.5]), format: "float32x2" }, // 顶点坐标数据 + geometry: { + vertices: { + position: { data: new Float32Array([0.0, 0.5, -0.5, -0.5, 0.5, -0.5]), format: "float32x2" }, // 顶点坐标数据 + }, + indices: new Uint16Array([0, 1, 2]), // 顶点索引数据 + draw: { __type__: "DrawIndexed", indexCount: 3 }, // 绘制命令 }, - indices: new Uint16Array([0, 1, 2]), // 顶点索引数据 - uniforms: { color: [1, 0, 0, 1] }, // Uniform 颜色值。 - drawIndexed: { indexCount: 3 }, // 绘制命令 + bindingResources: { color: [1, 0, 0, 1] }, // Uniform 颜色值。 }] }, ] diff --git a/examples/src/webgl-examples/sample2.ts b/examples/src/webgl-examples/sample2.ts index be3e0e323351f21993b212042f8907b2d732cca8..05e340d55e61faaf62afbe5e06adfcb2868726af 100644 --- a/examples/src/webgl-examples/sample2.ts +++ b/examples/src/webgl-examples/sample2.ts @@ -1,4 +1,4 @@ -import { IRenderPass } from "@feng3d/render-api"; +import { RenderPass } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; @@ -10,9 +10,9 @@ function main() const { projectionMatrix, modelViewMatrix } = drawScene(canvas); - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0, 0, 0, 1], @@ -25,7 +25,6 @@ function main() }, renderObjects: [{ pipeline: { - primitive: { topology: "triangle-strip" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -42,22 +41,25 @@ function main() }` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x2", - data: new Float32Array([ - 1.0, 1.0, - -1.0, 1.0, - 1.0, -1.0, - -1.0, -1.0, - ]), - } + geometry: { + primitive: { topology: "triangle-strip" }, + vertices: { + aVertexPosition: { + format: "float32x2", + data: new Float32Array([ + 1.0, 1.0, + -1.0, 1.0, + 1.0, -1.0, + -1.0, -1.0, + ]), + } + }, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 4 }, }, - uniforms: { + bindingResources: { uProjectionMatrix: projectionMatrix, uModelViewMatrix: modelViewMatrix, }, - drawVertex: { firstVertex: 0, vertexCount: 4 }, }], }; diff --git a/examples/src/webgl-examples/sample3.ts b/examples/src/webgl-examples/sample3.ts index b666ea90f45effc850ccfc70360f114250e8a562..3c9443e7e1278d7ba880b34f6c02b8584cf7897d 100644 --- a/examples/src/webgl-examples/sample3.ts +++ b/examples/src/webgl-examples/sample3.ts @@ -1,4 +1,4 @@ -import { IRenderPass } from "@feng3d/render-api"; +import { RenderPass } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; @@ -14,9 +14,9 @@ function main() // Draw the scene const { projectionMatrix, modelViewMatrix } = drawScene(canvas); - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -29,7 +29,6 @@ function main() }, renderObjects: [{ pipeline: { - primitive: { topology: "triangle-strip" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -54,31 +53,34 @@ function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x2", - data: new Float32Array([ - 1.0, 1.0, - -1.0, 1.0, - 1.0, -1.0, - -1.0, -1.0, - ]), - }, - aVertexColor: { - format: "float32x4", - data: new Float32Array([ - 1.0, 1.0, 1.0, 1.0, // white - 1.0, 0.0, 0.0, 1.0, // red - 0.0, 1.0, 0.0, 1.0, // green - 0.0, 0.0, 1.0, 1.0, // blue - ]), + geometry: { + primitive: { topology: "triangle-strip" }, + vertices: { + aVertexPosition: { + format: "float32x2", + data: new Float32Array([ + 1.0, 1.0, + -1.0, 1.0, + 1.0, -1.0, + -1.0, -1.0, + ]), + }, + aVertexColor: { + format: "float32x4", + data: new Float32Array([ + 1.0, 1.0, 1.0, 1.0, // white + 1.0, 0.0, 0.0, 1.0, // red + 0.0, 1.0, 0.0, 1.0, // green + 0.0, 0.0, 1.0, 1.0, // blue + ]), + }, }, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 4 }, }, - uniforms: { + bindingResources: { uProjectionMatrix: projectionMatrix, uModelViewMatrix: modelViewMatrix, }, - drawVertex: { firstVertex: 0, vertexCount: 4 }, }], }; diff --git a/examples/src/webgl-examples/sample4.ts b/examples/src/webgl-examples/sample4.ts index bf89c1f6e748764bfc449eaaaed35a99fddbf8c4..63f20cc06fcfded9752fc5b991c4e0e3493f34bd 100644 --- a/examples/src/webgl-examples/sample4.ts +++ b/examples/src/webgl-examples/sample4.ts @@ -1,4 +1,4 @@ -import { IRenderObject, IRenderPass } from "@feng3d/render-api"; +import { RenderPass, RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; @@ -13,11 +13,10 @@ function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-strip" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -42,31 +41,34 @@ function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x2", - data: new Float32Array([ - 1.0, 1.0, - -1.0, 1.0, - 1.0, -1.0, - -1.0, -1.0, - ]), - }, - aVertexColor: { - format: "float32x4", - data: new Float32Array([ - 1.0, 1.0, 1.0, 1.0, // white - 1.0, 0.0, 0.0, 1.0, // red - 0.0, 1.0, 0.0, 1.0, // green - 0.0, 0.0, 1.0, 1.0, // blue - ]), + geometry:{ + primitive: { topology: "triangle-strip" }, + vertices: { + aVertexPosition: { + format: "float32x2", + data: new Float32Array([ + 1.0, 1.0, + -1.0, 1.0, + 1.0, -1.0, + -1.0, -1.0, + ]), + }, + aVertexColor: { + format: "float32x4", + data: new Float32Array([ + 1.0, 1.0, 1.0, 1.0, // white + 1.0, 0.0, 0.0, 1.0, // red + 0.0, 1.0, 0.0, 1.0, // green + 0.0, 0.0, 1.0, 1.0, // blue + ]), + }, }, + draw: { __type__: "DrawVertex", firstVertex: 0, vertexCount: 4 }, }, - uniforms: {}, - drawVertex: { firstVertex: 0, vertexCount: 4 }, + bindingResources: {}, }; - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -91,8 +93,8 @@ function main() const { projectionMatrix, modelViewMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; webgl.submit({ commandEncoders: [{ passEncoders: [renderPass] }] }); diff --git a/examples/src/webgl-examples/sample5.ts b/examples/src/webgl-examples/sample5.ts index b05b586c89a2d5ebb03295ff4cc90e2faf2a8f4d..c86be974a860faae1bdc682633d0d6d7a7ceb02c 100644 --- a/examples/src/webgl-examples/sample5.ts +++ b/examples/src/webgl-examples/sample5.ts @@ -1,4 +1,4 @@ -import { IRenderObject, IRenderPass } from "@feng3d/render-api"; +import { RenderPass, RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; @@ -13,15 +13,14 @@ function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // Here's where we call the routine that builds all the // objects we'll be drawing. const buffers = initBuffers(); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-list" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -46,22 +45,25 @@ function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x3", - data: buffers.position, - }, - aVertexColor: { - format: "float32x4", - data: buffers.color, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: { + aVertexPosition: { + format: "float32x3", + data: buffers.position, + }, + aVertexColor: { + format: "float32x4", + data: buffers.color, + }, }, + indices: buffers.indices, + draw: { __type__: "DrawIndexed", firstIndex: 0, indexCount: 36 }, }, - indices: buffers.indices, - uniforms: {}, - drawIndexed: { firstIndex: 0, indexCount: 36 }, + bindingResources: {}, }; - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -86,8 +88,8 @@ function main() const { projectionMatrix, modelViewMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; webgl.submit({ commandEncoders: [{ passEncoders: [renderPass] }] }); diff --git a/examples/src/webgl-examples/sample6.ts b/examples/src/webgl-examples/sample6.ts index 847c71c9b8b03f05325b2ae1b5a934fbaa793c23..ce5c132d2b103729cad23b65dbb6014767cc1dc3 100644 --- a/examples/src/webgl-examples/sample6.ts +++ b/examples/src/webgl-examples/sample6.ts @@ -1,4 +1,4 @@ -import { IRenderObject, IRenderPass, ISampler, ITexture } from "@feng3d/render-api"; +import { RenderPass, Sampler, Texture, RenderObject } from "@feng3d/render-api"; import { WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; @@ -13,7 +13,7 @@ async function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // Here's where we call the routine that builds all the // objects we'll be drawing. @@ -21,9 +21,8 @@ async function main() const texture = await loadTexture("../../cubetexture.png"); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-list" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -50,22 +49,25 @@ async function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x3", - data: buffers.position, - }, - aTextureCoord: { - format: "float32x2", - data: buffers.textureCoord, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: { + aVertexPosition: { + format: "float32x3", + data: buffers.position, + }, + aTextureCoord: { + format: "float32x2", + data: buffers.textureCoord, + }, }, + indices: buffers.indices, + draw: { __type__: "DrawIndexed", firstIndex: 0, indexCount: 36 }, }, - indices: buffers.indices, - uniforms: { uSampler: texture }, - drawIndexed: { firstIndex: 0, indexCount: 36 }, + bindingResources: { uSampler: texture }, }; - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -90,8 +92,8 @@ async function main() const { projectionMatrix, modelViewMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; webgl.submit({ commandEncoders: [{ passEncoders: [renderPass] }] }); @@ -220,14 +222,14 @@ async function loadTexture(url: string) const generateMipmap = isPowerOf2(img.width) && isPowerOf2(img.height); - const texture: ITexture = { + const texture: Texture = { size: [img.width, img.height], format: "rgba8unorm", sources: [{ image: img }], generateMipmap, }; - let sampler: ISampler = {}; + let sampler: Sampler = {}; if (generateMipmap) { sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "linear" }; diff --git a/examples/src/webgl-examples/sample7.ts b/examples/src/webgl-examples/sample7.ts index a171f9c2689c80028c6dd44bdebfdf6292ec8e79..7eedfddb39770fe7787ead020b54435efe520a30 100644 --- a/examples/src/webgl-examples/sample7.ts +++ b/examples/src/webgl-examples/sample7.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, ISampler, ITexture } from "@feng3d/render-api"; -import { IGLSamplerTexture, WebGL } from "@feng3d/webgl"; +import { RenderPass, Sampler, Texture, RenderObject } from "@feng3d/render-api"; +import { SamplerTexture, WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; let cubeRotation = 0.0; @@ -13,7 +13,7 @@ async function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // Here's where we call the routine that builds all the // objects we'll be drawing. @@ -21,9 +21,8 @@ async function main() const texture = await loadTexture("../../cubetexture.png"); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-list" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -67,26 +66,29 @@ async function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x3", - data: buffers.position, - }, - aVertexNormal: { - format: "float32x3", - data: buffers.normal, - }, - aTextureCoord: { - format: "float32x2", - data: buffers.textureCoord, + bindingResources: { uSampler: texture }, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: { + aVertexPosition: { + format: "float32x3", + data: buffers.position, + }, + aVertexNormal: { + format: "float32x3", + data: buffers.normal, + }, + aTextureCoord: { + format: "float32x2", + data: buffers.textureCoord, + }, }, - }, - indices: buffers.indices, - uniforms: { uSampler: texture }, - drawIndexed: { firstIndex: 0, indexCount: 36 }, + indices: buffers.indices, + draw: { __type__: "DrawIndexed", firstIndex: 0, indexCount: 36 }, + } }; - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -111,9 +113,9 @@ async function main() const { projectionMatrix, modelViewMatrix, normalMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; - renderObject.uniforms.uNormalMatrix = normalMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uNormalMatrix = normalMatrix; webgl.submit({ commandEncoders: [{ passEncoders: [renderPass] }] }); @@ -288,21 +290,21 @@ async function loadTexture(url: string) const generateMipmap = isPowerOf2(img.width) && isPowerOf2(img.height); - const texture: ITexture = { + const texture: Texture = { size: [img.width, img.height], format: "rgba8unorm", sources: [{ image: img }], generateMipmap, }; - let sampler: ISampler = {}; + let sampler: Sampler = {}; if (!generateMipmap) { sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "linear" }; } - return { texture, sampler } as IGLSamplerTexture; + return { texture, sampler } as SamplerTexture; } function isPowerOf2(value: number) diff --git a/examples/src/webgl-examples/sample8.ts b/examples/src/webgl-examples/sample8.ts index 4369f56f6003eedef56279f4680b670324878743..8b1ccccb10dc0389b317098a1d0aed51bbbd6044 100644 --- a/examples/src/webgl-examples/sample8.ts +++ b/examples/src/webgl-examples/sample8.ts @@ -1,5 +1,5 @@ -import { IRenderObject, IRenderPass, ISampler, ITexture } from "@feng3d/render-api"; -import { IGLSamplerTexture, WebGL } from "@feng3d/webgl"; +import { RenderObject, RenderPass, Sampler, Texture } from "@feng3d/render-api"; +import { SamplerTexture, WebGL } from "@feng3d/webgl"; import { mat4 } from "gl-matrix"; let cubeRotation = 0.0; @@ -15,7 +15,7 @@ function main() { const canvas = document.querySelector("#glcanvas") as HTMLCanvasElement; - const webgl = new WebGL({ canvasId: "glcanvas", contextId: "webgl" }); + const webgl = new WebGL({ canvasId: "glcanvas", webGLcontextId: "webgl" }); // Here's where we call the routine that builds all the // objects we'll be drawing. @@ -25,9 +25,8 @@ function main() const video = setupVideo("../../Firefox.mp4"); - const renderObject: IRenderObject = { + const renderObject: RenderObject = { pipeline: { - primitive: { topology: "triangle-list" }, vertex: { code: ` attribute vec4 aVertexPosition; @@ -71,26 +70,29 @@ function main() ` }, depthStencil: { depthCompare: "less-equal" } }, - vertices: { - aVertexPosition: { - format: "float32x3", - data: buffers.position, - }, - aVertexNormal: { - format: "float32x3", - data: buffers.normal, - }, - aTextureCoord: { - format: "float32x2", - data: buffers.textureCoord, + geometry: { + primitive: { topology: "triangle-list" }, + vertices: { + aVertexPosition: { + format: "float32x3", + data: buffers.position, + }, + aVertexNormal: { + format: "float32x3", + data: buffers.normal, + }, + aTextureCoord: { + format: "float32x2", + data: buffers.textureCoord, + }, }, + indices: buffers.indices, + draw: { __type__: "DrawIndexed", firstIndex: 0, indexCount: 36 }, }, - indices: buffers.indices, - uniforms: { uSampler: texture }, - drawIndexed: { firstIndex: 0, indexCount: 36 }, + bindingResources: { uSampler: texture }, }; - const renderPass: IRenderPass = { + const renderPass: RenderPass = { descriptor: { colorAttachments: [{ clearValue: [0.0, 0.0, 0.0, 1.0], @@ -120,9 +122,9 @@ function main() const { projectionMatrix, modelViewMatrix, normalMatrix } = drawScene(canvas, deltaTime); - renderObject.uniforms.uProjectionMatrix = projectionMatrix; - renderObject.uniforms.uModelViewMatrix = modelViewMatrix; - renderObject.uniforms.uNormalMatrix = normalMatrix; + renderObject.bindingResources.uProjectionMatrix = projectionMatrix; + renderObject.bindingResources.uModelViewMatrix = modelViewMatrix; + renderObject.bindingResources.uNormalMatrix = normalMatrix; webgl.submit({ commandEncoders: [{ passEncoders: [renderPass] }] }); @@ -318,14 +320,14 @@ function initBuffers() // // Initialize a texture. // -function initTexture(): IGLSamplerTexture +function initTexture(): SamplerTexture { - const texture: ITexture = { + const texture: Texture = { size: [1, 1], format: "rgba8unorm", - sources: [{ __type: "TextureDataSource", size: [1, 1], data: new Uint8Array([0, 0, 255, 255]) }], + sources: [{ __type__: "TextureDataSource", size: [1, 1], data: new Uint8Array([0, 0, 255, 255]) }], }; - const sampler: ISampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "linear" }; + const sampler: Sampler = { addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge", minFilter: "linear" }; return { texture, sampler }; } @@ -333,7 +335,7 @@ function initTexture(): IGLSamplerTexture // // copy the video texture // -function updateTexture(texture: ITexture, video: HTMLVideoElement) +function updateTexture(texture: Texture, video: HTMLVideoElement) { // 修改纹理尺寸 if (texture.size[0] !== video.videoWidth || texture.size[1] !== video.videoHeight) diff --git a/package.json b/package.json index 08ce72fabc4bc5d810821734fcb29e75b2e75b10..b244dc59e313f5af7eca7dad6bc8fa65f6e2aa2b 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,6 @@ }, "dependencies": { "@feng3d/render-api": "0.0.2", - "@feng3d/watcher": "^0.8.8" + "@feng3d/watcher": "^0.8.12" } } \ No newline at end of file diff --git a/src/RunWebGL.ts b/src/RunWebGL.ts index fa75989995d4f285878ad5bb3c5dddb9c90801c7..979cd580d7af97de04bfa99ac90a7e470ca27bf7 100644 --- a/src/RunWebGL.ts +++ b/src/RunWebGL.ts @@ -1,56 +1,39 @@ -import { getBlendConstantColor, IBlendComponent, IBufferBinding, IColorTargetState, ICommandEncoder, ICopyBufferToBuffer, ICopyTextureToTexture, ICullFace, IDepthStencilState, IDrawIndexed, IDrawVertex, IFrontFace, IIndicesDataTypes, IPrimitiveState, IRenderObject, IRenderPass, IRenderPassColorAttachment, IRenderPassDepthStencilAttachment, IRenderPassDescriptor, IRenderPassObject, IRenderPipeline, ISampler, IScissorRect, ISubmit, ITextureView, IUniforms, IVertexAttribute, IVertexAttributes, IViewport, TypedArray, UnReadonly } from "@feng3d/render-api"; +import { BindingResources, BlendComponent, BlendState, BufferBinding, ColorTargetState, CommandEncoder, CopyBufferToBuffer, CopyTextureToTexture, CullFace, DepthStencilState, DrawIndexed, DrawVertex, FrontFace, GBuffer, IIndicesDataTypes, OcclusionQuery, PrimitiveState, RenderObject, RenderPass, RenderPassDescriptor, RenderPassObject, RenderPipeline, Sampler, ScissorRect, Submit, TextureView, TypedArray, UnReadonly, VertexAttribute, VertexAttributes, vertexFormatMap, Viewport } from "@feng3d/render-api"; +import { getGLBlitFramebuffer } from "./caches/getGLBlitFramebuffer"; import { getGLBuffer } from "./caches/getGLBuffer"; +import { getGLDrawMode, GLDrawMode } from "./caches/getGLDrawMode"; import { getGLFramebuffer } from "./caches/getGLFramebuffer"; -import { getGLProgram } from "./caches/getGLProgram"; +import { getGLProgram, UniformItemInfo } from "./caches/getGLProgram"; import { getGLRenderOcclusionQuery } from "./caches/getGLRenderOcclusionQuery"; -import { getGLSampler, getIGLTextureMagFilter, getIGLTextureMinFilter, getIGLTextureWrap } from "./caches/getGLSampler"; +import { getGLRenderPassDescriptorWithMultisample } from "./caches/getGLRenderPassDescriptorWithMultisample"; +import { getGLSampler, getIGLTextureMagFilter, getIGLTextureMinFilter, getIGLTextureWrap, GLTextureMagFilter, GLTextureMinFilter, GLTextureWrap } from "./caches/getGLSampler"; +import { getGLTextureTarget, GLTextureTarget } from "./caches/getGLTextureTarget"; import { getGLTransformFeedback } from "./caches/getGLTransformFeedback"; -import { getIGLBlitFramebuffer } from "./caches/getIGLBlitFramebuffer"; -import { getIGLDrawMode, IGLDrawMode } from "./caches/getIGLDrawMode"; -import { getIGLRenderPassDescriptorWithMultisample } from "./caches/getIGLRenderPassDescriptorWithMultisample"; -import { getIGLTextureTarget } from "./caches/getIGLTextureTarget"; import { _GL_Submit_Times } from "./const/const"; -import { IGLUniformBufferType } from "./const/IGLUniformType"; -import { IGLBlitFramebuffer } from "./data/IGLBlitFramebuffer"; -import { IGLDrawElementType, IGLUniformBuffer } from "./data/IGLBuffer"; -import { IGLCompareFunction, IGLStencilFunc, IGLStencilOp } from "./data/IGLDepthStencilState"; -import { IGLOcclusionQuery } from "./data/IGLOcclusionQuery"; -import { IGLTextureMagFilter, IGLTextureMinFilter, IGLTextureWrap } from "./data/IGLSampler"; -import { IGLSamplerTexture } from "./data/IGLSamplerTexture"; -import { IGLTextureTarget } from "./data/IGLTexture"; -import { IGLTransformFeedback } from "./data/IGLTransformFeedback"; -import { IGLTransformFeedbackObject, IGLTransformFeedbackPass, IGLTransformFeedbackPipeline } from "./data/IGLTransformFeedbackPass"; -import { IUniformItemInfo } from "./data/IGLUniformInfo"; +import { GLUniformBufferType } from "./const/GLUniformType"; +import { BlitFramebuffer } from "./data/BlitFramebuffer"; +import { DrawElementType } from "./data/polyfills/Buffer"; +import { SamplerTexture } from "./data/SamplerTexture"; +import { TransformFeedback, TransformFeedbackObject, TransformFeedbackPass, TransformFeedbackPipeline } from "./data/TransformFeedbackPass"; import { getGLTexture } from "./internal"; -import { getIGLIndexBuffer, getIGLUniformBuffer, getIGLVertexBuffer } from "./runs/getIGLBuffer"; +import { getIGLBuffer } from "./runs/getIGLBuffer"; import { getIGLBlendEquation, getIGLBlendFactor, IGLBlendEquation, IGLBlendFactor } from "./runs/runColorTargetStates"; import { getIGLCompareFunction } from "./runs/runDepthState"; import { getIGLStencilFunc, getIGLStencilOp } from "./runs/runStencilState"; -import { ChainMap } from "./utils/ChainMap"; -import { getGLRenderPassAttachmentSize } from "./utils/getGLRenderPassAttachmentSize"; -import { getIGLCullFace, IGLCullFace } from "./utils/getIGLCullFace"; -import { getIGLFrontFace, IGLFrontFace } from "./utils/getIGLFrontFace"; -import { getIGLVertexFormat } from "./utils/getIVertexFormat"; import { updateBufferBinding } from "./utils/updateBufferBinding"; -declare global -{ - interface WebGLRenderingContext - { - _vertexArrays: ChainMap<[IRenderPipeline, IVertexAttributes, IIndicesDataTypes], WebGLVertexArrayObject>; - } -} +import "./data/polyfills/OcclusionQuery"; declare global { interface WebGLTexture { - minFilter?: IGLTextureMinFilter, - magFilter?: IGLTextureMagFilter, - wrapS?: IGLTextureWrap, - wrapT?: IGLTextureWrap, - wrapR?: IGLTextureWrap, + minFilter?: GLTextureMinFilter, + magFilter?: GLTextureMagFilter, + wrapS?: GLTextureWrap, + wrapT?: GLTextureWrap, + wrapR?: GLTextureWrap, maxAnisotropy?: number, lodMinClamp?: number; lodMaxClamp?: number; @@ -59,7 +42,7 @@ declare global export class RunWebGL { - runSubmit(gl: WebGLRenderingContext, submit: ISubmit) + runSubmit(gl: WebGLRenderingContext, submit: Submit) { const commandBuffers = submit.commandEncoders.map((v) => { @@ -72,31 +55,31 @@ export class RunWebGL gl[_GL_Submit_Times] = ~~gl[_GL_Submit_Times] + 1; } - protected runCommandEncoder(gl: WebGLRenderingContext, commandEncoder: ICommandEncoder) + protected runCommandEncoder(gl: WebGLRenderingContext, commandEncoder: CommandEncoder) { commandEncoder.passEncoders.forEach((passEncoder) => { - if (!passEncoder.__type) + if (!passEncoder.__type__) { - this.runRenderPass(gl, passEncoder as IRenderPass); + this.runRenderPass(gl, passEncoder as RenderPass); } - else if (passEncoder.__type === "RenderPass") + else if (passEncoder.__type__ === "RenderPass") { this.runRenderPass(gl, passEncoder); } - else if (passEncoder.__type === "TransformFeedbackPass") + else if (passEncoder.__type__ === "TransformFeedbackPass") { this.runTransformFeedbackPass(gl, passEncoder); } - else if (passEncoder.__type === "BlitFramebuffer") + else if (passEncoder.__type__ === "BlitFramebuffer") { this.runBlitFramebuffer(gl, passEncoder); } - else if (passEncoder.__type === "CopyTextureToTexture") + else if (passEncoder.__type__ === "CopyTextureToTexture") { this.runCopyTextureToTexture(gl, passEncoder); } - else if (passEncoder.__type === "CopyBufferToBuffer") + else if (passEncoder.__type__ === "CopyBufferToBuffer") { this.runCopyBuffer(gl, passEncoder); } @@ -107,7 +90,7 @@ export class RunWebGL }); } - protected runTransformFeedbackPass(gl: WebGLRenderingContext, transformFeedbackPass: IGLTransformFeedbackPass) + protected runTransformFeedbackPass(gl: WebGLRenderingContext, transformFeedbackPass: TransformFeedbackPass) { // 执行变换反馈通道时关闭光栅化功能 if (gl instanceof WebGL2RenderingContext) @@ -124,7 +107,7 @@ export class RunWebGL } } - protected runRenderPass(gl: WebGLRenderingContext, renderPass: IRenderPass) + protected runRenderPass(gl: WebGLRenderingContext, renderPass: RenderPass) { // 获取附件尺寸 const attachmentSize = getGLRenderPassAttachmentSize(gl, renderPass.descriptor); @@ -134,9 +117,9 @@ export class RunWebGL // occlusionQuery.init(); - if (renderPass.descriptor?.sampleCount && (renderPass.descriptor.colorAttachments[0].view as ITextureView).texture) + if (renderPass.descriptor?.sampleCount && (renderPass.descriptor.colorAttachments[0].view as TextureView).texture) { - const { passDescriptor, blitFramebuffer } = getIGLRenderPassDescriptorWithMultisample(renderPass.descriptor); + const { passDescriptor, blitFramebuffer } = getGLRenderPassDescriptorWithMultisample(renderPass.descriptor); this.runRenderPassDescriptor(gl, passDescriptor); @@ -154,25 +137,30 @@ export class RunWebGL occlusionQuery.resolve(renderPass); } - private runRenderPassDescriptor(gl: WebGLRenderingContext, passDescriptor: IRenderPassDescriptor) + private runRenderPassDescriptor(gl: WebGLRenderingContext, passDescriptor: RenderPassDescriptor) { passDescriptor = passDescriptor || {}; - // - const colorAttachment = Object.assign({}, defaultRenderPassColorAttachment, passDescriptor.colorAttachments?.[0]); // const framebuffer = getGLFramebuffer(gl, passDescriptor); gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); // - const { clearValue, loadOp } = colorAttachment; + const colorAttachment = passDescriptor.colorAttachments?.[0]; + // + const clearValue = colorAttachment?.clearValue ?? [0, 0, 0, 0]; + const loadOp = colorAttachment?.loadOp ?? "clear"; gl.clearColor(clearValue[0], clearValue[1], clearValue[2], clearValue[3]); // - const depthStencilAttachment = Object.assign({}, defaultDepthStencilAttachment, passDescriptor.depthStencilAttachment); - const { depthClearValue, depthLoadOp, stencilClearValue, stencilLoadOp } = depthStencilAttachment; + const depthStencilAttachment = passDescriptor.depthStencilAttachment; + const depthClearValue = depthStencilAttachment?.depthClearValue ?? 1; + const depthLoadOp = depthStencilAttachment?.depthLoadOp ?? "load"; + const stencilClearValue = depthStencilAttachment?.stencilClearValue ?? 0; + const stencilLoadOp = depthStencilAttachment?.stencilLoadOp ?? "load"; + // gl.clearDepth(depthClearValue); gl.clearStencil(stencilClearValue); @@ -183,11 +171,11 @@ export class RunWebGL ); } - private runRenderObjects(gl: WebGLRenderingContext, attachmentSize: { width: number, height: number }, renderObjects?: readonly IRenderPassObject[]) + private runRenderObjects(gl: WebGLRenderingContext, attachmentSize: { width: number, height: number }, renderObjects?: readonly RenderPassObject[]) { renderObjects?.forEach((renderObject) => { - if (renderObject.__type === "OcclusionQuery") + if (renderObject.__type__ === "OcclusionQuery") { this.runOcclusionQuery(gl, attachmentSize, renderObject); } @@ -198,12 +186,9 @@ export class RunWebGL }); } - private runRenderObject(gl: WebGLRenderingContext, attachmentSize: { width: number, height: number }, renderObject: IRenderObject) + private runRenderObject(gl: WebGLRenderingContext, attachmentSize: { width: number, height: number }, renderObject: RenderObject) { - const { viewport, scissorRect, pipeline, vertices, indices, uniforms, drawIndexed, drawVertex } = renderObject; - - const topology = pipeline.primitive?.topology || "triangle-list"; - const drawMode = getIGLDrawMode(topology); + const { viewport, scissorRect, pipeline, geometry, bindingResources: uniforms } = renderObject; this.runViewPort(gl, attachmentSize, viewport); @@ -211,40 +196,47 @@ export class RunWebGL this.runRenderPipeline(gl, pipeline); + this.runUniforms(gl, pipeline, uniforms); + + const { vertices, indices, draw, primitive } = geometry; + this.runVertexArray(gl, pipeline, vertices, indices); - this.runUniforms(gl, pipeline, uniforms); + this.runPrimitiveState(gl, primitive); - if (drawVertex) + const topology = primitive?.topology || "triangle-list"; + const drawMode = getGLDrawMode(topology); + + if (draw.__type__ === 'DrawVertex') { - this.runDrawVertex(gl, drawMode, drawVertex); + this.runDrawVertex(gl, drawMode, draw); } - if (drawIndexed) + else { - this.runDrawIndexed(gl, drawMode, indices, drawIndexed); + this.runDrawIndexed(gl, drawMode, indices, draw); } } - private runTransformFeedbackObject(gl: WebGLRenderingContext, renderObject: IGLTransformFeedbackObject) + private runTransformFeedbackObject(gl: WebGLRenderingContext, renderObject: TransformFeedbackObject) { - const { pipeline, vertices, uniforms, transformFeedback, drawVertex } = renderObject; + const { pipeline: material, vertices, uniforms, transformFeedback, draw } = renderObject; - const drawMode = getIGLDrawMode("point-list"); + const drawMode = getGLDrawMode("point-list"); - this.runTransformFeedbackPipeline(gl, pipeline); + this.runTransformFeedbackPipeline(gl, material); - this.runVertexArray(gl, pipeline, vertices, undefined); + this.runVertexArray(gl, material, vertices, undefined); - this.runUniforms(gl, pipeline, uniforms); + this.runUniforms(gl, material, uniforms); this.runTransformFeedback(gl, transformFeedback, drawMode); - this.runDrawVertex(gl, drawMode, drawVertex); + this.runDrawVertex(gl, drawMode, draw); this.endTransformFeedback(gl, transformFeedback); } - private endTransformFeedback(gl: WebGLRenderingContext, transformFeedback: IGLTransformFeedback) + private endTransformFeedback(gl: WebGLRenderingContext, transformFeedback: TransformFeedback) { // if (transformFeedback) @@ -259,9 +251,9 @@ export class RunWebGL } } - private runDrawIndexed(gl: WebGLRenderingContext, drawMode: IGLDrawMode, indices: IIndicesDataTypes, drawIndexed: IDrawIndexed) + private runDrawIndexed(gl: WebGLRenderingContext, drawMode: GLDrawMode, indices: IIndicesDataTypes, drawIndexed: DrawIndexed) { - const type: IGLDrawElementType = indices.BYTES_PER_ELEMENT === 2 ? "UNSIGNED_SHORT" : "UNSIGNED_INT"; + const type: DrawElementType = indices.BYTES_PER_ELEMENT === 2 ? "UNSIGNED_SHORT" : "UNSIGNED_INT"; // const indexCount = drawIndexed.indexCount; const firstIndex = drawIndexed.firstIndex || 0; @@ -288,7 +280,7 @@ export class RunWebGL } } - private runDrawVertex(gl: WebGLRenderingContext, drawMode: IGLDrawMode, drawArrays: IDrawVertex) + private runDrawVertex(gl: WebGLRenderingContext, drawMode: GLDrawMode, drawArrays: DrawVertex) { // const vertexCount = drawArrays.vertexCount; @@ -316,9 +308,9 @@ export class RunWebGL /** * 激活常量 */ - private runUniforms(gl: WebGLRenderingContext, pipeline: IRenderPipeline, uniforms: IUniforms) + private runUniforms(gl: WebGLRenderingContext, material: RenderPipeline, uniforms: BindingResources) { - const webGLProgram = getGLProgram(gl, pipeline); + const webGLProgram = getGLProgram(gl, material); webGLProgram.uniforms.forEach((uniformInfo) => { @@ -341,11 +333,11 @@ export class RunWebGL if (isTexture) { - this.runSamplerTexture(gl, v, uniformData as IGLSamplerTexture); + this.runSamplerTexture(gl, v, uniformData as SamplerTexture); } else { - this.runUniform(gl, type as IGLUniformBufferType, v, uniformData); + this.runUniform(gl, type as GLUniformBufferType, v, uniformData); } }); }); @@ -355,22 +347,21 @@ export class RunWebGL webGLProgram.uniformBlocks.forEach((uniformBlock) => { const { name, index } = uniformBlock; - const uniformData = uniforms[name] as TypedArray | IBufferBinding; + const uniformData = uniforms[name] as TypedArray | BufferBinding; // - let buffer: IGLUniformBuffer; - const typedArray = uniformData as TypedArray; - if (typedArray.buffer && typedArray.BYTES_PER_ELEMENT) + let typedArray = uniformData as TypedArray; + if (!(typedArray.buffer && typedArray.BYTES_PER_ELEMENT)) { - buffer = getIGLUniformBuffer(typedArray); - } - else - { - const bufferBinding = uniforms[name] as IBufferBinding; + const bufferBinding = uniforms[name] as BufferBinding; updateBufferBinding(uniformBlock.bufferBindingInfo, bufferBinding); - buffer = getIGLUniformBuffer(bufferBinding.bufferView); + typedArray = bufferBinding.bufferView; } - (buffer as UnReadonly