spam const over local variables and function parameters

This commit is contained in:
jacekpoz 2024-08-13 11:57:33 +02:00
parent 43707826d9
commit ca212ac65e
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
13 changed files with 149 additions and 146 deletions

View file

@ -8,7 +8,7 @@ int main(void) {
return EXIT_FAILURE;
}
PtkColor red = { .r = 1.0f, .g = 0.0f, .b = 0.0f };
const PtkColor red = { .r = 1.0f, .g = 0.0f, .b = 0.0f };
return ptk_run(PTK_BOX(
ptk_rect((PtkPos){ .x = 250.0f, .y = 200.0f }, (PtkSize){ .w = 300.0f, .h = 200.0f }, red),

View file

@ -4,7 +4,7 @@
static PtkRect *r;
static void on_press(int button, int action, int mods) {
static void on_press(const int button, const int action, const int mods) {
(void)button; (void)action; (void)mods;
PTK_DEBUG("pressed!");
r->color.r = r->color.r + (1.0f / 255.0f);
@ -25,6 +25,6 @@ int main(void) {
);
return ptk_run(
ptk_button((PtkHandle)r, on_press)
ptk_clickable((PtkHandle)r, on_press)
);
}

View file

@ -14,7 +14,7 @@ typedef struct {
uint32_t patch;
} PtkVersion;
bool ptk_init(size_t width, size_t height, const char *title, PtkVersion application_version);
bool ptk_init(const size_t width, const size_t height, const char *title, const PtkVersion application_version);
typedef struct PtkComponent *PtkHandle;
#define PTK_NULL_HANDLE (void *)0
@ -56,20 +56,20 @@ PTK_COMPONENT_DEFINE(PtkEllipse,
PtkColor color;
);
typedef void (*MouseButtonCallback)(int button, int action, int mods);
typedef void (*MouseButtonCallback)(const int button, const int action, const int mods);
PTK_COMPONENT_DEFINE(PtkClickable,
PtkHandle hitbox;
MouseButtonCallback on_press;
);
PtkHandle ptk_box(size_t child_count, PtkHandle *children);
PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color);
PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color);
PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color);
PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color);
PtkHandle ptk_circle(PtkPos center, float radius, PtkColor color);
PtkHandle ptk_clickable(PtkHandle hitbox, MouseButtonCallback on_press);
PtkHandle ptk_box(const size_t child_count, PtkHandle *children);
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkColor color);
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkColor color);
PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkColor color);
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkColor color);
PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkColor color);
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press);
#define PTK_BOX(...) ptk_box(sizeof((PtkHandle []){ __VA_ARGS__ }) / sizeof(PtkHandle), (PtkHandle []) { __VA_ARGS__ })

View file

@ -14,15 +14,15 @@ typedef enum {
PTK_LOG_LEVEL_ALL = INT_MAX,
} PtkLogLevel;
void ptk_log_init(PtkLogLevel level);
void ptk_log_init(const PtkLogLevel level);
void ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, ...);
void ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, ...);
void ptk_err (const char *file, int line, const char *fmt, ...);
void ptk_warn (const char *file, int line, const char *fmt, ...);
void ptk_info (const char *file, int line, const char *fmt, ...);
void ptk_debug(const char *file, int line, const char *fmt, ...);
void ptk_trace(const char *file, int line, const char *fmt, ...);
void ptk_err (const char *file, const int line, const char *fmt, ...);
void ptk_warn (const char *file, const int line, const char *fmt, ...);
void ptk_info (const char *file, const int line, const char *fmt, ...);
void ptk_debug(const char *file, const int line, const char *fmt, ...);
void ptk_trace(const char *file, const int line, const char *fmt, ...);
#define PTK_LOG(level, ...) ptk_log(__FILE__, __LINE__, level, __VA_ARGS__)

View file

