1818import static org .lwjgl .vulkan .VK11 .*;
1919import static org .vulkanb .eng .graph .vk .VulkanUtils .vkCheck ;
2020
21- // TODO: Pre-record commands
22- // TODO: Update lights method
23- // TODO: Update lights Uniform per swap chain image?
2421public class LightingRenderActivity {
2522
2623 private static final String LIGHTING_FRAGMENT_SHADER_FILE_GLSL = "resources/shaders/lighting_fragment.glsl" ;
@@ -40,9 +37,9 @@ public class LightingRenderActivity {
4037 private VulkanBuffer invProjBuffer ;
4138 private MatrixDescriptorSet invProjMatrixDescriptorSet ;
4239 private LightingFrameBuffer lightingFrameBuffer ;
43- private VulkanBuffer lightsBuffer ;
44- private LightsDescriptorSet lightsDescriptorSet ;
40+ private VulkanBuffer [] lightsBuffers ;
4541 private LightsDescriptorSetLayout lightsDescriptorSetLayout ;
42+ private LightsDescriptorSet [] lightsDescriptorSets ;
4643 private MatrixDescriptorSetLayout matrixDescriptorSetLayout ;
4744 private Pipeline pipeline ;
4845 private PipelineCache pipelineCache ;
@@ -60,12 +57,15 @@ public LightingRenderActivity(SwapChain swapChain, CommandPool commandPool, Pipe
6057 int numImages = swapChain .getNumImages ();
6158 createShaders ();
6259 createDescriptorPool ();
63- createUniforms ();
64- createDescriptorSets (geometryFrameBuffer );
60+ createUniforms (numImages );
61+ createDescriptorSets (geometryFrameBuffer , numImages );
6562 createPipeline ();
6663 createCommandBuffers (commandPool , numImages );
64+ updateInvProjMatrix (scene );
6765
68- VulkanUtils .copyMatrixToBuffer (device , invProjBuffer , scene .getPerspective ().getPerspectiveMatrix ());
66+ for (int i = 0 ; i < numImages ; i ++) {
67+ preRecordCommandBuffer (i );
68+ }
6969 }
7070
7171 public void cleanup () {
@@ -75,7 +75,7 @@ public void cleanup() {
7575 attachmentsLayout .cleanup ();
7676 descriptorPool .cleanup ();
7777 ambientLightBuffer .cleanup ();
78- lightsBuffer . cleanup ( );
78+ Arrays . stream ( lightsBuffers ). forEach ( VulkanBuffer :: cleanup );
7979 pipeline .cleanup ();
8080 invProjBuffer .cleanup ();
8181 lightingFrameBuffer .cleanup ();
@@ -101,7 +101,7 @@ private void createDescriptorPool() {
101101 descriptorPool = new DescriptorPool (device , descriptorTypeCounts );
102102 }
103103
104- private void createDescriptorSets (GeometryFrameBuffer geometryFrameBuffer ) {
104+ private void createDescriptorSets (GeometryFrameBuffer geometryFrameBuffer , int numImages ) {
105105 attachmentsLayout = new AttachmentsLayout (device , GeometryAttachments .NUMBER_ATTACHMENTS );
106106 lightsDescriptorSetLayout = new LightsDescriptorSetLayout (device );
107107 matrixDescriptorSetLayout = new MatrixDescriptorSetLayout (device , 0 , VK_SHADER_STAGE_FRAGMENT_BIT );
@@ -113,10 +113,14 @@ private void createDescriptorSets(GeometryFrameBuffer geometryFrameBuffer) {
113113
114114 attachmentsDescriptorSet = new AttachmentsDescriptorSet (descriptorPool , attachmentsLayout ,
115115 geometryFrameBuffer , 0 );
116- lightsDescriptorSet = new LightsDescriptorSet (descriptorPool , lightsDescriptorSetLayout ,
117- lightsBuffer , ambientLightBuffer , 0 );
118116 invProjMatrixDescriptorSet = new MatrixDescriptorSet (descriptorPool , matrixDescriptorSetLayout ,
119117 invProjBuffer , 0 );
118+
119+ lightsDescriptorSets = new LightsDescriptorSet [numImages ];
120+ for (int i = 0 ; i < numImages ; i ++) {
121+ lightsDescriptorSets [i ] = new LightsDescriptorSet (descriptorPool , lightsDescriptorSetLayout ,
122+ lightsBuffers [i ], ambientLightBuffer , 0 );
123+ }
120124 }
121125
122126 private void createPipeline () {
@@ -140,33 +144,29 @@ private void createShaders() {
140144 });
141145 }
142146
143- private void createUniforms () {
147+ private void createUniforms (int numImages ) {
144148 ambientLightBuffer = new VulkanBuffer (device , GraphConstants .VEC4_SIZE , VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ,
145149 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT );
146- lightsBuffer = new VulkanBuffer (device ,
147- GraphConstants .INT_LENGTH * 4 + GraphConstants .VEC4_SIZE * 2 * GraphConstants .MAX_LIGHTS ,
148- VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT );
149150 invProjBuffer = new VulkanBuffer (device , GraphConstants .MAT4X4_SIZE , VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ,
150151 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT );
151- }
152152
153- public void recordCommandBuffer (Scene scene ) {
154- updateLights (scene .getAmbientLight (), scene .getLights (), scene .getCamera ().getViewMatrix ());
153+ lightsBuffers = new VulkanBuffer [numImages ];
154+ for (int i = 0 ; i < numImages ; i ++) {
155+ lightsBuffers [i ] = new VulkanBuffer (device ,
156+ GraphConstants .INT_LENGTH * 4 + GraphConstants .VEC4_SIZE * 2 * GraphConstants .MAX_LIGHTS ,
157+ VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT );
158+ }
159+ }
155160
161+ public void preRecordCommandBuffer (int idx ) {
156162 try (MemoryStack stack = MemoryStack .stackPush ()) {
157163 VkExtent2D swapChainExtent = swapChain .getSwapChainExtent ();
158164 int width = swapChainExtent .width ();
159165 int height = swapChainExtent .height ();
160- int idx = swapChain .getCurrentFrame ();
161166
162167 FrameBuffer frameBuffer = lightingFrameBuffer .getFrameBuffers ()[idx ];
163-
164- Fence fence = fences [idx ];
165168 CommandBuffer commandBuffer = commandBuffers [idx ];
166169
167- fence .fenceWait ();
168- fence .reset ();
169-
170170 commandBuffer .reset ();
171171 VkClearValue .Buffer clearValues = VkClearValue .callocStack (1 , stack );
172172 clearValues .apply (0 , v -> v .color ().float32 (0 , 0.5f ).float32 (1 , 0.7f ).float32 (2 , 0.9f ).float32 (3 , 1 ));
@@ -209,7 +209,7 @@ public void recordCommandBuffer(Scene scene) {
209209
210210 LongBuffer descriptorSets = stack .mallocLong (3 )
211211 .put (0 , attachmentsDescriptorSet .getVkDescriptorSet ())
212- .put (1 , lightsDescriptorSet .getVkDescriptorSet ())
212+ .put (1 , lightsDescriptorSets [ idx ] .getVkDescriptorSet ())
213213 .put (2 , invProjMatrixDescriptorSet .getVkDescriptorSet ());
214214 vkCmdBindDescriptorSets (cmdHandle , VK_PIPELINE_BIND_POINT_GRAPHICS ,
215215 pipeline .getVkPipelineLayout (), 0 , descriptorSets , null );
@@ -221,15 +221,30 @@ public void recordCommandBuffer(Scene scene) {
221221 }
222222 }
223223
224+ public void recordCommandBuffer (Scene scene ) {
225+ int idx = swapChain .getCurrentFrame ();
226+ Fence fence = fences [idx ];
227+
228+ fence .fenceWait ();
229+ fence .reset ();
230+
231+ updateLights (scene .getAmbientLight (), scene .getLights (), scene .getCamera ().getViewMatrix (),
232+ lightsBuffers [idx ]);
233+ }
234+
224235 public void resize (SwapChain swapChain , GeometryFrameBuffer geometryFrameBuffer , Scene scene ) {
225236 lightingFrameBuffer .cleanup ();
226237 pipeline .cleanup ();
227238 attachmentsDescriptorSet .update (geometryFrameBuffer );
228239
229240 lightingFrameBuffer = new LightingFrameBuffer (swapChain );
230241 createPipeline ();
242+ updateInvProjMatrix (scene );
231243
232- VulkanUtils .copyMatrixToBuffer (device , invProjBuffer , scene .getPerspective ().getPerspectiveMatrix ());
244+ int numImages = swapChain .getNumImages ();
245+ for (int i = 0 ; i < numImages ; i ++) {
246+ preRecordCommandBuffer (i );
247+ }
233248 }
234249
235250 public void submit (Queue queue ) {
@@ -245,7 +260,13 @@ public void submit(Queue queue) {
245260 }
246261 }
247262
248- private void updateLights (Vector4f ambientLight , Light [] lights , Matrix4f viewMatrix ) {
263+ private void updateInvProjMatrix (Scene scene ) {
264+ Matrix4f invProj = new Matrix4f (scene .getPerspective ().getPerspectiveMatrix ()).invert ();
265+ VulkanUtils .copyMatrixToBuffer (device , invProjBuffer , invProj );
266+ }
267+
268+ private void updateLights (Vector4f ambientLight , Light [] lights , Matrix4f viewMatrix ,
269+ VulkanBuffer lightsBuffer ) {
249270 VulkanUtils .copyVectortoBuffer (device , ambientLightBuffer , ambientLight );
250271
251272 try (MemoryStack stack = MemoryStack .stackPush ()) {
0 commit comments