Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,944 @@
#include "G4.h"
#include "kinc/graphics4/graphics.h"
#include "kinc/window.h"
#include <kinc/backend/graphics4/indexbuffer.h>
#include <kinc/backend/graphics4/pipeline.h>
#include <kinc/backend/graphics4/vertexbuffer.h>
#include <kinc/color.h>
#include <kinc/graphics4/compute.h>
#include <kinc/graphics4/constantbuffer.h>
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics4/pipeline.h>
#include <kinc/graphics4/rendertarget.h>
#include <kinc/graphics4/shader.h>
#include <kinc/graphics4/texture.h>
#include <kinc/graphics4/texturearray.h>
#include <kinc/graphics4/vertexbuffer.h>
#include <kinc/graphics5/commandlist.h>
#include <kinc/graphics5/compute.h>
#include <kinc/graphics5/constantbuffer.h>
#include <kinc/graphics5/graphics.h>
#include <kinc/graphics5/sampler.h>
#include <kinc/io/filereader.h>
#include <kinc/math/core.h>
#include <kinc/math/matrix.h>
#include <kinc/system.h>
#include <assert.h>
#include <string.h>
kinc_g5_command_list_t commandList;
uint64_t frameNumber = 0;
bool waitAfterNextDraw = false;
static kinc_g5_constant_buffer_t vertexConstantBuffer;
static kinc_g5_constant_buffer_t fragmentConstantBuffer;
static kinc_g5_constant_buffer_t computeConstantBuffer;
#define constantBufferSize 4096
#define constantBufferMultiply 100
static int constantBufferIndex = 0;
void kinc_g4_internal_init(void) {
kinc_g5_internal_init();
}
void kinc_g4_internal_destroy(void) {
kinc_g5_internal_destroy();
}
void kinc_g4_internal_destroy_window(int window) {
kinc_g5_internal_destroy_window(window);
}
#define bufferCount 2
#define renderTargetCount 8
static struct {
int currentBuffer;
kinc_g5_render_target_t framebuffers[bufferCount];
kinc_g4_render_target_t *current_render_targets[renderTargetCount];
int current_render_target_count;
bool resized;
} windows[16] = {0};
static int current_window;
#define MAX_VERTEX_BUFFERS 16
#define MAX_TEXTURES 16
typedef struct render_state {
kinc_g5_pipeline_t *pipeline;
kinc_g5_compute_shader *compute_shader;
kinc_g5_index_buffer_t *index_buffer;
kinc_g5_vertex_buffer_t *vertex_buffers[MAX_VERTEX_BUFFERS];
int vertex_buffer_offsets[MAX_VERTEX_BUFFERS];
int vertex_buffer_count;
bool blend_constant_set;
float blend_constant_r;
float blend_constant_g;
float blend_constant_b;
float blend_constant_a;
bool viewport_set;
int viewport_x;
int viewport_y;
int viewport_width;
int viewport_height;
bool scissor_set;
int scissor_x;
int scissor_y;
int scissor_width;
int scissor_height;
kinc_g5_texture_t *textures[MAX_TEXTURES];
kinc_g5_texture_unit_t texture_units[MAX_TEXTURES];
int texture_count;
kinc_g5_render_target_t *render_targets[MAX_TEXTURES];
kinc_g5_texture_unit_t render_target_units[MAX_TEXTURES];
int render_target_count;
kinc_g5_render_target_t *depth_render_targets[MAX_TEXTURES];
kinc_g5_texture_unit_t depth_render_target_units[MAX_TEXTURES];
int depth_render_target_count;
uint8_t vertex_constant_data[constantBufferSize];
uint8_t fragment_constant_data[constantBufferSize];
uint8_t compute_constant_data[constantBufferSize];
} render_state;
static render_state current_state;
void kinc_g4_on_g5_internal_resize(int window, int width, int height) {
windows[window].resized = true;
}
void kinc_g4_on_g5_internal_restore_render_target(void) {
windows[current_window].current_render_targets[0] = NULL;
kinc_g5_render_target_t *render_target = &windows[current_window].framebuffers[windows[current_window].currentBuffer];
kinc_g5_command_list_set_render_targets(&commandList, &render_target, 1);
windows[current_window].current_render_target_count = 1;
}
void kinc_g4_internal_init_window(int window, int depthBufferBits, int stencilBufferBits, bool vsync) {
kinc_g5_internal_init_window(window, depthBufferBits, stencilBufferBits, vsync);
kinc_g5_command_list_init(&commandList);
windows[window].currentBuffer = -1;
for (int i = 0; i < bufferCount; ++i) {
kinc_g5_render_target_init_framebuffer(&windows[window].framebuffers[i], kinc_window_width(window), kinc_window_height(window),
KINC_G5_RENDER_TARGET_FORMAT_32BIT, depthBufferBits, 0);
}
kinc_g5_constant_buffer_init(&vertexConstantBuffer, constantBufferSize * constantBufferMultiply);
kinc_g5_constant_buffer_init(&fragmentConstantBuffer, constantBufferSize * constantBufferMultiply);
kinc_g5_constant_buffer_init(&computeConstantBuffer, constantBufferSize * constantBufferMultiply);
// to support doing work after kinc_g4_end and before kinc_g4_begin
kinc_g5_command_list_begin(&commandList);
}
void kinc_g4_on_g5_internal_set_samplers(int count, kinc_g5_texture_unit_t *texture_units) {
for (int i = 0; i < count; ++i) {
for (int j = 0; j < KINC_G5_SHADER_TYPE_COUNT; ++j) {
if (texture_units[i].stages[j] >= 0) {
kinc_g5_sampler_t *sampler = get_current_sampler(j, texture_units[i].stages[j]);
kinc_g5_texture_unit_t unit;
for (int k = 0; k < KINC_G5_SHADER_TYPE_COUNT; ++k) {
unit.stages[k] = -1;
}
unit.stages[j] = texture_units[i].stages[j];
kinc_g5_command_list_set_sampler(&commandList, unit, sampler);
}
}
}
}
static void startDraw(bool compute) {
if ((constantBufferIndex + 1) >= constantBufferMultiply || waitAfterNextDraw) {
memcpy(current_state.vertex_constant_data, vertexConstantBuffer.data, constantBufferSize);
memcpy(current_state.fragment_constant_data, fragmentConstantBuffer.data, constantBufferSize);
memcpy(current_state.compute_constant_data, computeConstantBuffer.data, constantBufferSize);
}
kinc_g5_constant_buffer_unlock(&vertexConstantBuffer);
kinc_g5_constant_buffer_unlock(&fragmentConstantBuffer);
kinc_g5_constant_buffer_unlock(&computeConstantBuffer);
kinc_g4_on_g5_internal_set_samplers(current_state.texture_count, current_state.texture_units);
kinc_g4_on_g5_internal_set_samplers(current_state.render_target_count, current_state.render_target_units);
kinc_g4_on_g5_internal_set_samplers(current_state.depth_render_target_count, current_state.depth_render_target_units);
if (compute) {
kinc_g5_command_list_set_compute_constant_buffer(&commandList, &computeConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
}
else {
kinc_g5_command_list_set_vertex_constant_buffer(&commandList, &vertexConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
kinc_g5_command_list_set_fragment_constant_buffer(&commandList, &fragmentConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
}
}
static void endDraw(bool compute) {
++constantBufferIndex;
if (constantBufferIndex >= constantBufferMultiply || waitAfterNextDraw) {
kinc_g5_command_list_end(&commandList);
kinc_g5_command_list_execute(&commandList);
kinc_g5_command_list_wait_for_execution_to_finish(&commandList);
kinc_g5_command_list_begin(&commandList);
if (windows[current_window].current_render_targets[0] == NULL) {
kinc_g4_on_g5_internal_restore_render_target();
}
else {
const int count = windows[current_window].current_render_target_count;
kinc_g5_render_target_t *render_targets[16];
for (int i = 0; i < count; ++i) {
render_targets[i] = &windows[current_window].current_render_targets[i]->impl._renderTarget;
}
kinc_g5_command_list_set_render_targets(&commandList, render_targets, count);
}
if (current_state.pipeline != NULL) {
kinc_g5_command_list_set_pipeline(&commandList, current_state.pipeline);
}
if (current_state.compute_shader != NULL) {
#ifndef KINC_METAL
// Metal still has some trouble switching between graphics and compute encoders
kinc_g5_command_list_set_compute_shader(&commandList, current_state.compute_shader);
#endif
}
if (current_state.index_buffer != NULL) {
kinc_g5_command_list_set_index_buffer(&commandList, current_state.index_buffer);
}
if (current_state.vertex_buffer_count > 0) {
kinc_g5_command_list_set_vertex_buffers(&commandList, current_state.vertex_buffers, current_state.vertex_buffer_offsets,
current_state.vertex_buffer_count);
}
if (current_state.blend_constant_set) {
kinc_g5_command_list_set_blend_constant(&commandList, current_state.blend_constant_r, current_state.blend_constant_g,
current_state.blend_constant_b, current_state.blend_constant_a);
}
if (current_state.viewport_set) {
kinc_g5_command_list_viewport(&commandList, current_state.viewport_x, current_state.viewport_y, current_state.viewport_width,
current_state.viewport_height);
}
if (current_state.scissor_set) {
kinc_g5_command_list_scissor(&commandList, current_state.scissor_x, current_state.scissor_y, current_state.scissor_width,
current_state.scissor_height);
}
for (int i = 0; i < current_state.texture_count; ++i) {
kinc_g5_command_list_set_texture(&commandList, current_state.texture_units[i], current_state.textures[i]);
}
for (int i = 0; i < current_state.render_target_count; ++i) {
kinc_g5_command_list_set_texture_from_render_target(&commandList, current_state.render_target_units[i], current_state.render_targets[i]);
}
for (int i = 0; i < current_state.depth_render_target_count; ++i) {
kinc_g5_command_list_set_texture_from_render_target_depth(&commandList, current_state.depth_render_target_units[i],
current_state.depth_render_targets[i]);
}
constantBufferIndex = 0;
waitAfterNextDraw = false;
kinc_g5_constant_buffer_lock(&vertexConstantBuffer, 0, constantBufferSize);
kinc_g5_constant_buffer_lock(&fragmentConstantBuffer, 0, constantBufferSize);
kinc_g5_constant_buffer_lock(&computeConstantBuffer, 0, constantBufferSize);
memcpy(vertexConstantBuffer.data, current_state.vertex_constant_data, constantBufferSize);
memcpy(fragmentConstantBuffer.data, current_state.fragment_constant_data, constantBufferSize);
memcpy(computeConstantBuffer.data, current_state.compute_constant_data, constantBufferSize);
}
else {
kinc_g5_constant_buffer_lock(&vertexConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
kinc_g5_constant_buffer_lock(&fragmentConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
kinc_g5_constant_buffer_lock(&computeConstantBuffer, constantBufferIndex * constantBufferSize, constantBufferSize);
}
}
void kinc_g4_draw_indexed_vertices(void) {
startDraw(false);
kinc_g5_command_list_draw_indexed_vertices(&commandList);
endDraw(false);
}
void kinc_g4_draw_indexed_vertices_from_to(int start, int count) {
startDraw(false);
kinc_g5_command_list_draw_indexed_vertices_from_to(&commandList, start, count);
endDraw(false);
}
void kinc_g4_draw_indexed_vertices_from_to_from(int start, int count, int vertex_offset) {
startDraw(false);
kinc_g5_command_list_draw_indexed_vertices_from_to_from(&commandList, start, count, vertex_offset);
endDraw(false);
}
void kinc_g4_draw_indexed_vertices_instanced(int instanceCount) {
startDraw(false);
kinc_g5_command_list_draw_indexed_vertices_instanced(&commandList, instanceCount);
endDraw(false);
}
void kinc_g4_draw_indexed_vertices_instanced_from_to(int instanceCount, int start, int count) {
startDraw(false);
kinc_g5_command_list_draw_indexed_vertices_instanced_from_to(&commandList, instanceCount, start, count);
endDraw(false);
}
void kinc_g4_clear(unsigned flags, unsigned color, float depth, int stencil) {
if (windows[current_window].current_render_target_count > 0) {
if (windows[current_window].current_render_targets[0] == NULL) {
kinc_g5_command_list_clear(&commandList, &windows[current_window].framebuffers[windows[current_window].currentBuffer], flags, color, depth,
stencil);
}
else {
if (windows[current_window].current_render_targets[0]->impl.state != KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) {
kinc_g5_command_list_texture_to_render_target_barrier(&commandList, &windows[current_window].current_render_targets[0]->impl._renderTarget);
windows[current_window].current_render_targets[0]->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}
kinc_g5_command_list_clear(&commandList, &windows[current_window].current_render_targets[0]->impl._renderTarget, flags, color, depth, stencil);
}
}
}
bool first_run = true;
void kinc_g4_begin(int window) {
// to support doing work after kinc_g4_end and before kinc_g4_begin
kinc_g5_command_list_end(&commandList);
kinc_g5_command_list_execute(&commandList);
current_window = window;
windows[current_window].currentBuffer = (windows[current_window].currentBuffer + 1) % bufferCount;
bool resized = windows[window].resized;
if (resized) {
for (int i = 0; i < bufferCount; ++i) {
kinc_g5_render_target_destroy(&windows[current_window].framebuffers[i]);
}
windows[current_window].currentBuffer = 0;
}
kinc_g5_begin(&windows[current_window].framebuffers[windows[current_window].currentBuffer], window);
if (resized) {
for (int i = 0; i < bufferCount; ++i) {
kinc_g5_render_target_init_framebuffer(&windows[current_window].framebuffers[i], kinc_window_width(window), kinc_window_height(window),
KINC_G5_RENDER_TARGET_FORMAT_32BIT, 16, 0);
}
windows[window].resized = false;
}
windows[current_window].current_render_targets[0] = NULL;
windows[current_window].current_render_target_count = 1;
current_state.pipeline = NULL;
current_state.compute_shader = NULL;
current_state.index_buffer = NULL;
for (int i = 0; i < MAX_VERTEX_BUFFERS; ++i) {
current_state.vertex_buffers[i] = NULL;
current_state.vertex_buffer_offsets[i] = 0;
}
current_state.vertex_buffer_count = 0;
current_state.blend_constant_set = false;
current_state.viewport_set = false;
current_state.scissor_set = false;
current_state.texture_count = 0;
current_state.render_target_count = 0;
current_state.depth_render_target_count = 0;
samplers_reset();
// commandList = new Graphics5::CommandList;
kinc_g5_command_list_begin(&commandList);
// Currently we do not necessarily wait at the end of a frame so for now it's endDraw
// constantBufferIndex = 0;
// kinc_g5_constant_buffer_lock(&vertexConstantBuffer, 0, constantBufferSize);
// kinc_g5_constant_buffer_lock(&fragmentConstantBuffer, 0, constantBufferSize);
endDraw(false);
kinc_g5_command_list_framebuffer_to_render_target_barrier(&commandList, &windows[current_window].framebuffers[windows[current_window].currentBuffer]);
kinc_g4_restore_render_target();
++frameNumber;
}
void kinc_g4_viewport(int x, int y, int width, int height) {
current_state.viewport_x = x;
current_state.viewport_y = y;
current_state.viewport_width = width;
current_state.viewport_height = height;
current_state.viewport_set = true;
kinc_g5_command_list_viewport(&commandList, x, y, width, height);
}
void kinc_g4_scissor(int x, int y, int width, int height) {
current_state.scissor_x = x;
current_state.scissor_y = y;
current_state.scissor_width = width;
current_state.scissor_height = height;
current_state.scissor_set = true;
kinc_g5_command_list_scissor(&commandList, x, y, width, height);
}
void kinc_g4_disable_scissor(void) {
current_state.scissor_set = false;
kinc_g5_command_list_disable_scissor(&commandList);
}
void kinc_g4_end(int window) {
kinc_g5_constant_buffer_unlock(&vertexConstantBuffer);
kinc_g5_constant_buffer_unlock(&fragmentConstantBuffer);
kinc_g5_constant_buffer_unlock(&computeConstantBuffer);
kinc_g5_command_list_render_target_to_framebuffer_barrier(&commandList, &windows[current_window].framebuffers[windows[current_window].currentBuffer]);
kinc_g5_command_list_end(&commandList);
kinc_g5_command_list_execute(&commandList);
// delete commandList;
// commandList = nullptr;
kinc_g5_end(window);
// to support doing work after kinc_g4_end and before kinc_g4_begin
kinc_g5_command_list_begin(&commandList);
}
/*void Graphics4::_changeFramebuffer(int window, Kore::FramebufferOptions* frame) {
}*/
bool kinc_g4_swap_buffers(void) {
return kinc_g5_swap_buffers();
}
void kinc_g4_flush(void) {
kinc_g5_flush();
}
void kinc_g4_set_stencil_reference_value(int value) {}
void kinc_g4_set_int(kinc_g4_constant_location_t location, int value) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_int(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_int(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_int(&computeConstantBuffer, location.impl._location.impl.computeOffset, value);
}
void kinc_g4_set_int2(kinc_g4_constant_location_t location, int value1, int value2) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_int2(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_int2(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_int2(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2);
}
void kinc_g4_set_int3(kinc_g4_constant_location_t location, int value1, int value2, int value3) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_int3(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2, value3);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_int3(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2, value3);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_int3(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2, value3);
}
void kinc_g4_set_int4(kinc_g4_constant_location_t location, int value1, int value2, int value3, int value4) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_int4(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2, value3, value4);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_int4(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2, value3, value4);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_int4(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2, value3, value4);
}
void kinc_g4_set_ints(kinc_g4_constant_location_t location, int *values, int count) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_ints(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, values, count);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_ints(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, values, count);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_ints(&computeConstantBuffer, location.impl._location.impl.computeOffset, values, count);
}
void kinc_g4_set_float(kinc_g4_constant_location_t location, float value) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_float(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_float(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_float(&computeConstantBuffer, location.impl._location.impl.computeOffset, value);
}
void kinc_g4_set_float2(kinc_g4_constant_location_t location, float value1, float value2) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_float2(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_float2(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_float2(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2);
}
void kinc_g4_set_float3(kinc_g4_constant_location_t location, float value1, float value2, float value3) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_float3(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2, value3);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_float3(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2, value3);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_float3(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2, value3);
}
void kinc_g4_set_float4(kinc_g4_constant_location_t location, float value1, float value2, float value3, float value4) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_float4(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value1, value2, value3, value4);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_float4(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value1, value2, value3, value4);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_float4(&computeConstantBuffer, location.impl._location.impl.computeOffset, value1, value2, value3, value4);
}
void kinc_g4_set_floats(kinc_g4_constant_location_t location, float *values, int count) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_floats(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, values, count);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_floats(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, values, count);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_floats(&computeConstantBuffer, location.impl._location.impl.computeOffset, values, count);
}
void kinc_g4_set_bool(kinc_g4_constant_location_t location, bool value) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_bool(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_bool(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_bool(&computeConstantBuffer, location.impl._location.impl.computeOffset, value);
}
void kinc_g4_set_matrix4(kinc_g4_constant_location_t location, kinc_matrix4x4_t *value) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_matrix4(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_matrix4(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_matrix4(&computeConstantBuffer, location.impl._location.impl.computeOffset, value);
}
void kinc_g4_set_matrix3(kinc_g4_constant_location_t location, kinc_matrix3x3_t *value) {
if (location.impl._location.impl.vertexOffset >= 0)
kinc_g5_constant_buffer_set_matrix3(&vertexConstantBuffer, location.impl._location.impl.vertexOffset, value);
if (location.impl._location.impl.fragmentOffset >= 0)
kinc_g5_constant_buffer_set_matrix3(&fragmentConstantBuffer, location.impl._location.impl.fragmentOffset, value);
if (location.impl._location.impl.computeOffset >= 0)
kinc_g5_constant_buffer_set_matrix3(&computeConstantBuffer, location.impl._location.impl.computeOffset, value);
}
void kinc_g4_set_texture_addressing(kinc_g4_texture_unit_t unit, kinc_g4_texture_direction_t dir, kinc_g4_texture_addressing_t addressing) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (unit.stages[i] >= 0) {
if (dir == KINC_G4_TEXTURE_DIRECTION_U) {
sampler_options[i][unit.stages[i]].u_addressing = (kinc_g5_texture_addressing_t)addressing;
}
if (dir == KINC_G4_TEXTURE_DIRECTION_V) {
sampler_options[i][unit.stages[i]].v_addressing = (kinc_g5_texture_addressing_t)addressing;
}
if (dir == KINC_G4_TEXTURE_DIRECTION_W) {
sampler_options[i][unit.stages[i]].w_addressing = (kinc_g5_texture_addressing_t)addressing;
}
}
}
}
void kinc_g4_set_texture3d_addressing(kinc_g4_texture_unit_t unit, kinc_g4_texture_direction_t dir, kinc_g4_texture_addressing_t addressing) {
kinc_g4_set_texture_addressing(unit, dir, addressing);
}
void kinc_g4_set_texture_magnification_filter(kinc_g4_texture_unit_t texunit, kinc_g4_texture_filter_t filter) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (texunit.stages[i] >= 0) {
sampler_options[i][texunit.stages[i]].magnification_filter = (kinc_g5_texture_filter_t)filter;
}
}
}
void kinc_g4_set_texture3d_magnification_filter(kinc_g4_texture_unit_t texunit, kinc_g4_texture_filter_t filter) {
kinc_g4_set_texture_magnification_filter(texunit, filter);
}
void kinc_g4_set_texture_minification_filter(kinc_g4_texture_unit_t texunit, kinc_g4_texture_filter_t filter) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (texunit.stages[i] >= 0) {
sampler_options[i][texunit.stages[i]].minification_filter = (kinc_g5_texture_filter_t)filter;
}
}
}
void kinc_g4_set_texture3d_minification_filter(kinc_g4_texture_unit_t texunit, kinc_g4_texture_filter_t filter) {
kinc_g4_set_texture_minification_filter(texunit, filter);
}
void kinc_g4_set_texture_mipmap_filter(kinc_g4_texture_unit_t texunit, kinc_g4_mipmap_filter_t filter) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (texunit.stages[i] >= 0) {
sampler_options[i][texunit.stages[i]].mipmap_filter = (kinc_g5_mipmap_filter_t)filter;
}
}
}
void kinc_g4_set_texture3d_mipmap_filter(kinc_g4_texture_unit_t texunit, kinc_g4_mipmap_filter_t filter) {
kinc_g4_set_texture_mipmap_filter(texunit, filter);
}
void kinc_g4_set_texture_compare_mode(kinc_g4_texture_unit_t unit, bool enabled) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (unit.stages[i] >= 0) {
sampler_options[i][unit.stages[i]].is_comparison = enabled;
}
}
}
void kinc_g4_set_texture_compare_func(kinc_g4_texture_unit_t unit, kinc_g4_compare_mode_t mode) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (unit.stages[i] >= 0) {
sampler_options[i][unit.stages[i]].compare_mode = (kinc_g5_compare_mode_t)mode;
}
}
}
void kinc_g4_set_cubemap_compare_mode(kinc_g4_texture_unit_t unit, bool enabled) {}
void kinc_g4_set_cubemap_compare_func(kinc_g4_texture_unit_t unit, kinc_g4_compare_mode_t mode) {}
void kinc_g4_set_texture_max_anisotropy(kinc_g4_texture_unit_t unit, uint16_t max_anisotropy) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (unit.stages[i] >= 0) {
sampler_options[i][unit.stages[i]].max_anisotropy = max_anisotropy;
}
}
}
void kinc_g4_set_cubemap_max_anisotropy(kinc_g4_texture_unit_t unit, uint16_t max_anisotropy) {}
void kinc_g4_set_texture_lod(kinc_g4_texture_unit_t unit, float lod_min_clamp, float lod_max_clamp) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
if (unit.stages[i] >= 0) {
sampler_options[i][unit.stages[i]].lod_min_clamp = lod_min_clamp;
sampler_options[i][unit.stages[i]].lod_max_clamp = lod_max_clamp;
}
}
}
void kinc_g4_set_cubemap_lod(kinc_g4_texture_unit_t unit, float lod_min_clamp, float lod_max_clamp) {}
void kinc_g4_restore_render_target(void) {
kinc_g4_on_g5_internal_restore_render_target();
current_state.viewport_set = false;
current_state.scissor_set = false;
}
void kinc_g4_set_render_targets(kinc_g4_render_target_t **targets, int count) {
for (int i = 0; i < count; ++i) {
windows[current_window].current_render_targets[i] = targets[i];
if (windows[current_window].current_render_targets[i]->impl.state != KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) {
kinc_g5_command_list_texture_to_render_target_barrier(&commandList, &windows[current_window].current_render_targets[i]->impl._renderTarget);
windows[current_window].current_render_targets[i]->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}
}
windows[current_window].current_render_target_count = count;
kinc_g5_render_target_t *render_targets[16];
assert(count <= 16);
for (int i = 0; i < count; ++i) {
render_targets[i] = &targets[i]->impl._renderTarget;
}
kinc_g5_command_list_set_render_targets(&commandList, render_targets, count);
current_state.viewport_set = false;
current_state.scissor_set = false;
}
void kinc_g4_set_render_target_face(kinc_g4_render_target_t *texture, int face) {
kinc_g5_command_list_set_render_target_face(&commandList, &texture->impl._renderTarget, face);
current_state.viewport_set = false;
current_state.scissor_set = false;
}
void kinc_g4_set_vertex_buffers(kinc_g4_vertex_buffer_t **buffers, int count) {
assert(count <= MAX_VERTEX_BUFFERS);
for (int i = 0; i < count; ++i) {
current_state.vertex_buffers[i] = &buffers[i]->impl._buffer;
int index = buffers[i]->impl._currentIndex;
current_state.vertex_buffer_offsets[i] = index * kinc_g4_vertex_buffer_count(buffers[i]);
}
current_state.vertex_buffer_count = count;
kinc_g5_command_list_set_vertex_buffers(&commandList, current_state.vertex_buffers, current_state.vertex_buffer_offsets, count);
}
int kinc_internal_g4_vertex_buffer_set(kinc_g4_vertex_buffer_t *buffer, int offset) {
kinc_g4_vertex_buffer_t *buffers[1];
buffers[0] = buffer;
kinc_g4_set_vertex_buffers(buffers, 1);
return 0;
}
void kinc_g4_set_index_buffer(kinc_g4_index_buffer_t *buffer) {
kinc_g5_index_buffer_t *g5_index_buffer = &buffer->impl._buffer;
current_state.index_buffer = g5_index_buffer;
kinc_g5_command_list_set_index_buffer(&commandList, g5_index_buffer);
}
#ifdef KINC_KONG
void kinc_g4_set_texture(uint32_t unit, kinc_g4_texture_t *texture) {
if (!texture->impl._uploaded) {
kinc_g5_command_list_upload_texture(&commandList, &texture->impl._texture);
texture->impl._uploaded = true;
}
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit = {0};
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
g5_unit.stages[i] = unit;
}
bool found = false;
for (int i = 0; i < current_state.texture_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.texture_units[i], &g5_unit)) {
current_state.textures[i] = &texture->impl._texture;
current_state.texture_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.texture_count < MAX_TEXTURES);
current_state.textures[current_state.texture_count] = &texture->impl._texture;
current_state.texture_units[current_state.texture_count] = g5_unit;
current_state.texture_count += 1;
}
kinc_g5_command_list_set_texture(&commandList, g5_unit, &texture->impl._texture);
}
#else
void kinc_g4_set_texture(kinc_g4_texture_unit_t unit, kinc_g4_texture_t *texture) {
if (!texture->impl._uploaded) {
kinc_g5_command_list_upload_texture(&commandList, &texture->impl._texture);
texture->impl._uploaded = true;
}
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit;
memcpy(&g5_unit.stages[0], &unit.stages[0], KINC_G5_SHADER_TYPE_COUNT * sizeof(int));
bool found = false;
for (int i = 0; i < current_state.texture_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.texture_units[i], &g5_unit)) {
current_state.textures[i] = &texture->impl._texture;
current_state.texture_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.texture_count < MAX_TEXTURES);
current_state.textures[current_state.texture_count] = &texture->impl._texture;
current_state.texture_units[current_state.texture_count] = g5_unit;
current_state.texture_count += 1;
}
kinc_g5_command_list_set_texture(&commandList, g5_unit, &texture->impl._texture);
}
#endif
void kinc_g4_set_image_texture(kinc_g4_texture_unit_t unit, kinc_g4_texture_t *texture) {
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit;
memcpy(&g5_unit.stages[0], &unit.stages[0], KINC_G5_SHADER_TYPE_COUNT * sizeof(int));
bool found = false;
for (int i = 0; i < current_state.texture_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.texture_units[i], &g5_unit)) {
current_state.textures[i] = &texture->impl._texture;
current_state.texture_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.texture_count < MAX_TEXTURES);
current_state.textures[current_state.texture_count] = &texture->impl._texture;
current_state.texture_units[current_state.texture_count] = g5_unit;
current_state.texture_count += 1;
}
kinc_g5_command_list_set_image_texture(&commandList, g5_unit, &texture->impl._texture);
}
int kinc_g4_max_bound_textures(void) {
return kinc_g5_max_bound_textures();
}
bool kinc_g4_init_occlusion_query(unsigned *occlusionQuery) {
return kinc_g5_command_list_init_occlusion_query(&commandList, occlusionQuery);
}
void kinc_g4_delete_occlusion_query(unsigned occlusionQuery) {
kinc_g5_command_list_delete_occlusion_query(&commandList, occlusionQuery);
}
bool kinc_g4_are_query_results_available(unsigned occlusionQuery) {
return kinc_g5_command_list_are_query_results_available(&commandList, occlusionQuery);
}
void kinc_g4_get_query_result(unsigned occlusionQuery, unsigned *pixelCount) {
kinc_g5_command_list_get_query_result(&commandList, occlusionQuery, pixelCount);
}
void kinc_g4_set_pipeline(kinc_g4_pipeline_t *pipeline) {
kinc_g5_pipeline_t *g5_pipeline = &pipeline->impl._pipeline;
current_state.pipeline = g5_pipeline;
kinc_g5_command_list_set_pipeline(&commandList, g5_pipeline);
}
void kinc_g4_set_blend_constant(float r, float g, float b, float a) {
current_state.blend_constant_set = true;
current_state.blend_constant_r = r;
current_state.blend_constant_g = g;
current_state.blend_constant_b = b;
current_state.blend_constant_a = a;
kinc_g5_command_list_set_blend_constant(&commandList, r, g, b, a);
}
void kinc_g4_set_texture_array(kinc_g4_texture_unit_t unit, struct kinc_g4_texture_array *array) {}
bool kinc_g4_supports_instanced_rendering(void) {
return kinc_g5_supports_instanced_rendering();
}
bool kinc_g4_supports_compute_shaders(void) {
return kinc_g5_supports_compute_shaders();
}
bool kinc_g4_supports_blend_constants(void) {
return kinc_g5_supports_blend_constants();
}
bool kinc_g4_supports_non_pow2_textures(void) {
return kinc_g5_supports_non_pow2_textures();
}
bool kinc_g4_render_targets_inverted_y(void) {
return kinc_g5_render_targets_inverted_y();
}
#ifdef KINC_KONG
void kinc_g4_render_target_use_color_as_texture(kinc_g4_render_target_t *render_target, uint32_t unit) {
if (render_target->impl.state != KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE) {
kinc_g5_command_list_render_target_to_texture_barrier(&commandList, &render_target->impl._renderTarget);
render_target->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE;
}
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit = {0};
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
g5_unit.stages[i] = unit;
}
bool found = false;
for (int i = 0; i < current_state.render_target_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.render_target_units[i], &g5_unit)) {
current_state.render_targets[i] = &render_target->impl._renderTarget;
current_state.render_target_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.render_target_count < MAX_TEXTURES);
current_state.render_targets[current_state.render_target_count] = &render_target->impl._renderTarget;
current_state.render_target_units[current_state.render_target_count] = g5_unit;
current_state.render_target_count += 1;
}
kinc_g5_command_list_set_texture_from_render_target(&commandList, g5_unit, &render_target->impl._renderTarget);
}
#else
void kinc_g4_render_target_use_color_as_texture(kinc_g4_render_target_t *render_target, kinc_g4_texture_unit_t unit) {
if (render_target->impl.state != KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE) {
kinc_g5_command_list_render_target_to_texture_barrier(&commandList, &render_target->impl._renderTarget);
render_target->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE;
}
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit;
memcpy(&g5_unit.stages[0], &unit.stages[0], KINC_G5_SHADER_TYPE_COUNT * sizeof(int));
bool found = false;
for (int i = 0; i < current_state.render_target_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.render_target_units[i], &g5_unit)) {
current_state.render_targets[i] = &render_target->impl._renderTarget;
current_state.render_target_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.render_target_count < MAX_TEXTURES);
current_state.render_targets[current_state.render_target_count] = &render_target->impl._renderTarget;
current_state.render_target_units[current_state.render_target_count] = g5_unit;
current_state.render_target_count += 1;
}
kinc_g5_command_list_set_texture_from_render_target(&commandList, g5_unit, &render_target->impl._renderTarget);
}
#endif
void kinc_g4_render_target_use_depth_as_texture(kinc_g4_render_target_t *render_target, kinc_g4_texture_unit_t unit) {
if (render_target->impl.state != KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE) {
kinc_g5_command_list_render_target_to_texture_barrier(&commandList, &render_target->impl._renderTarget);
render_target->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE;
}
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g5_texture_unit_t g5_unit;
memcpy(&g5_unit.stages[0], &unit.stages[0], KINC_G5_SHADER_TYPE_COUNT * sizeof(int));
bool found = false;
for (int i = 0; i < current_state.depth_render_target_count; ++i) {
if (kinc_g5_texture_unit_equals(&current_state.depth_render_target_units[i], &g5_unit)) {
current_state.depth_render_targets[i] = &render_target->impl._renderTarget;
current_state.depth_render_target_units[i] = g5_unit;
found = true;
break;
}
}
if (!found) {
assert(current_state.depth_render_target_count < MAX_TEXTURES);
current_state.depth_render_targets[current_state.depth_render_target_count] = &render_target->impl._renderTarget;
current_state.depth_render_target_units[current_state.depth_render_target_count] = g5_unit;
current_state.depth_render_target_count += 1;
}
kinc_g5_command_list_set_texture_from_render_target_depth(&commandList, g5_unit, &render_target->impl._renderTarget);
}
#ifdef KINC_KONG
void kinc_g4_set_constant_buffer(uint32_t id, struct kinc_g4_constant_buffer *buffer) {
kinc_g5_command_list_set_vertex_constant_buffer(&commandList, &buffer->impl.buffer, 0, kinc_g5_constant_buffer_size(&buffer->impl.buffer));
}
#endif
void kinc_g4_set_compute_shader(kinc_g4_compute_shader *shader) {
kinc_g5_compute_shader *g5_shader = &shader->impl.shader;
current_state.compute_shader = g5_shader;
kinc_g5_command_list_set_compute_shader(&commandList, g5_shader);
}
void kinc_g4_compute(int x, int y, int z) {
startDraw(true);
kinc_g5_command_list_compute(&commandList, x, y, z);
endDraw(true);
}

