Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjglgamedev committed Sep 13, 2020
1 parent b8723bd commit 1c8499e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class Render {
private Queue.GraphicsQueue graphQueue;
private Instance instance;
private LightingRenderActivity lightingRenderActivity;
private MemoryAllocator memoryAllocator;
private List<VulkanMesh> meshList;
private PhysicalDevice physicalDevice;
private PipelineCache pipelineCache;
Expand All @@ -40,7 +39,6 @@ public void cleanup() {
commandPool.cleanup();
swapChain.cleanup();
surface.cleanup();
memoryAllocator.cleanUp();
device.cleanup();
physicalDevice.cleanup();
instance.cleanup();
Expand All @@ -50,8 +48,7 @@ public void init(Window window, Scene scene) {
EngineProperties engProps = EngineProperties.getInstance();
instance = new Instance(engProps.isValidate());
physicalDevice = PhysicalDevice.createPhysicalDevice(instance, engProps.getPhysDeviceName());
device = new Device(physicalDevice);
memoryAllocator = new MemoryAllocator(instance, device);
device = new Device(instance, physicalDevice);
surface = new Surface(physicalDevice, window.getWindowHandle());
graphQueue = new Queue.GraphicsQueue(device, 0);
presentQueue = new Queue.PresentQueue(device, surface, 0);
Expand All @@ -61,14 +58,14 @@ public void init(Window window, Scene scene) {
pipelineCache = new PipelineCache(device);
meshList = new ArrayList<>();
textureCache = new TextureCache();
geometryRenderActivity = new GeometryRenderActivity(memoryAllocator, swapChain, commandPool, pipelineCache, scene);
lightingRenderActivity = new LightingRenderActivity(memoryAllocator, swapChain, commandPool, pipelineCache,
geometryRenderActivity = new GeometryRenderActivity(swapChain, commandPool, pipelineCache, scene);
lightingRenderActivity = new LightingRenderActivity(swapChain, commandPool, pipelineCache,
geometryRenderActivity.getAttachments(), scene);
}

public void loadMeshes(MeshData[] meshDataList) {
LOGGER.debug("Loading {} meshe(s)", meshDataList.length);
VulkanMesh[] meshes = VulkanMesh.loadMeshes(memoryAllocator, textureCache, commandPool, graphQueue, meshDataList);
VulkanMesh[] meshes = VulkanMesh.loadMeshes(textureCache, commandPool, graphQueue, meshDataList);
LOGGER.debug("Loaded {} meshe(s)", meshes.length);
meshList.addAll(Arrays.asList(meshes));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public void cleanup() {
textureMap.clear();
}

public Texture createTexture(MemoryAllocator memoryAllocator, String texturePath, int format) {
public Texture createTexture(Device device, String texturePath, int format) {
String path = texturePath;
if (texturePath == null || texturePath.trim().isEmpty()) {
EngineProperties engProperties = EngineProperties.getInstance();
path = engProperties.getDefaultTexturePath();
}
Texture texture = textureMap.get(path);
if (texture == null) {
texture = new Texture(memoryAllocator, path, format);
texture = new Texture(device, path, format);
textureMap.put(path, texture);
}
return texture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class GeometryRenderActivity {
private VulkanBuffer materialsBuffer;
private MaterialDescriptorSet materialsDescriptorSet;
private MatrixDescriptorSetLayout matrixDescriptorSetLayout;
private MemoryAllocator memoryAllocator;
private Pipeline pipeLine;
private PipelineCache pipelineCache;
private MatrixDescriptorSet projMatrixDescriptorSet;
Expand All @@ -45,9 +44,7 @@ public class GeometryRenderActivity {
private VulkanBuffer[] viewMatricesBuffer;
private MatrixDescriptorSet[] viewMatricesDescriptorSets;

public GeometryRenderActivity(MemoryAllocator memoryAllocator, SwapChain swapChain, CommandPool commandPool,
PipelineCache pipelineCache, Scene scene) {
this.memoryAllocator = memoryAllocator;
public GeometryRenderActivity(SwapChain swapChain, CommandPool commandPool, PipelineCache pipelineCache, Scene scene) {
this.swapChain = swapChain;
this.pipelineCache = pipelineCache;
device = swapChain.getDevice();
Expand Down Expand Up @@ -112,18 +109,18 @@ private void createDescriptorSets(int numImages) {
EngineProperties engineProps = EngineProperties.getInstance();
descriptorSetMap = new HashMap<>();
textureSampler = new TextureSampler(device, 1);
projMatrixUniform = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
projMatrixUniform = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
projMatrixDescriptorSet = new MatrixDescriptorSet(descriptorPool, matrixDescriptorSetLayout, projMatrixUniform, 0);

viewMatricesDescriptorSets = new MatrixDescriptorSet[numImages];
viewMatricesBuffer = new VulkanBuffer[numImages];
materialsBuffer = new VulkanBuffer(memoryAllocator, (long) materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(),
materialsBuffer = new VulkanBuffer(device, (long) materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(),
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
materialsDescriptorSet = new MaterialDescriptorSet(descriptorPool, materialDescriptorSetLayout,
materialsBuffer, 0);
for (int i = 0; i < numImages; i++) {
viewMatricesBuffer[i] = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
viewMatricesBuffer[i] = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
viewMatricesDescriptorSets[i] = new MatrixDescriptorSet(descriptorPool, matrixDescriptorSetLayout,
viewMatricesBuffer[i], 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ public class LightingRenderActivity {
private LightsDescriptorSetLayout lightsDescriptorSetLayout;
private LightsDescriptorSet[] lightsDescriptorSets;
private MatrixDescriptorSetLayout matrixDescriptorSetLayout;
private MemoryAllocator memoryAllocator;
private Pipeline pipeline;
private PipelineCache pipelineCache;
private ShaderProgram shaderProgram;
private SpecializationConstants specializationConstants;
private SwapChain swapChain;

public LightingRenderActivity(MemoryAllocator memoryAllocator, SwapChain swapChain, CommandPool commandPool,
PipelineCache pipelineCache, Attachment[] attachments, Scene scene) {
this.memoryAllocator = memoryAllocator;
public LightingRenderActivity(SwapChain swapChain, CommandPool commandPool, PipelineCache pipelineCache,
Attachment[] attachments, Scene scene) {
this.swapChain = swapChain;
device = swapChain.getDevice();
this.pipelineCache = pipelineCache;
Expand Down Expand Up @@ -152,12 +150,12 @@ private void createShaders(int numSamples) {
}

private void createUniforms(int numImages) {
invProjBuffer = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
invProjBuffer = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);

lightsBuffers = new VulkanBuffer[numImages];
for (int i = 0; i < numImages; i++) {
lightsBuffers[i] = new VulkanBuffer(memoryAllocator,
lightsBuffers[i] = new VulkanBuffer(device,
GraphConstants.INT_LENGTH * 4 + GraphConstants.VEC4_SIZE * 2 * GraphConstants.MAX_LIGHTS +
GraphConstants.VEC4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
public class Device {

private static final Logger LOGGER = LogManager.getLogger();
private MemoryAllocator memoryAllocator;
private PhysicalDevice physicalDevice;
private boolean sampleRateShading;
private boolean samplerAnisotropy;
private VkDevice vkDevice;

public Device(PhysicalDevice physicalDevice) {
public Device(Instance instance, PhysicalDevice physicalDevice) {
LOGGER.debug("Creating device");

this.physicalDevice = physicalDevice;
Expand Down Expand Up @@ -62,14 +63,21 @@ public Device(PhysicalDevice physicalDevice) {
vkCheck(vkCreateDevice(physicalDevice.getVkPhysicalDevice(), deviceCreateInfo, null, pp),
"Failed to create device");
vkDevice = new VkDevice(pp.get(0), physicalDevice.getVkPhysicalDevice(), deviceCreateInfo);

memoryAllocator = new MemoryAllocator(instance, physicalDevice, vkDevice);
}
}

public void cleanup() {
LOGGER.debug("Destroying Vulkan device");
memoryAllocator.cleanUp();
vkDestroyDevice(vkDevice, null);
}

public MemoryAllocator getMemoryAllocator() {
return memoryAllocator;
}

public PhysicalDevice getPhysicalDevice() {
return physicalDevice;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.vma.*;
import org.lwjgl.vulkan.VkDevice;

import static org.lwjgl.util.vma.Vma.*;
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;

public class MemoryAllocator {

private Device device;
private long vmaAllocator;

public MemoryAllocator(Instance instance, Device device) {
this.device = device;
public MemoryAllocator(Instance instance, PhysicalDevice physicalDevice, VkDevice vkDevice) {
try (MemoryStack stack = MemoryStack.stackPush()) {
PointerBuffer pAllocator = stack.mallocPointer(1);

VmaVulkanFunctions vmaVulkanFunctions = VmaVulkanFunctions.callocStack(stack)
.set(instance.getVkInstance(), device.getVkDevice());
.set(instance.getVkInstance(), vkDevice);

VmaAllocatorCreateInfo createInfo = VmaAllocatorCreateInfo.callocStack(stack)
.flags(bitIf(VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT,
device.getVkDevice().getCapabilities().VK_KHR_dedicated_allocation))
.device(device.getVkDevice())
.physicalDevice(device.getPhysicalDevice().getVkPhysicalDevice())
.device(vkDevice)
.physicalDevice(physicalDevice.getVkPhysicalDevice())
.pVulkanFunctions(vmaVulkanFunctions);
vkCheck(vmaCreateAllocator(createInfo, pAllocator),
"Failed to create VMA allocator");
Expand All @@ -33,16 +30,8 @@ public MemoryAllocator(Instance instance, Device device) {
}
}

private static int bitIf(int bit, boolean condition) {
return condition ? bit : 0;
}

public void cleanUp() {
vmaDestroyAllocator(this.vmaAllocator);
}

public Device getDevice() {
return device;
vmaDestroyAllocator(vmaAllocator);
}

public long getVmaAllocator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Texture {
private VulkanBuffer stgBuffer;
private int width;

public Texture(MemoryAllocator memoryAllocator, String fileName, int imageFormat) {
public Texture(Device device, String fileName, int imageFormat) {
LOGGER.debug("Creating texture [{}]", fileName);
this.fileName = fileName;
ByteBuffer buf;
Expand All @@ -42,8 +42,7 @@ public Texture(MemoryAllocator memoryAllocator, String fileName, int imageFormat
height = h.get();
mipLevels = (int) Math.floor(log2(Math.min(width, height))) + 1;

createStgBuffer(memoryAllocator, buf);
Device device = memoryAllocator.getDevice();
createStgBuffer(device, buf);
image = new Image(device, width, height, imageFormat,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
mipLevels, 1);
Expand All @@ -66,9 +65,9 @@ public void cleanupStgBuffer() {
}
}

private void createStgBuffer(MemoryAllocator memoryAllocator, ByteBuffer data) {
private void createStgBuffer(Device device, ByteBuffer data) {
int size = width * height * BYTES_PER_PIXEL;
stgBuffer = new VulkanBuffer(memoryAllocator, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
stgBuffer = new VulkanBuffer(device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
long mappedMemory = stgBuffer.map();
ByteBuffer buffer = MemoryUtil.memByteBuffer(mappedMemory, (int) stgBuffer.getRequestedSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class VulkanBuffer {

private long allocation;
private long buffer;
private Device device;
private long mappedMemory;
private MemoryAllocator memoryAllocator;
private PointerBuffer pb;
private long requestedSize;

public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage, int memoryUsage,
public VulkanBuffer(Device device, long size, int bufferUsage, int memoryUsage,
int requiredFlags) {
this.memoryAllocator = memoryAllocator;
this.device = device;
requestedSize = size;
try (MemoryStack stack = MemoryStack.stackPush()) {
VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo.callocStack(stack)
Expand All @@ -38,7 +38,7 @@ public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage,

PointerBuffer pAllocation = stack.callocPointer(1);
LongBuffer lp = stack.mallocLong(1);
vkCheck(vmaCreateBuffer(memoryAllocator.getVmaAllocator(), bufferCreateInfo, allocInfo, lp,
vkCheck(vmaCreateBuffer(device.getMemoryAllocator().getVmaAllocator(), bufferCreateInfo, allocInfo, lp,
pAllocation, null), "Failed to create buffer");
buffer = lp.get(0);
allocation = pAllocation.get(0);
Expand All @@ -49,7 +49,7 @@ public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage,
public void cleanup() {
pb.free();
unMap();
vmaDestroyBuffer(memoryAllocator.getVmaAllocator(), buffer, allocation);
vmaDestroyBuffer(device.getMemoryAllocator().getVmaAllocator(), buffer, allocation);
}

public long getBuffer() {
Expand All @@ -62,7 +62,7 @@ public long getRequestedSize() {

public long map() {
if (mappedMemory == NULL) {
vkCheck(vmaMapMemory(memoryAllocator.getVmaAllocator(), allocation, pb),
vkCheck(vmaMapMemory(device.getMemoryAllocator().getVmaAllocator(), allocation, pb),
"Failed to map allocation");
mappedMemory = pb.get(0);
}
Expand All @@ -71,7 +71,7 @@ public long map() {

public void unMap() {
if (mappedMemory != NULL) {
vmaUnmapMemory(memoryAllocator.getVmaAllocator(), allocation);
vmaUnmapMemory(device.getMemoryAllocator().getVmaAllocator(), allocation);
mappedMemory = NULL;
}
}
Expand Down
Loading

0 comments on commit 1c8499e

Please sign in to comment.