WebKit Bugzilla
Attachment 372411 Details for
Bug 198985
: [WHLSL] Hook up RWTextures
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Test
whlsl-textures-store.html (text/html), 6.79 KB, created by
Myles C. Maxfield
on 2019-06-18 17:33:05 PDT
(
hide
)
Description:
Test
Filename:
MIME Type:
Creator:
Myles C. Maxfield
Created:
2019-06-18 17:33:05 PDT
Size:
6.79 KB
patch
obsolete
><!DOCTYPE html> ><html> ><head> ></head> ><body> ><canvas id="canvas" width="400" height="400"></canvas> ><script> >const shaderSource = ` >[numthreads(1, 1, 1)] >compute void computeShader(RWTexture2D<float4> theTexture : register(t0)) { > Store(theTexture, float4(1.0, 1.0, 1.0, 1.0), uint2(0, 0)); >} >vertex float4 vertexShader(float4 position : attribute(0)) : SV_Position { > return position; >} > >fragment float4 fragmentShader(RWTexture2D<float4> theTexture : register(t0)) : SV_Target 0 { > return Load(theTexture, int2(0, 0)); >} >`; >let resultsUint8Array; >async function start() { > const adapter = await navigator.gpu.requestAdapter(); > const device = await adapter.requestDevice(); > > const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true}); > const computeStage = {module: shaderModule, entryPoint: "computeShader"}; > const vertexStage = {module: shaderModule, entryPoint: "vertexShader"}; > const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"}; > > const attribute = {shaderLocation: 0, format: "float4"}; > const input = {stride: 16, attributeSet: [attribute]}; > const vertexInput = {vertexBuffers: [input]}; > > const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "sampled-texture"}]}; > const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor); > const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]}; > const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor); > > const computePipelineDescriptor = {computeStage, layout: pipelineLayout}; > const computePipeline = device.createComputePipeline(computePipelineDescriptor); > > const primitiveTopology = "triangle-strip"; > const rasterizationState = {frontFace: "cw", cullMode: "none"}; > const alphaBlend = {}; > const colorBlend = {}; > const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL > const depthStencilState = null; > const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout}; > const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor); > > const vertexBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE}; > const vertexBuffer = device.createBuffer(vertexBufferDescriptor); > const vertexBufferArrayBuffer = await vertexBuffer.mapWriteAsync(); > const vertexBufferFloat32Array = new Float32Array(vertexBufferArrayBuffer); > vertexBufferFloat32Array[0] = -0.5; > vertexBufferFloat32Array[1] = -0.5; > vertexBufferFloat32Array[2] = 1.0; > vertexBufferFloat32Array[3] = 1; > vertexBufferFloat32Array[4] = -0.5; > vertexBufferFloat32Array[5] = 0.5; > vertexBufferFloat32Array[6] = 1.0; > vertexBufferFloat32Array[7] = 1; > vertexBufferFloat32Array[8] = 0.5; > vertexBufferFloat32Array[9] = -0.5; > vertexBufferFloat32Array[10] = 1.0; > vertexBufferFloat32Array[11] = 1; > vertexBufferFloat32Array[12] = 0.5; > vertexBufferFloat32Array[13] = 0.5; > vertexBufferFloat32Array[14] = 1.0; > vertexBufferFloat32Array[15] = 1; > vertexBuffer.unmap(); > > const textureDataBufferDescriptor = {size: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2, usage: GPUBufferUsage.TRANSFER_SRC | GPUBufferUsage.MAP_WRITE}; > const textureDataBuffer = device.createBuffer(textureDataBufferDescriptor); > const textureDataBufferArrayBuffer = await textureDataBuffer.mapWriteAsync(); > const textureDataBufferUint8Array = new Uint8Array(textureDataBufferArrayBuffer); > textureDataBufferUint8Array[0] = 0; > textureDataBufferUint8Array[1] = 0; > textureDataBufferUint8Array[2] = 0; > textureDataBufferUint8Array[3] = 0; > textureDataBufferUint8Array[4] = 0; > textureDataBufferUint8Array[5] = 0; > textureDataBufferUint8Array[6] = 0; > textureDataBufferUint8Array[7] = 0; > textureDataBufferUint8Array[8] = 0; > textureDataBufferUint8Array[9] = 0; > textureDataBufferUint8Array[10] = 0; > textureDataBufferUint8Array[11] = 0; > textureDataBufferUint8Array[12] = 0; > textureDataBufferUint8Array[13] = 0; > textureDataBufferUint8Array[14] = 0; > textureDataBufferUint8Array[15] = 0; > textureDataBuffer.unmap(); > > const textureDescriptor = {size: {width: 2, height: 2, depth: 1}, format: "rgba8unorm", usage: GPUTextureUsage.STORAGE | GPUTextureUsage.TRANSFER_DST}; > const texture = device.createTexture(textureDescriptor); > const textureView = texture.createDefaultView(); > > const bindGroupBinding = {binding: 0, resource: textureView}; > const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]}; > const bindGroup = device.createBindGroup(bindGroupDescriptor); > > const canvas = document.getElementById("canvas"); > const context = canvas.getContext("gpu"); > const swapChainDescriptor = {device, format: "bgra8unorm"}; > const swapChain = context.configureSwapChain(swapChainDescriptor); > const outputTexture = swapChain.getCurrentTexture(); > const outputTextureView = outputTexture.createDefaultView(); > > const bufferCopyView = {buffer: textureDataBuffer, rowPitch: Uint8Array.BYTES_PER_ELEMENT * 4 * 2, imageHeight: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2}; > const textureCopyView = {texture}; > const copySize = {width: 2, height: 2, depth: 1}; > > const commandEncoder = device.createCommandEncoder(); // {} > commandEncoder.copyBufferToTexture(bufferCopyView, textureCopyView, copySize); > const computePassEncoder = commandEncoder.beginComputePass(); > computePassEncoder.setPipeline(computePipeline); > computePassEncoder.setBindGroup(0, bindGroup); > computePassEncoder.dispatch(1, 1, 1); > computePassEncoder.endPass(); > const red = {r: 0, g: 0, b: 1, a: 1}; > const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}]; > const depthStencilAttachment = null; > const renderPassDescriptor = {colorAttachments, depthStencilAttachment}; > const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); > renderPassEncoder.setPipeline(renderPipeline); > renderPassEncoder.setBindGroup(0, bindGroup); > renderPassEncoder.setVertexBuffers(0, [vertexBuffer], [0]); > renderPassEncoder.draw(4, 1, 0, 0); > renderPassEncoder.endPass(); > const commandBuffer = commandEncoder.finish(); > device.getQueue().submit([commandBuffer]); >} >if (window.testRunner) > testRunner.waitUntilDone(); >window.addEventListener("load", function() { > start().then(function() { > if (window.testRunner) > testRunner.notifyDone(); > }, function(e) { > alert(e); > if (window.testRunner) > testRunner.notifyDone(); > }); >}); ></script> ></body> ></html>
<!DOCTYPE html> <html> <head> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script> const shaderSource = ` [numthreads(1, 1, 1)] compute void computeShader(RWTexture2D<float4> theTexture : register(t0)) { Store(theTexture, float4(1.0, 1.0, 1.0, 1.0), uint2(0, 0)); } vertex float4 vertexShader(float4 position : attribute(0)) : SV_Position { return position; } fragment float4 fragmentShader(RWTexture2D<float4> theTexture : register(t0)) : SV_Target 0 { return Load(theTexture, int2(0, 0)); } `; let resultsUint8Array; async function start() { const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true}); const computeStage = {module: shaderModule, entryPoint: "computeShader"}; const vertexStage = {module: shaderModule, entryPoint: "vertexShader"}; const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"}; const attribute = {shaderLocation: 0, format: "float4"}; const input = {stride: 16, attributeSet: [attribute]}; const vertexInput = {vertexBuffers: [input]}; const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "sampled-texture"}]}; const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor); const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]}; const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor); const computePipelineDescriptor = {computeStage, layout: pipelineLayout}; const computePipeline = device.createComputePipeline(computePipelineDescriptor); const primitiveTopology = "triangle-strip"; const rasterizationState = {frontFace: "cw", cullMode: "none"}; const alphaBlend = {}; const colorBlend = {}; const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL const depthStencilState = null; const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout}; const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor); const vertexBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE}; const vertexBuffer = device.createBuffer(vertexBufferDescriptor); const vertexBufferArrayBuffer = await vertexBuffer.mapWriteAsync(); const vertexBufferFloat32Array = new Float32Array(vertexBufferArrayBuffer); vertexBufferFloat32Array[0] = -0.5; vertexBufferFloat32Array[1] = -0.5; vertexBufferFloat32Array[2] = 1.0; vertexBufferFloat32Array[3] = 1; vertexBufferFloat32Array[4] = -0.5; vertexBufferFloat32Array[5] = 0.5; vertexBufferFloat32Array[6] = 1.0; vertexBufferFloat32Array[7] = 1; vertexBufferFloat32Array[8] = 0.5; vertexBufferFloat32Array[9] = -0.5; vertexBufferFloat32Array[10] = 1.0; vertexBufferFloat32Array[11] = 1; vertexBufferFloat32Array[12] = 0.5; vertexBufferFloat32Array[13] = 0.5; vertexBufferFloat32Array[14] = 1.0; vertexBufferFloat32Array[15] = 1; vertexBuffer.unmap(); const textureDataBufferDescriptor = {size: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2, usage: GPUBufferUsage.TRANSFER_SRC | GPUBufferUsage.MAP_WRITE}; const textureDataBuffer = device.createBuffer(textureDataBufferDescriptor); const textureDataBufferArrayBuffer = await textureDataBuffer.mapWriteAsync(); const textureDataBufferUint8Array = new Uint8Array(textureDataBufferArrayBuffer); textureDataBufferUint8Array[0] = 0; textureDataBufferUint8Array[1] = 0; textureDataBufferUint8Array[2] = 0; textureDataBufferUint8Array[3] = 0; textureDataBufferUint8Array[4] = 0; textureDataBufferUint8Array[5] = 0; textureDataBufferUint8Array[6] = 0; textureDataBufferUint8Array[7] = 0; textureDataBufferUint8Array[8] = 0; textureDataBufferUint8Array[9] = 0; textureDataBufferUint8Array[10] = 0; textureDataBufferUint8Array[11] = 0; textureDataBufferUint8Array[12] = 0; textureDataBufferUint8Array[13] = 0; textureDataBufferUint8Array[14] = 0; textureDataBufferUint8Array[15] = 0; textureDataBuffer.unmap(); const textureDescriptor = {size: {width: 2, height: 2, depth: 1}, format: "rgba8unorm", usage: GPUTextureUsage.STORAGE | GPUTextureUsage.TRANSFER_DST}; const texture = device.createTexture(textureDescriptor); const textureView = texture.createDefaultView(); const bindGroupBinding = {binding: 0, resource: textureView}; const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]}; const bindGroup = device.createBindGroup(bindGroupDescriptor); const canvas = document.getElementById("canvas"); const context = canvas.getContext("gpu"); const swapChainDescriptor = {device, format: "bgra8unorm"}; const swapChain = context.configureSwapChain(swapChainDescriptor); const outputTexture = swapChain.getCurrentTexture(); const outputTextureView = outputTexture.createDefaultView(); const bufferCopyView = {buffer: textureDataBuffer, rowPitch: Uint8Array.BYTES_PER_ELEMENT * 4 * 2, imageHeight: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2}; const textureCopyView = {texture}; const copySize = {width: 2, height: 2, depth: 1}; const commandEncoder = device.createCommandEncoder(); // {} commandEncoder.copyBufferToTexture(bufferCopyView, textureCopyView, copySize); const computePassEncoder = commandEncoder.beginComputePass(); computePassEncoder.setPipeline(computePipeline); computePassEncoder.setBindGroup(0, bindGroup); computePassEncoder.dispatch(1, 1, 1); computePassEncoder.endPass(); const red = {r: 0, g: 0, b: 1, a: 1}; const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}]; const depthStencilAttachment = null; const renderPassDescriptor = {colorAttachments, depthStencilAttachment}; const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); renderPassEncoder.setPipeline(renderPipeline); renderPassEncoder.setBindGroup(0, bindGroup); renderPassEncoder.setVertexBuffers(0, [vertexBuffer], [0]); renderPassEncoder.draw(4, 1, 0, 0); renderPassEncoder.endPass(); const commandBuffer = commandEncoder.finish(); device.getQueue().submit([commandBuffer]); } if (window.testRunner) testRunner.waitUntilDone(); window.addEventListener("load", function() { start().then(function() { if (window.testRunner) testRunner.notifyDone(); }, function(e) { alert(e); if (window.testRunner) testRunner.notifyDone(); }); }); </script> </body> </html>
View Attachment As Raw
Actions:
View
Attachments on
bug 198985
: 372411