View File

@ -0,0 +1,4 @@
#pragma once
#include <kinc/graphics4/graphics.h>
#include <kinc/math/matrix.h>

View File

@ -0,0 +1,37 @@
#include <kinc/graphics4/compute.h>
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/rendertarget.h>
#include <kinc/graphics4/texture.h>
#include <kinc/image.h>
#include <kinc/log.h>
#include <kinc/math/core.h>
#include <stdio.h>
#include <string.h>
extern kinc_g5_command_list_t commandList;
void kinc_g4_compute_shader_init(kinc_g4_compute_shader *shader, void *source, int length) {
kinc_g5_compute_shader_init(&shader->impl.shader, source, length);
}
void kinc_g4_compute_shader_destroy(kinc_g4_compute_shader *shader) {
kinc_g5_compute_shader_destroy(&shader->impl.shader);
}
#ifndef KINC_KONG
kinc_g4_constant_location_t kinc_g4_compute_shader_get_constant_location(kinc_g4_compute_shader *shader, const char *name) {
kinc_g4_constant_location_t location;
location.impl._location = kinc_g5_compute_shader_get_constant_location(&shader->impl.shader, name);
return location;
}
kinc_g4_texture_unit_t kinc_g4_compute_shader_get_texture_unit(kinc_g4_compute_shader *shader, const char *name) {
kinc_g5_texture_unit_t g5_unit = kinc_g5_compute_shader_get_texture_unit(&shader->impl.shader, name);
kinc_g4_texture_unit_t g4_unit;
for (int i = 0; i < KINC_G4_SHADER_TYPE_COUNT; ++i) {
g4_unit.stages[i] = g5_unit.stages[i];
}
return g4_unit;
}
#endif

