示例#1
0
        public static bool redraw()
        {
            IntPtr backBufView = WebGPUNative.wgpuSwapChainGetCurrentTextureView(SwapChain); // create textureView

            WGPURenderPassColorAttachmentDescriptor colorDesc = new WGPURenderPassColorAttachmentDescriptor
            {
                attachment = backBufView,
                loadOp     = WGPULoadOp.WGPULoadOp_Clear,
                storeOp    = WGPUStoreOp.WGPUStoreOp_Store,
                clearColor = new WGPUColor
                {
                    r = 0.3f,
                    g = 0.3f,
                    b = 0.3f,
                    a = 1.0f
                }
            };

            WGPURenderPassDescriptor renderPass = new WGPURenderPassDescriptor
            {
                colorAttachmentCount = 1,
                colorAttachments     = &colorDesc
            };

            IntPtr encoder = WebGPUNative.wgpuDeviceCreateCommandEncoder(Device, null); // create encoder
            IntPtr pass    = WebGPUNative.wgpuCommandEncoderBeginRenderPass(encoder, &renderPass);

            // update the rotation
            rotDeg += 0.1f;
            fixed(void *data = &rotDeg)
            {
                WebGPUNative.wgpuQueueWriteBuffer(Queue, uRotBuf, 0, data, sizeof(float));
            }

            // draw the triangle (comment these five lines to simply clear the screen)
            WebGPUNative.wgpuRenderPassEncoderSetPipeline(pass, pipeline);
            WebGPUNative.wgpuRenderPassEncoderSetBindGroup(pass, 0, bindGroup, 0, null);
            WebGPUNative.wgpuRenderPassEncoderSetVertexBuffer(pass, 0, vertBuf, 0, 0);
            WebGPUNative.wgpuRenderPassEncoderSetIndexBufferWithFormat(pass, indxBuf, WGPUIndexFormat.WGPUIndexFormat_Uint16, 0, 0);
            WebGPUNative.wgpuRenderPassEncoderDrawIndexed(pass, 3, 1, 0, 0, 0);

            WebGPUNative.wgpuRenderPassEncoderEndPass(pass);
            WebGPUNative.wgpuRenderPassEncoderRelease(pass);                         // release pass
            IntPtr commands = WebGPUNative.wgpuCommandEncoderFinish(encoder, null);  // create commands

            WebGPUNative.wgpuCommandEncoderRelease(encoder);                         // release encoder

            WebGPUNative.wgpuQueueSubmit(Queue, 1, &commands);
            WebGPUNative.wgpuCommandBufferRelease(commands);                         // release commands

            // TODO EMSCRIPTEN: wgpuSwapChainPresent is unsupported in Emscripten, so what do we do?
            WebGPUNative.wgpuSwapChainPresent(SwapChain);

            WebGPUNative.wgpuTextureViewRelease(backBufView);                        // release textureView

            return(true);
        }
示例#2
0
        /**
         * Helper to create a buffer.
         *
         * \param[in] data pointer to the start of the raw data
         * \param[in] size number of bytes in \a data
         * \param[in] usage type of buffer
         */
        private static IntPtr CreateBuffer(void *data, ulong size, WGPUBufferUsage usage)
        {
            WGPUBufferDescriptor desc = new WGPUBufferDescriptor
            {
                usage = WGPUBufferUsage.WGPUBufferUsage_CopyDst | usage,
                size  = size
            };
            IntPtr buffer = WebGPUNative.wgpuDeviceCreateBuffer(Device, ref desc);

            WebGPUNative.wgpuQueueWriteBuffer(Queue, buffer, 0, data, size);
            return(buffer);
        }