Skip to content

Commit

Permalink
BaseApplication: move updateDescriptorSet() to the Draw class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Aug 9, 2023
1 parent b6ea58d commit 1bf440e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import org.lwjgl.vulkan.VK10;
import org.lwjgl.vulkan.VkAllocationCallbacks;
import org.lwjgl.vulkan.VkApplicationInfo;
import org.lwjgl.vulkan.VkCopyDescriptorSet;
import org.lwjgl.vulkan.VkDebugUtilsMessengerCallbackDataEXT;
import org.lwjgl.vulkan.VkDebugUtilsMessengerCreateInfoEXT;
import org.lwjgl.vulkan.VkDescriptorBufferInfo;
import org.lwjgl.vulkan.VkDescriptorImageInfo;
import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding;
import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo;
import org.lwjgl.vulkan.VkDevice;
Expand Down Expand Up @@ -91,7 +88,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import org.lwjgl.vulkan.VkVertexInputAttributeDescription;
import org.lwjgl.vulkan.VkVertexInputBindingDescription;
import org.lwjgl.vulkan.VkViewport;
import org.lwjgl.vulkan.VkWriteDescriptorSet;

/**
* A single-window, 3-D visualization application using LWJGL v3, GLFW, and
Expand Down Expand Up @@ -1641,10 +1637,8 @@ private static void renderFrame(Frame frame) {
Geometry geometry = geometries.get(geometryI);
geometry.writeUniformValuesTo(nonGlobalUbo);

Texture texture = geometry.getTexture();
long descriptorSetHandle = draw.descriptorSetHandle();
updateDescriptorSet(texture, samplerHandle, globalUbo,
nonGlobalUbo, descriptorSetHandle);
draw.updateDescriptorSet(
geometry, samplerHandle, globalUbo);

long pipelineHandle = createPipeline(pipelineLayoutHandle,
framebufferExtent, passHandle, geometry);
Expand Down Expand Up @@ -1736,73 +1730,4 @@ private void updateBase() {
Frame frame = inFlightFrames[currentFrameIndex];
renderFrame(frame);
}

/**
* Update the descriptor sets after a change.
*
* @param texture the texture to be sampled (not null)
* @param samplerHandle the handle of the VkSampler for textures
* @param globalUbo the global UBO (not null)
* @param nonGlobalUbo the per-geometry UBO (not null)
* @param descriptorSetHandle the handle of the descriptor set
*/
private static void updateDescriptorSet(
Texture texture, long samplerHandle,
BufferResource globalUbo, BufferResource nonGlobalUbo,
long descriptorSetHandle) {
try (MemoryStack stack = MemoryStack.stackPush()) {
VkDescriptorBufferInfo.Buffer pBufferInfo
= VkDescriptorBufferInfo.calloc(2, stack);

VkDescriptorBufferInfo guDbi = pBufferInfo.get(0);
guDbi.offset(0);
int numBytes = UniformValues.numBytes();
guDbi.range(numBytes);
long guHandle = globalUbo.handle();
guDbi.buffer(guHandle);

VkDescriptorBufferInfo nguDbi = pBufferInfo.get(1);
nguDbi.offset(0);
numBytes = NonGlobalUniformValues.numBytes();
nguDbi.range(numBytes);
long nguHandle = nonGlobalUbo.handle();
nguDbi.buffer(nguHandle);

VkDescriptorImageInfo.Buffer pImageInfo
= VkDescriptorImageInfo.calloc(1, stack);
pImageInfo.imageLayout(
VK10.VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
long viewHandle = texture.viewHandle();
pImageInfo.imageView(viewHandle);
pImageInfo.sampler(samplerHandle);

// Configure the descriptors to be written:
VkWriteDescriptorSet.Buffer pWrites
= VkWriteDescriptorSet.calloc(2, stack);

VkWriteDescriptorSet uboWrite = pWrites.get(0);
uboWrite.sType(VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);

uboWrite.descriptorCount(2); // 2 UBOs
uboWrite.descriptorType(VK10.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uboWrite.dstArrayElement(0);
uboWrite.dstBinding(0);
uboWrite.dstSet(descriptorSetHandle);
uboWrite.pBufferInfo(pBufferInfo);

VkWriteDescriptorSet samplerWrite = pWrites.get(1);
samplerWrite.sType(VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);

samplerWrite.descriptorCount(1);
samplerWrite.descriptorType(
VK10.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
samplerWrite.dstArrayElement(0);
samplerWrite.dstBinding(2);
samplerWrite.dstSet(descriptorSetHandle);
samplerWrite.pImageInfo(pImageInfo);

VkCopyDescriptorSet.Buffer pCopies = null;
VK10.vkUpdateDescriptorSets(vkDevice, pWrites, pCopies);
}
}
}
71 changes: 71 additions & 0 deletions lib/src/main/java/com/github/stephengold/vsport/Draw.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VK10;
import org.lwjgl.vulkan.VkAllocationCallbacks;
import org.lwjgl.vulkan.VkCopyDescriptorSet;
import org.lwjgl.vulkan.VkDescriptorBufferInfo;
import org.lwjgl.vulkan.VkDescriptorImageInfo;
import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo;
import org.lwjgl.vulkan.VkDevice;
import org.lwjgl.vulkan.VkWriteDescriptorSet;

/**
* Resources required to generate the commands for a single draw: a non-global
Expand Down Expand Up @@ -143,6 +147,73 @@ void setPipeline(long handle) {

this.pipelineHandle = handle;
}

/**
* Update the descriptor set for rendering.
*
* @param geometry the geometry to be rendered (not null)
* @param samplerHandle the handle of the VkSampler for textures (not null)
* @param globalUbo the global UBO (not null)
*/
void updateDescriptorSet(
Geometry geometry, long samplerHandle, BufferResource globalUbo) {
try (MemoryStack stack = MemoryStack.stackPush()) {
VkDescriptorBufferInfo.Buffer pBufferInfo
= VkDescriptorBufferInfo.calloc(2, stack);

VkDescriptorBufferInfo guDbi = pBufferInfo.get(0);
guDbi.offset(0);
int numBytes = UniformValues.numBytes();
guDbi.range(numBytes);
long guHandle = globalUbo.handle();
guDbi.buffer(guHandle);

VkDescriptorBufferInfo nguDbi = pBufferInfo.get(1);
nguDbi.offset(0);
numBytes = NonGlobalUniformValues.numBytes();
nguDbi.range(numBytes);
long nguHandle = nonGlobalUbo.handle();
nguDbi.buffer(nguHandle);

VkDescriptorImageInfo.Buffer pImageInfo
= VkDescriptorImageInfo.calloc(1, stack);
pImageInfo.imageLayout(
VK10.VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Texture texture = geometry.getTexture();
long viewHandle = texture.viewHandle();
pImageInfo.imageView(viewHandle);
pImageInfo.sampler(samplerHandle);

// Configure the descriptors to be written:
VkWriteDescriptorSet.Buffer pWrites
= VkWriteDescriptorSet.calloc(2, stack);

VkWriteDescriptorSet uboWrite = pWrites.get(0);
uboWrite.sType(VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);

uboWrite.descriptorCount(2); // 2 UBOs
uboWrite.descriptorType(VK10.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uboWrite.dstArrayElement(0);
uboWrite.dstBinding(0);
uboWrite.dstSet(descriptorSetHandle);
uboWrite.pBufferInfo(pBufferInfo);

VkWriteDescriptorSet samplerWrite = pWrites.get(1);
samplerWrite.sType(VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);

samplerWrite.descriptorCount(1);
samplerWrite.descriptorType(
VK10.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
samplerWrite.dstArrayElement(0);
samplerWrite.dstBinding(2);
samplerWrite.dstSet(descriptorSetHandle);
samplerWrite.pImageInfo(pImageInfo);

VkDevice vkDevice = BaseApplication.getVkDevice();
VkCopyDescriptorSet.Buffer pCopies = null;
VK10.vkUpdateDescriptorSets(vkDevice, pWrites, pCopies);
}
}
// *************************************************************************
// new methods exposed

Expand Down

0 comments on commit 1bf440e

Please sign in to comment.