View File

@ -0,0 +1,7 @@
#pragma once
#include <kinc/graphics5/compute.h>
typedef struct kinc_g4_compute_shader_impl {
kinc_g5_compute_shader shader;
} kinc_g4_compute_shader_impl;

View File

@ -0,0 +1,35 @@
#ifdef KINC_KONG
#include <kinc/graphics4/constantbuffer.h>
void kinc_g4_constant_buffer_init(kinc_g4_constant_buffer *buffer, size_t size) {
kinc_g5_constant_buffer_init(&buffer->impl.buffer, (int)size);
}
void kinc_g4_constant_buffer_destroy(kinc_g4_constant_buffer *buffer) {
kinc_g5_constant_buffer_destroy(&buffer->impl.buffer);
}
uint8_t *kinc_g4_constant_buffer_lock_all(kinc_g4_constant_buffer *buffer) {
kinc_g5_constant_buffer_lock_all(&buffer->impl.buffer);
return buffer->impl.buffer.data;
}
uint8_t *kinc_g4_constant_buffer_lock(kinc_g4_constant_buffer *buffer, size_t start, size_t size) {
kinc_g5_constant_buffer_lock(&buffer->impl.buffer, (int)start, (int)size);
return buffer->impl.buffer.data;
}
void kinc_g4_constant_buffer_unlock_all(kinc_g4_constant_buffer *buffer) {
kinc_g5_constant_buffer_unlock(&buffer->impl.buffer);
}
void kinc_g4_constant_buffer_unlock(kinc_g4_constant_buffer *buffer, size_t count) {
kinc_g5_constant_buffer_unlock(&buffer->impl.buffer);
}
size_t kinc_g4_constant_buffer_size(kinc_g4_constant_buffer *buffer) {
return kinc_g5_constant_buffer_size(&buffer->impl.buffer);
}
#endif

