Skip to content

Commit 9081116

Browse files
committed
Progress in lighting
1 parent cbc1319 commit 9081116

File tree

8 files changed

+58
-40
lines changed

8 files changed

+58
-40
lines changed

booksamples/chapter-10/resources/shaders/geometry_fragment.glsl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ void main()
3737
{
3838
outAlbedo = material.diffuseColor + texture(textSampler, inTextCoords);
3939

40+
// Hack to avoid transparent PBR artifacts
41+
if (outAlbedo.a < 0.5) {
42+
discard;
43+
}
44+
4045
mat3 TBN = mat3(inTangent, inBitangent, inNormal);
4146
vec3 newNormal = calcNormal(material.hasNormalMap, inNormal, inTextCoords, TBN);
4247
// Transform normals from [-1, 1] to [0, 1]
4348
outNormal = vec4(0.5 * newNormal + 0.5, 1.0);
4449

45-
float ao = 1.0f;
50+
float ao = 0.5f;
4651
float roughnessFactor = 0.0f;
4752
float metallicFactor = 0.0f;
4853
if (material.hasMetalRoughMap > 0) {
4954
vec4 metRoughValue = texture(metRoughSampler, inTextCoords);
50-
ao = metRoughValue.r;
5155
roughnessFactor = metRoughValue.g;
5256
metallicFactor = metRoughValue.b;
5357
} else {
Binary file not shown.

booksamples/chapter-10/resources/shaders/lighting_fragment.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#version 450
2+
precision highp float;
23

34
const int MAX_LIGHTS = 10;
45
const float PI = 3.14159265359;

booksamples/chapter-10/src/main/java/org/vulkanb/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void init(Window window, Scene scene, Render render) {
108108
Light light = new Light();
109109
light.getPosition().set(0, 40, 0, 1.0f);
110110
light.getColor().set(0.0f, 1.0f, 0.0f, 1.0f);
111-
//lights.add(light);
111+
lights.add(light);
112112

113113
Light[] lightArr = new Light[lights.size()];
114114
lightArr = lights.toArray(lightArr);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void recordCommandBuffers(List<VulkanMesh> meshes, Scene scene) {
205205
if (attachments[i].isDepthAttachment()) {
206206
clearValues.apply(i, v -> v.depthStencil().depth(1.0f));
207207
} else {
208-
clearValues.apply(i, v -> v.color().float32(0, 0.5f).float32(1, 0.7f).float32(2, 0.9f).float32(3, 1));
208+
clearValues.apply(i, v -> v.color().float32(0, 0.0f).float32(1, 0.0f).float32(2, 0.0f).float32(3, 1));
209209
}
210210
}
211211

booksamples/chapter-10/src/main/java/org/vulkanb/eng/graph/geometry/GeometryRenderPass.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.lwjgl.vulkan.VK11.*;
1010
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
1111

12-
// TODO: Samples
1312
public class GeometryRenderPass {
1413

1514
private static final int MAX_SAMPLES = 1;

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

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
import static org.lwjgl.vulkan.VK11.*;
1919
import 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?
2421
public 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()) {

booksamples/chapter-10/src/main/java/org/vulkanb/eng/scene/Perspective.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@
55

66
public class Perspective {
77

8-
private Matrix4f invPerspectiveMatrix;
98
private Matrix4f perspectiveMatrix;
109

1110
public Perspective() {
1211
perspectiveMatrix = new Matrix4f();
13-
invPerspectiveMatrix = new Matrix4f();
14-
}
15-
16-
public Matrix4f getInvPerspectiveMatrix() {
17-
return invPerspectiveMatrix;
1812
}
1913

2014
public Matrix4f getPerspectiveMatrix() {
@@ -26,6 +20,5 @@ public void resize(int width, int height) {
2620
perspectiveMatrix.identity();
2721
perspectiveMatrix.perspective(engProps.getFov(), (float) width / (float) height,
2822
engProps.getZNear(), engProps.getZFar(), true);
29-
perspectiveMatrix.invert(invPerspectiveMatrix);
3023
}
3124
}

0 commit comments

Comments
 (0)