-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfw-surface-test.lua
161 lines (135 loc) · 6.09 KB
/
glfw-surface-test.lua
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
local webgpu_cdefs = require("cdefs")
local ffi = require("ffi")
ffi.cdef(webgpu_cdefs)
local glfw_cdefs = [[
// Platform-specific (don't care)
typedef void* GLFWwindow;
typedef void* GLFWmonitor;
int glfwInit(void);
void glfwWindowHint(int hint, int value);
int glfwWindowShouldClose(GLFWwindow window);
void glfwPollEvents(void);
void glfwDestroyWindow(GLFWwindow window);
void glfwTerminate(void);
GLFWwindow glfwCreateWindow(int width, int height, const char* title, GLFWmonitor monitor, GLFWwindow share);
]]
ffi.cdef(glfw_cdefs)
local glfw_ext_cdefs = [[
// Custom extension
WGPUSurface glfwGetWGPUSurface(WGPUInstance instance, GLFWwindow* window);
]]
ffi.cdef(glfw_ext_cdefs)
local webgpu = ffi.load("wgpu_native")
local glfw = ffi.load("glfw3")
local glfwExt = ffi.load("glfw3webgpu")
print(webgpu, glfw, glfwExt)
local desc = ffi.new("WGPUInstanceDescriptor")
-- desc.nextInChain = nullptr
local instance = webgpu.wgpuCreateInstance(desc)
if not instance then
error("Could not initialize WebGPU!")
end
if not glfw.glfwInit() then
error("Could not initialize GLFW")
end
local GLFW_CLIENT_API = 0x00022001
local GLFW_NO_API = 0
glfw.glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API)
local window = glfw.glfwCreateWindow(640, 480, "Learn WebGPU", nil, nil)
if not window then
error("Could not open window!")
end
print("Requesting adaptor...")
-- // Utility function provided by glfw3webgpu.h
-- TBD not actually part of glfw... easier if in the runtime :/
local surface = glfwExt.glfwGetWGPUSurface(instance, window)
-- WGPUSurface surface = glfwGetWGPUSurface(instance, window)
print(surface)
-- // Adapter options: we need the adapter to draw to the window's surface
local adapterOpts = ffi.new("WGPURequestAdapterOptions")
-- adapterOpts.nextInChain = nullptr
adapterOpts.compatibleSurface = surface
-- // Get the adapter, see the comments in the definition of the body of the
-- // requestAdapter function above.
local requestedAdapter
local function onAdapterRequested(status, adapter, message, pUserData)
print("onAdapterRequested", status, adapter, message, pUserData)
assert(status == webgpu.WGPURequestAdapterStatus_Success, "Failed to request adapter")
requestedAdapter = adapter
-- TBD
end
local userdata = nil -- TBD
webgpu.wgpuInstanceRequestAdapter(instance, adapterOpts, onAdapterRequested, userdata)
print("Got adapter: ", requestedAdapter)
local function inspectAdapter(adapter)
-- std::vector<WGPUFeatureName> features;
local featureCount = webgpu.wgpuAdapterEnumerateFeatures(adapter, nil)
local features = ffi.new("WGPUFeatureName[?]", featureCount)
webgpu.wgpuAdapterEnumerateFeatures(adapter, features)
print("Adapter features:")
for index = 0, tonumber(featureCount) - 1 do
local feature = features[index]
print(index + 1, feature)
end
-- end
-- WGPUSupportedLimits limits = {};
local limits = ffi.new("WGPUSupportedLimits")
-- limits.nextInChain = nullptr;
local success = webgpu.wgpuAdapterGetLimits(adapter, limits)
if not success then
error("Failed to get adapter limits")
end
print("Adapter limits:")
print("\tmaxTextureDimension1D: ", limits.limits.maxTextureDimension1D)
print("\tmaxTextureDimension2D: ", limits.limits.maxTextureDimension2D)
print("\tmaxTextureDimension3D: ", limits.limits.maxTextureDimension3D)
print("\tmaxTextureArrayLayers: ", limits.limits.maxTextureArrayLayers)
print("\tmaxBindGroups: ", limits.limits.maxBindGroups)
print("\tmaxDynamicUniformBuffersPerPipelineLayout: ", limits.limits.maxDynamicUniformBuffersPerPipelineLayout)
print("\tmaxDynamicStorageBuffersPerPipelineLayout: ", limits.limits.maxDynamicStorageBuffersPerPipelineLayout)
print("\tmaxSampledTexturesPerShaderStage: ", limits.limits.maxSampledTexturesPerShaderStage)
print("\tmaxSamplersPerShaderStage: ", limits.limits.maxSamplersPerShaderStage)
print("\tmaxStorageBuffersPerShaderStage: ", limits.limits.maxStorageBuffersPerShaderStage)
print("\tmaxStorageTexturesPerShaderStage: ", limits.limits.maxStorageTexturesPerShaderStage)
print("\tmaxUniformBuffersPerShaderStage: ", limits.limits.maxUniformBuffersPerShaderStage)
print("\tmaxUniformBufferBindingSize: ", limits.limits.maxUniformBufferBindingSize)
print("\tmaxStorageBufferBindingSize: ", limits.limits.maxStorageBufferBindingSize)
print("\tminUniformBufferOffsetAlignment: ", limits.limits.minUniformBufferOffsetAlignment)
print("\tminStorageBufferOffsetAlignment: ", limits.limits.minStorageBufferOffsetAlignment)
print("\tmaxVertexBuffers: ", limits.limits.maxVertexBuffers)
print("\tmaxVertexAttributes: ", limits.limits.maxVertexAttributes)
print("\tmaxVertexBufferArrayStride: ", limits.limits.maxVertexBufferArrayStride)
print("\tmaxInterStageShaderComponents: ", limits.limits.maxInterStageShaderComponents)
print("\tmaxComputeWorkgroupStorageSize: ", limits.limits.maxComputeWorkgroupStorageSize)
print("\tmaxComputeInvocationsPerWorkgroup: ", limits.limits.maxComputeInvocationsPerWorkgroup)
print("\tmaxComputeWorkgroupSizeX: ", limits.limits.maxComputeWorkgroupSizeX)
print("\tmaxComputeWorkgroupSizeY: ", limits.limits.maxComputeWorkgroupSizeY)
print("\tmaxComputeWorkgroupSizeZ: ", limits.limits.maxComputeWorkgroupSizeZ)
print("\tmaxComputeWorkgroupsPerDimension: ", limits.limits.maxComputeWorkgroupsPerDimension)
-- WGPUAdapterProperties properties = {};
local properties = ffi.new("WGPUAdapterProperties")
-- properties.nextInChain = nullptr;
webgpu.wgpuAdapterGetProperties(adapter, properties)
print("Adapter properties:")
print("\tvendorID: ", properties.vendorID)
print("\tdeviceID: ", properties.deviceID)
print("\tname: ", properties.name)
if properties.driverDescription then
print("\tdriverDescription: ", properties.driverDescription)
end
print("\tadapterType: ", properties.adapterType)
print("\tbackendType: ", properties.backendType)
end
inspectAdapter(requestedAdapter)
local function runMainLoop()
local uv = require("uv")
local timer = uv.new_timer()
timer:start(0, 16, function() -- starts immediately, repeats every 16 milliseconds
glfw.glfwPollEvents()
if glfw.glfwWindowShouldClose(window) ~= 0 then
timer:stop()
uv.stop()
end
end)
end
runMainLoop()