diff --git a/Makefile b/Makefile index 6317453..8e37fc6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -I/usr/local/include -O0 -g -Wall -Wextra +CFLAGS = -fsanitize=address -I/usr/local/include -O0 -g -Wall -Wextra LDFLAGS = -L/usr/local/lib -lglfw -lvulkan -ldl -Xlinker -rpath -Xlinker /usr/local/lib CC = clang GDB = lldb diff --git a/shader_src/texture.frag b/shader_src/texture.frag index e4fa5f1..fc83647 100644 --- a/shader_src/texture.frag +++ b/shader_src/texture.frag @@ -1,6 +1,6 @@ #version 450 -layout(binding = 0) uniform sampler2D texSampler; +layout(set = 2, binding = 0) uniform sampler2D texSampler; layout(location = 0) in vec3 fragColor; layout(location = 1) in vec2 fragTex; @@ -8,5 +8,5 @@ layout(location = 1) in vec2 fragTex; layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(fragTex, 0.0f, 1.0); + outColor = texture(texSampler, fragTex); } diff --git a/src/main.c b/src/main.c index a6b48cb..a8f903a 100644 --- a/src/main.c +++ b/src/main.c @@ -333,10 +333,10 @@ const struct Vertex vertices[] = { }; const struct TextureVertex texture_vertices[] = { - {.pos = {-0.5f, -0.5f, 0.5f}, .color = {1.0f, 0.0f, 0.0f}, .tex = {0.0f, 1.0f}}, - {.pos = { 0.5f, -0.5f, 0.5f}, .color = {0.0f, 1.0f, 0.0f}, .tex = {1.0f, 0.0f}}, - {.pos = { 0.5f, 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .tex = {1.0f, 1.0f}}, - {.pos = {-0.5f, 0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .tex = {0.5f, 0.5f}}, + {.pos = {-0.5f, -0.5f, 0.5f}, .color = {1.0f, 0.0f, 0.0f}, .tex = {1.0f, 1.0f}}, + {.pos = { 0.5f, -0.5f, 0.5f}, .color = {0.0f, 1.0f, 0.0f}, .tex = {0.0f, 1.0f}}, + {.pos = { 0.5f, 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .tex = {0.0f, 0.0f}}, + {.pos = {-0.5f, 0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .tex = {1.0f, 0.0f}}, }; const uint16_t indices[] = { @@ -346,8 +346,7 @@ const uint16_t indices[] = { const char * validation_layers[] = { "VK_LAYER_KHRONOS_validation", //"VK_LAYER_LUNARG_api_dump", - //"VK_LAYER_KHRONOS_profiles", - //"VK_LAYER_KHRONOS_synchronization2", + "VK_LAYER_KHRONOS_synchronization2", "VK_LAYER_KHRONOS_shader_object", }; uint32_t validation_layer_count = sizeof(validation_layers) / sizeof(const char *); @@ -1424,7 +1423,6 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman return ret; } - VkImageView view; VkImageViewCreateInfo view_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, @@ -1461,7 +1459,7 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, - .anisotropyEnable = VK_TRUE, + .anisotropyEnable = VK_FALSE, .maxAnisotropy = 2.0f, .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, .unnormalizedCoordinates = VK_FALSE, @@ -1474,8 +1472,8 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman }; result = vkCreateSampler(device, &sampler_info, 0, &sampler); + deallocate_buffer(device, staging); if(result != VK_SUCCESS) { - deallocate_buffer(device, staging); deallocate_image(device, image); return ret; } @@ -1717,7 +1715,6 @@ void command_draw_object(Material material, Object object, uint32_t frame_num, V } VkDescriptorSet* descriptor_sets = maybe_descriptors.value; - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 2, 1, &descriptor_sets[frame_num], 0, 0); } @@ -1979,24 +1976,22 @@ Material create_material( VkDescriptorSetLayout material_set_layout; VkDescriptorSetLayout mesh_set_layout; - VkDescriptorSetLayout all_layouts[3] = {scene_ubo_layout, VK_NULL_HANDLE, VK_NULL_HANDLE}; - uint32_t num_layouts = 1; VkDescriptorPool material_descriptor_pool = VK_NULL_HANDLE; VkDescriptorSet* material_descriptors = 0; - if(pipeline_layout.material_bindings_count > 0) { - VkDescriptorSetLayoutCreateInfo layout_info = { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = pipeline_layout.material_bindings_count, - .pBindings = pipeline_layout.material_bindings, - }; + VkDescriptorSetLayoutCreateInfo material_layout_info = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = pipeline_layout.material_bindings_count, + .pBindings = pipeline_layout.material_bindings, + }; - VkResult result = vkCreateDescriptorSetLayout(device, &layout_info, 0, &material_set_layout); - if(result != VK_SUCCESS) { - return zero_material; - } + VkResult result = vkCreateDescriptorSetLayout(device, &material_layout_info, 0, &material_set_layout); + if(result != VK_SUCCESS) { + return zero_material; + } + if(pipeline_layout.material_bindings_count > 0) { material_descriptors = malloc(sizeof(VkDescriptorSet)*max_frames_in_flight); if(material_descriptors == 0) { return zero_material; @@ -2051,37 +2046,30 @@ Material create_material( vkDestroyDescriptorPool(device, material_descriptor_pool, 0); return zero_material; } - - all_layouts[num_layouts] = material_set_layout; - num_layouts += 1; } - if(pipeline_layout.mesh_bindings_count > 0) { - VkDescriptorSetLayoutCreateInfo layout_info = { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = pipeline_layout.mesh_bindings_count, - .pBindings = pipeline_layout.mesh_bindings, - }; - - VkResult result = vkCreateDescriptorSetLayout(device, &layout_info, 0, &mesh_set_layout); - if(result != VK_SUCCESS) { - return zero_material; - } + VkDescriptorSetLayoutCreateInfo mesh_layout_info = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = pipeline_layout.mesh_bindings_count, + .pBindings = pipeline_layout.mesh_bindings, + }; - all_layouts[num_layouts] = mesh_set_layout; - num_layouts += 1; + result = vkCreateDescriptorSetLayout(device, &mesh_layout_info, 0, &mesh_set_layout); + if(result != VK_SUCCESS) { + return zero_material; } + VkDescriptorSetLayout all_layouts[3] = {scene_ubo_layout, material_set_layout, mesh_set_layout}; VkPipelineLayout layout; - - VkPipelineLayoutCreateInfo layout_info = {}; - layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - layout_info.setLayoutCount = num_layouts; - layout_info.pSetLayouts = all_layouts; - layout_info.pushConstantRangeCount = 0; // TODO - layout_info.pPushConstantRanges = 0; // TODO - - VkResult result = vkCreatePipelineLayout(device, &layout_info, 0, &layout); + VkPipelineLayoutCreateInfo layout_info = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 3, + .pSetLayouts = all_layouts, + .pushConstantRangeCount = 0, + .pPushConstantRanges = 0, + }; + + result = vkCreatePipelineLayout(device, &layout_info, 0, &layout); if(result != VK_SUCCESS) { return zero_material; } @@ -2633,6 +2621,58 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { fprintf(stderr, "failed to add texture to renderable triangle object\n"); } + VkExtent2D texture_size = { + .width = 10, + .height = 10, + }; + struct __attribute__((__packed__)) texel { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + }; + + struct texel WHT = {255, 255, 255, 255}; + struct texel BLK = {0, 0, 0, 255}; + struct texel RED = {255, 0, 0, 255}; + struct texel GRN = {0, 255, 0, 255}; + struct texel BLU = {0, 0, 255, 255}; + + struct texel texture_data[100] = { + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK, + }; + + Texture test_texture = load_texture(context->physical_device, context->device, context->transfer_command_pool, context->queues.transfer, texture_size, 4, VK_FORMAT_R8G8B8A8_SRGB, texture_data); + + for(uint32_t i = 0; i < max_frames_in_flight; i++) { + VkDescriptorImageInfo image_info = { + .sampler = test_texture.sampler, + .imageView = test_texture.view, + .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + }; + + VkWriteDescriptorSet descriptor_write = {}; + descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptor_write.dstSet = TODO_sets[i]; + descriptor_write.dstBinding = 0; + descriptor_write.dstArrayElement = 0; + descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + descriptor_write.descriptorCount = 1; + descriptor_write.pBufferInfo = 0; + descriptor_write.pImageInfo = &image_info; + descriptor_write.pTexelBufferView = 0; + + vkUpdateDescriptorSets(device, 1, &descriptor_write, 0, 0); + } return context; }