@ -54,7 +54,7 @@ PTK_OPTION(PtkLogLevel) get_log_level(void) {
return PTK_OPTION_NONE(PtkLogLevel);
}
size_t log_level_len = strlen(log_level);
const size_t log_level_len = strlen(log_level);
char *lowercase = malloc(log_level_len * sizeof(char));
@ -88,8 +88,8 @@ PTK_OPTION(PtkLogLevel) get_log_level(void) {
return ret;
}
bool ptk_init(size_t width, size_t height, const char *title, PtkVersion application_version) {
PTK_OPTION(PtkLogLevel) log_level = get_log_level();
bool ptk_init(const size_t width, const size_t height, const char *title, const PtkVersion application_version) {
const PTK_OPTION(PtkLogLevel) log_level = get_log_level();
if (log_level.exists) {
ptk_log_init(log_level.value);
}
@ -132,7 +132,7 @@ bool ptk_init(size_t width, size_t height, const char *title, PtkVersion applica
return true;
}
PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
PtkHandle ptk_box(const size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent));
*ret = (PtkComponent){
.type = PTK_COMPONENT_TYPE_BOX,
@ -143,7 +143,7 @@ PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
return (PtkHandle)ret;
}
PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) {
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkColor color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle));
*ret = (PtkTriangle){
.type = PTK_COMPONENT_TYPE_TRIANGLE,
@ -154,7 +154,7 @@ PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) {
return (PtkHandle)ret;
}
PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) {
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkColor color) {
PtkRect *ret = malloc(sizeof(PtkRect));
*ret = (PtkRect){
.type = PTK_COMPONENT_TYPE_RECT,
@ -166,11 +166,11 @@ PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) {
return (PtkHandle)ret;
}
PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color) {
PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkColor color) {
return ptk_rect(top_left, (PtkSize){ .w = size, .h = size }, color);
}
PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) {
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkColor color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse));
*ret = (PtkEllipse){
.type = PTK_COMPONENT_TYPE_ELLIPSE,
@ -182,11 +182,11 @@ PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) {
return (PtkHandle)ret;
}
PtkHandle ptk_circle(PtkPos center, float radius, PtkColor color) {
PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkColor color) {
return ptk_ellipse(center, (PtkSize){ .w = radius, .h = radius }, color);
}
PtkHandle ptk_clickable(PtkHandle hitbox, MouseButtonCallback on_press) {
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) {
PtkClickable *ret = malloc(sizeof(PtkClickable));
*ret = (PtkClickable){
.type = PTK_COMPONENT_TYPE_CLICKABLE,

View file

@ -8,7 +8,7 @@
#define BG(r, g, b) "\033[48;2;" #r ";" #g ";" #b "m"
#define RESET "\033[0m"
static const char *log_level_to_str(PtkLogLevel level) {
static const char *log_level_to_str(const PtkLogLevel level) {
const char *ret;
switch (level) {
case PTK_LOG_LEVEL_ERR: ret = FG(235, 28, 35) " ERR" RESET; break;
@ -27,11 +27,11 @@ static PtkLogLevel log_level = PTK_LOG_LEVEL_DEBUG;
static PtkLogLevel log_level = PTK_LOG_LEVEL_WARN;
#endif
void ptk_log_init(PtkLogLevel level) {
void ptk_log_init(const PtkLogLevel level) {
log_level = level;
}
void _ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, va_list args) {
void _ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, va_list args) {
if (log_level < level) { return; }
fprintf(stderr, "[%s | %s:%d] ", log_level_to_str(level), file, line);
@ -41,15 +41,15 @@ void _ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, va
fprintf(stderr, "\n");
}
void ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, ...) {
void ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_ptk_log(file, line, level, fmt, args);
va_end(args);
}
void ptk_err (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_ERR, fmt, args); va_end(args); }
void ptk_warn (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_WARN, fmt, args); va_end(args); }
void ptk_info (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_INFO, fmt, args); va_end(args); }
void ptk_debug(const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_DEBUG, fmt, args); va_end(args); }
void ptk_trace(const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_TRACE, fmt, args); va_end(args); }
void ptk_err (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_ERR, fmt, args); va_end(args); }
void ptk_warn (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_WARN, fmt, args); va_end(args); }
void ptk_info (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_INFO, fmt, args); va_end(args); }
void ptk_debug(const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_DEBUG, fmt, args); va_end(args); }
void ptk_trace(const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_TRACE, fmt, args); va_end(args); }

View file

