forked from LeenkxTeam/LNXSDK
Update Files
This commit is contained in:
0
Kha/Kinc/Tests/Display/Deployment/keepme
Normal file
0
Kha/Kinc/Tests/Display/Deployment/keepme
Normal file
44
Kha/Kinc/Tests/Display/Sources/display.c
Normal file
44
Kha/Kinc/Tests/Display/Sources/display.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <kinc/display.h>
|
||||
#include <kinc/log.h>
|
||||
|
||||
void print_mode(const char *indent, kinc_display_mode_t mode) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%sx: %i", indent, mode.x);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%sy: %i", indent, mode.y);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%swidth: %i", indent, mode.width);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%sheight: %i", indent, mode.height);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%spixels_per_inch: %i", indent, mode.pixels_per_inch);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%sfrequency: %i", indent, mode.frequency);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "%sbits_per_pixel: %i", indent, mode.bits_per_pixel);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
bool print_modes = false;
|
||||
if (argc > 1 && strcmp(argv[1], "--print-modes") == 0) {
|
||||
print_modes = true;
|
||||
}
|
||||
kinc_display_init();
|
||||
int count = kinc_count_displays();
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "display count: %i", count);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "primary display: %i", kinc_primary_display());
|
||||
for (int i = 0; i < count; i++) {
|
||||
bool available = kinc_display_available(i);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "display %i:", i);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\tavailable: %s", available ? "true" : "false");
|
||||
if (available) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\tname: %s", kinc_display_name(i));
|
||||
kinc_display_mode_t mode = kinc_display_current_mode(i);
|
||||
print_mode("\t", mode);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "");
|
||||
int mode_count = kinc_display_count_available_modes(i);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\tavailable modes: %i", mode_count);
|
||||
if (print_modes) {
|
||||
for (int j = 0; j < mode_count; j++) {
|
||||
kinc_display_mode_t mode = kinc_display_available_mode(i, j);
|
||||
print_mode("\t\t", mode);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
10
Kha/Kinc/Tests/Display/kfile.js
Normal file
10
Kha/Kinc/Tests/Display/kfile.js
Normal file
@ -0,0 +1,10 @@
|
||||
const project = new Project('DisplayTest');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
1
Kha/Kinc/Tests/Empty/Deployment/keepme.txt
Normal file
1
Kha/Kinc/Tests/Empty/Deployment/keepme.txt
Normal file
@ -0,0 +1 @@
|
||||
Don't read me, but please keep me.
|
104
Kha/Kinc/Tests/Empty/Sources/shader.c
Normal file
104
Kha/Kinc/Tests/Empty/Sources/shader.c
Normal file
@ -0,0 +1,104 @@
|
||||
#include <kinc/graphics4/graphics.h>
|
||||
#include <kinc/graphics4/indexbuffer.h>
|
||||
#include <kinc/graphics4/pipeline.h>
|
||||
#include <kinc/graphics4/shader.h>
|
||||
#include <kinc/graphics4/vertexbuffer.h>
|
||||
#include <kinc/io/filereader.h>
|
||||
#include <kinc/system.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static kinc_g4_shader_t vertex_shader;
|
||||
static kinc_g4_shader_t fragment_shader;
|
||||
static kinc_g4_pipeline_t pipeline;
|
||||
static kinc_g4_vertex_buffer_t vertices;
|
||||
static kinc_g4_index_buffer_t indices;
|
||||
|
||||
#define HEAP_SIZE 1024 * 1024
|
||||
static uint8_t *heap = NULL;
|
||||
static size_t heap_top = 0;
|
||||
|
||||
static void *allocate(size_t size) {
|
||||
size_t old_top = heap_top;
|
||||
heap_top += size;
|
||||
assert(heap_top <= HEAP_SIZE);
|
||||
return &heap[old_top];
|
||||
}
|
||||
|
||||
static void update(void *data) {
|
||||
kinc_g4_begin(0);
|
||||
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0, 0.0f, 0);
|
||||
|
||||
kinc_g4_set_pipeline(&pipeline);
|
||||
kinc_g4_set_vertex_buffer(&vertices);
|
||||
kinc_g4_set_index_buffer(&indices);
|
||||
kinc_g4_draw_indexed_vertices();
|
||||
|
||||
kinc_g4_end(0);
|
||||
kinc_g4_swap_buffers();
|
||||
}
|
||||
|
||||
static void load_shader(const char *filename, kinc_g4_shader_t *shader, kinc_g4_shader_type_t shader_type) {
|
||||
kinc_file_reader_t file;
|
||||
kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
|
||||
size_t data_size = kinc_file_reader_size(&file);
|
||||
uint8_t *data = allocate(data_size);
|
||||
kinc_file_reader_read(&file, data, data_size);
|
||||
kinc_file_reader_close(&file);
|
||||
kinc_g4_shader_init(shader, data, data_size, shader_type);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
kinc_init("Shader", 1024, 768, NULL, NULL);
|
||||
kinc_set_update_callback(update, NULL);
|
||||
|
||||
heap = (uint8_t *)malloc(HEAP_SIZE);
|
||||
assert(heap != NULL);
|
||||
|
||||
load_shader("shader.vert", &vertex_shader, KINC_G4_SHADER_TYPE_VERTEX);
|
||||
load_shader("shader.frag", &fragment_shader, KINC_G4_SHADER_TYPE_FRAGMENT);
|
||||
|
||||
kinc_g4_vertex_structure_t structure;
|
||||
kinc_g4_vertex_structure_init(&structure);
|
||||
kinc_g4_vertex_structure_add(&structure, "pos", KINC_G4_VERTEX_DATA_F32_3X);
|
||||
kinc_g4_pipeline_init(&pipeline);
|
||||
pipeline.vertex_shader = &vertex_shader;
|
||||
pipeline.fragment_shader = &fragment_shader;
|
||||
pipeline.input_layout[0] = &structure;
|
||||
pipeline.input_layout[1] = NULL;
|
||||
kinc_g4_pipeline_compile(&pipeline);
|
||||
|
||||
kinc_g4_vertex_buffer_init(&vertices, 3, &structure, KINC_G4_USAGE_STATIC, 0);
|
||||
{
|
||||
float *v = kinc_g4_vertex_buffer_lock_all(&vertices);
|
||||
int i = 0;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = 1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = 1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
kinc_g4_vertex_buffer_unlock_all(&vertices);
|
||||
}
|
||||
|
||||
kinc_g4_index_buffer_init(&indices, 3, KINC_G4_INDEX_BUFFER_FORMAT_16BIT, KINC_G4_USAGE_STATIC);
|
||||
{
|
||||
uint16_t *i = (uint16_t *)kinc_g4_index_buffer_lock_all(&indices);
|
||||
i[0] = 0;
|
||||
i[1] = 1;
|
||||
i[2] = 2;
|
||||
kinc_g4_index_buffer_unlock_all(&indices);
|
||||
}
|
||||
|
||||
kinc_start();
|
||||
|
||||
return 0;
|
||||
}
|
7
Kha/Kinc/Tests/Empty/Sources/shader.frag.glsl
Normal file
7
Kha/Kinc/Tests/Empty/Sources/shader.frag.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
out vec4 frag;
|
||||
|
||||
void main() {
|
||||
frag = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
7
Kha/Kinc/Tests/Empty/Sources/shader.vert.glsl
Normal file
7
Kha/Kinc/Tests/Empty/Sources/shader.vert.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
in vec3 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos.x, pos.y, 0.5, 1.0);
|
||||
}
|
10
Kha/Kinc/Tests/Empty/kfile.js
Normal file
10
Kha/Kinc/Tests/Empty/kfile.js
Normal file
@ -0,0 +1,10 @@
|
||||
let project = new Project('Empty');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
1
Kha/Kinc/Tests/Input/Deployment/keepme.txt
Normal file
1
Kha/Kinc/Tests/Input/Deployment/keepme.txt
Normal file
@ -0,0 +1 @@
|
||||
Don't read me, but please keep me.
|
65
Kha/Kinc/Tests/Input/Sources/input.c
Normal file
65
Kha/Kinc/Tests/Input/Sources/input.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include "kinc/input/keyboard.h"
|
||||
#include "kinc/log.h"
|
||||
#include <kinc/graphics4/graphics.h>
|
||||
#include <kinc/input/mouse.h>
|
||||
#include <kinc/system.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void update(void *data) {
|
||||
kinc_g4_begin(0);
|
||||
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0, 0.0f, 0);
|
||||
kinc_g4_end(0);
|
||||
kinc_g4_swap_buffers();
|
||||
}
|
||||
|
||||
static void mouse_enter_window(int window, void* data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_enter_window -- window: %i", window);
|
||||
}
|
||||
static void mouse_leave_window(int window, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_leave_window -- window: %i", window);
|
||||
}
|
||||
static void mouse_press(int window, int button, int x, int y, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_press -- window: %i, button: %i, x: %i, y: %i", window, button, x, y);
|
||||
}
|
||||
static void mouse_release(int window, int button, int x, int y, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_release -- window: %i, button: %i, x: %i, y: %i", window, button, x, y);
|
||||
}
|
||||
static void mouse_move(int window, int x, int y, int mov_x, int mov_y, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_move -- window: %i, x: %i, y: %i, movement_x: %i, movement_y: %i", window, x, y, mov_x, mov_y);
|
||||
}
|
||||
static void mouse_scroll(int window, int delta, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "mouse_scroll -- window: %i, delta: %i", window, delta);
|
||||
}
|
||||
static void key_down(int key, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "key_down -- key: %i", key);
|
||||
}
|
||||
static void key_up(int key, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "key_up -- key: %i", key);
|
||||
}
|
||||
static void key_press(unsigned character, void *data) {
|
||||
char str[5] = {0};
|
||||
wctomb(str, character);
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "key_press -- char: %s", str);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
kinc_init("Shader", 1024, 768, NULL, NULL);
|
||||
kinc_set_update_callback(update, NULL);
|
||||
|
||||
kinc_mouse_set_enter_window_callback(mouse_enter_window, NULL);
|
||||
kinc_mouse_set_leave_window_callback(mouse_leave_window, NULL);
|
||||
kinc_mouse_set_press_callback(mouse_press, NULL);
|
||||
kinc_mouse_set_release_callback(mouse_release, NULL);
|
||||
kinc_mouse_set_move_callback(mouse_move, NULL);
|
||||
kinc_mouse_set_scroll_callback(mouse_scroll, NULL);
|
||||
|
||||
kinc_keyboard_set_key_down_callback(key_down, NULL);
|
||||
kinc_keyboard_set_key_up_callback(key_up, NULL);
|
||||
kinc_keyboard_set_key_press_callback(key_press, NULL);
|
||||
|
||||
kinc_start();
|
||||
|
||||
return 0;
|
||||
}
|
10
Kha/Kinc/Tests/Input/kfile.js
Normal file
10
Kha/Kinc/Tests/Input/kfile.js
Normal file
@ -0,0 +1,10 @@
|
||||
let project = new Project('Input');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
0
Kha/Kinc/Tests/MultiWindow/Deployment/keepme
Normal file
0
Kha/Kinc/Tests/MultiWindow/Deployment/keepme
Normal file
167
Kha/Kinc/Tests/MultiWindow/Sources/multiwindow.c
Normal file
167
Kha/Kinc/Tests/MultiWindow/Sources/multiwindow.c
Normal file
@ -0,0 +1,167 @@
|
||||
#include <kinc/graphics4/graphics.h>
|
||||
#include <kinc/graphics4/indexbuffer.h>
|
||||
#include <kinc/graphics4/pipeline.h>
|
||||
#include <kinc/graphics4/shader.h>
|
||||
#include <kinc/graphics4/vertexbuffer.h>
|
||||
#include <kinc/input/mouse.h>
|
||||
#include <kinc/io/filereader.h>
|
||||
#include <kinc/log.h>
|
||||
#include <kinc/system.h>
|
||||
#include <kinc/window.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static kinc_g4_shader_t vertex_shader;
|
||||
static kinc_g4_shader_t fragment_shader;
|
||||
static kinc_g4_pipeline_t pipeline;
|
||||
static kinc_g4_vertex_buffer_t vertices;
|
||||
static kinc_g4_index_buffer_t indices;
|
||||
|
||||
#define WINDOW_COUNT 2
|
||||
|
||||
static struct window {
|
||||
int index;
|
||||
bool open;
|
||||
bool mouse_down;
|
||||
} windows[WINDOW_COUNT] = {0};
|
||||
|
||||
#define HEAP_SIZE 1024 * 1024
|
||||
static uint8_t *heap = NULL;
|
||||
static size_t heap_top = 0;
|
||||
|
||||
static void *allocate(size_t size) {
|
||||
size_t old_top = heap_top;
|
||||
heap_top += size;
|
||||
assert(heap_top <= HEAP_SIZE);
|
||||
return &heap[old_top];
|
||||
}
|
||||
|
||||
static void update(void *data) {
|
||||
for (int window = 0; window < WINDOW_COUNT; window++) {
|
||||
if (windows[window].open) {
|
||||
kinc_g4_begin(window);
|
||||
kinc_g4_clear(KINC_G4_CLEAR_COLOR, windows[window].mouse_down ? 0xff0000ff : 0, 0.0f, 0);
|
||||
|
||||
kinc_g4_set_pipeline(&pipeline);
|
||||
kinc_g4_set_vertex_buffer(&vertices);
|
||||
kinc_g4_set_index_buffer(&indices);
|
||||
kinc_g4_draw_indexed_vertices();
|
||||
|
||||
kinc_g4_end(window);
|
||||
}
|
||||
}
|
||||
kinc_g4_swap_buffers();
|
||||
}
|
||||
|
||||
static void load_shader(const char *filename, kinc_g4_shader_t *shader, kinc_g4_shader_type_t shader_type) {
|
||||
kinc_file_reader_t file;
|
||||
kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
|
||||
size_t data_size = kinc_file_reader_size(&file);
|
||||
uint8_t *data = allocate(data_size);
|
||||
kinc_file_reader_read(&file, data, data_size);
|
||||
kinc_file_reader_close(&file);
|
||||
kinc_g4_shader_init(shader, data, data_size, shader_type);
|
||||
}
|
||||
|
||||
static void mouse_down(int window, int button, int x, int y) {
|
||||
windows[window].mouse_down = true;
|
||||
}
|
||||
|
||||
static void mouse_up(int window, int button, int x, int y) {
|
||||
windows[window].mouse_down = false;
|
||||
}
|
||||
|
||||
static bool window_close(void *data) {
|
||||
struct window *window = data;
|
||||
window->open = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static char *copy_callback(void *data) {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
static void paste_callback(char *text, void *data) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Pasted: %s", text);
|
||||
}
|
||||
|
||||
static void drop_files_callback(wchar_t *text, void *data) {
|
||||
char dest[1024];
|
||||
wcstombs(dest, text, sizeof(dest));
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Dropped: %s", dest);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
kinc_window_options_t first_window_options;
|
||||
kinc_window_options_set_defaults(&first_window_options);
|
||||
// first_window_options.mode = KINC_WINDOW_MODE_FULLSCREEN;
|
||||
int first_window = kinc_init("MultiWindow", 1024, 768, &first_window_options, NULL);
|
||||
// return 0;
|
||||
kinc_set_update_callback(update, NULL);
|
||||
|
||||
heap = (uint8_t *)malloc(HEAP_SIZE);
|
||||
assert(heap != NULL);
|
||||
|
||||
load_shader("shader.vert", &vertex_shader, KINC_G4_SHADER_TYPE_VERTEX);
|
||||
load_shader("shader.frag", &fragment_shader, KINC_G4_SHADER_TYPE_FRAGMENT);
|
||||
|
||||
kinc_g4_vertex_structure_t structure;
|
||||
kinc_g4_vertex_structure_init(&structure);
|
||||
kinc_g4_vertex_structure_add(&structure, "pos", KINC_G4_VERTEX_DATA_FLOAT3);
|
||||
kinc_g4_pipeline_init(&pipeline);
|
||||
pipeline.vertex_shader = &vertex_shader;
|
||||
pipeline.fragment_shader = &fragment_shader;
|
||||
pipeline.input_layout[0] = &structure;
|
||||
pipeline.input_layout[1] = NULL;
|
||||
kinc_g4_pipeline_compile(&pipeline);
|
||||
|
||||
kinc_g4_vertex_buffer_init(&vertices, 3, &structure, KINC_G4_USAGE_STATIC, 0);
|
||||
{
|
||||
float *v = kinc_g4_vertex_buffer_lock_all(&vertices);
|
||||
int i = 0;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = 1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = 1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
kinc_g4_vertex_buffer_unlock_all(&vertices);
|
||||
}
|
||||
|
||||
kinc_g4_index_buffer_init(&indices, 3, KINC_G4_INDEX_BUFFER_FORMAT_32BIT, KINC_G4_USAGE_STATIC);
|
||||
{
|
||||
uint32_t *i = (uint32_t *)kinc_g4_index_buffer_lock_all(&indices);
|
||||
i[0] = 0;
|
||||
i[1] = 1;
|
||||
i[2] = 2;
|
||||
kinc_g4_index_buffer_unlock_all(&indices);
|
||||
}
|
||||
kinc_window_options_t options;
|
||||
kinc_framebuffer_options_t frame_options;
|
||||
kinc_window_options_set_defaults(&options);
|
||||
kinc_framebuffer_options_set_defaults(&frame_options);
|
||||
int window_two = kinc_window_create(&options, &frame_options);
|
||||
windows[0].index = 0;
|
||||
windows[0].open = true;
|
||||
windows[1].index = 1;
|
||||
windows[1].open = true;
|
||||
kinc_window_set_close_callback(first_window, window_close, &windows[0]);
|
||||
kinc_window_set_close_callback(window_two, window_close, &windows[1]);
|
||||
|
||||
kinc_mouse_set_press_callback(mouse_down);
|
||||
kinc_mouse_set_release_callback(mouse_up);
|
||||
kinc_set_copy_callback(copy_callback, NULL);
|
||||
kinc_set_paste_callback(paste_callback, NULL);
|
||||
kinc_set_drop_files_callback(drop_files_callback, NULL);
|
||||
kinc_start();
|
||||
|
||||
return 0;
|
||||
}
|
7
Kha/Kinc/Tests/MultiWindow/Sources/shader.frag.glsl
Normal file
7
Kha/Kinc/Tests/MultiWindow/Sources/shader.frag.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
7
Kha/Kinc/Tests/MultiWindow/Sources/shader.vert.glsl
Normal file
7
Kha/Kinc/Tests/MultiWindow/Sources/shader.vert.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
in vec3 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos.x, pos.y, 0.5, 1.0);
|
||||
}
|
10
Kha/Kinc/Tests/MultiWindow/kfile.js
Normal file
10
Kha/Kinc/Tests/MultiWindow/kfile.js
Normal file
@ -0,0 +1,10 @@
|
||||
const project = new Project('MultiWindow');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
1
Kha/Kinc/Tests/SIMD/Deployment/keepme.txt
Normal file
1
Kha/Kinc/Tests/SIMD/Deployment/keepme.txt
Normal file
@ -0,0 +1 @@
|
||||
Don't read me, but please keep me.
|
553
Kha/Kinc/Tests/SIMD/Sources/simd.c
Normal file
553
Kha/Kinc/Tests/SIMD/Sources/simd.c
Normal file
@ -0,0 +1,553 @@
|
||||
#include <kinc/log.h>
|
||||
#include <kinc/math/core.h>
|
||||
#include <kinc/simd/float32x4.h>
|
||||
#include <kinc/simd/int16x8.h>
|
||||
#include <kinc/simd/int32x4.h>
|
||||
#include <kinc/simd/int8x16.h>
|
||||
#include <kinc/simd/uint16x8.h>
|
||||
#include <kinc/simd/uint32x4.h>
|
||||
#include <kinc/simd/uint8x16.h>
|
||||
#include <kinc/system.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define EPSILON 0.00001f
|
||||
|
||||
static int total_tests = 0;
|
||||
|
||||
static bool check_f32(const char *name, kinc_float32x4_t result, const float expected[4], float epsilon) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (kinc_abs(kinc_float32x4_get(result, i) - expected[i]) > epsilon) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\texpected {%f, %f, %f, %f} got {%f, %f, %f, %f}", expected[0], expected[1], expected[2], expected[3],
|
||||
kinc_float32x4_get(result, 0), kinc_float32x4_get(result, 1), kinc_float32x4_get(result, 2), kinc_float32x4_get(result, 3));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_i8(const char *name, kinc_int8x16_t result, const int8_t expected[16]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
if (kinc_int8x16_get(result, i) != expected[i]) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(
|
||||
KINC_LOG_LEVEL_INFO,
|
||||
"\texpected {%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d} got {%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d}",
|
||||
expected[0], expected[1], expected[2], expected[3], expected[4], expected[5], expected[6], expected[7], expected[8], expected[9], expected[10],
|
||||
expected[11], expected[12], expected[13], expected[14], expected[15], kinc_int8x16_get(result, 0), kinc_int8x16_get(result, 1),
|
||||
kinc_int8x16_get(result, 2), kinc_int8x16_get(result, 3), kinc_int8x16_get(result, 4), kinc_int8x16_get(result, 5), kinc_int8x16_get(result, 6),
|
||||
kinc_int8x16_get(result, 7), kinc_int8x16_get(result, 8), kinc_int8x16_get(result, 9), kinc_int8x16_get(result, 10), kinc_int8x16_get(result, 11),
|
||||
kinc_int8x16_get(result, 12), kinc_int8x16_get(result, 13), kinc_int8x16_get(result, 14), kinc_int8x16_get(result, 15));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_u8(const char *name, kinc_uint8x16_t result, const uint8_t expected[16]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
if ((kinc_uint8x16_get(result, i) ^ expected[i]) != 0) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(
|
||||
KINC_LOG_LEVEL_INFO,
|
||||
"\texpected {%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u} got {%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u}",
|
||||
expected[0], expected[1], expected[2], expected[3], expected[4], expected[5], expected[6], expected[7], expected[8], expected[9], expected[10],
|
||||
expected[11], expected[12], expected[13], expected[14], expected[15], kinc_uint8x16_get(result, 0), kinc_uint8x16_get(result, 1),
|
||||
kinc_uint8x16_get(result, 2), kinc_uint8x16_get(result, 3), kinc_uint8x16_get(result, 4), kinc_uint8x16_get(result, 5),
|
||||
kinc_uint8x16_get(result, 6), kinc_uint8x16_get(result, 7), kinc_uint8x16_get(result, 8), kinc_uint8x16_get(result, 9),
|
||||
kinc_uint8x16_get(result, 10), kinc_uint8x16_get(result, 11), kinc_uint8x16_get(result, 12), kinc_uint8x16_get(result, 13),
|
||||
kinc_uint8x16_get(result, 14), kinc_uint8x16_get(result, 15));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_i16(const char *name, kinc_int16x8_t result, const int16_t expected[8]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (kinc_int16x8_get(result, i) != expected[i]) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\texpected {%d, %d, %d, %d, %d, %d, %d, %d} got {%d, %d, %d, %d, %d, %d, %d, %d}", expected[0], expected[1], expected[2],
|
||||
expected[3], expected[4], expected[5], expected[6], expected[7], kinc_int16x8_get(result, 0), kinc_int16x8_get(result, 1),
|
||||
kinc_int16x8_get(result, 2), kinc_int16x8_get(result, 3), kinc_int16x8_get(result, 4), kinc_int16x8_get(result, 5),
|
||||
kinc_int16x8_get(result, 6), kinc_int16x8_get(result, 7));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_u16(const char *name, kinc_uint16x8_t result, const uint16_t expected[8]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if ((kinc_uint16x8_get(result, i) ^ expected[i]) != 0) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\texpected {%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu} got {%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu}", expected[0],
|
||||
expected[1], expected[2], expected[3], expected[4], expected[5], expected[6], expected[7], kinc_uint16x8_get(result, 0),
|
||||
kinc_uint16x8_get(result, 1), kinc_uint16x8_get(result, 2), kinc_uint16x8_get(result, 3), kinc_uint16x8_get(result, 4),
|
||||
kinc_uint16x8_get(result, 5), kinc_uint16x8_get(result, 6), kinc_uint16x8_get(result, 7));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_i32(const char *name, kinc_int32x4_t result, const int32_t expected[4]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (kinc_int32x4_get(result, i) != expected[i]) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\texpected {%d, %d, %d, %d} got {%d, %d, %d, %d}", expected[0], expected[1], expected[2], expected[3],
|
||||
kinc_int32x4_get(result, 0), kinc_int32x4_get(result, 1), kinc_int32x4_get(result, 2), kinc_int32x4_get(result, 3));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool check_u32(const char *name, kinc_uint32x4_t result, const uint32_t expected[4]) {
|
||||
++total_tests;
|
||||
bool success = true;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if ((kinc_uint32x4_get(result, i) ^ expected[i]) != 0) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "Test %s %s", name, success ? "PASS" : "FAIL");
|
||||
if (!success) {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\texpected {%lu, %lu, %lu, %lu} got {%lu, %lu, %lu, %lu}", expected[0], expected[1], expected[2], expected[3],
|
||||
kinc_uint32x4_get(result, 0), kinc_uint32x4_get(result, 1), kinc_uint32x4_get(result, 2), kinc_uint32x4_get(result, 3));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
#if defined(KINC_SSE2)
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Using SSE2\n");
|
||||
#elif defined(KINC_SSE)
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Using SSE for float32x4 and scalar fallback for all integer types\n");
|
||||
#elif defined(KINC_NEON)
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Using NEON\n");
|
||||
#else
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "Using scalar fallback implementation\n");
|
||||
#endif
|
||||
|
||||
int failed = 0;
|
||||
{
|
||||
kinc_float32x4_t a = kinc_float32x4_load(-1.0f, 2.0f, 3.0f, 4.0f);
|
||||
kinc_float32x4_t b = kinc_float32x4_load_all(2.0f);
|
||||
|
||||
kinc_float32x4_mask_t mask;
|
||||
kinc_float32x4_t result;
|
||||
|
||||
result = kinc_float32x4_abs(a);
|
||||
failed += check_f32("float32x4 abs", result, (float[4]){1.0f, 2.0f, 3.0f, 4.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_add(a, b);
|
||||
failed += check_f32("float32x4 add", result, (float[4]){1.0f, 4.0f, 5.0f, 6.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_sub(a, b);
|
||||
failed += check_f32("float32x4 sub", result, (float[4]){-3.0f, 0.0f, 1.0f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_mul(a, b);
|
||||
failed += check_f32("float32x4 mul", result, (float[4]){-2.0f, 4.0f, 6.0f, 8.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_div(a, b);
|
||||
failed += check_f32("float32x4 div", result, (float[4]){-0.5f, 1.0f, 1.5f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_neg(a);
|
||||
failed += check_f32("float32x4 neg", result, (float[4]){1.0f, -2.0f, -3.0f, -4.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_reciprocal_approximation(a);
|
||||
failed += check_f32("float32x4 reciprocal_approximation", result, (float[4]){1.0f / -1.0f, 1.0f / 2.0f, 1.0f / 3.0f, 1.0f / 4.0f}, 0.002f) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_reciprocal_sqrt_approximation(b);
|
||||
failed += check_f32("float32x4 reciprocal_sqrt_approximation", result,
|
||||
(float[4]){1.0f / sqrtf(2.0f), 1.0f / sqrtf(2.0f), 1.0f / sqrtf(2.0f), 1.0f / sqrtf(2.0f)}, 0.003f)
|
||||
? failed
|
||||
: 1;
|
||||
|
||||
result = kinc_float32x4_sqrt(b);
|
||||
failed += check_f32("float32x4 sqrt", result, (float[4]){sqrtf(2.0f), sqrtf(2.0f), sqrtf(2.0f), sqrtf(2.0f)}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_max(a, b);
|
||||
failed += check_f32("float32x4 max", result, (float[4]){2.0f, 2.0f, 3.0f, 4.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
result = kinc_float32x4_min(a, b);
|
||||
failed += check_f32("float32x4 min", result, (float[4]){-1.0f, 2.0f, 2.0f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmpeq(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmpeq & sel", result, (float[4]){2.0f, 2.0f, 2.0f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmpge(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmpge & sel", result, (float[4]){2.0f, 2.0f, 3.0f, 4.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmpgt(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmpgt & sel", result, (float[4]){2.0f, 2.0f, 3.0f, 4.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmple(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmple & sel", result, (float[4]){-1.0f, 2.0f, 2.0f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmplt(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmplt & sel", result, (float[4]){-1.0f, 2.0f, 2.0f, 2.0f}, EPSILON) ? 0 : 1;
|
||||
|
||||
mask = kinc_float32x4_cmpneq(a, b);
|
||||
result = kinc_float32x4_sel(a, b, mask);
|
||||
failed += check_f32("float32x4 cmpneq & sel", result, (float[4]){-1.0f, 2.0f, 3.0f, 4.0f}, EPSILON) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_int8x16_t a = kinc_int8x16_load((int8_t[16]){-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8});
|
||||
kinc_int8x16_t b = kinc_int8x16_load_all(2);
|
||||
|
||||
kinc_int8x16_mask_t mask;
|
||||
kinc_int8x16_t result;
|
||||
|
||||
result = kinc_int8x16_add(a, b);
|
||||
failed += check_i8("int8x16 add", result, (int8_t[16]){-6, -5, -4, -3, -2, -1, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10}) ? 0 : 1;
|
||||
|
||||
result = kinc_int8x16_sub(a, b);
|
||||
failed += check_i8("int8x16 sub", result, (int8_t[16]){-10, -9, -8, -7, -6, -5, -4, -3, -1, 0, 1, 2, 3, 4, 5, 6}) ? 0 : 1;
|
||||
|
||||
result = kinc_int8x16_max(a, b);
|
||||
failed += check_i8("int8x16 max", result, (int8_t[16]){2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
result = kinc_int8x16_min(a, b);
|
||||
failed += check_i8("int8x16 min", result, (int8_t[16]){-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmpeq(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmpeq & sel", result, (int8_t[16]){2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmpge(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmpge & sel", result, (int8_t[16]){2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmpgt(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmpgt & sel", result, (int8_t[16]){2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmple(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmple & sel", result, (int8_t[16]){-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmplt(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmplt & sel", result, (int8_t[16]){-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int8x16_cmpneq(a, b);
|
||||
result = kinc_int8x16_sel(a, b, mask);
|
||||
failed += check_i8("int8x16 cmpneq & sel", result, (int8_t[16]){-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
result = kinc_int8x16_or(a, b);
|
||||
failed += check_i8("int8x16 or", result,
|
||||
(int8_t[16]){-8 | 2, -7 | 2, -6 | 2, -5 | 2, -4 | 2, -3 | 2, -2 | 2, -1 | 2, 1 | 2, 2 | 2, 3 | 2, 4 | 2, 5 | 2, 6 | 2, 7 | 2, 8 | 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_int8x16_and(a, b);
|
||||
failed += check_i8("int8x16 and", result,
|
||||
(int8_t[16]){-8 & 2, -7 & 2, -6 & 2, -5 & 2, -4 & 2, -3 & 2, -2 & 2, -1 & 2, 1 & 2, 2 & 2, 3 & 2, 4 & 2, 5 & 2, 6 & 2, 7 & 2, 8 & 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_int8x16_xor(a, b);
|
||||
failed +=
|
||||
check_i8("int8x16 xor", result,
|
||||
(int8_t[16]){-8 ^ 2, -7 ^ 2, -6 ^ 2, -5 ^ 2, -4 ^ 2, -3 ^ 2, -0x2 ^ 2, -1 ^ 2, 1 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2, 5 ^ 2, 6 ^ 2, 7 ^ 2, 8 ^ 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_int8x16_not(a);
|
||||
failed += check_i8("int8x16 not", result, (int8_t[16]){~-8, ~-7, ~-6, ~-5, ~-4, ~-3, ~-2, ~-1, ~1, ~2, ~3, ~4, ~5, ~6, ~7, ~8}) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_uint8x16_t a = kinc_uint8x16_load((uint8_t[16]){1, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8});
|
||||
kinc_uint8x16_t b = kinc_uint8x16_load_all(2);
|
||||
|
||||
kinc_uint8x16_mask_t mask;
|
||||
kinc_uint8x16_t result;
|
||||
|
||||
result = kinc_uint8x16_add(a, b);
|
||||
failed += check_u8("uint8x16 add", result, (uint8_t[16]){3, 4, 5, 6, 7, 8, 9, 10, 6, 4, 5, 6, 7, 8, 9, 10}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint8x16_sub(a, b);
|
||||
failed += check_u8("uint8x16 sub", result, (uint8_t[16]){255, 0, 1, 2, 3, 4, 5, 6, 2, 0, 1, 2, 3, 4, 5, 6}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint8x16_max(a, b);
|
||||
failed += check_u8("uint8x16 max", result, (uint8_t[16]){2, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint8x16_min(a, b);
|
||||
failed += check_u8("uint8x16 min", result, (uint8_t[16]){1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmpeq(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmpeq & sel", result, (uint8_t[16]){2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmpge(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmpge & sel", result, (uint8_t[16]){2, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmpgt(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmpgt & sel", result, (uint8_t[16]){2, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmple(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmple & sel", result, (uint8_t[16]){1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmplt(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmplt & sel", result, (uint8_t[16]){1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint8x16_cmpneq(a, b);
|
||||
result = kinc_uint8x16_sel(a, b, mask);
|
||||
failed += check_u8("uint8x16 cmpneq & sel", result, (uint8_t[16]){1, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint8x16_or(a, b);
|
||||
failed += check_u8("uint8x16 or", result,
|
||||
(uint8_t[16]){1 | 2, 2 | 2, 3 | 2, 4 | 2, 5 | 2, 6 | 2, 7 | 2, 8 | 2, 4 | 2, 2 | 2, 3 | 2, 4 | 2, 5 | 2, 6 | 2, 7 | 2, 8 | 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_uint8x16_and(a, b);
|
||||
failed += check_u8("uint8x16 and", result,
|
||||
(uint8_t[16]){1 & 2, 2 & 2, 3 & 2, 4 & 2, 5 & 2, 6 & 2, 7 & 2, 8 & 2, 4 & 2, 2 & 2, 3 & 2, 4 & 2, 5 & 2, 6 & 2, 7 & 2, 8 & 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_uint8x16_xor(a, b);
|
||||
failed += check_u8("uint8x16 xor", result,
|
||||
(uint8_t[16]){1 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2, 5 ^ 2, 6 ^ 2, 7 ^ 2, 8 ^ 2, 4 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2, 5 ^ 2, 6 ^ 2, 7 ^ 2, 8 ^ 2})
|
||||
? 0
|
||||
: 1;
|
||||
|
||||
result = kinc_uint8x16_not(a);
|
||||
uint8_t chk[16] = {1, 2, 3, 4, 5, 6, 7, 8, 4, 2, 3, 4, 5, 6, 7, 8};
|
||||
for (int i = 0; i < 16; ++i) chk[i] = (uint8_t)(~chk[i]);
|
||||
failed += check_u8("uint8x16 not", result, chk) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_int16x8_t a = kinc_int16x8_load((int16_t[8]){-4, -3, -2, -1, 1, 2, 3, 4});
|
||||
kinc_int16x8_t b = kinc_int16x8_load_all(2);
|
||||
|
||||
kinc_int16x8_mask_t mask;
|
||||
kinc_int16x8_t result;
|
||||
|
||||
result = kinc_int16x8_add(a, b);
|
||||
failed += check_i16("int16x8 add", result, (int16_t[8]){-2, -1, 0, 1, 3, 4, 5, 6}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_sub(a, b);
|
||||
failed += check_i16("int16x8 sub", result, (int16_t[8]){-6, -5, -4, -3, -1, 0, 1, 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_max(a, b);
|
||||
failed += check_i16("int16x8 max", result, (int16_t[8]){2, 2, 2, 2, 2, 2, 3, 4}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_min(a, b);
|
||||
failed += check_i16("int16x8 min", result, (int16_t[8]){-4, -3, -2, -1, 1, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmpeq(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmpeq & sel", result, (int16_t[8]){2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmpge(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmpge & sel", result, (int16_t[8]){2, 2, 2, 2, 2, 2, 3, 4}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmpgt(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmpgt & sel", result, (int16_t[8]){2, 2, 2, 2, 2, 2, 3, 4}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmple(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmple & sel", result, (int16_t[8]){-4, -3, -2, -1, 1, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmplt(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmplt & sel", result, (int16_t[8]){-4, -3, -2, -1, 1, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int16x8_cmpneq(a, b);
|
||||
result = kinc_int16x8_sel(a, b, mask);
|
||||
failed += check_i16("int16x8 cmpneq & sel", result, (int16_t[8]){-4, -3, -2, -1, 1, 2, 3, 4}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_or(a, b);
|
||||
failed += check_i16("int16x8 or", result, (int16_t[8]){-4 | 2, -3 | 2, -2 | 2, -1 | 2, 1 | 2, 2 | 2, 3 | 2, 4 | 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_and(a, b);
|
||||
failed += check_i16("int16x8 and", result, (int16_t[8]){-4 & 2, -3 & 2, -2 & 2, -1 & 2, 1 & 2, 2 & 2, 3 & 2, 4 & 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_xor(a, b);
|
||||
failed += check_i16("int16x8 xor", result, (int16_t[8]){-4 ^ 2, -3 ^ 2, -0x2 ^ 2, -1 ^ 2, 1 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int16x8_not(a);
|
||||
failed += check_i16("int16x8 not", result, (int16_t[8]){~-4, ~-3, ~-2, ~-1, ~1, ~2, ~3, ~4}) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_uint16x8_t a = kinc_uint16x8_load((uint16_t[8]){1, 2, 3, 4, 5, 6, 7, 8});
|
||||
kinc_uint16x8_t b = kinc_uint16x8_load_all(2);
|
||||
|
||||
kinc_uint16x8_mask_t mask;
|
||||
kinc_uint16x8_t result;
|
||||
|
||||
result = kinc_uint16x8_add(a, b);
|
||||
failed += check_u16("uint16x8 add", result, (uint16_t[8]){3, 4, 5, 6, 7, 8, 9, 10}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint16x8_sub(a, b);
|
||||
failed += check_u16("uint16x8 sub", result, (uint16_t[8]){65535, 0, 1, 2, 3, 4, 5, 6}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint16x8_cmpeq(a, b);
|
||||
result = kinc_uint16x8_sel(a, b, mask);
|
||||
failed += check_u16("uint16x8 cmpeq & sel", result, (uint16_t[8]){2, 2, 2, 2, 2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint16x8_cmpneq(a, b);
|
||||
result = kinc_uint16x8_sel(a, b, mask);
|
||||
failed += check_u16("uint16x8 cmpneq & sel", result, (uint16_t[8]){1, 2, 3, 4, 5, 6, 7, 8}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint16x8_or(a, b);
|
||||
failed += check_u16("uint16x8 or", result, (uint16_t[8]){1 | 2, 2 | 2, 3 | 2, 4 | 2, 5 | 2, 6 | 2, 7 | 2, 8 | 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint16x8_and(a, b);
|
||||
failed += check_u16("uint16x8 and", result, (uint16_t[8]){1 & 2, 2 & 2, 3 & 2, 4 & 2, 5 & 2, 6 & 2, 7 & 2, 8 & 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint16x8_xor(a, b);
|
||||
failed += check_u16("uint16x8 xor", result, (uint16_t[8]){1 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2, 5 ^ 2, 6 ^ 2, 7 ^ 2, 8 ^ 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint16x8_not(a);
|
||||
uint16_t chk[8] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
for (int i = 0; i < 8; ++i) chk[i] = (uint16_t)(~chk[i]);
|
||||
failed += check_u16("uint16x8 not", result, chk) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_int32x4_t a = kinc_int32x4_load((int32_t[4]){-2, -1, 1, 2});
|
||||
kinc_int32x4_t b = kinc_int32x4_load_all(2);
|
||||
|
||||
kinc_int32x4_mask_t mask;
|
||||
kinc_int32x4_t result;
|
||||
|
||||
result = kinc_int32x4_add(a, b);
|
||||
failed += check_i32("int32x4 add", result, (int32_t[4]){0, 1, 3, 4}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_sub(a, b);
|
||||
failed += check_i32("int32x4 sub", result, (int32_t[4]){-4, -3, -1, 0}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_max(a, b);
|
||||
failed += check_i32("int32x4 max", result, (int32_t[4]){2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_min(a, b);
|
||||
failed += check_i32("int32x4 min", result, (int32_t[4]){-2, -1, 1, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmpeq(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmpeq & sel", result, (int32_t[4]){2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmpge(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmpge & sel", result, (int32_t[4]){2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmpgt(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmpgt & sel", result, (int32_t[4]){2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmple(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmple & sel", result, (int32_t[4]){-2, -1, 1, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmplt(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmplt & sel", result, (int32_t[4]){-2, -1, 1, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_int32x4_cmpneq(a, b);
|
||||
result = kinc_int32x4_sel(a, b, mask);
|
||||
failed += check_i32("int32x4 cmpneq & sel", result, (int32_t[4]){-2, -1, 1, 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_or(a, b);
|
||||
failed += check_i32("int32x4 or", result, (int32_t[4]){-2 | 2, -1 | 2, 1 | 2, 2 | 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_and(a, b);
|
||||
failed += check_i32("int32x4 and", result, (int32_t[4]){-2 & 2, -1 & 2, 1 & 2, 2 & 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_xor(a, b);
|
||||
failed += check_i32("int32x4 xor", result, (int32_t[4]){-0x2 ^ 2, -1 ^ 2, 1 ^ 2, 0x2 ^ 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_int32x4_not(a);
|
||||
failed += check_i32("int32x4 not", result, (int32_t[4]){~-2, ~-1, ~1, ~2}) ? 0 : 1;
|
||||
}
|
||||
|
||||
{
|
||||
kinc_uint32x4_t a = kinc_uint32x4_load((uint32_t[4]){1, 2, 3, 4});
|
||||
kinc_uint32x4_t b = kinc_uint32x4_load_all(2);
|
||||
|
||||
kinc_uint32x4_mask_t mask;
|
||||
kinc_uint32x4_t result;
|
||||
|
||||
result = kinc_uint32x4_add(a, b);
|
||||
failed += check_u32("uint32x4 add", result, (uint32_t[4]){3, 4, 5, 6}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint32x4_sub(a, b);
|
||||
failed += check_u32("uint32x4 sub", result, (uint32_t[4]){4294967295, 0, 1, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint32x4_cmpeq(a, b);
|
||||
result = kinc_uint32x4_sel(a, b, mask);
|
||||
failed += check_u32("uint32x4 cmpeq & sel", result, (uint32_t[4]){2, 2, 2, 2}) ? 0 : 1;
|
||||
|
||||
mask = kinc_uint32x4_cmpneq(a, b);
|
||||
result = kinc_uint32x4_sel(a, b, mask);
|
||||
failed += check_u32("uint32x4 cmpneq & sel", result, (uint32_t[4]){1, 2, 3, 4}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint32x4_or(a, b);
|
||||
failed += check_u32("uint32x4 or", result, (uint32_t[4]){1 | 2, 2 | 2, 3 | 2, 4 | 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint32x4_and(a, b);
|
||||
failed += check_u32("uint32x4 and", result, (uint32_t[4]){1 & 2, 2 & 2, 3 & 2, 4 & 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint32x4_xor(a, b);
|
||||
failed += check_u32("uint32x4 xor", result, (uint32_t[4]){1 ^ 2, 0x2 ^ 2, 3 ^ 2, 4 ^ 2}) ? 0 : 1;
|
||||
|
||||
result = kinc_uint32x4_not(a);
|
||||
uint32_t chk[4] = {1, 2, 3, 4};
|
||||
for (int i = 0; i < 4; ++i) chk[i] = (uint32_t)(~chk[i]);
|
||||
failed += check_u32("uint32x4 not", result, chk) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "\nERROR! %d of %d test(s) failed", failed, total_tests);
|
||||
}
|
||||
else {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "\nSUCCESS %d tests passed", total_tests);
|
||||
}
|
||||
|
||||
return failed;
|
||||
}
|
10
Kha/Kinc/Tests/SIMD/kfile.js
Normal file
10
Kha/Kinc/Tests/SIMD/kfile.js
Normal file
@ -0,0 +1,10 @@
|
||||
let project = new Project('SIMD');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
0
Kha/Kinc/Tests/Shader-G5/Deployment/keepme
Normal file
0
Kha/Kinc/Tests/Shader-G5/Deployment/keepme
Normal file
7
Kha/Kinc/Tests/Shader-G5/Shaders/shader.frag.glsl
Normal file
7
Kha/Kinc/Tests/Shader-G5/Shaders/shader.frag.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
7
Kha/Kinc/Tests/Shader-G5/Shaders/shader.vert.glsl
Normal file
7
Kha/Kinc/Tests/Shader-G5/Shaders/shader.vert.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
in vec3 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos.x, pos.y, 0.5, 1.0);
|
||||
}
|
117
Kha/Kinc/Tests/Shader-G5/Sources/shader.c
Normal file
117
Kha/Kinc/Tests/Shader-G5/Sources/shader.c
Normal file
@ -0,0 +1,117 @@
|
||||
#include <kinc/global.h>
|
||||
#include <kinc/io/filereader.h>
|
||||
#include <kinc/graphics5/commandlist.h>
|
||||
#include <kinc/graphics5/graphics.h>
|
||||
#include <kinc/graphics5/pipeline.h>
|
||||
#include <kinc/graphics5/shader.h>
|
||||
#include <kinc/graphics5/vertexbuffer.h>
|
||||
#include <kinc/graphics5/indexbuffer.h>
|
||||
#include <kinc/system.h>
|
||||
#include <kinc/window.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define BUFFER_COUNT 2
|
||||
static int current_buffer = -1;
|
||||
static kinc_g5_render_target_t framebuffers[BUFFER_COUNT];
|
||||
static kinc_g5_command_list_t command_list;
|
||||
static kinc_g5_shader_t vertex_shader;
|
||||
static kinc_g5_shader_t fragment_shader;
|
||||
static kinc_g5_pipeline_t pipeline;
|
||||
static kinc_g5_vertex_buffer_t vertices;
|
||||
static kinc_g5_index_buffer_t indices;
|
||||
|
||||
#define HEAP_SIZE 1024 * 1024
|
||||
static uint8_t *heap = NULL;
|
||||
static size_t heap_top = 0;
|
||||
|
||||
static void *allocate(size_t size) {
|
||||
size_t old_top = heap_top;
|
||||
heap_top += size;
|
||||
assert(heap_top <= HEAP_SIZE);
|
||||
return &heap[old_top];
|
||||
}
|
||||
|
||||
static void update(void *data) {
|
||||
current_buffer = (current_buffer + 1) % BUFFER_COUNT;
|
||||
|
||||
kinc_g5_begin(&framebuffers[current_buffer], 0);
|
||||
|
||||
kinc_g5_command_list_begin(&command_list);
|
||||
kinc_g5_command_list_framebuffer_to_render_target_barrier(&command_list, &framebuffers[current_buffer]);
|
||||
kinc_g5_render_target_t *renderTargets[1] = {&framebuffers[current_buffer]};
|
||||
kinc_g5_command_list_set_render_targets(&command_list, renderTargets, 1);
|
||||
|
||||
kinc_g5_command_list_clear(&command_list, &framebuffers[current_buffer], KINC_G5_CLEAR_COLOR, 0, 0.0f, 0);
|
||||
kinc_g5_command_list_set_pipeline(&command_list, &pipeline);
|
||||
kinc_g5_command_list_set_pipeline_layout(&command_list);
|
||||
|
||||
int offsets[1] = { 0 };
|
||||
kinc_g5_vertex_buffer_t *vertex_buffers[1] = { &vertices };
|
||||
kinc_g5_command_list_set_vertex_buffers(&command_list, vertex_buffers, offsets, 1);
|
||||
kinc_g5_command_list_set_index_buffer(&command_list, &indices);
|
||||
kinc_g5_command_list_draw_indexed_vertices(&command_list);
|
||||
|
||||
kinc_g5_command_list_render_target_to_framebuffer_barrier(&command_list, &framebuffers[current_buffer]);
|
||||
kinc_g5_command_list_end(&command_list);
|
||||
kinc_g5_command_list_execute_and_wait(&command_list);
|
||||
|
||||
kinc_g5_end(0);
|
||||
kinc_g5_swap_buffers();
|
||||
}
|
||||
|
||||
static void load_shader(const char *filename, kinc_g5_shader_t *shader, kinc_g5_shader_type_t shader_type) {
|
||||
kinc_file_reader_t file;
|
||||
kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
|
||||
size_t data_size = kinc_file_reader_size(&file);
|
||||
uint8_t *data = allocate(data_size);
|
||||
kinc_file_reader_read(&file, data, data_size);
|
||||
kinc_file_reader_close(&file);
|
||||
kinc_g5_shader_init(shader, data, data_size, shader_type);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char** argv) {
|
||||
kinc_init("Shader", 1024, 768, NULL, NULL);
|
||||
kinc_set_update_callback(update, NULL);
|
||||
|
||||
heap = (uint8_t*)malloc(HEAP_SIZE);
|
||||
assert(heap != NULL);
|
||||
|
||||
load_shader("shader.vert", &vertex_shader, KINC_G5_SHADER_TYPE_VERTEX);
|
||||
load_shader("shader.frag", &fragment_shader, KINC_G5_SHADER_TYPE_FRAGMENT);
|
||||
|
||||
kinc_g5_vertex_structure_t structure;
|
||||
kinc_g4_vertex_structure_init(&structure);
|
||||
kinc_g4_vertex_structure_add(&structure, "pos", KINC_G4_VERTEX_DATA_FLOAT3);
|
||||
kinc_g5_pipeline_init(&pipeline);
|
||||
pipeline.vertexShader = &vertex_shader;
|
||||
pipeline.fragmentShader = &fragment_shader;
|
||||
pipeline.inputLayout[0] = &structure;
|
||||
pipeline.inputLayout[1] = NULL;
|
||||
kinc_g5_pipeline_compile(&pipeline);
|
||||
|
||||
kinc_g5_command_list_init(&command_list);
|
||||
for (int i = 0; i < BUFFER_COUNT; ++i) {
|
||||
kinc_g5_render_target_init(&framebuffers[i], kinc_window_width(0), kinc_window_height(0), 16, false, KINC_G5_RENDER_TARGET_FORMAT_32BIT, -1,
|
||||
-i - 1 /* hack in an index for backbuffer render targets */);
|
||||
}
|
||||
|
||||
kinc_g5_vertex_buffer_init(&vertices, 3, &structure, true, 0);
|
||||
float *v = kinc_g5_vertex_buffer_lock_all(&vertices);
|
||||
v[0] = -1; v[1] = -1; v[2] = 0.5;
|
||||
v[3] = 1; v[4] = -1; v[5] = 0.5;
|
||||
v[6] = -1; v[7] = 1; v[8] = 0.5;
|
||||
kinc_g5_vertex_buffer_unlock_all(&vertices);
|
||||
kinc_g5_command_list_upload_vertex_buffer(&command_list, &vertices);
|
||||
|
||||
kinc_g5_index_buffer_init(&indices, 3, KINC_G5_INDEX_BUFFER_FORMAT_32BIT, true);
|
||||
int *i = kinc_g5_index_buffer_lock_all(&indices);
|
||||
i[0] = 0; i[1] = 1; i[2] = 2;
|
||||
kinc_g5_index_buffer_unlock_all(&indices);
|
||||
kinc_g5_command_list_upload_index_buffer(&command_list, &indices);
|
||||
|
||||
kinc_start();
|
||||
|
||||
return 0;
|
||||
}
|
11
Kha/Kinc/Tests/Shader-G5/kfile.js
Normal file
11
Kha/Kinc/Tests/Shader-G5/kfile.js
Normal file
@ -0,0 +1,11 @@
|
||||
const project = new Project('Shader-G5');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.addFile('Shaders/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
1
Kha/Kinc/Tests/Shader/Deployment/keepme
Normal file
1
Kha/Kinc/Tests/Shader/Deployment/keepme
Normal file
@ -0,0 +1 @@
|
||||
Don't read me, just keep me.
|
7
Kha/Kinc/Tests/Shader/Shaders/shader.frag.glsl
Normal file
7
Kha/Kinc/Tests/Shader/Shaders/shader.frag.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
7
Kha/Kinc/Tests/Shader/Shaders/shader.vert.glsl
Normal file
7
Kha/Kinc/Tests/Shader/Shaders/shader.vert.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
in vec3 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos.x, pos.y, 0.5, 1.0);
|
||||
}
|
130
Kha/Kinc/Tests/Shader/Sources/shader.c
Normal file
130
Kha/Kinc/Tests/Shader/Sources/shader.c
Normal file
@ -0,0 +1,130 @@
|
||||
#include <kinc/graphics4/graphics.h>
|
||||
#include <kinc/graphics4/indexbuffer.h>
|
||||
#include <kinc/graphics4/pipeline.h>
|
||||
#include <kinc/graphics4/rendertarget.h>
|
||||
#include <kinc/graphics4/shader.h>
|
||||
#include <kinc/graphics4/vertexbuffer.h>
|
||||
#include <kinc/io/filereader.h>
|
||||
#include <kinc/system.h>
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image_write.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static kinc_g4_shader_t vertex_shader;
|
||||
static kinc_g4_shader_t fragment_shader;
|
||||
static kinc_g4_pipeline_t pipeline;
|
||||
static kinc_g4_vertex_buffer_t vertices;
|
||||
static kinc_g4_index_buffer_t indices;
|
||||
static kinc_g4_render_target_t render_target;
|
||||
|
||||
#define HEAP_SIZE 1024 * 1024
|
||||
static uint8_t *heap = NULL;
|
||||
static size_t heap_top = 0;
|
||||
|
||||
static void *allocate(size_t size) {
|
||||
size_t old_top = heap_top;
|
||||
heap_top += size;
|
||||
assert(heap_top <= HEAP_SIZE);
|
||||
return &heap[old_top];
|
||||
}
|
||||
|
||||
static void update(void *data) {
|
||||
kinc_g4_begin(0);
|
||||
|
||||
kinc_g4_render_target_t *render_targets = {&render_target};
|
||||
kinc_g4_set_render_targets(&render_targets, 1);
|
||||
|
||||
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0xFF000000, 0.0f, 0);
|
||||
kinc_g4_set_pipeline(&pipeline);
|
||||
kinc_g4_set_vertex_buffer(&vertices);
|
||||
kinc_g4_set_index_buffer(&indices);
|
||||
kinc_g4_draw_indexed_vertices();
|
||||
|
||||
kinc_g4_end(0);
|
||||
kinc_g4_swap_buffers();
|
||||
|
||||
uint8_t *pixels = (uint8_t *)malloc(2048 * 2048 * 4);
|
||||
kinc_g4_render_target_get_pixels(&render_target, pixels);
|
||||
if (kinc_g4_render_targets_inverted_y()) {
|
||||
uint8_t *inverted_pixels = (uint8_t *)malloc(2048 * 2048 * 4);
|
||||
for (int y = 0; y < 2048; ++y) {
|
||||
for (int x = 0; x < 2048; ++x) {
|
||||
for (int c = 0; c < 4; ++c) {
|
||||
inverted_pixels[y * 2048 * 4 + x * 4 + c] = pixels[(2047 - y) * 2048 * 4 + x * 4 + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
pixels = inverted_pixels;
|
||||
}
|
||||
stbi_write_png("test.png", 2048, 2048, 4, pixels, 2048 * 4);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static void load_shader(const char *filename, kinc_g4_shader_t *shader, kinc_g4_shader_type_t shader_type) {
|
||||
kinc_file_reader_t file;
|
||||
kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
|
||||
size_t data_size = kinc_file_reader_size(&file);
|
||||
uint8_t *data = allocate(data_size);
|
||||
kinc_file_reader_read(&file, data, data_size);
|
||||
kinc_file_reader_close(&file);
|
||||
kinc_g4_shader_init(shader, data, data_size, shader_type);
|
||||
}
|
||||
|
||||
int kickstart(int argc, char **argv) {
|
||||
kinc_init("Shader", 1024, 768, NULL, NULL);
|
||||
kinc_set_update_callback(update, NULL);
|
||||
|
||||
heap = (uint8_t *)malloc(HEAP_SIZE);
|
||||
assert(heap != NULL);
|
||||
|
||||
load_shader("shader.vert", &vertex_shader, KINC_G4_SHADER_TYPE_VERTEX);
|
||||
load_shader("shader.frag", &fragment_shader, KINC_G4_SHADER_TYPE_FRAGMENT);
|
||||
|
||||
kinc_g4_vertex_structure_t structure;
|
||||
kinc_g4_vertex_structure_init(&structure);
|
||||
kinc_g4_vertex_structure_add(&structure, "pos", KINC_G4_VERTEX_DATA_F32_3X);
|
||||
kinc_g4_pipeline_init(&pipeline);
|
||||
pipeline.vertex_shader = &vertex_shader;
|
||||
pipeline.fragment_shader = &fragment_shader;
|
||||
pipeline.input_layout[0] = &structure;
|
||||
pipeline.input_layout[1] = NULL;
|
||||
kinc_g4_pipeline_compile(&pipeline);
|
||||
|
||||
kinc_g4_vertex_buffer_init(&vertices, 3, &structure, KINC_G4_USAGE_STATIC, 0);
|
||||
{
|
||||
float *v = kinc_g4_vertex_buffer_lock_all(&vertices);
|
||||
int i = 0;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = 1;
|
||||
v[i++] = -1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
v[i++] = -1;
|
||||
v[i++] = 1;
|
||||
v[i++] = 0.5;
|
||||
|
||||
kinc_g4_vertex_buffer_unlock_all(&vertices);
|
||||
}
|
||||
|
||||
kinc_g4_index_buffer_init(&indices, 3, KINC_G4_INDEX_BUFFER_FORMAT_16BIT, KINC_G4_USAGE_STATIC);
|
||||
{
|
||||
uint16_t *i = (uint16_t *)kinc_g4_index_buffer_lock_all(&indices);
|
||||
i[0] = 0;
|
||||
i[1] = 1;
|
||||
i[2] = 2;
|
||||
kinc_g4_index_buffer_unlock_all(&indices);
|
||||
}
|
||||
|
||||
kinc_g4_render_target_init(&render_target, 2048, 2048, KINC_G4_RENDER_TARGET_FORMAT_32BIT, 16, 0);
|
||||
|
||||
kinc_start();
|
||||
|
||||
return 0;
|
||||
}
|
1759
Kha/Kinc/Tests/Shader/Sources/stb_image_write.h
Normal file
1759
Kha/Kinc/Tests/Shader/Sources/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load Diff
11
Kha/Kinc/Tests/Shader/kfile.js
Normal file
11
Kha/Kinc/Tests/Shader/kfile.js
Normal file
@ -0,0 +1,11 @@
|
||||
const project = new Project('ShaderTest');
|
||||
|
||||
await project.addProject('../../');
|
||||
|
||||
project.addFile('Sources/**');
|
||||
project.addFile('Shaders/**');
|
||||
project.setDebugDir('Deployment');
|
||||
|
||||
project.flatten();
|
||||
|
||||
resolve(project);
|
BIN
Kha/Kinc/Tests/Shader/reference.png
Normal file
BIN
Kha/Kinc/Tests/Shader/reference.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
Reference in New Issue
Block a user