FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orillusion-webgpu-samples/src/basicTriangle.ts at main · 3d-src/orillusion-webgpu-samples · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
3d-src
/
orillusion-webgpu-samples
Public
forked from
Orillusion/orillusion-webgpu-samples
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
orillusion-webgpu-samples
/
src
/
basicTriangle.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (93 loc) · 3.48 KB
Breadcrumbs
orillusion-webgpu-samples
/
src
/
basicTriangle.ts
Copy path
File metadata and controls
96 lines (93 loc) · 3.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import
triangleVert
from
'./shaders/triangle.vert.wgsl?raw'
import
redFrag
from
'./shaders/red.frag.wgsl?raw'
// initialize webgpu device & config canvas context
async
function
initWebGPU
(
canvas
:
HTMLCanvasElement
)
{
if
(
!
navigator
.
gpu
)
throw
new
Error
(
'Not Support WebGPU'
)
const
adapter
=
await
navigator
.
gpu
.
requestAdapter
(
{
powerPreference
:
'high-performance'
// powerPreference: 'low-power'
}
)
if
(
!
adapter
)
throw
new
Error
(
'No Adapter Found'
)
const
device
=
await
adapter
.
requestDevice
(
)
const
context
=
canvas
.
getContext
(
'webgpu'
)
as
GPUCanvasContext
const
format
=
navigator
.
gpu
.
getPreferredCanvasFormat
?
navigator
.
gpu
.
getPreferredCanvasFormat
(
)
:
context
.
getPreferredFormat
(
adapter
)
const
devicePixelRatio
=
window
.
devicePixelRatio
||
1
canvas
.
width
=
canvas
.
clientWidth
*
devicePixelRatio
canvas
.
height
=
canvas
.
clientHeight
*
devicePixelRatio
const
size
=
{
width
:
canvas
.
width
,
height
:
canvas
.
height
}
context
.
configure
(
{
// json specific format when key and value are the same
device
,
format
,
// prevent chrome warning
alphaMode
:
'opaque'
}
)
return
{
device
,
context
,
format
,
size
}
}
// create a simple pipiline
async
function
initPipeline
(
device
:
GPUDevice
,
format
:
GPUTextureFormat
)
:
Promise
<
GPURenderPipeline
>
{
const
descriptor
:
GPURenderPipelineDescriptor
=
{
layout
:
'auto'
,
vertex
:
{
module
:
device
.
createShaderModule
(
{
code
:
triangleVert
}
)
,
entryPoint
:
'main'
}
,
primitive
:
{
topology
:
'triangle-list'
// try point-list, line-list, line-strip, triangle-strip?
}
,
fragment
:
{
module
:
device
.
createShaderModule
(
{
code
:
redFrag
}
)
,
entryPoint
:
'main'
,
targets
:
[
{
format
:
format
}
]
}
}
return
await
device
.
createRenderPipelineAsync
(
descriptor
)
}
// create & submit device commands
function
draw
(
device
:
GPUDevice
,
context
:
GPUCanvasContext
,
pipeline
:
GPURenderPipeline
)
{
const
commandEncoder
=
device
.
createCommandEncoder
(
)
const
view
=
context
.
getCurrentTexture
(
)
.
createView
(
)
const
renderPassDescriptor
:
GPURenderPassDescriptor
=
{
colorAttachments
:
[
{
view
:
view
,
clearValue
:
{
r
:
0
,
g
:
0
,
b
:
0
,
a
:
1.0
}
,
loadOp
:
'clear'
,
// clear/load
storeOp
:
'store'
// store/discard
}
]
}
const
passEncoder
=
commandEncoder
.
beginRenderPass
(
renderPassDescriptor
)
passEncoder
.
setPipeline
(
pipeline
)
// 3 vertex form a triangle
passEncoder
.
draw
(
3
)
passEncoder
.
end
(
)
// webgpu run in a separate process, all the commands will be executed after submit
device
.
queue
.
submit
(
[
commandEncoder
.
finish
(
)
]
)
}
async
function
run
(
)
{
const
canvas
=
document
.
querySelector
(
'canvas'
)
if
(
!
canvas
)
throw
new
Error
(
'No Canvas'
)
const
{
device
,
context
,
format
}
=
await
initWebGPU
(
canvas
)
const
pipeline
=
await
initPipeline
(
device
,
format
)
// start draw
draw
(
device
,
context
,
pipeline
)
// re-configure context on resize
window
.
addEventListener
(
'resize'
,
(
)
=>
{
canvas
.
width
=
canvas
.
clientWidth
*
devicePixelRatio
canvas
.
height
=
canvas
.
clientHeight
*
devicePixelRatio
// don't need to recall context.configure() after v104
draw
(
device
,
context
,
pipeline
)
}
)
}
run
(
)
Back
|
FazBrowse Home
|
New Git URL