Skip to content

Commit 1c8499e

Browse files
committed
Refactor
1 parent b8723bd commit 1c8499e

File tree

9 files changed

+52
-65
lines changed

9 files changed

+52
-65
lines changed

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/Render.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class Render {
1919
private Queue.GraphicsQueue graphQueue;
2020
private Instance instance;
2121
private LightingRenderActivity lightingRenderActivity;
22-
private MemoryAllocator memoryAllocator;
2322
private List<VulkanMesh> meshList;
2423
private PhysicalDevice physicalDevice;
2524
private PipelineCache pipelineCache;
@@ -40,7 +39,6 @@ public void cleanup() {
4039
commandPool.cleanup();
4140
swapChain.cleanup();
4241
surface.cleanup();
43-
memoryAllocator.cleanUp();
4442
device.cleanup();
4543
physicalDevice.cleanup();
4644
instance.cleanup();
@@ -50,8 +48,7 @@ public void init(Window window, Scene scene) {
5048
EngineProperties engProps = EngineProperties.getInstance();
5149
instance = new Instance(engProps.isValidate());
5250
physicalDevice = PhysicalDevice.createPhysicalDevice(instance, engProps.getPhysDeviceName());
53-
device = new Device(physicalDevice);
54-
memoryAllocator = new MemoryAllocator(instance, device);
51+
device = new Device(instance, physicalDevice);
5552
surface = new Surface(physicalDevice, window.getWindowHandle());
5653
graphQueue = new Queue.GraphicsQueue(device, 0);
5754
presentQueue = new Queue.PresentQueue(device, surface, 0);
@@ -61,14 +58,14 @@ public void init(Window window, Scene scene) {
6158
pipelineCache = new PipelineCache(device);
6259
meshList = new ArrayList<>();
6360
textureCache = new TextureCache();
64-
geometryRenderActivity = new GeometryRenderActivity(memoryAllocator, swapChain, commandPool, pipelineCache, scene);
65-
lightingRenderActivity = new LightingRenderActivity(memoryAllocator, swapChain, commandPool, pipelineCache,
61+
geometryRenderActivity = new GeometryRenderActivity(swapChain, commandPool, pipelineCache, scene);
62+
lightingRenderActivity = new LightingRenderActivity(swapChain, commandPool, pipelineCache,
6663
geometryRenderActivity.getAttachments(), scene);
6764
}
6865

6966
public void loadMeshes(MeshData[] meshDataList) {
7067
LOGGER.debug("Loading {} meshe(s)", meshDataList.length);
71-
VulkanMesh[] meshes = VulkanMesh.loadMeshes(memoryAllocator, textureCache, commandPool, graphQueue, meshDataList);
68+
VulkanMesh[] meshes = VulkanMesh.loadMeshes(textureCache, commandPool, graphQueue, meshDataList);
7269
LOGGER.debug("Loaded {} meshe(s)", meshes.length);
7370
meshList.addAll(Arrays.asList(meshes));
7471

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/TextureCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public void cleanup() {
2020
textureMap.clear();
2121
}
2222

23-
public Texture createTexture(MemoryAllocator memoryAllocator, String texturePath, int format) {
23+
public Texture createTexture(Device device, String texturePath, int format) {
2424
String path = texturePath;
2525
if (texturePath == null || texturePath.trim().isEmpty()) {
2626
EngineProperties engProperties = EngineProperties.getInstance();
2727
path = engProperties.getDefaultTexturePath();
2828
}
2929
Texture texture = textureMap.get(path);
3030
if (texture == null) {
31-
texture = new Texture(memoryAllocator, path, format);
31+
texture = new Texture(device, path, format);
3232
textureMap.put(path, texture);
3333
}
3434
return texture;

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/geometry/GeometryRenderActivity.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class GeometryRenderActivity {
3333
private VulkanBuffer materialsBuffer;
3434
private MaterialDescriptorSet materialsDescriptorSet;
3535
private MatrixDescriptorSetLayout matrixDescriptorSetLayout;
36-
private MemoryAllocator memoryAllocator;
3736
private Pipeline pipeLine;
3837
private PipelineCache pipelineCache;
3938
private MatrixDescriptorSet projMatrixDescriptorSet;
@@ -45,9 +44,7 @@ public class GeometryRenderActivity {
4544
private VulkanBuffer[] viewMatricesBuffer;
4645
private MatrixDescriptorSet[] viewMatricesDescriptorSets;
4746

48-
public GeometryRenderActivity(MemoryAllocator memoryAllocator, SwapChain swapChain, CommandPool commandPool,
49-
PipelineCache pipelineCache, Scene scene) {
50-
this.memoryAllocator = memoryAllocator;
47+
public GeometryRenderActivity(SwapChain swapChain, CommandPool commandPool, PipelineCache pipelineCache, Scene scene) {
5148
this.swapChain = swapChain;
5249
this.pipelineCache = pipelineCache;
5350
device = swapChain.getDevice();
@@ -112,18 +109,18 @@ private void createDescriptorSets(int numImages) {
112109
EngineProperties engineProps = EngineProperties.getInstance();
113110
descriptorSetMap = new HashMap<>();
114111
textureSampler = new TextureSampler(device, 1);
115-
projMatrixUniform = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
112+
projMatrixUniform = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
116113
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
117114
projMatrixDescriptorSet = new MatrixDescriptorSet(descriptorPool, matrixDescriptorSetLayout, projMatrixUniform, 0);
118115

119116
viewMatricesDescriptorSets = new MatrixDescriptorSet[numImages];
120117
viewMatricesBuffer = new VulkanBuffer[numImages];
121-
materialsBuffer = new VulkanBuffer(memoryAllocator, (long) materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(),
118+
materialsBuffer = new VulkanBuffer(device, (long) materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(),
122119
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
123120
materialsDescriptorSet = new MaterialDescriptorSet(descriptorPool, materialDescriptorSetLayout,
124121
materialsBuffer, 0);
125122
for (int i = 0; i < numImages; i++) {
126-
viewMatricesBuffer[i] = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
123+
viewMatricesBuffer[i] = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
127124
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
128125
viewMatricesDescriptorSets[i] = new MatrixDescriptorSet(descriptorPool, matrixDescriptorSetLayout,
129126
viewMatricesBuffer[i], 0);

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/lighting/LightingRenderActivity.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ public class LightingRenderActivity {
4040
private LightsDescriptorSetLayout lightsDescriptorSetLayout;
4141
private LightsDescriptorSet[] lightsDescriptorSets;
4242
private MatrixDescriptorSetLayout matrixDescriptorSetLayout;
43-
private MemoryAllocator memoryAllocator;
4443
private Pipeline pipeline;
4544
private PipelineCache pipelineCache;
4645
private ShaderProgram shaderProgram;
4746
private SpecializationConstants specializationConstants;
4847
private SwapChain swapChain;
4948

50-
public LightingRenderActivity(MemoryAllocator memoryAllocator, SwapChain swapChain, CommandPool commandPool,
51-
PipelineCache pipelineCache, Attachment[] attachments, Scene scene) {
52-
this.memoryAllocator = memoryAllocator;
49+
public LightingRenderActivity(SwapChain swapChain, CommandPool commandPool, PipelineCache pipelineCache,
50+
Attachment[] attachments, Scene scene) {
5351
this.swapChain = swapChain;
5452
device = swapChain.getDevice();
5553
this.pipelineCache = pipelineCache;
@@ -152,12 +150,12 @@ private void createShaders(int numSamples) {
152150
}
153151

154152
private void createUniforms(int numImages) {
155-
invProjBuffer = new VulkanBuffer(memoryAllocator, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
153+
invProjBuffer = new VulkanBuffer(device, GraphConstants.MAT4X4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
156154
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
157155

158156
lightsBuffers = new VulkanBuffer[numImages];
159157
for (int i = 0; i < numImages; i++) {
160-
lightsBuffers[i] = new VulkanBuffer(memoryAllocator,
158+
lightsBuffers[i] = new VulkanBuffer(device,
161159
GraphConstants.INT_LENGTH * 4 + GraphConstants.VEC4_SIZE * 2 * GraphConstants.MAX_LIGHTS +
162160
GraphConstants.VEC4_SIZE, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
163161
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/vk/Device.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
public class Device {
1414

1515
private static final Logger LOGGER = LogManager.getLogger();
16+
private MemoryAllocator memoryAllocator;
1617
private PhysicalDevice physicalDevice;
1718
private boolean sampleRateShading;
1819
private boolean samplerAnisotropy;
1920
private VkDevice vkDevice;
2021

21-
public Device(PhysicalDevice physicalDevice) {
22+
public Device(Instance instance, PhysicalDevice physicalDevice) {
2223
LOGGER.debug("Creating device");
2324

2425
this.physicalDevice = physicalDevice;
@@ -62,14 +63,21 @@ public Device(PhysicalDevice physicalDevice) {
6263
vkCheck(vkCreateDevice(physicalDevice.getVkPhysicalDevice(), deviceCreateInfo, null, pp),
6364
"Failed to create device");
6465
vkDevice = new VkDevice(pp.get(0), physicalDevice.getVkPhysicalDevice(), deviceCreateInfo);
66+
67+
memoryAllocator = new MemoryAllocator(instance, physicalDevice, vkDevice);
6568
}
6669
}
6770

6871
public void cleanup() {
6972
LOGGER.debug("Destroying Vulkan device");
73+
memoryAllocator.cleanUp();
7074
vkDestroyDevice(vkDevice, null);
7175
}
7276

77+
public MemoryAllocator getMemoryAllocator() {
78+
return memoryAllocator;
79+
}
80+
7381
public PhysicalDevice getPhysicalDevice() {
7482
return physicalDevice;
7583
}

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/vk/MemoryAllocator.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@
33
import org.lwjgl.PointerBuffer;
44
import org.lwjgl.system.MemoryStack;
55
import org.lwjgl.util.vma.*;
6+
import org.lwjgl.vulkan.VkDevice;
67

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

1011
public class MemoryAllocator {
1112

12-
private Device device;
1313
private long vmaAllocator;
1414

15-
public MemoryAllocator(Instance instance, Device device) {
16-
this.device = device;
15+
public MemoryAllocator(Instance instance, PhysicalDevice physicalDevice, VkDevice vkDevice) {
1716
try (MemoryStack stack = MemoryStack.stackPush()) {
1817
PointerBuffer pAllocator = stack.mallocPointer(1);
1918

2019
VmaVulkanFunctions vmaVulkanFunctions = VmaVulkanFunctions.callocStack(stack)
21-
.set(instance.getVkInstance(), device.getVkDevice());
20+
.set(instance.getVkInstance(), vkDevice);
2221

2322
VmaAllocatorCreateInfo createInfo = VmaAllocatorCreateInfo.callocStack(stack)
24-
.flags(bitIf(VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT,
25-
device.getVkDevice().getCapabilities().VK_KHR_dedicated_allocation))
26-
.device(device.getVkDevice())
27-
.physicalDevice(device.getPhysicalDevice().getVkPhysicalDevice())
23+
.device(vkDevice)
24+
.physicalDevice(physicalDevice.getVkPhysicalDevice())
2825
.pVulkanFunctions(vmaVulkanFunctions);
2926
vkCheck(vmaCreateAllocator(createInfo, pAllocator),
3027
"Failed to create VMA allocator");
@@ -33,16 +30,8 @@ public MemoryAllocator(Instance instance, Device device) {
3330
}
3431
}
3532

36-
private static int bitIf(int bit, boolean condition) {
37-
return condition ? bit : 0;
38-
}
39-
4033
public void cleanUp() {
41-
vmaDestroyAllocator(this.vmaAllocator);
42-
}
43-
44-
public Device getDevice() {
45-
return device;
34+
vmaDestroyAllocator(vmaAllocator);
4635
}
4736

4837
public long getVmaAllocator() {

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/vk/Texture.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Texture {
2323
private VulkanBuffer stgBuffer;
2424
private int width;
2525

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

45-
createStgBuffer(memoryAllocator, buf);
46-
Device device = memoryAllocator.getDevice();
45+
createStgBuffer(device, buf);
4746
image = new Image(device, width, height, imageFormat,
4847
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
4948
mipLevels, 1);
@@ -66,9 +65,9 @@ public void cleanupStgBuffer() {
6665
}
6766
}
6867

69-
private void createStgBuffer(MemoryAllocator memoryAllocator, ByteBuffer data) {
68+
private void createStgBuffer(Device device, ByteBuffer data) {
7069
int size = width * height * BYTES_PER_PIXEL;
71-
stgBuffer = new VulkanBuffer(memoryAllocator, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
70+
stgBuffer = new VulkanBuffer(device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
7271
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
7372
long mappedMemory = stgBuffer.map();
7473
ByteBuffer buffer = MemoryUtil.memByteBuffer(mappedMemory, (int) stgBuffer.getRequestedSize());

booksamples/chapter-12/src/main/java/org/vulkanb/eng/graph/vk/VulkanBuffer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public class VulkanBuffer {
1616

1717
private long allocation;
1818
private long buffer;
19+
private Device device;
1920
private long mappedMemory;
20-
private MemoryAllocator memoryAllocator;
2121
private PointerBuffer pb;
2222
private long requestedSize;
2323

24-
public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage, int memoryUsage,
24+
public VulkanBuffer(Device device, long size, int bufferUsage, int memoryUsage,
2525
int requiredFlags) {
26-
this.memoryAllocator = memoryAllocator;
26+
this.device = device;
2727
requestedSize = size;
2828
try (MemoryStack stack = MemoryStack.stackPush()) {
2929
VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo.callocStack(stack)
@@ -38,7 +38,7 @@ public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage,
3838

3939
PointerBuffer pAllocation = stack.callocPointer(1);
4040
LongBuffer lp = stack.mallocLong(1);
41-
vkCheck(vmaCreateBuffer(memoryAllocator.getVmaAllocator(), bufferCreateInfo, allocInfo, lp,
41+
vkCheck(vmaCreateBuffer(device.getMemoryAllocator().getVmaAllocator(), bufferCreateInfo, allocInfo, lp,
4242
pAllocation, null), "Failed to create buffer");
4343
buffer = lp.get(0);
4444
allocation = pAllocation.get(0);
@@ -49,7 +49,7 @@ public VulkanBuffer(MemoryAllocator memoryAllocator, long size, int bufferUsage,
4949
public void cleanup() {
5050
pb.free();
5151
unMap();
52-
vmaDestroyBuffer(memoryAllocator.getVmaAllocator(), buffer, allocation);
52+
vmaDestroyBuffer(device.getMemoryAllocator().getVmaAllocator(), buffer, allocation);
5353
}
5454

5555
public long getBuffer() {
@@ -62,7 +62,7 @@ public long getRequestedSize() {
6262

6363
public long map() {
6464
if (mappedMemory == NULL) {
65-
vkCheck(vmaMapMemory(memoryAllocator.getVmaAllocator(), allocation, pb),
65+
vkCheck(vmaMapMemory(device.getMemoryAllocator().getVmaAllocator(), allocation, pb),
6666
"Failed to map allocation");
6767
mappedMemory = pb.get(0);
6868
}
@@ -71,7 +71,7 @@ public long map() {
7171

7272
public void unMap() {
7373
if (mappedMemory != NULL) {
74-
vmaUnmapMemory(memoryAllocator.getVmaAllocator(), allocation);
74+
vmaUnmapMemory(device.getMemoryAllocator().getVmaAllocator(), allocation);
7575
mappedMemory = NULL;
7676
}
7777
}

0 commit comments

Comments
 (0)