-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
288 lines (252 loc) · 8.41 KB
/
main.go
File metadata and controls
288 lines (252 loc) · 8.41 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Example: Instanced Rendering
// Demonstrates drawing many objects efficiently using GPU instancing.
// Instead of issuing separate draw calls, we draw all instances in one call.
package main
import (
"fmt"
"log"
"math"
"unsafe"
"github.com/go-webgpu/webgpu/wgpu"
"github.com/gogpu/gputypes"
)
// Vertex data: position (x, y) + color (r, g, b)
// Size: 20 bytes per vertex (2*4 + 3*4)
type Vertex struct {
Position [2]float32
Color [3]float32
}
// Instance data: offset (x, y) + scale + padding
// Size: 16 bytes per instance (must be aligned for GPU)
type InstanceData struct {
Offset [2]float32
Scale float32
_pad float32 // Padding to align to 16 bytes
}
// Triangle vertices (centered at origin)
var triangleVertices = []Vertex{
{Position: [2]float32{0.0, 0.1}, Color: [3]float32{1.0, 0.0, 0.0}}, // Top - red
{Position: [2]float32{-0.1, -0.1}, Color: [3]float32{0.0, 1.0, 0.0}}, // Bottom-left - green
{Position: [2]float32{0.1, -0.1}, Color: [3]float32{0.0, 0.0, 1.0}}, // Bottom-right - blue
}
// Generate instance data for a grid of triangles
func generateInstanceData(gridSize int) []InstanceData {
instances := make([]InstanceData, gridSize*gridSize)
spacing := 2.0 / float32(gridSize+1)
for y := 0; y < gridSize; y++ {
for x := 0; x < gridSize; x++ {
idx := y*gridSize + x
// Position instances in a grid from -1 to 1
instances[idx] = InstanceData{
Offset: [2]float32{
-1.0 + spacing*float32(x+1),
-1.0 + spacing*float32(y+1),
},
Scale: 0.5 + 0.5*float32(math.Sin(float64(idx)*0.5)), // Varying scale
}
}
}
return instances
}
const shaderCode = `
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) color: vec3<f32>,
// Per-instance attributes
@location(2) offset: vec2<f32>,
@location(3) scale: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec3<f32>,
}
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
// Apply scale and offset to position
let scaled_pos = in.position * in.scale;
let final_pos = scaled_pos + in.offset;
out.position = vec4<f32>(final_pos, 0.0, 1.0);
out.color = in.color;
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color, 1.0);
}
`
func main() {
// Initialize
if err := wgpu.Init(); err != nil {
log.Fatal(err)
}
instance, err := wgpu.CreateInstance(nil)
if err != nil {
log.Fatal(err)
}
defer instance.Release()
adapter, err := instance.RequestAdapter(nil)
if err != nil {
log.Fatal(err)
}
defer adapter.Release()
device, err := adapter.RequestDevice(nil)
if err != nil {
log.Fatal(err)
}
defer device.Release()
queue := device.GetQueue()
defer queue.Release()
// Create shader module
shader := device.CreateShaderModuleWGSL(shaderCode)
if shader == nil {
log.Fatal("failed to create shader module")
}
defer shader.Release()
// Generate instance data (5x5 grid = 25 instances)
const gridSize = 5
instanceData := generateInstanceData(gridSize)
instanceCount := uint32(len(instanceData))
fmt.Printf("Drawing %d triangles using instancing\n", instanceCount)
// Create vertex buffer (per-vertex data)
vertexBufferSize := uint64(len(triangleVertices)) * uint64(unsafe.Sizeof(triangleVertices[0]))
vertexBuffer := device.CreateBuffer(&wgpu.BufferDescriptor{
Usage: gputypes.BufferUsageVertex | gputypes.BufferUsageCopyDst,
Size: vertexBufferSize,
MappedAtCreation: wgpu.True,
})
if vertexBuffer == nil {
log.Fatal("failed to create vertex buffer")
}
defer vertexBuffer.Release()
// Copy vertex data
ptr := vertexBuffer.GetMappedRange(0, vertexBufferSize)
if ptr != nil {
mappedSlice := unsafe.Slice((*Vertex)(ptr), len(triangleVertices))
copy(mappedSlice, triangleVertices)
}
vertexBuffer.Unmap()
// Create instance buffer (per-instance data)
instanceBufferSize := uint64(len(instanceData)) * uint64(unsafe.Sizeof(instanceData[0]))
instanceBuffer := device.CreateBuffer(&wgpu.BufferDescriptor{
Usage: gputypes.BufferUsageVertex | gputypes.BufferUsageCopyDst,
Size: instanceBufferSize,
MappedAtCreation: wgpu.True,
})
if instanceBuffer == nil {
log.Fatal("failed to create instance buffer")
}
defer instanceBuffer.Release()
// Copy instance data
ptr = instanceBuffer.GetMappedRange(0, instanceBufferSize)
if ptr != nil {
mappedSlice := unsafe.Slice((*InstanceData)(ptr), len(instanceData))
copy(mappedSlice, instanceData)
}
instanceBuffer.Unmap()
// Define vertex attributes for per-vertex buffer
vertexAttributes := []wgpu.VertexAttribute{
{Format: gputypes.VertexFormatFloat32x2, Offset: 0, ShaderLocation: 0}, // position
{Format: gputypes.VertexFormatFloat32x3, Offset: 8, ShaderLocation: 1}, // color
}
// Define vertex attributes for per-instance buffer
instanceAttributes := []wgpu.VertexAttribute{
{Format: gputypes.VertexFormatFloat32x2, Offset: 0, ShaderLocation: 2}, // offset
{Format: gputypes.VertexFormatFloat32, Offset: 8, ShaderLocation: 3}, // scale
}
// Create render pipeline with two vertex buffer layouts:
// - Slot 0: Per-vertex data (position, color) with VertexStepModeVertex
// - Slot 1: Per-instance data (offset, scale) with VertexStepModeInstance
pipeline := device.CreateRenderPipeline(&wgpu.RenderPipelineDescriptor{
Vertex: wgpu.VertexState{
Module: shader,
EntryPoint: "vs_main",
Buffers: []wgpu.VertexBufferLayout{
// Per-vertex buffer (slot 0)
{
ArrayStride: uint64(unsafe.Sizeof(Vertex{})),
StepMode: gputypes.VertexStepModeVertex,
AttributeCount: uintptr(len(vertexAttributes)),
Attributes: &vertexAttributes[0],
},
// Per-instance buffer (slot 1)
{
ArrayStride: uint64(unsafe.Sizeof(InstanceData{})),
StepMode: gputypes.VertexStepModeInstance, // Key: advances per instance, not per vertex
AttributeCount: uintptr(len(instanceAttributes)),
Attributes: &instanceAttributes[0],
},
},
},
Fragment: &wgpu.FragmentState{
Module: shader,
EntryPoint: "fs_main",
Targets: []wgpu.ColorTargetState{
{Format: gputypes.TextureFormatRGBA8Unorm, WriteMask: gputypes.ColorWriteMaskAll},
},
},
Primitive: wgpu.PrimitiveState{
Topology: gputypes.PrimitiveTopologyTriangleList,
FrontFace: gputypes.FrontFaceCCW,
CullMode: gputypes.CullModeNone,
},
Multisample: wgpu.MultisampleState{
Count: 1,
Mask: 0xFFFFFFFF,
},
})
if pipeline == nil {
log.Fatal("failed to create render pipeline")
}
defer pipeline.Release()
// Create output texture
outputTexture := device.CreateTexture(&wgpu.TextureDescriptor{
Size: gputypes.Extent3D{Width: 256, Height: 256, DepthOrArrayLayers: 1},
Format: gputypes.TextureFormatRGBA8Unorm,
Usage: gputypes.TextureUsageRenderAttachment | gputypes.TextureUsageCopySrc,
})
if outputTexture == nil {
log.Fatal("failed to create output texture")
}
defer outputTexture.Release()
outputView := outputTexture.CreateView(nil)
defer outputView.Release()
// Create command encoder
encoder := device.CreateCommandEncoder(nil)
if encoder == nil {
log.Fatal("failed to create command encoder")
}
// Begin render pass
renderPass := encoder.BeginRenderPass(&wgpu.RenderPassDescriptor{
ColorAttachments: []wgpu.RenderPassColorAttachment{
{
View: outputView,
LoadOp: gputypes.LoadOpClear,
StoreOp: gputypes.StoreOpStore,
ClearValue: wgpu.Color{R: 0.1, G: 0.1, B: 0.1, A: 1.0},
},
},
})
renderPass.SetPipeline(pipeline)
renderPass.SetVertexBuffer(0, vertexBuffer, 0, vertexBufferSize) // Per-vertex data
renderPass.SetVertexBuffer(1, instanceBuffer, 0, instanceBufferSize) // Per-instance data
// Draw all instances in ONE call!
// vertexCount=3 (triangle), instanceCount=25 (grid)
renderPass.Draw(3, instanceCount, 0, 0)
renderPass.End()
renderPass.Release()
// Submit
cmdBuffer := encoder.Finish(nil)
encoder.Release()
queue.Submit(cmdBuffer)
cmdBuffer.Release()
fmt.Println("Instanced rendering complete!")
fmt.Println()
fmt.Println("Key concepts demonstrated:")
fmt.Println(" - VertexStepModeVertex: data advances per vertex")
fmt.Println(" - VertexStepModeInstance: data advances per instance")
fmt.Printf(" - Draw(vertexCount=3, instanceCount=%d): draws %d triangles in one call\n", instanceCount, instanceCount)
fmt.Println()
fmt.Printf("Without instancing: %d draw calls\n", instanceCount)
fmt.Println("With instancing: 1 draw call")
}