Skip to content

Commit

Permalink
update readme, gitignore, testing fractal algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
esettes committed Dec 7, 2022
1 parent d88fefc commit 65e7171
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.vscode
*.o
*.o
gfxrecon_*
imgtest*
*.raw
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ obj:

clean:
@rm -f $(OBJ_DIR)*.o
@rm -f *.raw
@echo "$(BWHITE)Clean objs $(G_OK)"

fclean: clean
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Vulkan compute shader

### Initialization of a Vulkan program to execute a compute shader.

Binary file modified comp_shader
Binary file not shown.
Binary file added img/fractal_cpu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/fractal_gpu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions inc/instance.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ Author: Roxana Stancu (esettes)
* @ Created: 2022/12/03 16:36
* @ Modified: 2022/12/06 12:13
* @ Modified: 2022/12/07 19:13
*/

# ifndef INSTANCE_H
Expand All @@ -10,7 +10,7 @@
#include <vulkan/vulkan.h>

#ifndef DEBUG
#define DEBUG 1
#define DEBUG 0
#endif

extern VkInstance g_instance;
Expand Down
13 changes: 7 additions & 6 deletions src/compute.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ Author: Roxana Stancu (esettes)
* @ Created: 2022/12/02 23:41
* @ Modified: 2022/12/06 22:49
* @ Modified: 2022/12/07 23:20
*
* @ Description: Alloc command buffer in command pool and submit queue. Create
* descriptor set for buffers.
Expand Down Expand Up @@ -65,7 +65,8 @@ void create_command_buffer(void)
the first. */
vkCmdBindDescriptorSets(g_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
g_pipeline_layout, 0, 1, &g_descriptor_set, 0, NULL);
vkCmdDispatch(g_command_buffer, 6, 1, 1);
/** Relationated with the shader dimensions */
vkCmdDispatch(g_command_buffer, 1000, 1, 1);
/* End recording */
if (vkEndCommandBuffer(g_command_buffer) != VK_SUCCESS)
{
Expand Down Expand Up @@ -109,10 +110,10 @@ int compute(void)
{
printf("[ERROR] Waiting for fence failed.\n");
}
else
{
printf("[INFO] Waiting for fence success.\n");
}
// else
// {
// printf("[INFO] Waiting for fence success.\n");
// }
vkDestroyFence(g_logical_device, fence, NULL);
return (0);
}
Expand Down
5 changes: 1 addition & 4 deletions src/device.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ Author: Roxana Stancu (esettes)
* @ Created: 2022/12/03 02:48
* @ Modified: 2022/12/06 20:45
* @ Modified: 2022/12/07 22:50
*
* @ Description: Open a device, create logical device and allocate execution
* queues from it.
Expand Down Expand Up @@ -49,9 +49,6 @@ void create_device_and_compute_queue(void)
{
g_comp_queue_family_index++;
}
/**
* If the suitable queue family is not found, print error.
*/
if (g_comp_queue_family_index == f_count)
{
printf("[ERROR] Compute queue not found\n");
Expand Down
7 changes: 3 additions & 4 deletions src/instance.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ Author: Roxana Stancu (esettes)
* @ Created: 2022/12/03 16:29
* @ Modified: 2022/12/06 22:13
* @ Modified: 2022/12/07 19:13
*
* @ Description: Obtain the physical device.
*
Expand Down Expand Up @@ -48,11 +48,10 @@ void create_instance(void)
memset(&instance_info, 0, sizeof(instance_info));
instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
//#if TEMP_DISABLED
/*until I figure out how to do it
#if DEBUG == 1*/
#if DEBUG == 1

check_validation_layer_support();
/*#endif*/
#endif
instance_info.ppEnabledLayerNames = layers;
instance_info.enabledLayerCount = 1;
//#endif
Expand Down
77 changes: 67 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ Author: Roxana Stancu (esettes)
* @ Created: 2022/12/02 23:40
* @ Modified: 2022/12/06 23:02
* @ Modified: 2022/12/07 23:38
*/

#include "compute.h"
Expand All @@ -10,37 +10,93 @@
#include "pipeline.h"
#include "memory.h"
#include <stdio.h>
#include "time.h"

/* Need to map GPUs memory to our memory */
/* Buffers mirrors */
uint32_t g_in_data[1000];
uint32_t g_out_data[1000];
uint32_t g_out_data[1000][1000];

void generate_fractal(void)
{
uint32_t height_size = 1000;
uint32_t width_size = 1000;
uint32_t color = 0;

for (uint32_t row = 0; row < height_size; row++)
for (uint32_t row = 0; row < 1000; row++)
{
for (uint32_t col = 0; col < width_size; col++)
for (uint32_t col = 0; col < 1000; col++)
{
float r = (float)row / 500.0 - 1.0;
float i = (float)col / 500.0 - 1.0;

uint32_t count = 0;
while ((r * r * i < 4.0) && (count < 1000))
{
float temp = r * r - i * i + 0.17;
i = 2.0 * r * i + i;
r = temp + r;
while (((r * r + i * i) < 4.0) && (count < 63))
{
float temp = r * r - i * i + 0.17;
i = 2 * r * i + 0.57;
r = temp;
count++;
}
/** Color depends on the counter*/
/**Color depends on the counter
* Shifted to the left by 10 bits and add Alpha so that
* the value is a real 32 bit RGB value.
*/
g_out_data[row][col] = (count << 10) | 0xff000000;
}
}
}

uint32_t get_time(void)
{
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}

int main(int argc, char **argv)
{
create_instance();
get_physical_device();
create_device_and_compute_queue();
create_pipeline();
create_descriptor_set();
create_buffers(sizeof(g_in_data), sizeof(g_out_data));
create_command_pool();
create_command_buffer();


copy_to_input_buffer(g_in_data, sizeof(g_in_data));

uint32_t start = get_time();
generate_fractal();
start = get_time() - start;
printf("CPU time: %ums.\n", start);

FILE *f = fopen("./img/raw/fractal_cpu_1000x100.raw", "wb");
fwrite(g_out_data, sizeof(g_out_data), 1, f);
fclose(f);

start = get_time();
compute();
start = get_time() - start;
printf("GPU time: %ums.\n", start);

copy_from_output_buffer(g_out_data, sizeof(g_out_data));

f = fopen("./img/raw/fractal_gpu_1000x100.raw", "wb");
fwrite(g_out_data, sizeof(g_out_data), 1, f);
fclose(f);

destroy_pipeline();
destroy_commandpool_logicaldevice();
(void)argv;
(void)argc;
return (0);
}

/*
int main(int argc, char **argv)
{
create_instance();
Expand Down Expand Up @@ -70,3 +126,4 @@ int main(int argc, char **argv)
(void)argc;
return (0);
}
*/
Binary file modified src/shader/bin/shader.spv
Binary file not shown.
23 changes: 21 additions & 2 deletions src/shader/shader.comp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 450

layout ( local_size_x = 120, local_size_y = 1, local_size_z = 1 ) in;
layout ( local_size_x = 1000, local_size_y = 1, local_size_z = 1 ) in;

layout ( binding = 0 ) buffer input_buffer
{
Expand All @@ -9,7 +9,9 @@ layout ( binding = 0 ) buffer input_buffer

layout ( binding = 1 ) buffer output_buffer
{
float values_out[];
uint values_out[1000][1000];
//uint values_out1[500][500];
//uint values_out2[500][500];
};

/* Each localinvocationindex has its x, y and z members */
Expand All @@ -18,5 +20,22 @@ void main()
/** Basic calculation
uint index = gl_LocalInvocationID.x + gl_WorkGroupID.x * 120;
values_out[index] = values_in[index] * 1000.0;*/
float r = gl_WorkGroupID.x / 500.0 - 1.0f;
float i = gl_LocalInvocationID.x / 500.0 - 1.0f;

uint count = 0;
/** Local size X dimension setted to 1000, so shader will be called with
the local invocation index for all values from 0 to 1000, so is not
necessary loop over columns. */
while (((r * r + i * i) < 4.0f) && (count < 63))
{
float temp = r * r - i * i + 0.17;
i = 4 * r * i + 0.57;
r = temp;
count++;
}
/** 2 bits shifted = red color */
values_out[gl_WorkGroupID.x][gl_LocalInvocationID.x] = (count << 2) | 0xff000000;


}

0 comments on commit 65e7171

Please sign in to comment.