<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WebGPU學習(九):學習“fractalCube”示例

      大家好,本文學習Chrome->webgpu-samplers->fractalCube示例。

      上一篇博文:
      WebGPU學習(八):學習“texturedCube”示例

      下一篇博文:
      WebGPU學習(十):介紹“GPU實現粒子效果”

      學習fractalCube.ts

      最終渲染結果:
      截屏2019-12-25上午8.14.22.png-107.3kB

      該示例展示了如何用上一幀渲染的結果作為下一幀的紋理。

      “texturedCube”示例相比,該示例的紋理并不是來自圖片,而是來自上一幀渲染的結果

      下面,我們打開fractalCube.ts文件,分析相關代碼:

      傳輸頂點的color

      它與“texturedCube”示例->“傳遞頂點的uv數據”類似,這里不再分析

      上一幀渲染的結果作為下一幀的紋理

      • 配置swapChain

      因為swapChain保存了上一幀渲染的結果,所以將其作為下一幀紋理的source。因此它的usage需要增加GPUTextureUsage.COPY_SRC:

        const swapChain = context.configureSwapChain({
          device,
          format: "bgra8unorm",
          usage: GPUTextureUsage.OUTPUT_ATTACHMENT | GPUTextureUsage.COPY_SRC,
        });
      
      • 創(chuàng)建空紋理(cubeTexture)和sampler,設置到uniform bind group中

      相關代碼如下:

      
        const fragmentShaderGLSL = `#version 450
        ...  
        layout(set = 0, binding = 2) uniform texture2D myTexture;
        ...  
      
        void main() {
          vec4 texColor = texture(sampler2D(myTexture, mySampler), fragUV * 0.8 + 0.1);
      
          ...
      
          outColor = mix(texColor, fragColor, f);
        }`;
      
        ...
      
        const cubeTexture = device.createTexture({
          size: { width: canvas.width, height: canvas.height, depth: 1 },
          format: "bgra8unorm",
          usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.SAMPLED,
        });
      
        const sampler = device.createSampler({
          magFilter: "linear",
          minFilter: "linear",
        });
      
        const uniformBindGroup = device.createBindGroup({
          layout: bindGroupLayout,
          bindings: [
          ...
          {
            binding: 1,
            resource: sampler,
          }, {
            binding: 2,
            //傳遞cubeTexture到fragment shader中
            resource: cubeTexture.createView(),
          }],
        });
      
      • 繪制和拷貝

      在每一幀中:
      繪制帶紋理的立方體;
      將渲染結果(swapChainTexture)拷貝到cubeTexture中。

      相關代碼如下:

        return function frame() {
          const swapChainTexture = swapChain.getCurrentTexture();
                         
          renderPassDescriptor.colorAttachments[0].attachment = swapChainTexture.createView();
          
          const commandEncoder = device.createCommandEncoder({});
          const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
          ...
          passEncoder.setBindGroup(0, uniformBindGroup); 
          ...
          passEncoder.draw(36, 1, 0, 0);
          passEncoder.endPass();
      
          commandEncoder.copyTextureToTexture({
            texture: swapChainTexture,
          }, {
            texture: cubeTexture,
          }, {
            width: canvas.width,
            height: canvas.height,
            depth: 1,
          });
          
          device.defaultQueue.submit([commandEncoder.finish()]); 
         
          ...
      
        }
      

      分析shader代碼

      本示例的vertex shader與“texturedCube”示例的vertex shader相比,增加了color attribute:

        const vertexShaderGLSL = `#version 450
        ...
        layout(location = 1) in vec4 color;
        ...
      
        layout(location = 0) out vec4 fragColor;
        ...
      
        void main() {
          ...
          fragColor = color;
          ...
        }`;
      

      fragment shader的代碼如下:

        const fragmentShaderGLSL = `#version 450
        layout(set = 0, binding = 1) uniform sampler mySampler;
        layout(set = 0, binding = 2) uniform texture2D myTexture;
      
        layout(location = 0) in vec4 fragColor;
        layout(location = 1) in vec2 fragUV;
        layout(location = 0) out vec4 outColor;
      
        void main() {
          vec4 texColor = texture(sampler2D(myTexture, mySampler), fragUV * 0.8 + 0.1);
      
          // 1.0 if we're sampling the background
          float f = float(length(texColor.rgb - vec3(0.5, 0.5, 0.5)) < 0.01);
      
          outColor = mix(texColor, fragColor, f);
        }`;
      

      第10行對fragUV進行了處理,我們會在分析渲染時間線中分析它。

      第13行和第15行相當于做了if判斷:

      if(紋理顏色 === 背景色){
          outColor = fragColor
      }
      else{
          outColor = 紋理顏色
      }
      

      這里之所以不用if判斷而使用計算的方式,是為了減少條件判斷,提高gpu的并行性

      分析渲染時間線

      下面分析下渲染的時間線:

      第一幀

      因為紋理為空紋理,它的顏色為背景色,所以fragment shader的outColor始終為fragColor,因此立方體的所有片段的顏色均為fragColor。

      第一幀的渲染結果如下:
      截屏2019-12-25下午12.23.39.png-116.5kB

      第一幀繪制結束后,渲染結果會被拷貝到cubeTexture中。

      第二幀

      分析執(zhí)行的fragment shader代碼:

        const fragmentShaderGLSL = `#version 450
        layout(set = 0, binding = 1) uniform sampler mySampler;
        layout(set = 0, binding = 2) uniform texture2D myTexture;
      
        layout(location = 0) in vec4 fragColor;
        layout(location = 1) in vec2 fragUV;
        layout(location = 0) out vec4 outColor;
      
        void main() {
          vec4 texColor = texture(sampler2D(myTexture, mySampler), fragUV * 0.8 + 0.1);
      
          // 1.0 if we're sampling the background
          float f = float(length(texColor.rgb - vec3(0.5, 0.5, 0.5)) < 0.01);
      
          outColor = mix(texColor, fragColor, f);
        }`;
      
      • 第10行的“fragUV * 0.8 + 0.1”是為了取紋理坐標u、v方向的[0.1-0.9]部分,從而使紋理中立方體所占比例更大。

      得到的紋理區(qū)域如下圖的紅色區(qū)域所示:
      截屏2019-12-25下午12.23.39.png-117kB

      • 第13行和第15行代碼,將紋理中的背景色替換為了fragColor

      第二幀的渲染結果如下:
      截屏2019-12-25下午12.24.49.png-141.1kB

      • 第三幀

      依次類推,第三幀的渲染結果如下:
      截屏2019-12-25下午12.47.45.png-157.2kB

      參考資料

      WebGPU規(guī)范
      webgpu-samplers Github Repo
      WebGPU-7

      posted @ 2019-12-25 15:52  楊元超  閱讀(667)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 日韩精品有码中文字幕| 亚洲成色av网站午夜影视| 亚洲av日韩av综合在线观看| 性做久久久久久久久| 国产成人av三级在线观看| 国产精品二区中文字幕| 喜德县| 亚洲精品无码久久毛片| 亚洲人成人无码网WWW电影首页 | 国产精品毛片在线完整版| 国产伦码精品一区二区| 亚洲一区二区精品极品| 亚洲高请码在线精品av| 人妻日韩人妻中文字幕| 托克逊县| 久久日韩在线观看视频| 成人动漫综合网| 免费AV片在线观看网址| 国产午夜福利视频第三区| 91国在线啪精品一区| 亚洲免费视频一区二区三区| 色综合人人超人人超级国碰| 亚洲欧美人成人让影院| 国产精品久久久久久久久久直播| 麻豆国产高清精品国在线| 亚洲韩欧美第25集完整版| 色噜噜在线视频免费观看| 国内少妇偷人精品免费| 国产95在线 | 亚洲| 韩国美女福利视频一区二区| 欧美不卡无线在线一二三区观| caoporn成人免费公开| 成人自拍小视频免费观看| 在线精品自拍亚洲第一区| 伊人久久大香线蕉av五月天| 久热这里只有精品视频3| 清纯唯美人妻少妇第一页| 99久久99久久精品免费看蜜桃| 一本大道久久香蕉成人网| 免费无码高潮流白浆视频| 麻豆人人妻人人妻人人片av|