View File

@ -0,0 +1,11 @@
#pragma once
#ifdef KINC_KONG
#include <kinc/graphics5/constantbuffer.h>
typedef struct kinc_g4_constant_buffer_impl {
kinc_g5_constant_buffer_t buffer;
} kinc_g4_constant_buffer_impl;
#endif

View File

@ -0,0 +1,12 @@
#include "samplers.c.h"
#include "G4.c.h"
#include "compute.c.h"
#include "constantbuffer.c.h"
#include "indexbuffer.c.h"
#include "pipeline.c.h"
#include "rendertarget.c.h"
#include "shader.c.h"
#include "texture.c.h"
#include "texturearray.c.h"
#include "vertexbuffer.c.h"

View File

@ -0,0 +1,6 @@
#pragma once
#include "IndexBufferImpl.h"
#include "RenderTargetImpl.h"
#include "TextureImpl.h"
#include "VertexBufferImpl.h"

View File

@ -0,0 +1,35 @@
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics5/commandlist.h>
extern kinc_g5_command_list_t commandList;
void kinc_g4_index_buffer_init(kinc_g4_index_buffer_t *buffer, int count, kinc_g4_index_buffer_format_t format, kinc_g4_usage_t usage) {
kinc_g5_index_buffer_init(&buffer->impl._buffer, count, (kinc_g5_index_buffer_format_t)format, usage == KINC_G4_USAGE_STATIC);
}
void kinc_g4_index_buffer_destroy(kinc_g4_index_buffer_t *buffer) {
kinc_g5_index_buffer_destroy(&buffer->impl._buffer);
}
void *kinc_g4_index_buffer_lock_all(kinc_g4_index_buffer_t *buffer) {
return kinc_g5_index_buffer_lock_all(&buffer->impl._buffer);
}
void *kinc_g4_index_buffer_lock(kinc_g4_index_buffer_t *buffer, int start, int count) {
return kinc_g5_index_buffer_lock(&buffer->impl._buffer, start, count);
}
void kinc_g4_index_buffer_unlock_all(kinc_g4_index_buffer_t *buffer) {
kinc_g5_index_buffer_unlock_all(&buffer->impl._buffer);
kinc_g5_command_list_upload_index_buffer(&commandList, &buffer->impl._buffer);
}
void kinc_g4_index_buffer_unlock(kinc_g4_index_buffer_t *buffer, int count) {
kinc_g5_index_buffer_unlock(&buffer->impl._buffer, count);
kinc_g5_command_list_upload_index_buffer(&commandList, &buffer->impl._buffer);
}
int kinc_g4_index_buffer_count(kinc_g4_index_buffer_t *buffer) {
return kinc_g5_index_buffer_count(&buffer->impl._buffer);
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <kinc/graphics5/indexbuffer.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
kinc_g5_index_buffer_t _buffer;
} kinc_g4_index_buffer_impl_t;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,64 @@
#include <kinc/backend/graphics4/pipeline.h>
#include <kinc/graphics4/pipeline.h>
#include <kinc/graphics4/shader.h>
#include <kinc/graphics5/pipeline.h>
void kinc_g4_pipeline_init(kinc_g4_pipeline_t *pipe) {
kinc_g4_internal_pipeline_set_defaults(pipe);
kinc_g5_pipeline_init(&pipe->impl._pipeline);
}
void kinc_g4_pipeline_destroy(kinc_g4_pipeline_t *pipe) {
kinc_g5_pipeline_destroy(&pipe->impl._pipeline);
}
#ifndef KINC_KONG
kinc_g4_constant_location_t kinc_g4_pipeline_get_constant_location(kinc_g4_pipeline_t *pipe, const char *name) {
kinc_g4_constant_location_t location;
location.impl._location = kinc_g5_pipeline_get_constant_location(&pipe->impl._pipeline, name);
return location;
}
kinc_g4_texture_unit_t kinc_g4_pipeline_get_texture_unit(kinc_g4_pipeline_t *pipe, const char *name) {
kinc_g5_texture_unit_t g5_unit = kinc_g5_pipeline_get_texture_unit(&pipe->impl._pipeline, name);
assert(KINC_G4_SHADER_TYPE_COUNT == KINC_G5_SHADER_TYPE_COUNT);
kinc_g4_texture_unit_t g4_unit;
memcpy(&g4_unit.stages[0], &g5_unit.stages[0], KINC_G4_SHADER_TYPE_COUNT * sizeof(int));
return g4_unit;
}
#endif
void kinc_g4_pipeline_compile(kinc_g4_pipeline_t *pipe) {
for (int i = 0; i < 16; ++i) {
pipe->impl._pipeline.inputLayout[i] = pipe->input_layout[i];
}
pipe->impl._pipeline.vertexShader = &pipe->vertex_shader->impl._shader;
pipe->impl._pipeline.fragmentShader = &pipe->fragment_shader->impl._shader;
pipe->impl._pipeline.geometryShader = pipe->geometry_shader != NULL ? &pipe->geometry_shader->impl._shader : NULL;
pipe->impl._pipeline.tessellationControlShader = pipe->tessellation_control_shader != NULL ? &pipe->tessellation_control_shader->impl._shader : NULL;
pipe->impl._pipeline.tessellationEvaluationShader =
pipe->tessellation_evaluation_shader != NULL ? &pipe->tessellation_evaluation_shader->impl._shader : NULL;
pipe->impl._pipeline.blend_source = (kinc_g5_blending_factor_t)pipe->blend_source;
pipe->impl._pipeline.blend_destination = (kinc_g5_blending_factor_t)pipe->blend_destination;
pipe->impl._pipeline.blend_operation = (kinc_g5_blending_operation_t)pipe->blend_operation;
pipe->impl._pipeline.alpha_blend_source = (kinc_g5_blending_factor_t)pipe->alpha_blend_source;
pipe->impl._pipeline.alpha_blend_destination = (kinc_g5_blending_factor_t)pipe->alpha_blend_destination;
pipe->impl._pipeline.alpha_blend_operation = (kinc_g5_blending_operation_t)pipe->alpha_blend_operation;
pipe->impl._pipeline.cullMode = (kinc_g5_cull_mode_t)pipe->cull_mode;
pipe->impl._pipeline.depthMode = (kinc_g5_compare_mode_t)pipe->depth_mode;
pipe->impl._pipeline.depthWrite = pipe->depth_write;
pipe->impl._pipeline.colorAttachmentCount = pipe->color_attachment_count;
for (int i = 0; i < 8; ++i) {
pipe->impl._pipeline.colorWriteMaskRed[i] = pipe->color_write_mask_red[i];
pipe->impl._pipeline.colorWriteMaskGreen[i] = pipe->color_write_mask_green[i];
pipe->impl._pipeline.colorWriteMaskBlue[i] = pipe->color_write_mask_blue[i];
pipe->impl._pipeline.colorWriteMaskAlpha[i] = pipe->color_write_mask_alpha[i];
pipe->impl._pipeline.colorAttachment[i] = (kinc_g5_render_target_format_t)pipe->color_attachment[i];
}
pipe->impl._pipeline.depthAttachmentBits = pipe->depth_attachment_bits;
pipe->impl._pipeline.stencilAttachmentBits = pipe->stencil_attachment_bits;
kinc_g5_pipeline_compile(&pipe->impl._pipeline);
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <kinc/graphics5/constantlocation.h>
#include <kinc/graphics5/pipeline.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
// PipelineStateImpl();
kinc_g5_pipeline_t _pipeline;
} kinc_g4_pipeline_impl_t;
typedef struct {
kinc_g5_constant_location_t _location;
} kinc_g4_constant_location_impl_t;
typedef struct {
int nothing;
} Kinc_G4_AttributeLocationImpl;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,36 @@
#include <kinc/backend/graphics4/rendertarget.h>
#include <kinc/graphics4/rendertarget.h>
#include <kinc/graphics5/commandlist.h>
#include <kinc/log.h>
extern kinc_g5_command_list_t commandList;
void kinc_g4_render_target_init_with_multisampling(kinc_g4_render_target_t *render_target, int width, int height, kinc_g4_render_target_format_t format,
int depthBufferBits, int stencilBufferBits, int samples_per_pixel) {
kinc_g5_render_target_init_with_multisampling(&render_target->impl._renderTarget, width, height, (kinc_g5_render_target_format_t)format, depthBufferBits,
stencilBufferBits, samples_per_pixel);
render_target->texWidth = render_target->width = width;
render_target->texHeight = render_target->height = height;
render_target->impl.state = KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}
void kinc_g4_render_target_init_cube_with_multisampling(kinc_g4_render_target_t *render_target, int cubeMapSize, kinc_g4_render_target_format_t format,
int depthBufferBits, int stencilBufferBits, int samples_per_pixel) {
kinc_g5_render_target_init_cube_with_multisampling(&render_target->impl._renderTarget, cubeMapSize, (kinc_g5_render_target_format_t)format, depthBufferBits,
stencilBufferBits, samples_per_pixel);
}
void kinc_g4_render_target_destroy(kinc_g4_render_target_t *render_target) {
kinc_g5_render_target_destroy(&render_target->impl._renderTarget);
}
void kinc_g4_render_target_set_depth_stencil_from(kinc_g4_render_target_t *render_target, kinc_g4_render_target_t *source) {
kinc_g5_render_target_set_depth_stencil_from(&render_target->impl._renderTarget, &source->impl._renderTarget);
}
void kinc_g4_render_target_get_pixels(kinc_g4_render_target_t *render_target, uint8_t *data) {
kinc_g5_command_list_get_render_target_pixels(&commandList, &render_target->impl._renderTarget, data);
}
void kinc_g4_render_target_generate_mipmaps(kinc_g4_render_target_t *render_target, int levels) {}

