Skip to content

Commit

Permalink
lib: argument validation and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Aug 9, 2023
1 parent 18715b8 commit 40979e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,7 @@ private static void createSurface() {
vkInstance, windowHandle, defaultAllocator, pHandle);
Utils.checkForError(retCode, "create window surface");
surfaceHandle = pHandle.get(0);
assert surfaceHandle != VK10.VK_NULL_HANDLE;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import java.util.Set;
import java.util.TreeSet;
import jme3utilities.MyString;
import jme3utilities.Validate;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.KHRSurface;
Expand Down Expand Up @@ -87,13 +88,15 @@ class PhysicalDevice {
/**
* Create a logical device based on this physical device.
*
* @param surfaceHandle the handle of the surface to use
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} for
* presentation (not null)
* @param enableDebugging true to produce debug output, false to suppress it
* @return a new instance
*/
VkDevice createLogicalDevice(long surfaceHandle, boolean enableDebugging) {
QueueFamilySummary queueFamilies = summarizeFamilies(surfaceHandle);
Validate.nonZero(surfaceHandle, "surface handle");

QueueFamilySummary queueFamilies = summarizeFamilies(surfaceHandle);
try (MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer distinctFamilies = queueFamilies.pListDistinct(stack);
int numDistinct = distinctFamilies.capacity();
Expand Down Expand Up @@ -262,12 +265,14 @@ int maxNumSamples() {
/**
* Rate the suitability of the device for graphics applications.
*
* @param surfaceHandle the surface for graphics display
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} for
* presentation (not null)
* @param diagnose true to print diagnostic messages, otherwise false
* @return a suitability score (>0, larger is more suitable) or zero if
* unsuitable
*/
float suitability(long surfaceHandle, boolean diagnose) {
Validate.nonZero(surfaceHandle, "surface handle");
if (diagnose) {
System.out.println(" Rating suitability of device " + this + ":");
}
Expand Down Expand Up @@ -314,10 +319,13 @@ float suitability(long surfaceHandle, boolean diagnose) {
/**
* Summarize the queue families provided by the device.
*
* @param surfaceHandle handle of the surface for presentation
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} for
* presentation (not null)
* @return a new instance (not null)
*/
QueueFamilySummary summarizeFamilies(long surfaceHandle) {
Validate.nonZero(surfaceHandle, "surface handle");

QueueFamilySummary result = new QueueFamilySummary();
try (MemoryStack stack = MemoryStack.stackPush()) {
// Count the available queue families:
Expand Down Expand Up @@ -357,11 +365,14 @@ QueueFamilySummary summarizeFamilies(long surfaceHandle) {
/**
* Summarize the properties of the specified surface on this device.
*
* @param surfaceHandle the handle of the surface to analyze
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} to analyze
* (not null)
* @param stack for memory allocation (not null)
* @return a new instance containing temporary buffers (not null)
*/
SurfaceSummary summarizeSurface(long surfaceHandle, MemoryStack stack) {
Validate.nonZero(surfaceHandle, "surface handle");

SurfaceSummary result = new SurfaceSummary(
vkPhysicalDevice, surfaceHandle, stack);
return result;
Expand Down Expand Up @@ -417,12 +428,15 @@ public String toString() {
/**
* Test whether the device has adequate swap-chain support.
*
* @param surfaceHandle the surface to test against (not null)
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} to for
* presentation (not null)
* @param diagnose true to print diagnostic messages, otherwise false
* @return true if the support is adequate, otherwise false
*/
private boolean hasAdequateSwapChainSupport(
long surfaceHandle, boolean diagnose) {
Validate.nonZero(surfaceHandle, "surface handle");

try (MemoryStack stack = MemoryStack.stackPush()) {
SurfaceSummary surface = new SurfaceSummary(
vkPhysicalDevice, surfaceHandle, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ class SurfaceSummary {
*
* @param physicalDevice the physical device to be used (not null,
* unaffected)
* @param surfaceHandle the handle of the surface to be analyzed
* @param surfaceHandle the handle of the {@code VkSurfaceKHR} to analyze
* (not null)
* @param stack for memory allocation (not null)
*/
SurfaceSummary(VkPhysicalDevice physicalDevice, long surfaceHandle,
MemoryStack stack) {
Validate.nonNull(physicalDevice, "physical device");
Validate.nonZero(surfaceHandle, "surface handle");

this.surfaceHandle = surfaceHandle;

// Obtain the capabilities of the VkSurfaceKHR:
Expand Down

0 comments on commit 40979e0

Please sign in to comment.