Skip to content

Commit

Permalink
Reworked synchronisation between resource loading thread and main gra…
Browse files Browse the repository at this point in the history
…phics thread
  • Loading branch information
MarconZet committed Jan 12, 2020
1 parent a6c0bb8 commit e4e3c11
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public interface EngineThread extends Executor {
*/
void start();
void interrupt();
void interruptThread();
boolean isRunning();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public void interrupt() {
running = false;
}

@Override
public void interruptThread() {
engineThread.interrupt();
}

@Override
public boolean isRunning() {
return running;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.warpgame.engine.core.context.task.RegisterTask;
import net.warpgame.engine.core.execution.task.EngineTask;
import net.warpgame.engine.graphics.core.InstanceManager;
import net.warpgame.engine.graphics.memory.VulkanLoadThread;
import net.warpgame.engine.graphics.rendering.pipeline.GraphicsPipeline;
import net.warpgame.engine.graphics.rendering.pipeline.RenderPass;
import net.warpgame.engine.graphics.window.SwapChain;
Expand All @@ -26,15 +25,12 @@
public class VulkanTask extends EngineTask {
private static final Logger logger = LoggerFactory.getLogger(VulkanTask.class);

private VulkanLoadThread vulkanLoadThread;

private InstanceManager instanceManager;
private SwapChain swapChain;
private RenderPass renderPass;
private GraphicsPipeline graphicsPipeline;

public VulkanTask(VulkanLoadThread vulkanLoadThread, InstanceManager instanceManager, SwapChain swapChain, RenderPass renderPass, GraphicsPipeline graphicsPipeline) {
this.vulkanLoadThread = vulkanLoadThread;
public VulkanTask(InstanceManager instanceManager, SwapChain swapChain, RenderPass renderPass, GraphicsPipeline graphicsPipeline) {
this.instanceManager = instanceManager;
this.swapChain = swapChain;
this.renderPass = renderPass;
Expand All @@ -46,7 +42,6 @@ protected void onInit() {
instanceManager.create();
logger.info("Creating Vulkan static resources");
swapChain.create();
vulkanLoadThread.interruptThread();
renderPass.create();
graphicsPipeline.create();
logger.info("Finished creating Vulkan static resources");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
@Service
@Profile("graphics")
@RegisterTask(thread = "loading")
public class VulkanLoadTask extends EngineTask {
private static final Logger logger = LoggerFactory.getLogger(VulkanLoadTask.class);
public class ResourceLoadTask extends EngineTask {
private static final Logger logger = LoggerFactory.getLogger(ResourceLoadTask.class);

private BlockingQueue<Loadable> loadQueue = new LinkedBlockingQueue<>();
private BlockingQueue<Loadable> unloadQueue = new LinkedBlockingQueue<>();
Expand All @@ -42,7 +42,7 @@ public class VulkanLoadTask extends EngineTask {
private Device device;


public VulkanLoadTask(RecordingTask recordingTask, Device device, Allocator allocator, SwapChain swapChain, QueueManager queueManager) {
public ResourceLoadTask(RecordingTask recordingTask, Device device, Allocator allocator, SwapChain swapChain, QueueManager queueManager) {
this.recordingTask = recordingTask;
this.device = device;
this.allocator = allocator;
Expand All @@ -52,46 +52,19 @@ public VulkanLoadTask(RecordingTask recordingTask, Device device, Allocator allo

@Override
protected void onInit() {

/* Object resource = new Object();
Thread a = new Thread() {
@Override
public void run() {
synchronized (resource) {
try {
resource.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread b = new Thread() {
@Override
public void run() {
Thread.sleep(200); //hard work
resource.notifyAll();
}
};*/

try {
if (!isReady())
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
if (!isReady()) {
throw new RuntimeException("Required resources are not ready",e);
synchronized (swapChain) {
if (!swapChain.isCreated())
swapChain.wait();
}
} catch (InterruptedException e) {
if (!swapChain.isCreated())
throw new RuntimeException("Required resources are not ready", e);
}
queue = queueManager.getTransportQueue();
commandPool = new OneTimeCommandPool(device, queue);
}

private boolean isReady() {
return device.isCreated() && queueManager.isCreated() && allocator.isCreated() && swapChain.isCreated();
}

@Override
public void update(int delta) {
Loadable loadable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
@Profile("graphics")
@EnableConfig
@RegisterExecutor("loading")
public class VulkanLoadThread extends SyncEngineThread {
public VulkanLoadThread(Config config) {
public class ResourceLoadThread extends SyncEngineThread {
public ResourceLoadThread(Config config) {
super(new SyncTimer(config.getValue("graphics.vulkan.loading.ups")), new OnePerUpdateExecutionStrategy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.warpgame.engine.graphics.command.poll.CommandPool;
import net.warpgame.engine.graphics.core.Device;
import net.warpgame.engine.graphics.memory.Allocator;
import net.warpgame.engine.graphics.memory.VulkanLoadTask;
import net.warpgame.engine.graphics.memory.ResourceLoadTask;

import java.io.FileNotFoundException;

Expand All @@ -23,7 +23,7 @@ public abstract class Loadable {

private int loadStatus = NOT_LOADED;

private VulkanLoadTask vulkanLoadTask;
private ResourceLoadTask resourceLoadTask;
protected Context loadedContext;

public void loadResource(Device device, Allocator allocator, CommandPool commandPool) throws FileNotFoundException {
Expand All @@ -44,15 +44,15 @@ public synchronized void scheduleForLoad(Property property) {
if (loadStatus == NOT_LOADED) {
loadStatus = SCHEDULED_TO_LOAD;
loadedContext = property.getOwner().getContext().getLoadedContext();
vulkanLoadTask = loadedContext.findOne(VulkanLoadTask.class).get();
vulkanLoadTask.addToLoad(this);
resourceLoadTask = loadedContext.findOne(ResourceLoadTask.class).get();
resourceLoadTask.addToLoad(this);
}
}

public synchronized void scheduleForUnload(Property property) {
if (loadStatus == LOADED) {
loadStatus = SCHEDULED_TO_UNLOAD;
vulkanLoadTask.addToUnload(this);
resourceLoadTask.addToUnload(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class RecordingTask extends EngineTask {
private static final Logger logger = LoggerFactory.getLogger(RecordingTask.class);

private Deque<DrawCommands> drawCommands = new LinkedList<>();
private boolean commandBuffersOffered;
private boolean recreate;
private boolean recreate = false;
private CommandPool commandPool;
private Set<Component> registeredComponents = Collections.newSetFromMap(new WeakHashMap<>());
private Set<VulkanRender> vulkanRenders = new HashSet<>();
Expand All @@ -63,7 +62,6 @@ public RecordingTask(DescriptorPool descriptorPool, Device device, SceneHolder s
this.renderPass = renderPass;
this.swapChain = swapChain;
this.graphicsPipeline = graphicsPipeline;
this.recreate = false;
this.sceneHolder = sceneHolder;
}

Expand All @@ -90,6 +88,7 @@ public void update(int delta) {

@Override
protected void onClose() {
drawCommands.forEach(x -> commandPool.freeCommandBuffer(x.getCommandBuffers()));
vulkanRenders.forEach(VulkanRender::destroy);
commandPool.destroy();
descriptorPool.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface CreateAndDestroy extends Destroyable {
void create();

default boolean isCreated() {
return true;
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public void create() {
createSwapChain();
getSwapChainImages();
createImageViews();
created = true;
synchronized (this) {
created = true;
notifyAll();
}
}

@Override
Expand All @@ -66,6 +69,7 @@ public void destroy() {


private boolean created = false;

@Override
public boolean isCreated() {
return created;
Expand Down Expand Up @@ -122,14 +126,14 @@ private void createSwapChain() {
private void getSwapChainImages() {
IntBuffer pImageCount = BufferUtils.createIntBuffer(1);
int err = vkGetSwapchainImagesKHR(device.get(), swapChain, pImageCount, null);
if(err != VK_SUCCESS){
if (err != VK_SUCCESS) {
throw new VulkanAssertionError("Failed to get number of swap chain images", err);
}
int imageCount = pImageCount.get(0);

LongBuffer swapChainImages = BufferUtils.createLongBuffer(imageCount);
err = vkGetSwapchainImagesKHR(device.get(), swapChain, pImageCount, swapChainImages);
if(err != VK_SUCCESS){
if (err != VK_SUCCESS) {
throw new VulkanAssertionError("Failed to get swap chain images", err);
}
Image[] res = new Image[imageCount];
Expand Down

0 comments on commit e4e3c11

Please sign in to comment.