View File

@ -0,0 +1,21 @@
#pragma once
#include <kinc/graphics5/rendertarget.h>
#ifdef __cplusplus
extern "C" {
#endif
enum kinc_internal_render_target_state { KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET, KINC_INTERNAL_RENDER_TARGET_STATE_TEXTURE };
typedef struct {
// RenderTargetImpl(int width, int height, int depthBufferBits, bool antialiasing, Graphics5::RenderTargetFormat format, int stencilBufferBits,
// int contextId);
// RenderTargetImpl(int cubeMapSize, int depthBufferBits, bool antialiasing, Graphics5::RenderTargetFormat format, int stencilBufferBits, int contextId);
kinc_g5_render_target_t _renderTarget;
enum kinc_internal_render_target_state state;
} kinc_g4_render_target_impl_t;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,50 @@
#include <kinc/graphics5/sampler.h>
#include <assert.h>
#define MAX_SAMPLERS_PER_STAGE 16
static kinc_g5_sampler_options_t sampler_options[KINC_G5_SHADER_TYPE_COUNT][MAX_SAMPLERS_PER_STAGE];
static void samplers_reset(void) {
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
for (int j = 0; j < MAX_SAMPLERS_PER_STAGE; ++j) {
kinc_g5_sampler_options_set_defaults(&sampler_options[i][j]);
}
}
}
bool sampler_options_equals(kinc_g5_sampler_options_t *options1, kinc_g5_sampler_options_t *options2) {
return options1->u_addressing == options2->u_addressing && options1->v_addressing == options2->v_addressing &&
options1->w_addressing == options2->w_addressing && options1->minification_filter == options2->minification_filter &&
options1->magnification_filter == options2->magnification_filter && options1->mipmap_filter == options2->mipmap_filter &&
options1->lod_min_clamp == options2->lod_min_clamp && options1->lod_max_clamp == options2->lod_max_clamp &&
options1->max_anisotropy == options2->max_anisotropy && options1->is_comparison == options2->is_comparison &&
options1->compare_mode == options2->compare_mode;
}
struct sampler_cache_entry {
kinc_g5_sampler_options_t options;
kinc_g5_sampler_t sampler;
};
#define MAX_SAMPLER_CACHE_SIZE 256
static struct sampler_cache_entry sampler_cache[MAX_SAMPLER_CACHE_SIZE];
static int sampler_cache_size = 0;
// TODO: Please make this much faster
static kinc_g5_sampler_t *get_current_sampler(int stage, int unit) {
for (int i = 0; i < sampler_cache_size; ++i) {
if (sampler_options_equals(&sampler_cache[i].options, &sampler_options[stage][unit])) {
return &sampler_cache[i].sampler;
}
}
assert(sampler_cache_size < MAX_SAMPLER_CACHE_SIZE);
kinc_g5_sampler_t *sampler = &sampler_cache[sampler_cache_size].sampler;
kinc_g5_sampler_init(sampler, &sampler_options[stage][unit]);
sampler_cache[sampler_cache_size].options = sampler_options[stage][unit];
sampler_cache_size += 1;
return sampler;
}

