Kinc
Kinc copied to clipboard
D3D11 de-interleaved buffers broken
I think it's broken since the https://github.com/Kode/Kore/commit/9253b0eb7423f5ae6e74da432e86fc9f7ed6e1e4, where the ugly interleavedLayout flag got cleaned up. Will think of a nicer solution eventually to fix this.
I wanted to give it a try, but i'm not sure about the intended api. I'm doing dynamic updates at every frames of all the vertex components so I don't even know if it could make any sense, but was interesting to check.
Hey, this works for me. We are talking about something like
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics4/pipeline.h>
#include <kinc/graphics4/shader.h>
#include <kinc/graphics4/texture.h>
#include <kinc/graphics4/vertexbuffer.h>
#include <kinc/image.h>
#include <kinc/io/filereader.h>
#include <kinc/system.h>
#include <assert.h>
#include <stdlib.h>
static kinc_g4_shader_t vertexShader;
static kinc_g4_shader_t fragmentShader;
static kinc_g4_pipeline_t pipeline;
static kinc_g4_vertex_buffer_t pos_vertices;
static kinc_g4_vertex_buffer_t tex_vertices;
static kinc_g4_index_buffer_t indices;
static kinc_g4_texture_t texture;
static kinc_g4_texture_unit_t texunit;
static kinc_g4_constant_location_t offset;
#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) {
kinc_g4_begin(0);
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0, 0.0f, 0);
kinc_g4_set_pipeline(&pipeline);
kinc_matrix3x3_t matrix = kinc_matrix3x3_rotation_z((float)kinc_time());
kinc_g4_set_matrix3(offset, &matrix);
kinc_g4_vertex_buffer_t *vertices[2] = {&pos_vertices, &tex_vertices};
kinc_g4_set_vertex_buffers(vertices, 2);
kinc_g4_set_index_buffer(&indices);
kinc_g4_set_texture(texunit, &texture);
kinc_g4_draw_indexed_vertices();
kinc_g4_end(0);
kinc_g4_swap_buffers();
}
int kickstart(int argc, char **argv) {
kinc_init("TextureTest", 1024, 768, NULL, NULL);
kinc_set_update_callback(update);
heap = (uint8_t *)malloc(HEAP_SIZE);
assert(heap != NULL);
{
kinc_image_t image;
void *image_mem = allocate(250 * 250 * 4);
kinc_image_init_from_file(&image, image_mem, "parrot.png");
kinc_g4_texture_init_from_image(&texture, &image);
kinc_image_destroy(&image);
}
{
kinc_file_reader_t reader;
kinc_file_reader_open(&reader, "texture.vert", KINC_FILE_TYPE_ASSET);
size_t size = kinc_file_reader_size(&reader);
uint8_t *data = allocate(size);
kinc_file_reader_read(&reader, data, size);
kinc_file_reader_close(&reader);
kinc_g4_shader_init(&vertexShader, data, size, KINC_G4_SHADER_TYPE_VERTEX);
}
{
kinc_file_reader_t reader;
kinc_file_reader_open(&reader, "texture.frag", KINC_FILE_TYPE_ASSET);
size_t size = kinc_file_reader_size(&reader);
uint8_t *data = allocate(size);
kinc_file_reader_read(&reader, data, size);
kinc_file_reader_close(&reader);
kinc_g4_shader_init(&fragmentShader, data, size, KINC_G4_SHADER_TYPE_FRAGMENT);
}
kinc_g4_vertex_structure_t pos_structure;
kinc_g4_vertex_structure_init(&pos_structure);
kinc_g4_vertex_structure_add(&pos_structure, "pos", KINC_G4_VERTEX_DATA_FLOAT3);
kinc_g4_vertex_structure_t tex_structure;
kinc_g4_vertex_structure_init(&tex_structure);
kinc_g4_vertex_structure_add(&tex_structure, "tex", KINC_G4_VERTEX_DATA_FLOAT2);
kinc_g4_pipeline_init(&pipeline);
pipeline.input_layout[0] = &pos_structure;
pipeline.input_layout[1] = &tex_structure;
pipeline.input_layout[2] = NULL;
pipeline.vertex_shader = &vertexShader;
pipeline.fragment_shader = &fragmentShader;
kinc_g4_pipeline_compile(&pipeline);
texunit = kinc_g4_pipeline_get_texture_unit(&pipeline, "texsampler");
offset = kinc_g4_pipeline_get_constant_location(&pipeline, "mvp");
{
kinc_g4_vertex_buffer_init(&pos_vertices, 3, &pos_structure, KINC_G4_USAGE_STATIC, 0);
float *v = kinc_g4_vertex_buffer_lock_all(&pos_vertices);
v[0] = -1.0f;
v[1] = -1.0f;
v[2] = 0.5f;
v[3] = 1.0f;
v[4] = -1.0f;
v[5] = 0.5f;
v[6] = -1.0f;
v[7] = 1.0f;
v[8] = 0.5f;
kinc_g4_vertex_buffer_unlock_all(&pos_vertices);
}
{
kinc_g4_vertex_buffer_init(&tex_vertices, 3, &tex_structure, KINC_G4_USAGE_STATIC, 0);
float *v = kinc_g4_vertex_buffer_lock_all(&tex_vertices);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 1.0f;
v[3] = 1.0f;
v[4] = 0.0f;
v[5] = 0.0f;
kinc_g4_vertex_buffer_unlock_all(&tex_vertices);
}
kinc_g4_index_buffer_init(&indices, 3, KINC_G4_INDEX_BUFFER_FORMAT_32BIT, KINC_G4_USAGE_STATIC);
int *i = kinc_g4_index_buffer_lock(&indices);
i[0] = 0;
i[1] = 1;
i[2] = 2;
kinc_g4_index_buffer_unlock(&indices);
kinc_start();
return 0;
}
right?