Skip to content

Commit

Permalink
Check for availability of VK_LAYER_KHRONOS_validation
Browse files Browse the repository at this point in the history
and disable Vulkan debugging if unavailable.
  • Loading branch information
httpdigest committed Oct 29, 2021
1 parent 73b08c6 commit ff1713f
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 31 deletions.
31 changes: 30 additions & 1 deletion res/org/lwjgl/demo/opengl/shader/downsampling/downsample.cs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#extension GL_KHR_shader_subgroup_shuffle : require

layout(location=0) uniform sampler2D baseImage;
layout(binding=0, rgba16f) uniform writeonly restrict image2D mips[3];
layout(binding=0, rgba16f) uniform writeonly restrict image2D mips[5];

/*
* The assumption here is that each subgroup item maps to its corresponding local workgroup item
Expand All @@ -30,6 +30,8 @@ int unpack(int x) {
return x;
}

shared vec4 sm[4][4];

void main(void) {
ivec2 ts = textureSize(baseImage, 0);

Expand Down Expand Up @@ -78,4 +80,31 @@ void main(void) {
t = (t + h + v + d) * vec4(0.25);
if ((gl_SubgroupInvocationID & 15) == 0)
imageStore(mips[2], i/ivec2(4), t);

// compute mip 4 using shared memory
/*
* For mip 4 we essentially have 8x8 work items.
*/
ivec2 smc = l / ivec2(4);
ivec2 smi = (smc + ivec2(1)) & ivec2(1);
if ((l.x & 3) == 0 && (l.y & 3) == 0)
sm[smc.x][smc.y] = t;
barrier();
if ((l.x & 7) == 0 && (l.y & 7) == 0) {
t = (sm[smc.x][smc.y] + sm[smi.x][smc.y] + sm[smc.x][smi.y] + sm[smi.x][smi.y]) * 0.25;
imageStore(mips[3], i/ivec2(8), t);
}

// compute mip 5 also using shared memory
/*
* For mip 5 we have 16x16 work items.
*/
smc = l / ivec2(8);
if ((l.x & 7) == 0 && (l.y & 7) == 0)
sm[smc.x][smc.y] = t;
barrier();
if (l.x == 0 && l.y == 0) {
t = (sm[0][0] + sm[1][0] + sm[0][1] + sm[1][1]) * 0.25;
imageStore(mips[4], i/ivec2(16), t);
}
}
21 changes: 15 additions & 6 deletions src/org/lwjgl/demo/opengl/shader/DownsamplingDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import org.joml.Random;
import org.lwjgl.demo.opengl.util.DemoUtils;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;
Expand All @@ -25,6 +26,7 @@
/**
* Computes 3 mip levels of a texture using only a single compute shader dispatch
* and GL_KHR_shader_subgroup.
* Then uses shared memory for mips 4 and 5.
*
* @author Kai Burjack
*/
Expand Down Expand Up @@ -97,8 +99,9 @@ private static void createComputeProgram() throws IOException {
computeProgram = program;
}

private static final Random rnd = new Random(1L);
private static byte v(int i) {
return i % 8 == 0 ? (byte) 255 : (byte) 0;
return (byte) (rnd.nextFloat() * 255);
}