View File

@ -0,0 +1,49 @@
#include <kinc/graphics4/shader.h>
void kinc_g4_shader_init(kinc_g4_shader_t *shader, const void *_data, size_t length, kinc_g4_shader_type_t type) {
kinc_g5_shader_init(&shader->impl._shader, _data, length, (kinc_g5_shader_type_t)type);
}
#ifdef KRAFIX_LIBRARY
extern int krafix_compile(const char *source, char *output, int *length, const char *targetlang, const char *system, const char *shadertype, int version);
#endif
int kinc_g4_shader_init_from_source(kinc_g4_shader_t *shader, const char *source, kinc_g4_shader_type_t type) {
#ifdef KRAFIX_LIBRARY
char *output = malloc(1024 * 1024);
int length;
#ifdef KINC_WINDOWS
const char *system = "windows";
#elif defined(KINC_MACOS)
const char *system = "macos";
#elif defined(KINC_LINUX)
const char *system = "linux";
#elif defined(KINC_ANDROID)
const char *system = "android";
#elif defined(KINC_IOS)
const char *system = "ios";
#endif
#ifdef KINC_VULKAN
const char *target = "spirv";
#elif defined(KINC_METAL)
const char *target = "metal";
#else
const char *target = "d3d11";
#endif
int errors = krafix_compile(source, output, &length, target, system, type == KINC_G4_SHADER_TYPE_FRAGMENT ? "frag" : "vert", -1);
if (errors > 0) {
return errors;
}
kinc_g4_shader_init(shader, output, length, type);
return 0;
#else
return 0;
#endif
}
void kinc_g4_shader_destroy(kinc_g4_shader_t *shader) {
kinc_g5_shader_destroy(&shader->impl._shader);
}

