Skip to content

Commit

Permalink
Initial fake implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Jul 14, 2024
1 parent ad998f4 commit 8444e0c
Show file tree
Hide file tree
Showing 9 changed files with 1,119 additions and 1 deletion.
46 changes: 46 additions & 0 deletions com.io7m.jcoronado.fake/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>com.io7m.jcoronado</artifactId>
<groupId>com.io7m.jcoronado</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.jcoronado.fake</artifactId>

<packaging>jar</packaging>
<name>com.io7m.jcoronado.fake</name>
<description>Type-safe Vulkan frontend (Fake implementation)</description>
<url>https://www.io7m.com/software/jcoronado</url>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>com.io7m.jcoronado.api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.io7m.junreachable</groupId>
<artifactId>com.io7m.junreachable.core</artifactId>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.jcoronado.fake;

import com.io7m.jcoronado.api.VulkanExtensionType;
import com.io7m.jcoronado.api.VulkanInstanceType;
import com.io7m.jcoronado.api.VulkanPhysicalDeviceType;
import com.io7m.jcoronado.api.VulkanVersion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

/**
* A fake instance.
*/

public final class VFakeInstance implements VulkanInstanceType
{
private final VFakeInstances owner;
private final AtomicBoolean closed;
private final ArrayList<VFakePhysicalDevice> physicalDevices;
private Map<String, VulkanExtensionType> extensions;

/**
* A fake instance.
*
* @param inOwner The owner
* @param startExtensions The initial extensions enabled
*/

public VFakeInstance(
final VFakeInstances inOwner,
final Map<String, VulkanExtensionType> startExtensions)
{
this.owner =
Objects.requireNonNull(inOwner, "owner");
this.closed =
new AtomicBoolean(false);
this.extensions = new HashMap<>();
this.extensions.putAll(startExtensions);

this.physicalDevices = new ArrayList<>();
this.physicalDevices.add(new VFakePhysicalDevice(this));
}

/**
* Set the extensions used.
*
* @param newExtensions The extensions
*/

public void setExtensions(
final Map<String, VulkanExtensionType> newExtensions)
{
this.extensions = Objects.requireNonNull(newExtensions, "extensions");
}

@Override
public void close()
{
if (this.closed.compareAndSet(false, true)) {
// Nothing
}
}

@Override
public boolean isClosed()
{
return this.closed.get();
}

@Override
public Stream<VulkanPhysicalDeviceType> enumeratePhysicalDevices()
{
return this.physicalDevices.stream()
.map(x -> x);
}

@Override
public VulkanVersion apiVersionMaximumSupported()
{
return this.owner.findSupportedInstanceVersion();
}

@Override
public VulkanVersion apiVersionUsed()
{
return this.owner.findSupportedInstanceVersion();
}

@Override
public Map<String, VulkanExtensionType> enabledExtensions()
{
return this.extensions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.jcoronado.fake;

import com.io7m.jcoronado.api.VulkanCallFailedException;
import com.io7m.jcoronado.api.VulkanException;
import com.io7m.jcoronado.api.VulkanExtensionProperties;
import com.io7m.jcoronado.api.VulkanHostAllocatorType;
import com.io7m.jcoronado.api.VulkanInstanceCreateInfo;
import com.io7m.jcoronado.api.VulkanInstanceProviderType;
import com.io7m.jcoronado.api.VulkanInstanceType;
import com.io7m.jcoronado.api.VulkanLayerProperties;
import com.io7m.jcoronado.api.VulkanVersion;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* A fake instance provider.
*/

public final class VFakeInstances
implements VulkanInstanceProviderType
{
private String providerName =
"com.io7m.jcoronado.fake";
private String providerVersion =
"1.0.0";
private VulkanVersion vulkanVersion =
VulkanVersion.of(1, 0, 0);

private Map<String, VulkanExtensionProperties> extensions = Map.of();
private Map<String, VulkanLayerProperties> layers = Map.of();
private VulkanInstanceType nextInstance;

/**
* A fake instance provider.
*/

public VFakeInstances()
{
this.nextInstance = new VFakeInstance(this, Map.of());
}

/**
* Set the extensions.
*
* @param newExtensions The new extensions
*/

public void setExtensions(
final Map<String, VulkanExtensionProperties> newExtensions)
{
this.extensions = Objects.requireNonNull(newExtensions, "extensions");
}

/**
* Set the layers.
*
* @param newLayers The new layers
*/

public void setLayers(
final Map<String, VulkanLayerProperties> newLayers)
{
this.layers =
Objects.requireNonNull(newLayers, "layers");
}

/**
* @return The next instance that will be returned
*/

public VulkanInstanceType getNextInstance()
{
return this.nextInstance;
}

/**
* Set the next instance that will be returned.
*
* @param setNextInstance The next instance
*/

public void setNextInstance(
final VulkanInstanceType setNextInstance)
{
this.nextInstance =
Objects.requireNonNull(setNextInstance, "nextInstance");
}

/**
* Set the provider name.
*
* @param newName The name
*/

public void setProviderName(
final String newName)
{
this.providerName =
Objects.requireNonNull(newName, "providerName");
}

/**
* Set the provider version.
*
* @param newVersion The version
*/

public void setProviderVersion(
final String newVersion)
{
this.providerVersion =
Objects.requireNonNull(newVersion, "providerVersion");
}

/**
* Set the Vulkan version.
*
* @param newVulkanVersion The version
*/

public void setVulkanVersion(
final VulkanVersion newVulkanVersion)
{
this.vulkanVersion =
Objects.requireNonNull(newVulkanVersion, "vulkanVersion");
}

@Override
public String providerName()
{
return this.providerName;
}

@Override
public String providerVersion()
{
return this.providerVersion;
}

@Override
public VulkanVersion findSupportedInstanceVersion()
{
return this.vulkanVersion;
}

@Override
public Map<String, VulkanExtensionProperties> extensions()
{
return this.extensions;
}

@Override
public Map<String, VulkanLayerProperties> layers()
{
return this.layers;
}

@Override
public VulkanInstanceType createInstance(
final VulkanInstanceCreateInfo info,
final Optional<VulkanHostAllocatorType> allocator)
throws VulkanException
{
if (this.nextInstance == null) {
throw new VulkanCallFailedException(
0,
"vkCreateInstance",
"No instance is available"
);
}
return this.nextInstance;
}
}
Loading

0 comments on commit 8444e0c

Please sign in to comment.