Skip to content

Commit fa15565

Browse files
committed
display: use GLFW when ::isLaptop is set (not just on mac)
1 parent f0e30a7 commit fa15565

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

virtual-programs/display.folk

+43-30
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
# to run pixel shaders with image and numerical parameters (so you
55
# can draw images, shapes, etc from Display.)
66

7+
set ::isLaptop 1
78
set makeGpu {
89
set macos [expr {$tcl_platform(os) eq "Darwin"}]
910

10-
if {!$macos} {
11+
if {!$::isLaptop} {
1112
foreach renderFile [glob -nocomplain "/dev/dri/render*"] {
1213
if {![file readable $renderFile]} {
1314
puts stderr "Gpu: Warning: $renderFile is not readable by current user; Vulkan device may not appear.
@@ -70,6 +71,8 @@ Try doing `sudo adduser folk render`."
7071
dc include <stdlib.h>
7172
if {$macos} {
7273
dc cflags -I/opt/homebrew/include -L/opt/homebrew/lib
74+
}
75+
if {$::isLaptop} {
7376
dc include <GLFW/glfw3.h>
7477
dc cflags -lglfw
7578
}
@@ -112,7 +115,7 @@ Try doing `sudo adduser folk render`."
112115
}
113116
dc proc init {int displayIdx} void [csubst {
114117
$[vktry volkInitialize()]
115-
$[if {$macos} { expr {"glfwInit();"} }]
118+
$[if {$::isLaptop} { expr {"glfwInit();"} }]
116119

117120
// Set up VkInstance instance:
118121
{
@@ -125,22 +128,30 @@ Try doing `sudo adduser folk render`."
125128
createInfo.enabledLayerCount = sizeof(validationLayers)/sizeof(validationLayers[0]);
126129
createInfo.ppEnabledLayerNames = validationLayers;
127130

128-
const char* enabledExtensions[] = $[expr { $macos ? {{
129-
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
130-
VK_KHR_SURFACE_EXTENSION_NAME,
131-
"VK_EXT_metal_surface",
131+
uint32_t enabledExtensionCount = 1;
132+
const char* enabledExtensions[10] = {
132133
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
133-
}} : {{
134-
// 2 extensions for non-X11/Wayland display
135-
VK_KHR_SURFACE_EXTENSION_NAME,
136-
VK_KHR_DISPLAY_EXTENSION_NAME,
137-
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
138-
}} }];
139-
createInfo.enabledExtensionCount = sizeof(enabledExtensions)/sizeof(enabledExtensions[0]);
140-
createInfo.ppEnabledExtensionNames = enabledExtensions;
134+
};
135+
141136
$[if {$macos} { expr {{
137+
enabledExtensions[enabledExtensionCount++] = VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME;
142138
createInfo.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
143139
}} }]
140+
$[if {$::isLaptop} { expr {{
141+
uint32_t extCount;
142+
const char** extensions = glfwGetRequiredInstanceExtensions(&extCount);
143+
144+
while (extCount > 0) {
145+
enabledExtensions[enabledExtensionCount++] = extensions[--extCount];
146+
}
147+
}} } else { expr {{
148+
enabledExtensions[enabledExtensionCount++] = VK_KHR_SURFACE_EXTENSION_NAME;
149+
enabledExtensions[enabledExtensionCount++] = VK_KHR_DISPLAY_EXTENSION_NAME;
150+
}} }]
151+
152+
createInfo.ppEnabledExtensionNames = enabledExtensions;
153+
createInfo.enabledExtensionCount = enabledExtensionCount;
154+
144155
VkResult res = vkCreateInstance(&createInfo, NULL, &instance);
145156
if (res != VK_SUCCESS) {
146157
fprintf(stderr, "Failed vkCreateInstance: %s (%d)\n",
@@ -202,14 +213,13 @@ Try doing `sudo adduser folk render`."
202213

203214
VkPhysicalDeviceFeatures deviceFeatures = {0};
204215

205-
const char *deviceExtensions[] = $[expr { $macos ? {{
216+
const char *deviceExtensions[] = {
206217
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
218+
VK_KHR_MAINTENANCE3_EXTENSION_NAME,
219+
$[expr { $macos ? {
207220
"VK_KHR_portability_subset",
208-
VK_KHR_MAINTENANCE3_EXTENSION_NAME
209-
}} : {{
210-
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
211-
VK_KHR_MAINTENANCE3_EXTENSION_NAME
212-
}} }];
221+
} : {} }]
222+
};
213223

214224
VkDeviceCreateInfo createInfo = {0};
215225
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
@@ -236,14 +246,14 @@ Try doing `sudo adduser folk render`."
236246

237247
// Get drawing surface.
238248
VkSurfaceKHR surface;
239-
$[expr { $macos ? {
249+
$[expr { $::isLaptop ? [csubst {
240250
GLFWwindow* window;
241251
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
242-
window = glfwCreateWindow(640, 480, "Window Title", NULL, NULL);
252+
window = glfwCreateWindow($WIDTH, $HEIGHT, "Window Title", NULL, NULL);
243253
if (glfwCreateWindowSurface(instance, window, NULL, &surface) != VK_SUCCESS) {
244254
fprintf(stderr, "Gpu: Failed to create GLFW window surface\n"); exit(1);
245255
}
246-
} : [csubst {
256+
}] : [csubst {
247257
// TODO: support multiple displays, pick best display mode
248258

249259
uint32_t displayCount;
@@ -291,7 +301,7 @@ Try doing `sudo adduser folk render`."
291301
if (capabilities.currentExtent.width != UINT32_MAX) {
292302
extent = capabilities.currentExtent;
293303
} else {
294-
$[expr { $macos ? {
304+
$[expr { $::isLaptop ? {
295305
glfwGetFramebufferSize(window, (int*) &extent.width, (int*) &extent.height);
296306
if (capabilities.minImageExtent.width > extent.width) { extent.width = capabilities.minImageExtent.width; }
297307
if (capabilities.maxImageExtent.width < extent.width) { extent.width = capabilities.maxImageExtent.width; }
@@ -808,12 +818,12 @@ Try doing `sudo adduser folk render`."
808818
presentInfo.pImageIndices = &imageIndex;
809819
presentInfo.pResults = NULL;
810820

811-
vkQueuePresentKHR(presentQueue, &presentInfo);
821+
$[vktry {vkQueuePresentKHR(presentQueue, &presentInfo)}]
812822
}
813823
}
814824

815825
dc proc poll {} void {
816-
$[expr { $macos ? { glfwPollEvents(); } : {} }]
826+
$[expr { $::isLaptop ? { glfwPollEvents(); } : {} }]
817827
}
818828

819829
# Construct a reusable GLSL function that can be linked into and
@@ -1554,7 +1564,7 @@ Try doing `sudo adduser folk render`."
15541564
source "lib/colors.tcl"
15551565

15561566
proc Start-display-process {code} {
1557-
if {$::tcl_platform(os) eq "Darwin"} {
1567+
if {$::isLaptop} {
15581568
# On macOS, you must talk to the GPU on the main thread, so we
15591569
# don't Start a subprocess here. We trampoline this code into
15601570
# a claim so we can run the rest of display.folk before
@@ -1574,6 +1584,7 @@ proc Start-display-process {code} {
15741584
Start-display-process {
15751585
puts "Display pid: [pid]"
15761586
set macos [expr {$::tcl_platform(os) eq "Darwin"}]
1587+
set ::isLaptop 1
15771588

15781589
source "virtual-programs/images.folk"
15791590
namespace eval Gpu $makeGpu
@@ -1646,7 +1657,9 @@ Start-display-process {
16461657
while true {
16471658
set ::displayList [dict create] ;# Keys are layer numbers; values are lists of commands
16481659
Step
1649-
if {$macos} { update }
1660+
if {$::isLaptop} {
1661+
update
1662+
}
16501663

16511664
foreach match [Statements::findMatches {/wisher/ wishes the GPU draws pipeline /name/ with /...options/}] {
16521665
try {
@@ -1682,7 +1695,7 @@ Start-display-process {
16821695
}
16831696
Gpu::drawEnd
16841697
}]
1685-
if {$macos} { Gpu::poll }
1698+
if {$::isLaptop} { Gpu::poll }
16861699

16871700
Commit { Claim the display time is "render $renderTime us ($::stepTime)" }
16881701
}
@@ -1700,7 +1713,7 @@ namespace eval ::Display {
17001713
variable WIDTH
17011714
variable HEIGHT
17021715
variable LAYER 0
1703-
if {$::tcl_platform(os) eq "Darwin"} {
1716+
if {$::isLaptop} {
17041717
set WIDTH [* 640 2]; set HEIGHT [* 480 2]
17051718
} else {
17061719
regexp {mode "(\d+)x(\d+)(?:-\d+)?"} [exec fbset] -> WIDTH HEIGHT

0 commit comments

Comments
 (0)