View File

@ -0,0 +1,16 @@
#pragma once
#include <kinc/graphics5/shader.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
// ShaderImpl(void *data, int length, Graphics5::ShaderType type);
kinc_g5_shader_t _shader;
} kinc_g4_shader_impl_t;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,70 @@
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/texture.h>
#include <kinc/graphics5/texture.h>
#include <kinc/io/filereader.h>
#include <kinc/backend/graphics4/texture.h>
void kinc_g4_texture_init_from_image(kinc_g4_texture_t *texture, kinc_image_t *image) {
texture->impl._uploaded = false;
kinc_g5_texture_init_from_image(&texture->impl._texture, image);
texture->tex_width = texture->impl._texture.texWidth;
texture->tex_height = texture->impl._texture.texHeight;
texture->tex_depth = 1;
texture->format = image->format;
}
void kinc_g4_texture_init_from_image3d(kinc_g4_texture_t *texture, kinc_image_t *image) {}
void kinc_g4_texture_init(kinc_g4_texture_t *texture, int width, int height, kinc_image_format_t format) {
texture->impl._uploaded = true;
kinc_g5_texture_init(&texture->impl._texture, width, height, format);
texture->tex_width = texture->impl._texture.texWidth;
texture->tex_height = texture->impl._texture.texHeight;
texture->tex_depth = 1;
texture->format = format;
}
void kinc_g4_texture_init3d(kinc_g4_texture_t *texture, int width, int height, int depth, kinc_image_format_t format) {}
void kinc_g4_texture_init_from_bytes(kinc_g4_texture_t *texture, void *data, int size, const char *format) {}
void kinc_g4_texture_init_from_bytes3d(kinc_g4_texture_t *texture, void *data, int width, int height, int depth, int format, bool readable) {}
void kinc_g4_texture_destroy(kinc_g4_texture_t *texture) {
// kinc_g4_internal_texture_unset(texture);
kinc_g5_texture_destroy(&texture->impl._texture);
}
void kinc_g4_internal_texture_unset(kinc_g4_texture_t *texture) {
// TODO
}
void kinc_g4_internal_texture_unmipmap(kinc_g4_texture_t *texture) {
// TODO
}
uint8_t *kinc_g4_texture_lock(kinc_g4_texture_t *texture) {
return kinc_g5_texture_lock(&texture->impl._texture);
}
void kinc_g4_texture_unlock(kinc_g4_texture_t *texture) {
kinc_g5_texture_unlock(&texture->impl._texture);
texture->impl._uploaded = false;
}
void kinc_g4_texture_clear(kinc_g4_texture_t *texture, int x, int y, int z, int width, int height, int depth, unsigned color) {
kinc_g5_texture_clear(&texture->impl._texture, x, y, z, width, height, depth, color);
}
int kinc_g4_texture_stride(kinc_g4_texture_t *texture) {
return kinc_g5_texture_stride(&texture->impl._texture);
}
void kinc_g4_texture_generate_mipmaps(kinc_g4_texture_t *texture, int levels) {
kinc_g5_texture_generate_mipmaps(&texture->impl._texture, levels);
}
void kinc_g4_texture_set_mipmap(kinc_g4_texture_t *texture, kinc_image_t *mipmap, int level) {
kinc_g5_texture_set_mipmap(&texture->impl._texture, mipmap, level);
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <kinc/graphics5/texture.h>
#include <kinc/graphics5/textureunit.h>
#include <kinc/image.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
kinc_g5_texture_unit_t _unit;
} kinc_g4_texture_unit_impl_t;
typedef struct {
/*TextureImpl();
TextureImpl(int width, int height, Image::Format format, bool readable);
TextureImpl(int width, int height, int depth, Image::Format format, bool readable);
~TextureImpl();
void unmipmap();
void unset();*/
kinc_g5_texture_t _texture;
bool _uploaded;
} kinc_g4_texture_impl_t;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,7 @@
#include <kinc/graphics4/texturearray.h>
void kinc_g4_texture_array_init(kinc_g4_texture_array_t *array, kinc_image_t *textures, int count) {}
void kinc_g4_texture_array_destroy(kinc_g4_texture_array_t *array) {}
// void TextureArrayImpl::set(TextureUnit unit) {}