@ -22,7 +22,7 @@ void vk_components_init(void) {
g_vertices = PTK_LIST_NEW(Vertex, 1);
}
void triangle(PtkTriangle *triangle, size_t uvs_offset) {
void triangle(const PtkTriangle *triangle, const size_t uvs_offset) {
for (size_t i = 0; i < 3; ++i) {
Vertex v = {
.pos = triangle->vertices[i],
@ -35,11 +35,11 @@ void triangle(PtkTriangle *triangle, size_t uvs_offset) {
}
}
void rect(PtkRect *rect) {
float x = rect->top_left.x;
float y = rect->top_left.y;
float w = rect->size.w;
float h = rect->size.h;
void rect(const PtkRect *rect) {
const float x = rect->top_left.x;
const float y = rect->top_left.y;
const float w = rect->size.w;
const float h = rect->size.h;
PtkPos t1_positions[3];
// top left
@ -67,13 +67,13 @@ void rect(PtkRect *rect) {
vk_transfer_vertex_data();
}
void ellipse(PtkEllipse *ellipse) {
PtkPos top_left = {
void ellipse(const PtkEllipse *ellipse) {
const PtkPos top_left = {
.x = ellipse->center.x - ellipse->radii.w,
.y = ellipse->center.y - ellipse->radii.h,
};
PtkSize size = {
const PtkSize size = {
.w = ellipse->radii.w * 2.0f,
.h = ellipse->radii.h * 2.0f,
};
@ -108,19 +108,19 @@ void vk_component(PtkHandle component) {
vk_transfer_vertex_data();
}
inline bool triangle_intersects(PtkTriangle t, PtkPos p) {
PtkPos p0 = t.vertices[0];
PtkPos p1 = t.vertices[1];
PtkPos p2 = t.vertices[2];
float a = 1.0f/2.0f * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
int sign = a < 0.0f ? -1 : 1;
float s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
float r = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
inline bool triangle_intersects(const PtkTriangle t, const PtkPos p) {
const PtkPos p0 = t.vertices[0];
const PtkPos p1 = t.vertices[1];
const PtkPos p2 = t.vertices[2];
const float a = 1.0f/2.0f * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
const int sign = a < 0.0f ? -1 : 1;
const float s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
const float r = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
return s > 0 && r > 0 && (s + r) < 2 * a * sign;
}
inline bool rect_intersects(PtkRect r, PtkPos p) {
inline bool rect_intersects(const PtkRect r, const PtkPos p) {
bool intersects_x = r.top_left.x <= p.x
&& r.top_left.x + r.top_left.w >= p.x;
@ -130,20 +130,19 @@ inline bool rect_intersects(PtkRect r, PtkPos p) {
return intersects_x && intersects_y;
}
inline bool ellipse_intersects(PtkEllipse e, PtkPos p) {
float x = p.x - e.center.x;
float rx = e.radii.x;
float y = p.y - e.center.y;
float ry = e.radii.y;
inline bool ellipse_intersects(const PtkEllipse e, const PtkPos p) {
const float x = p.x - e.center.x;
const float rx = e.radii.x;
const float y = p.y - e.center.y;
const float ry = e.radii.y;
return ((x * x) / (rx * rx)) + ((y * y) / (ry * ry)) <= 1.0f;
}
bool intersects(PtkHandle component, PtkPos point) {
bool intersects(const PtkHandle component, const PtkPos point) {
switch (component->type) {
case PTK_COMPONENT_TYPE_BOX: {
PtkComponent *box = (PtkComponent *)component;
for (size_t i = 0; i < box->children.size; ++i) {
if (intersects(box->children.data[i], point)) {
for (size_t i = 0; i < component->children.size; ++i) {
if (intersects(component->children.data[i], point)) {
return true;
}
}
@ -161,14 +160,14 @@ bool intersects(PtkHandle component, PtkPos point) {
}
}
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mods) {
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mods) {
for (size_t i = 0; i < m_components.size; ++i) {
PtkHandle current = m_components.data[i];
const PtkHandle current = m_components.data[i];
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
continue;
}
PtkClickable *c = (PtkClickable *)current;
const PtkClickable *c = (PtkClickable *)current;
if (intersects((PtkHandle)c, cursor_pos)) {
c->on_press(button, action, mods);

View file

@ -21,7 +21,7 @@ void vk_components_init(void);
void vk_component(PtkHandle component);
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mode);
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mode);
void vk_components_cleanup(void);

View file

@ -16,7 +16,7 @@ bool vk_draw_frame(void) {
vkWaitForFences(g_dev, 1, &g_in_flight_fences.data[g_current_frame], VK_TRUE, UINT64_MAX);
uint32_t image_index;
VkResult res = vkAcquireNextImageKHR(
const VkResult acquire_next_image_result = vkAcquireNextImageKHR(
g_dev,
g_swapchain,
UINT64_MAX,
@ -25,13 +25,13 @@ bool vk_draw_frame(void) {
&image_index
);
if (res == VK_ERROR_OUT_OF_DATE_KHR) {
if (acquire_next_image_result == VK_ERROR_OUT_OF_DATE_KHR) {
if (!vk_recreate_swapchain()) {
return false;
}
return true;
} else if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR) {
PTK_ERR("%s", vk_result_string(res));
} else if (acquire_next_image_result != VK_SUCCESS && acquire_next_image_result != VK_SUBOPTIMAL_KHR) {
PTK_ERR("%s", vk_result_string(acquire_next_image_result));
return false;
}
@ -45,7 +45,7 @@ bool vk_draw_frame(void) {
return false;
}
VkSemaphore signal_semaphores[] = {g_render_finished_semaphores.data[g_current_frame]};
const VkSemaphore signal_semaphores[] = {g_render_finished_semaphores.data[g_current_frame]};
VK_TRY(false,
vkQueueSubmit(
@ -66,7 +66,7 @@ bool vk_draw_frame(void) {
)
);
res = vkQueuePresentKHR(g_present_queue, &(VkPresentInfoKHR){
const VkResult queue_present_result = vkQueuePresentKHR(g_present_queue, &(VkPresentInfoKHR){
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = NULL,
.waitSemaphoreCount = 1,
@ -77,14 +77,18 @@ bool vk_draw_frame(void) {
.pResults = NULL,
});
if (res == VK_ERROR_OUT_OF_DATE_KHR || res == VK_SUBOPTIMAL_KHR || g_framebuffer_resized) {
if (
queue_present_result == VK_ERROR_OUT_OF_DATE_KHR ||
queue_present_result == VK_SUBOPTIMAL_KHR ||
g_framebuffer_resized
) {
g_framebuffer_resized = false;
if (!vk_recreate_swapchain()) {
return false;
}
PTK_TRACE("recreated swapchain");
} else if (res != VK_SUCCESS) {
PTK_ERR("%s", vk_result_string(res));
} else if (queue_present_result != VK_SUCCESS) {
PTK_ERR("%s", vk_result_string(queue_present_result));
return false;
}

View file

@ -145,7 +145,7 @@ static const char * const g_validation_layers[] = {
"VK_LAYER_KHRONOS_validation"
};
bool check_validation_layers(const char * const *validation_layers, uint32_t validation_layer_count) {
bool check_validation_layers(const char * const *validation_layers, const uint32_t validation_layer_count) {
PTK_LIST(VkLayerProperties) available_layers;
vkEnumerateInstanceLayerProperties(&available_layers.allocated, NULL);
@ -182,11 +182,11 @@ bool create_vk_instance(const char *title, const PtkVersion version) {
PTK_ERR("couldn't find requested validation layer");
return false;
}
uint32_t enabledLayerCount = g_validation_layer_count;
const char * const *ppEnabledLayerNames = g_validation_layers;
const uint32_t enabled_layer_count = g_validation_layer_count;
const char * const *enabled_layer_names = g_validation_layers;
#else
uint32_t enabledLayerCount = 0;
const char * const *ppEnabledLayerNames = NULL;
const uint32_t enabled_layer_count = 0;
const char * const *enabled_layer_names = NULL;
#endif
uint32_t extension_count = 0;
@ -207,8 +207,8 @@ bool create_vk_instance(const char *title, const PtkVersion version) {
.engineVersion = VK_MAKE_API_VERSION(0, PTK_VERSION_MAJOR, PTK_VERSION_MINOR, PTK_VERSION_PATCH),
.apiVersion = VK_API_VERSION_1_3,
},
.enabledLayerCount = enabledLayerCount,
.ppEnabledLayerNames = ppEnabledLayerNames,
.enabledLayerCount = enabled_layer_count,
.ppEnabledLayerNames = enabled_layer_names,
.enabledExtensionCount = extension_count,
.ppEnabledExtensionNames = extension_names,
},
@ -294,7 +294,7 @@ bool select_physical_dev(void) {
VkSurfaceFormatKHR select_swap_surface_format(const PTK_LIST(VkSurfaceFormatKHR) available_formats) {
for (size_t i = 0; i < available_formats.size; ++i) {
VkSurfaceFormatKHR current_format = available_formats.data[i];
const VkSurfaceFormatKHR current_format = available_formats.data[i];
if (current_format.format == VK_FORMAT_B8G8R8A8_SRGB && current_format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return current_format;
}
@ -305,7 +305,7 @@ VkSurfaceFormatKHR select_swap_surface_format(const PTK_LIST(VkSurfaceFormatKHR)
VkPresentModeKHR select_swap_present_mode(const PTK_LIST(VkPresentModeKHR) available_present_modes) {
for (size_t i = 0; i < available_present_modes.size; ++i) {
VkPresentModeKHR current_present_mode = available_present_modes.data[i];
const VkPresentModeKHR current_present_mode = available_present_modes.data[i];
if (current_present_mode == VK_PRESENT_MODE_MAILBOX_KHR) {
return current_present_mode;
}
@ -314,7 +314,7 @@ VkPresentModeKHR select_swap_present_mode(const PTK_LIST(VkPresentModeKHR) avail
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D select_swap_extent(VkSurfaceCapabilitiesKHR capabilities) {
VkExtent2D select_swap_extent(const VkSurfaceCapabilitiesKHR capabilities) {
if (capabilities.currentExtent.width != UINT32_MAX) {
return capabilities.currentExtent;
}
@ -343,7 +343,7 @@ VkExtent2D select_swap_extent(VkSurfaceCapabilitiesKHR capabilities) {
return actual_extent;
}
SwapchainSupportInfo query_swapchain_support(VkPhysicalDevice dev) {
SwapchainSupportInfo query_swapchain_support(const VkPhysicalDevice dev) {
SwapchainSupportInfo info;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev, m_surface, &info.capabilities);
@ -378,7 +378,7 @@ SwapchainSupportInfo query_swapchain_support(VkPhysicalDevice dev) {
return info;
}
bool are_extensions_supported(VkPhysicalDevice dev) {
bool are_extensions_supported(const VkPhysicalDevice dev) {
PTK_LIST(VkExtensionProperties) available_extensions;
vkEnumerateDeviceExtensionProperties(dev, NULL, &available_extensions.allocated, NULL);
@ -400,7 +400,7 @@ bool are_extensions_supported(VkPhysicalDevice dev) {
return supported_extensions == m_device_extension_count;
}
bool is_device_suitable(VkPhysicalDevice dev) {
bool is_device_suitable(const VkPhysicalDevice dev) {
PTK_LIST(VkQueueFamilyProperties) queue_families;
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families.allocated, NULL);
@ -422,13 +422,13 @@ bool is_device_suitable(VkPhysicalDevice dev) {
}
}
bool indices_found = m_queue_family_indices.graphics.exists
const bool indices_found = m_queue_family_indices.graphics.exists
&& m_queue_family_indices.present.exists;
bool extensions_supported = are_extensions_supported(dev);
const bool extensions_supported = are_extensions_supported(dev);
bool swapchain_adequate = false;
if (extensions_supported) {
SwapchainSupportInfo swapchain_support = query_swapchain_support(dev);
const SwapchainSupportInfo swapchain_support = query_swapchain_support(dev);
swapchain_adequate = swapchain_support.formats.size != 0
&& swapchain_support.present_modes.size != 0;
PTK_LIST_FREE(swapchain_support.formats);
@ -449,7 +449,7 @@ bool create_logical_dev(void) {
VkDeviceQueueCreateInfo queue_create_infos[m_queue_family_count];
for (size_t i = 0; i < m_queue_family_count; ++i) {
PTK_OPTION(uint32_t) index = *(((PTK_OPTION(uint32_t) *)&m_queue_family_indices) + i);
const PTK_OPTION(uint32_t) index = *(((PTK_OPTION(uint32_t) *)&m_queue_family_indices) + i);
queue_create_infos[i] = (VkDeviceQueueCreateInfo){
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
@ -462,11 +462,11 @@ bool create_logical_dev(void) {
}
#ifdef DEBUG
uint32_t enabledLayerCount = g_validation_layer_count;
const char * const *ppEnabledLayerNames = g_validation_layers;
const uint32_t enabled_layer_count = g_validation_layer_count;
const char * const *enabled_layer_names = g_validation_layers;
#else
uint32_t enabledLayerCount = 0;
const char * const *ppEnabledLayerNames = NULL;
const uint32_t enabled_layer_count = 0;
const char * const *enabled_layer_names = NULL;
#endif
VK_TRY(false,
@ -478,8 +478,8 @@ bool create_logical_dev(void) {
.flags = 0,
.queueCreateInfoCount = m_queue_family_count,
.pQueueCreateInfos = queue_create_infos,
.enabledLayerCount = enabledLayerCount,
.ppEnabledLayerNames = ppEnabledLayerNames,
.enabledLayerCount = enabled_layer_count,
.ppEnabledLayerNames = enabled_layer_names,
.enabledExtensionCount = m_device_extension_count,
.ppEnabledExtensionNames = m_device_extensions,
.pEnabledFeatures = &(VkPhysicalDeviceFeatures){0},
@ -496,11 +496,11 @@ bool create_logical_dev(void) {
}
bool create_swapchain(void) {
SwapchainSupportInfo swapchain_support = query_swapchain_support(m_physical_dev);
const SwapchainSupportInfo swapchain_support = query_swapchain_support(m_physical_dev);
VkSurfaceFormatKHR surface_format = select_swap_surface_format(swapchain_support.formats);
VkPresentModeKHR present_mode = select_swap_present_mode(swapchain_support.present_modes);
VkExtent2D extent = select_swap_extent(swapchain_support.capabilities);
const VkSurfaceFormatKHR surface_format = select_swap_surface_format(swapchain_support.formats);
const VkPresentModeKHR present_mode = select_swap_present_mode(swapchain_support.present_modes);
const VkExtent2D extent = select_swap_extent(swapchain_support.capabilities);
uint32_t image_count = swapchain_support.capabilities.minImageCount + 1;
@ -508,7 +508,7 @@ bool create_swapchain(void) {
image_count = swapchain_support.capabilities.maxImageCount;
}
bool queue_families_differ = m_queue_family_indices.graphics.value != m_queue_family_indices.present.value;
const bool queue_families_differ = m_queue_family_indices.graphics.value != m_queue_family_indices.present.value;
VK_TRY(false,
vkCreateSwapchainKHR(
@ -622,7 +622,7 @@ PTK_STRING read_spv(const char *filename) {
buffer = PTK_STRING_NEW(buffer.allocated);
size_t items_read = fread(buffer.data, sizeof(char), buffer.allocated, file);
const size_t items_read = fread(buffer.data, sizeof(char), buffer.allocated, file);
if (items_read != buffer.allocated) {
if (feof(file)) {
@ -640,7 +640,7 @@ PTK_STRING read_spv(const char *filename) {
return buffer;
}
PTK_OPTION(VkShaderModule) create_shader_module(PTK_STRING code) {
PTK_OPTION(VkShaderModule) create_shader_module(const PTK_STRING code) {
VkShaderModule shader_module;
VK_TRY(PTK_OPTION_NONE(VkShaderModule),
@ -742,24 +742,24 @@ bool create_descriptor_set_layout(void) {
}
bool create_graphics_pipeline(void) {
PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv");
PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv");
const PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv");
const PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv");
PTK_OPTION(VkShaderModule) vert_shader_module = create_shader_module(vert_shader_code);
const PTK_OPTION(VkShaderModule) vert_shader_module = create_shader_module(vert_shader_code);
if (!vert_shader_module.exists) {
PTK_ERR("failed creating vert shader module");
return false;
}
PTK_OPTION(VkShaderModule) frag_shader_module = create_shader_module(frag_shader_code);
const PTK_OPTION(VkShaderModule) frag_shader_module = create_shader_module(frag_shader_code);
if (!frag_shader_module.exists) {
PTK_ERR("failed creating frag shader module");
return false;
}
VkSpecializationInfo spec_info = {
const VkSpecializationInfo spec_info = {
.mapEntryCount = 1,
.pMapEntries = &(VkSpecializationMapEntry){
.constantID = 0,
@ -770,7 +770,7 @@ bool create_graphics_pipeline(void) {
.pData = &(int){PTK_COMPONENT_TYPE_ELLIPSE},
};
VkPipelineShaderStageCreateInfo shader_stages[] = {
const VkPipelineShaderStageCreateInfo shader_stages[] = {
(VkPipelineShaderStageCreateInfo){
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = NULL,
@ -791,7 +791,7 @@ bool create_graphics_pipeline(void) {
},
};
VkPipelineVertexInputStateCreateInfo vertex_input_info = {
const VkPipelineVertexInputStateCreateInfo vertex_input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -801,7 +801,7 @@ bool create_graphics_pipeline(void) {
.pVertexAttributeDescriptions = m_vertex_attribute_descriptions.data,
};
VkPipelineDynamicStateCreateInfo dynamic_state = {
const VkPipelineDynamicStateCreateInfo dynamic_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -809,7 +809,7 @@ bool create_graphics_pipeline(void) {
.pDynamicStates = NULL,
};
VkPipelineInputAssemblyStateCreateInfo input_assembly = {
const VkPipelineInputAssemblyStateCreateInfo input_assembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -817,7 +817,7 @@ bool create_graphics_pipeline(void) {
.primitiveRestartEnable = VK_FALSE,
};
VkPipelineViewportStateCreateInfo viewport_state = {
const VkPipelineViewportStateCreateInfo viewport_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -840,7 +840,7 @@ bool create_graphics_pipeline(void) {
},
};
VkPipelineRasterizationStateCreateInfo rasterizer = {
const VkPipelineRasterizationStateCreateInfo rasterizer = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -856,7 +856,7 @@ bool create_graphics_pipeline(void) {
.lineWidth = 1.0f,
};
VkPipelineMultisampleStateCreateInfo multisampling = {
const VkPipelineMultisampleStateCreateInfo multisampling = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -868,7 +868,7 @@ bool create_graphics_pipeline(void) {
.alphaToOneEnable = VK_FALSE,
};
VkPipelineColorBlendAttachmentState color_blend_attachment = {
const VkPipelineColorBlendAttachmentState color_blend_attachment = {
.blendEnable = VK_TRUE,
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
@ -879,7 +879,7 @@ bool create_graphics_pipeline(void) {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo color_blending = {
const VkPipelineColorBlendStateCreateInfo color_blending = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -993,7 +993,7 @@ bool create_command_pool(void) {
return true;
}
PTK_OPTION(uint32_t) find_memory_type(uint32_t type_filter, VkMemoryPropertyFlags props) {
PTK_OPTION(uint32_t) find_memory_type(const uint32_t type_filter, const VkMemoryPropertyFlags props) {
VkPhysicalDeviceMemoryProperties mem_props;
vkGetPhysicalDeviceMemoryProperties(m_physical_dev, &mem_props);
@ -1006,7 +1006,7 @@ PTK_OPTION(uint32_t) find_memory_type(uint32_t type_filter, VkMemoryPropertyFlag
return PTK_OPTION_NONE(uint32_t);
}
bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags props, VkBuffer *buffer, VkDeviceMemory *buffer_memory) {
bool create_buffer(const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props, VkBuffer *buffer, VkDeviceMemory *buffer_memory) {
VK_TRY(false,
vkCreateBuffer(
g_dev,
@ -1028,7 +1028,7 @@ bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryProperty
VkMemoryRequirements mem_reqs;
vkGetBufferMemoryRequirements(g_dev, *buffer, &mem_reqs);
PTK_OPTION(uint32_t) memory_type = find_memory_type(mem_reqs.memoryTypeBits, props);
const PTK_OPTION(uint32_t) memory_type = find_memory_type(mem_reqs.memoryTypeBits, props);
if (!memory_type.exists) {
PTK_ERR("failed to find suitable memory type");
@ -1054,7 +1054,7 @@ bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryProperty
return true;
}
bool copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size) {
bool copy_buffer(const VkBuffer src, const VkBuffer dst, const VkDeviceSize size) {
VkCommandBuffer command_buffer;
VK_TRY(false,
@ -1118,12 +1118,12 @@ bool vk_transfer_vertex_data(void) {
PTK_DEBUG("vertices updated!");
for (size_t i = 0; i < g_vertices.size; ++i) {
PTK_DEBUG("v[%d]:", i);
Vertex current = g_vertices.data[i];
const Vertex current = g_vertices.data[i];
PTK_DEBUG(" pos: (%f, %f)", current.pos.x, current.pos.y);
PTK_DEBUG(" color: (%f, %f, %f)", current.color.r, current.color.g, current.color.b);
}
PTK_DEBUG("transferring vertices to gpu…");
VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
VkBuffer staging_buffer;
VkDeviceMemory staging_buffer_memory;
@ -1157,8 +1157,8 @@ bool vk_transfer_vertex_data(void) {
}
bool create_vertex_buffer(void) {
// VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
VkDeviceSize buffer_size = 65536;
// const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
const VkDeviceSize buffer_size = 65536;
if (
!create_buffer(
buffer_size,
@ -1176,7 +1176,7 @@ bool create_vertex_buffer(void) {
}
bool create_uniform_buffers(void) {
VkDeviceSize buffer_size = sizeof(UniformBufferObject);
const VkDeviceSize buffer_size = sizeof(UniformBufferObject);
m_uniform_buffers = PTK_LIST_NEW(VkBuffer, g_max_frames_in_flight);
m_uniform_buffers_memory = PTK_LIST_NEW(VkDeviceMemory, g_max_frames_in_flight);
@ -1298,7 +1298,7 @@ bool allocate_command_buffers(void) {
return true;
}
bool vk_record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) {
bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32_t image_index) {
VK_TRY(false,
vkBeginCommandBuffer(
command_buffer,
@ -1360,7 +1360,7 @@ bool create_sync_objects(void) {
g_render_finished_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight);
g_in_flight_fences = PTK_LIST_NEW(VkFence, g_max_frames_in_flight);
VkSemaphoreCreateInfo semaphore_info = {
const VkSemaphoreCreateInfo semaphore_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -1438,7 +1438,7 @@ bool vk_recreate_swapchain(void) {
return true;
}
bool vk_update_uniform_buffer(size_t current_frame) {
bool vk_update_uniform_buffer(const size_t current_frame) {
m_uniform_buffer_object.window_size.w = m_swapchain_extent.width;
m_uniform_buffer_object.window_size.h = m_swapchain_extent.height;
@ -1447,7 +1447,7 @@ bool vk_update_uniform_buffer(size_t current_frame) {
return true;
}
bool vk_init(GLFWwindow *window, size_t width, size_t height, const char *title, const PtkVersion version) {
bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const char *title, const PtkVersion version) {
m_window = window;
m_uniform_buffer_object.initial_window_size.w = width;
m_uniform_buffer_object.initial_window_size.h = height;

View file

@ -28,15 +28,15 @@ extern PTK_LIST(VkSemaphore) g_image_available_semaphores;
extern PTK_LIST(VkSemaphore) g_render_finished_semaphores;
extern PTK_LIST(VkFence) g_in_flight_fences;
bool vk_init(GLFWwindow *window, size_t width, size_t height, const char *title, const PtkVersion version);
bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const char *title, const PtkVersion version);
bool vk_transfer_vertex_data(void);
bool vk_record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index);
bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32_t image_index);
bool vk_recreate_swapchain(void);
bool vk_update_uniform_buffer(size_t current_frame);
bool vk_update_uniform_buffer(const size_t current_frame);
void vk_cleanup(void);

View file

@ -9,7 +9,7 @@
const char *vk_result_string(VkResult res);
#define VK_TRY(return_value, ...) do {\
VkResult res = __VA_ARGS__;\
const VkResult res = __VA_ARGS__;\
if (res != VK_SUCCESS) {\
PTK_ERR("%s", vk_result_string(res));\
return return_value;\

View file

@ -7,7 +7,7 @@ PTK_LIST_DEFINE(uint32_t);
TEST_START()
TEST("create list", {
size_t size = 5;
const size_t size = 5;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
TEST_ASSERT(list.allocated == size, "incorrect list allocation");
@ -15,7 +15,7 @@ TEST_START()
});
TEST("add elements without growing", {
size_t size = 5;
const size_t size = 5;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -29,7 +29,7 @@ TEST_START()
});
TEST("add elements and grow", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -45,7 +45,7 @@ TEST_START()
});
TEST("add multiple elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD_ALL(uint32_t, list, { 21, 37, 2137, 31, 27, 7312 });
@ -56,7 +56,7 @@ TEST_START()
});
TEST("remove elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -73,7 +73,7 @@ TEST_START()
});
TEST("remove multiple elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -90,7 +90,7 @@ TEST_START()
});
TEST("remove elements at index", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});