private static void createTextures() {
Expand All @@ -108,12 +111,14 @@ private static void createTextures() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexStorage2D(GL_TEXTURE_2D, 4, GL_RGBA16F, width, height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 6);
glTexStorage2D(GL_TEXTURE_2D, 6, GL_RGBA16F, width, height);
ByteBuffer pixels = MemoryUtil.memAlloc(width * height * 4);
// fill the first level of the texture with some pattern
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
pixels.put(v(x)).put(v(y)).put((byte) 127).put((byte) 255);
pixels.put(v(x)).put(v(y)).put(v(x)).put((byte) 255);
pixels.flip();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
MemoryUtil.memFree(pixels);
Expand All @@ -125,10 +130,12 @@ private static void downsample() {

// read mip level 0
glBindTexture(GL_TEXTURE_2D, texture);
// write mip levels 1-3
// write mip levels 1-4
glBindImageTexture(0, texture, 1, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(1, texture, 2, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(2, texture, 3, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(3, texture, 4, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(4, texture, 5, false, 0, GL_WRITE_ONLY, GL_RGBA16F);

int texelsPerWorkItem = 2;
int numGroupsX = (int) ceil((double) width / texelsPerWorkItem / 16);
Expand All @@ -142,12 +149,14 @@ private static void downsample() {
glBindImageTexture(0, 0, 1, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(1, 0, 2, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(2, 0, 3, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(3, 0, 3, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glBindImageTexture(4, 0, 3, false, 0, GL_WRITE_ONLY, GL_RGBA16F);
glUseProgram(0);
}

private static void present() {
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, width/(1<<level), height/(1<<level));
glViewport(0, 0, width, height);
glUseProgram(quadProgram);
glUniform1i(levelUniform, level);
glBindVertexArray(nullVao);
Expand Down Expand Up @@ -179,7 +188,7 @@ private static void init() throws IOException {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, true);
else if (key == GLFW_KEY_UP && action == GLFW_RELEASE)
level = min(3, level + 1);
level = min(5, level + 1);
else if (key == GLFW_KEY_DOWN && action == GLFW_RELEASE)
level = max(0, level - 1);
});
Expand Down
26 changes: 23 additions & 3 deletions src/org/lwjgl/demo/vulkan/ClearScreenDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.lwjgl.demo.vulkan;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.joml.Math.*;
import static org.lwjgl.demo.vulkan.VKUtil.*;
Expand Down Expand Up @@ -187,9 +188,14 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
}
ppEnabledExtensionNames = pointers(stack, requiredExtensions, stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
}
PointerBuffer enabledLayers = null;
PointerBuffer ppEnabledLayerNames = null;
if (DEBUG) {
enabledLayers = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
List<String> supportedLayers = enumerateSupportedInstanceLayers();
if (!supportedLayers.contains("VK_LAYER_KHRONOS_validation")) {
System.err.println("DEBUG requested but layer VK_LAYER_KHRONOS_validation is unavailable. Install the Vulkan SDK for your platform. Vulkan debug layer will not be used.");
} else {
ppEnabledLayerNames = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
}
}
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo
.calloc(stack)
Expand All @@ -198,7 +204,7 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
.calloc(stack)
.sType$Default()
.apiVersion(VK_API_VERSION_1_1))
.ppEnabledLayerNames(enabledLayers)
.ppEnabledLayerNames(ppEnabledLayerNames)
.ppEnabledExtensionNames(ppEnabledExtensionNames);
PointerBuffer pInstance = stack.mallocPointer(1);
_CHECK_(vkCreateInstance(pCreateInfo, null, pInstance), "Failed to create VkInstance");
Expand Down Expand Up @@ -344,6 +350,20 @@ private static DeviceAndQueueFamilies selectBestPhysicalDevice() {
}
}

private static final List<String> enumerateSupportedInstanceLayers() {
try (MemoryStack stack = stackPush()) {
IntBuffer pPropertyCount = stack.mallocInt(1);
vkEnumerateInstanceLayerProperties(pPropertyCount, null);
int count = pPropertyCount.get(0);
if (count > 0) {
VkLayerProperties.Buffer pProperties = VkLayerProperties.malloc(count, stack);
vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
return pProperties.stream().map(VkLayerProperties::layerNameString).collect(toList());
}
}
return emptyList();
}

private static VkDevice createDevice(List<String> requiredExtensions) {
List<String> supportedDeviceExtensions = enumerateSupportedDeviceExtensions();
for (String requiredExtension : requiredExtensions) {
Expand Down
26 changes: 23 additions & 3 deletions src/org/lwjgl/demo/vulkan/raytracing/HybridMagicaVoxel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static java.lang.ClassLoader.getSystemResourceAsStream;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.joml.Math.*;
import static org.lwjgl.demo.vulkan.VKUtil.*;
Expand Down Expand Up @@ -277,6 +278,20 @@ private static List<String> enumerateSupportedInstanceExtensions() {
}
}

private static final List<String> enumerateSupportedInstanceLayers() {
try (MemoryStack stack = stackPush()) {
IntBuffer pPropertyCount = stack.mallocInt(1);
vkEnumerateInstanceLayerProperties(pPropertyCount, null);
int count = pPropertyCount.get(0);
if (count > 0) {
VkLayerProperties.Buffer pProperties = VkLayerProperties.malloc(count, stack);
vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
return pProperties.stream().map(VkLayerProperties::layerNameString).collect(toList());
}
}
return emptyList();
}

private static VkInstance createInstance(PointerBuffer requiredExtensions) {
List<String> supportedInstanceExtensions = enumerateSupportedInstanceExtensions();
try (MemoryStack stack = stackPush()) {
Expand All @@ -287,9 +302,14 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
}
ppEnabledExtensionNames = pointers(stack, requiredExtensions, stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
}
PointerBuffer enabledLayers = null;
PointerBuffer ppEnabledLayerNames = null;
if (DEBUG) {
enabledLayers = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
List<String> supportedLayers = enumerateSupportedInstanceLayers();
if (!supportedLayers.contains("VK_LAYER_KHRONOS_validation")) {
System.err.println("DEBUG requested but layer VK_LAYER_KHRONOS_validation is unavailable. Install the Vulkan SDK for your platform. Vulkan debug layer will not be used.");
} else {
ppEnabledLayerNames = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
}
}
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo
.calloc(stack)
Expand All @@ -298,7 +318,7 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
.calloc(stack)
.sType$Default()
.apiVersion(VK_API_VERSION_1_2))
.ppEnabledLayerNames(enabledLayers)
.ppEnabledLayerNames(ppEnabledLayerNames)
.ppEnabledExtensionNames(ppEnabledExtensionNames);
PointerBuffer pInstance = stack.mallocPointer(1);
_CHECK_(vkCreateInstance(pCreateInfo, null, pInstance), "Failed to create VkInstance");
Expand Down
26 changes: 23 additions & 3 deletions src/org/lwjgl/demo/vulkan/raytracing/ReflectiveMagicaVoxel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static java.lang.ClassLoader.getSystemResourceAsStream;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.joml.Math.*;
import static org.lwjgl.demo.vulkan.VKUtil.*;
Expand Down Expand Up @@ -273,9 +274,14 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
}
ppEnabledExtensionNames = pointers(stack, requiredExtensions, stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
}
PointerBuffer enabledLayers = null;
PointerBuffer ppEnabledLayerNames = null;
if (DEBUG) {
enabledLayers = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
List<String> supportedLayers = enumerateSupportedInstanceLayers();
if (!supportedLayers.contains("VK_LAYER_KHRONOS_validation")) {
System.err.println("DEBUG requested but layer VK_LAYER_KHRONOS_validation is unavailable. Install the Vulkan SDK for your platform. Vulkan debug layer will not be used.");
} else {
ppEnabledLayerNames = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
}
}
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo
.calloc(stack)
Expand All @@ -284,7 +290,7 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
.calloc(stack)
.sType$Default()
.apiVersion(VK_API_VERSION_1_1))
.ppEnabledLayerNames(enabledLayers)
.ppEnabledLayerNames(ppEnabledLayerNames)
.ppEnabledExtensionNames(ppEnabledExtensionNames);
PointerBuffer pInstance = stack.mallocPointer(1);
_CHECK_(vkCreateInstance(pCreateInfo, null, pInstance), "Failed to create VkInstance");
Expand Down Expand Up @@ -475,6 +481,20 @@ private static DeviceAndQueueFamilies selectPhysicalDevice() {
}
}

private static final List<String> enumerateSupportedInstanceLayers() {
try (MemoryStack stack = stackPush()) {
IntBuffer pPropertyCount = stack.mallocInt(1);
vkEnumerateInstanceLayerProperties(pPropertyCount, null);
int count = pPropertyCount.get(0);
if (count > 0) {
VkLayerProperties.Buffer pProperties = VkLayerProperties.malloc(count, stack);
vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
return pProperties.stream().map(VkLayerProperties::layerNameString).collect(toList());
}
}
return emptyList();
}

private static VkDevice createDevice(List<String> requiredExtensions) {
List<String> supportedDeviceExtensions = enumerateSupportedDeviceExtensions();
for (String requiredExtension : requiredExtensions) {
Expand Down
32 changes: 26 additions & 6 deletions src/org/lwjgl/demo/vulkan/raytracing/SdfBricks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static java.lang.ClassLoader.getSystemResourceAsStream;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.joml.Math.*;
import static org.lwjgl.demo.vulkan.VKUtil.*;
Expand Down Expand Up @@ -271,9 +272,14 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
}
ppEnabledExtensionNames = pointers(stack, requiredExtensions, stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
}
PointerBuffer enabledLayers = null;
PointerBuffer ppEnabledLayerNames = null;
if (DEBUG) {
enabledLayers = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
List<String> supportedLayers = enumerateSupportedInstanceLayers();
if (!supportedLayers.contains("VK_LAYER_KHRONOS_validation")) {
System.err.println("DEBUG requested but layer VK_LAYER_KHRONOS_validation is unavailable. Install the Vulkan SDK for your platform. Vulkan debug layer will not be used.");
} else {
ppEnabledLayerNames = stack.pointers(stack.UTF8("VK_LAYER_KHRONOS_validation"));
}
}
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo
.calloc(stack)
Expand All @@ -282,7 +288,7 @@ private static VkInstance createInstance(PointerBuffer requiredExtensions) {
.calloc(stack)
.sType$Default()
.apiVersion(VK_API_VERSION_1_1))
.ppEnabledLayerNames(enabledLayers)
.ppEnabledLayerNames(ppEnabledLayerNames)
.ppEnabledExtensionNames(ppEnabledExtensionNames);
PointerBuffer pInstance = stack.mallocPointer(1);
_CHECK_(vkCreateInstance(pCreateInfo, null, pInstance), "Failed to create VkInstance");
Expand Down Expand Up @@ -471,6 +477,20 @@ private static DeviceAndQueueFamilies selectPhysicalDevice() {
}
}

private static final List<String> enumerateSupportedInstanceLayers() {
try (MemoryStack stack = stackPush()) {
IntBuffer pPropertyCount = stack.mallocInt(1);
vkEnumerateInstanceLayerProperties(pPropertyCount, null);
int count = pPropertyCount.get(0);
if (count > 0) {
VkLayerProperties.Buffer pProperties = VkLayerProperties.malloc(count, stack);
vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
return pProperties.stream().map(VkLayerProperties::layerNameString).collect(toList());
}
}
return emptyList();
}

private static VkDevice createDevice(List<String> requiredExtensions) {
List<String> supportedDeviceExtensions = enumerateSupportedDeviceExtensions();
for (String requiredExtension : requiredExtensions) {
Expand Down Expand Up @@ -989,7 +1009,7 @@ private static VoxelField buildVoxelField() throws IOException {
Vector3i min = new Vector3i(Integer.MAX_VALUE);
Vector3i max = new Vector3i(Integer.MIN_VALUE);
byte[] field = new byte[(256 + 2) * (256 + 2) * (256 + 2)];
try (InputStream is = getSystemResourceAsStream("org/lwjgl/demo/models/mikelovesrobots_mmmm/scene_house5.vox");
try (InputStream is = getSystemResourceAsStream("voxelmodel/vox/monument/monu2.vox");
BufferedInputStream bis = new BufferedInputStream(is)) {
new MagicaVoxelLoader().read(bis, new MagicaVoxelLoader.Callback() {
public void voxel(int x, int y, int z, byte c) {
Expand Down Expand Up @@ -1022,8 +1042,8 @@ private static Geometry createGeometry() throws IOException {
int[] num = {0};
new GreedyVoxels(voxelField.ny, voxelField.py, voxelField.w, voxelField.d, new GreedyVoxels.Callback() {
public void voxel(int x0, int y0, int z0, int w, int h, int d, int v) {
aabbs.putFloat(x0).putFloat(y0).putFloat(z0);
aabbs.putFloat(x0+w).putFloat(y0+h+0.1f).putFloat(z0+d);
aabbs.putFloat(x0*8).putFloat(y0*9.6f).putFloat(z0*8);
aabbs.putFloat((x0+w)*8).putFloat((y0+h)*9.6f+1.8f).putFloat((z0+d)*8);
num[0]++;
}
}).merge(voxelField.field);
Expand Down
Loading

0 comments on commit ff1713f

Please sign in to comment.