View File

@ -0,0 +1,14 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
// void set(Kore::Graphics4::TextureUnit unit);
int nothing;
} kinc_g4_texture_array_impl_t;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,64 @@
#include <kinc/backend/graphics4/vertexbuffer.h>
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/vertexbuffer.h>
extern uint64_t frameNumber;
extern bool waitAfterNextDraw;
void kinc_g4_vertex_buffer_init(kinc_g4_vertex_buffer_t *buffer, int count, kinc_g4_vertex_structure_t *structure, kinc_g4_usage_t usage,
int instanceDataStepRate) {
int multiple = usage == KINC_G4_USAGE_STATIC ? 1 : 500;
kinc_g5_vertex_buffer_init(&buffer->impl._buffer, count * multiple, structure, usage == KINC_G4_USAGE_STATIC, instanceDataStepRate);
buffer->impl._multiple = multiple;
buffer->impl._lastFrameNumber = 0;
buffer->impl._currentIndex = 0;
buffer->impl.myCount = count;
}
void kinc_g4_vertex_buffer_destroy(kinc_g4_vertex_buffer_t *buffer) {
kinc_g5_vertex_buffer_destroy(&buffer->impl._buffer);
}
static void prepareLock(kinc_g4_vertex_buffer_t *buffer) {
/*if (frameNumber > _lastFrameNumber) {
_lastFrameNumber = frameNumber;
_currentIndex = 0;
}
else {*/
++buffer->impl._currentIndex;
if (buffer->impl._currentIndex >= buffer->impl._multiple - 1) {
waitAfterNextDraw = true;
}
if (buffer->impl._currentIndex >= buffer->impl._multiple) {
buffer->impl._currentIndex = 0;
}
//}
}
float *kinc_g4_vertex_buffer_lock_all(kinc_g4_vertex_buffer_t *buffer) {
prepareLock(buffer);
return kinc_g5_vertex_buffer_lock(&buffer->impl._buffer, buffer->impl._currentIndex * kinc_g4_vertex_buffer_count(buffer),
kinc_g4_vertex_buffer_count(buffer));
}
float *kinc_g4_vertex_buffer_lock(kinc_g4_vertex_buffer_t *buffer, int start, int count) {
prepareLock(buffer);
return kinc_g5_vertex_buffer_lock(&buffer->impl._buffer, start + buffer->impl._currentIndex * kinc_g4_vertex_buffer_count(buffer), count);
}
void kinc_g4_vertex_buffer_unlock_all(kinc_g4_vertex_buffer_t *buffer) {
kinc_g5_vertex_buffer_unlock_all(&buffer->impl._buffer);
}
void kinc_g4_vertex_buffer_unlock(kinc_g4_vertex_buffer_t *buffer, int count) {
kinc_g5_vertex_buffer_unlock(&buffer->impl._buffer, count);
}
int kinc_g4_vertex_buffer_count(kinc_g4_vertex_buffer_t *buffer) {
return buffer->impl.myCount;
}
int kinc_g4_vertex_buffer_stride(kinc_g4_vertex_buffer_t *buffer) {
return kinc_g5_vertex_buffer_stride(&buffer->impl._buffer);
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <kinc/graphics5/vertexbuffer.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int myCount;
// void prepareLock();
kinc_g5_vertex_buffer_t _buffer;
int _currentIndex;
int _multiple;
uint64_t _lastFrameNumber;
} kinc_g4_vertex_buffer_impl_t;
#ifdef __cplusplus
}
#endif