Fixed alignment issue of point lights buffer
Instead of using an array of descriptors for storing the lights, now only one descriptor maps the entire array.
This commit is contained in:
parent
503ed33aec
commit
93e91d7902
2 changed files with 26 additions and 27 deletions
|
|
@ -2,7 +2,13 @@
|
|||
|
||||
#define MAX_POINT_LIGHTS 1024
|
||||
|
||||
|
||||
struct PointLight {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 data;
|
||||
};
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
|
|
@ -17,14 +23,9 @@ layout (binding = 2) uniform DirectionalLight {
|
|||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
} directional_light;
|
||||
layout (binding = 5) uniform PointLight {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 data;
|
||||
|
||||
} point_lights[MAX_POINT_LIGHTS];
|
||||
layout (binding = 5) uniform PointLights {
|
||||
PointLight point_lights[MAX_POINT_LIGHTS];
|
||||
} point_lights;
|
||||
|
||||
layout (binding = 3) uniform ViewUniform {
|
||||
vec3 pos;
|
||||
|
|
@ -49,19 +50,19 @@ vec3 calc_directional_light(vec3 normal, vec3 viewDir) {
|
|||
}
|
||||
|
||||
vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir) {
|
||||
float constant = point_lights[index].data[0];
|
||||
float linear = point_lights[index].data[1];
|
||||
float quadratic = point_lights[index].data[2];
|
||||
float constant = point_lights.point_lights[index].data[0];
|
||||
float linear = point_lights.point_lights[index].data[1];
|
||||
float quadratic = point_lights.point_lights[index].data[2];
|
||||
|
||||
vec3 lightDir = normalize(point_lights[index].position - fragPos);
|
||||
vec3 lightDir = normalize(point_lights.point_lights[index].position - fragPos);
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 2);
|
||||
float distance = length(point_lights[index].position - fragPos);
|
||||
float distance = length(point_lights.point_lights[index].position - fragPos);
|
||||
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
|
||||
vec3 ambient = point_lights[index].ambient * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 diffuse = point_lights[index].diffuse * diff * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 specular = point_lights[index].specular * spec * vec3(texture(diffuseSampler, TexCoords));
|
||||
vec3 ambient = point_lights.point_lights[index].ambient * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 diffuse = point_lights.point_lights[index].diffuse * diff * vec3(texture(textureSampler, TexCoords));
|
||||
vec3 specular = point_lights.point_lights[index].specular * spec * vec3(texture(diffuseSampler, TexCoords));
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
|
|
|||
|
|
@ -318,7 +318,7 @@ pub fn init(allocator: Allocator, device: vk.Device, swapchain: vk.Swapchain, re
|
|||
const point_lights_binding = c.VkDescriptorSetLayoutBinding{
|
||||
.binding = 5,
|
||||
.descriptorType = c.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
.descriptorCount = max_point_lights,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = c.VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
};
|
||||
|
||||
|
|
@ -409,7 +409,7 @@ pub fn init(allocator: Allocator, device: vk.Device, swapchain: vk.Swapchain, re
|
|||
|
||||
const size = c.VkDescriptorPoolSize{
|
||||
.type = c.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
.descriptorCount = 5 + max_point_lights,
|
||||
.descriptorCount = 6,
|
||||
};
|
||||
|
||||
const sampler_size = c.VkDescriptorPoolSize{
|
||||
|
|
@ -584,21 +584,19 @@ pub fn init(allocator: Allocator, device: vk.Device, swapchain: vk.Swapchain, re
|
|||
|
||||
const point_lights: []lights.PointLight = @as([*]lights.PointLight, @alignCast(@ptrCast(point_lights_data)))[0..max_point_lights];
|
||||
|
||||
var point_lights_descriptor_buffer_info: [max_point_lights]c.VkDescriptorBufferInfo = undefined;
|
||||
for (0..max_point_lights) |i| {
|
||||
point_lights_descriptor_buffer_info[i].buffer = point_lights_buffer.handle;
|
||||
point_lights_descriptor_buffer_info[i].offset = @sizeOf(lights.PointLight) * i;
|
||||
point_lights_descriptor_buffer_info[i].range = @sizeOf(lights.PointLight);
|
||||
}
|
||||
var point_lights_descriptor_buffer_info: c.VkDescriptorBufferInfo = undefined;
|
||||
point_lights_descriptor_buffer_info.buffer = point_lights_buffer.handle;
|
||||
point_lights_descriptor_buffer_info.offset = 0;
|
||||
point_lights_descriptor_buffer_info.range = @sizeOf(lights.PointLight) * max_point_lights;
|
||||
|
||||
const write_point_lights_descriptor_set = c.VkWriteDescriptorSet{
|
||||
.sType = c.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||
.dstSet = descriptor_set,
|
||||
.dstBinding = 5,
|
||||
.dstArrayElement = 0,
|
||||
.descriptorCount = max_point_lights,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = c.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
.pBufferInfo = @ptrCast(point_lights_descriptor_buffer_info[0..].ptr),
|
||||
.pBufferInfo = @ptrCast(&point_lights_descriptor_buffer_info),
|
||||
};
|
||||
|
||||
c.vkUpdateDescriptorSets(device.handle, 1, &write_point_lights_descriptor_set, 0, null);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue