Skip to content

Commit

Permalink
Adjust various G4 things for Kong
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 16, 2023
1 parent 30b4c5c commit 8fafd9f
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 16 deletions.
51 changes: 36 additions & 15 deletions Backends/Graphics4/Direct3D11/Sources/kinc/backend/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,45 @@ static uint8_t constantsMemory[1024 * 4];

static int getMultipleOf16(int value) {
int ret = 16;
while (ret < value) ret += 16;
while (ret < value)
ret += 16;
return ret;
}

static void setInt(uint8_t *constants, uint32_t offset, uint32_t size, int value) {
if (size == 0) return;
if (size == 0)
return;
int *ints = (int *)&constants[offset];
ints[0] = value;
}

static void setFloat(uint8_t *constants, uint32_t offset, uint32_t size, float value) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
floats[0] = value;
}

static void setFloat2(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
floats[0] = value1;
floats[1] = value2;
}

static void setFloat3(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2, float value3) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
floats[0] = value1;
floats[1] = value2;
floats[2] = value3;
}

static void setFloat4(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2, float value3, float value4) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
floats[0] = value1;
floats[1] = value2;
Expand All @@ -63,7 +69,8 @@ static void setFloat4(uint8_t *constants, uint32_t offset, uint32_t size, float
}

static void setFloats(uint8_t *constants, uint32_t offset, uint32_t size, uint8_t columns, uint8_t rows, float *values, int count) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
if (columns == 4 && rows == 4) {
for (int i = 0; i < count / 16 && i < (int)size / 4; ++i) {
Expand Down Expand Up @@ -100,13 +107,15 @@ static void setFloats(uint8_t *constants, uint32_t offset, uint32_t size, uint8_
}

static void setBool(uint8_t *constants, uint32_t offset, uint32_t size, bool value) {
if (size == 0) return;
if (size == 0)
return;
int *ints = (int *)&constants[offset];
ints[0] = value ? 1 : 0;
}

static void setMatrix4(uint8_t *constants, uint32_t offset, uint32_t size, kinc_matrix4x4_t *value) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 4; ++x) {
Expand All @@ -116,7 +125,8 @@ static void setMatrix4(uint8_t *constants, uint32_t offset, uint32_t size, kinc_
}

static void setMatrix3(uint8_t *constants, uint32_t offset, uint32_t size, kinc_matrix3x3_t *value) {
if (size == 0) return;
if (size == 0)
return;
float *floats = (float *)&constants[offset];
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
Expand All @@ -129,13 +139,15 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
unsigned index = 0;
uint8_t *data = (uint8_t *)_data;

#ifndef KINC_KONG
memset(&shader->impl.attributes, 0, sizeof(shader->impl.attributes));
int attributesCount = data[index++];
for (int i = 0; i < attributesCount; ++i) {
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0) break;
if (name[i2] == 0)
break;
}
shader->impl.attributes[i].hash = kinc_internal_hash_name(name);
shader->impl.attributes[i].index = data[index++];
Expand All @@ -147,7 +159,8 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0) break;
if (name[i2] == 0)
break;
}
shader->impl.textures[i].hash = kinc_internal_hash_name(name);
shader->impl.textures[i].index = data[index++];
Expand All @@ -160,7 +173,8 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0) break;
if (name[i2] == 0)
break;
}
kinc_compute_internal_shader_constant_t constant;
constant.hash = kinc_internal_hash_name(name);
Expand All @@ -176,6 +190,7 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
shader->impl.constants[i] = constant;
shader->impl.constantsSize = constant.offset + constant.size;
}
#endif

shader->impl.length = (int)(length - index);
shader->impl.data = (uint8_t *)malloc(shader->impl.length);
Expand All @@ -190,6 +205,7 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
return;
}

#ifndef KINC_KONG
D3D11_BUFFER_DESC desc;
desc.ByteWidth = getMultipleOf16(shader->impl.constantsSize);
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
Expand All @@ -198,6 +214,7 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le
desc.MiscFlags = 0;
desc.StructureByteStride = 0;
kinc_microsoft_affirm(dx_ctx.device->lpVtbl->CreateBuffer(dx_ctx.device, &desc, NULL, &shader->impl.constantBuffer));
#endif
}

void kinc_compute_shader_destroy(kinc_compute_shader_t *shader) {}
Expand All @@ -220,6 +237,7 @@ static kinc_internal_hash_index_t *findTextureUnit(kinc_internal_hash_index_t *u
return NULL;
}

#ifndef KINC_KONG
kinc_compute_constant_location_t kinc_compute_shader_get_constant_location(kinc_compute_shader_t *shader, const char *name) {
kinc_compute_constant_location_t location;

Expand Down Expand Up @@ -250,7 +268,8 @@ kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_sh
char unitName[64];
int unitOffset = 0;
size_t len = strlen(name);
if (len > 63) len = 63;
if (len > 63)
len = 63;
strncpy(unitName, name, len + 1);
if (unitName[len - 1] == ']') { // Check for array - mySampler[2]
unitOffset = (int)(unitName[len - 2] - '0'); // Array index is unit offset
Expand Down Expand Up @@ -280,6 +299,7 @@ kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_sh
}
return unit;
}
#endif

void kinc_compute_set_bool(kinc_compute_constant_location_t location, bool value) {
setBool(constantsMemory, location.impl.offset, location.impl.size, value);
Expand Down Expand Up @@ -350,9 +370,10 @@ void kinc_compute_set_texture3d_mipmap_filter(kinc_compute_texture_unit_t unit,

void kinc_compute_set_shader(kinc_compute_shader_t *shader) {
dx_ctx.context->lpVtbl->CSSetShader(dx_ctx.context, (ID3D11ComputeShader *)shader->impl.shader, NULL, 0);

#ifndef KINC_KONG
dx_ctx.context->lpVtbl->UpdateSubresource(dx_ctx.context, (ID3D11Resource *)shader->impl.constantBuffer, 0, NULL, constantsMemory, 0, 0);
dx_ctx.context->lpVtbl->CSSetConstantBuffers(dx_ctx.context, 0, 1, &shader->impl.constantBuffer);
#endif
}

void kinc_compute(int x, int y, int z) {
Expand Down
4 changes: 3 additions & 1 deletion Backends/Graphics4/Direct3D11/Sources/kinc/backend/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ typedef struct {
} kinc_compute_internal_shader_constant_t;

typedef struct {
#ifndef KINC_KONG
kinc_compute_internal_shader_constant_t constants[64];
int constantsSize;
kinc_internal_hash_index_t attributes[64];
kinc_internal_hash_index_t textures[64];
struct ID3D11Buffer *constantBuffer;
#endif
void *shader;
uint8_t *data;
int length;
struct ID3D11Buffer *constantBuffer;
} kinc_compute_shader_impl_t;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ static D3D11_STENCIL_OP get_stencil_action(kinc_g4_stencil_action_t action) {
}

void kinc_internal_set_constants(void) {
#ifndef KINC_KONG
if (currentPipeline->vertex_shader->impl.constantsSize > 0) {
dx_ctx.context->lpVtbl->UpdateSubresource(dx_ctx.context, (ID3D11Resource *)currentPipeline->impl.vertexConstantBuffer, 0, NULL, vertexConstants, 0, 0);
dx_ctx.context->lpVtbl->VSSetConstantBuffers(dx_ctx.context, 0, 1, &currentPipeline->impl.vertexConstantBuffer);
Expand All @@ -115,6 +116,7 @@ void kinc_internal_set_constants(void) {
0);
dx_ctx.context->lpVtbl->DSSetConstantBuffers(dx_ctx.context, 0, 1, &currentPipeline->impl.tessEvalConstantBuffer);
}
#endif
}

void kinc_g4_pipeline_init(struct kinc_g4_pipeline *state) {
Expand Down Expand Up @@ -209,6 +211,7 @@ void kinc_internal_pipeline_rebind() {
}
}

#ifndef KINC_KONG
static kinc_internal_shader_constant_t *findConstant(kinc_internal_shader_constant_t *constants, uint32_t hash) {
for (int i = 0; i < 64; ++i) {
if (constants[i].hash == hash) {
Expand Down Expand Up @@ -368,6 +371,7 @@ kinc_g4_texture_unit_t kinc_g4_pipeline_get_texture_unit(struct kinc_g4_pipeline

return unit;
}
#endif

static char stringCache[1024];
static int stringCacheIndex = 0;
Expand Down Expand Up @@ -432,6 +436,7 @@ static void createRenderTargetBlendDesc(struct kinc_g4_pipeline *pipe, D3D11_REN
}

void kinc_g4_pipeline_compile(struct kinc_g4_pipeline *state) {
#ifndef KINC_KONG
if (state->vertex_shader->impl.constantsSize > 0) {
D3D11_BUFFER_DESC desc;
desc.ByteWidth = (UINT)get_multiple_of_16(state->vertex_shader->impl.constantsSize);
Expand Down Expand Up @@ -482,6 +487,7 @@ void kinc_g4_pipeline_compile(struct kinc_g4_pipeline *state) {
desc.StructureByteStride = 0;
kinc_microsoft_affirm(dx_ctx.device->lpVtbl->CreateBuffer(dx_ctx.device, &desc, NULL, &state->impl.tessEvalConstantBuffer));
}
#endif

int all = 0;
for (int stream = 0; state->input_layout[stream] != NULL; ++stream) {
Expand All @@ -495,14 +501,21 @@ void kinc_g4_pipeline_compile(struct kinc_g4_pipeline *state) {
}
}

#ifndef KINC_KONG
bool used[usedCount];
for (int i = 0; i < usedCount; ++i)
used[i] = false;
for (int i = 0; i < 64; ++i) {
used[state->vertex_shader->impl.attributes[i].index] = true;
}
#endif
stringCacheIndex = 0;
D3D11_INPUT_ELEMENT_DESC *vertexDesc = (D3D11_INPUT_ELEMENT_DESC *)alloca(sizeof(D3D11_INPUT_ELEMENT_DESC) * all);

#ifdef KINC_KONG
#define getAttributeLocation(a, b, c) index
#endif

int i = 0;
for (int stream = 0; state->input_layout[stream] != NULL; ++stream) {
for (int index = 0; index < state->input_layout[stream]->size; ++index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void kinc_g4_shader_init(kinc_g4_shader_t *shader, void *_data, size_t length, k
uint8_t *data = (uint8_t *)_data;
shader->impl.type = (int)type;

#ifndef KINC_KONG
memset(&shader->impl.attributes, 0, sizeof(shader->impl.attributes));
int attributesCount = data[index++];
for (int i = 0; i < attributesCount; ++i) {
Expand Down Expand Up @@ -62,6 +63,7 @@ void kinc_g4_shader_init(kinc_g4_shader_t *shader, void *_data, size_t length, k
shader->impl.constants[i] = constant;
shader->impl.constantsSize = constant.offset + constant.size;
}
#endif

shader->impl.length = (int)(length - index);
shader->impl.data = (uint8_t *)malloc(shader->impl.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ typedef struct {
} kinc_internal_shader_constant_t;

typedef struct {
#ifndef KINC_KONG
kinc_internal_shader_constant_t constants[64];
int constantsSize;
kinc_internal_hash_index_t attributes[64];
kinc_internal_hash_index_t textures[64];
#endif
void *shader;
uint8_t *data;
int length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void kinc_g5_pipeline_destroy(kinc_g5_pipeline_t *pipe) {
kinc_g4_pipeline_destroy(&pipe->impl.pipe);
}

#ifndef KINC_KONG
kinc_g5_constant_location_t kinc_g5_pipeline_get_constant_location(kinc_g5_pipeline_t *pipe, const char *name) {
kinc_g5_constant_location_t location;
location.impl.location = kinc_g4_pipeline_get_constant_location(&pipe->impl.pipe, name);
Expand All @@ -52,6 +53,7 @@ kinc_g5_texture_unit_t kinc_g5_pipeline_get_texture_unit(kinc_g5_pipeline_t *pip

return g5_unit;
}
#endif

void kinc_g5_pipeline_compile(kinc_g5_pipeline_t *pipe) {
for (int i = 0; i < 16; ++i) {
Expand Down
2 changes: 2 additions & 0 deletions Sources/kinc/compute/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ KINC_FUNC void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *sou
/// <param name="shader">The shader-object to destroy</param>
KINC_FUNC void kinc_compute_shader_destroy(kinc_compute_shader_t *shader);

#ifndef KINC_KONG
/// <summary>
/// Finds the location of a constant/uniform inside of a shader.
/// </summary>
Expand All @@ -62,6 +63,7 @@ KINC_FUNC kinc_compute_constant_location_t kinc_compute_shader_get_constant_loca
/// <param name="name">The texture-name to look for</param>
/// <returns>The found texture-unit</returns>
KINC_FUNC kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_shader_t *shader, const char *name);
#endif

#ifdef KORE_OPENGL
typedef struct kinc_shader_storage_buffer {
Expand Down
19 changes: 19 additions & 0 deletions Sources/kinc/graphics1/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
#include <kinc/graphics4/vertexbuffer.h>
#include <kinc/io/filereader.h>

#ifdef KINC_KONG
#include <kong.h>
#endif

#ifndef KINC_KONG
static kinc_g4_shader_t vertexShader;
static kinc_g4_shader_t fragmentShader;
static kinc_g4_pipeline_t pipeline;
static kinc_g4_texture_unit_t tex;
#endif
static kinc_g4_vertex_buffer_t vb;
static kinc_g4_index_buffer_t ib;
static kinc_g4_texture_t texture;
Expand All @@ -30,8 +36,15 @@ void kinc_g1_end(void) {

kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0xff000000, 0.0f, 0);

#ifdef KINC_KONG
kinc_g4_set_pipeline(&kinc_g1_pipeline);
#else
kinc_g4_set_pipeline(&pipeline);
#endif

#ifndef KINC_KONG
kinc_g4_set_texture(tex, &texture);
#endif
kinc_g4_set_vertex_buffer(&vb);
kinc_g4_set_index_buffer(&ib);
kinc_g4_draw_indexed_vertices();
Expand All @@ -44,6 +57,7 @@ void kinc_g1_init(int width, int height) {
kinc_internal_g1_w = width;
kinc_internal_g1_h = height;

#ifndef KINC_KONG
{
kinc_file_reader_t file;
kinc_file_reader_open(&file, "g1.vert", KINC_FILE_TYPE_ASSET);
Expand Down Expand Up @@ -76,6 +90,7 @@ void kinc_g1_init(int width, int height) {
kinc_g4_pipeline_compile(&pipeline);

tex = kinc_g4_pipeline_get_texture_unit(&pipeline, "texy");
#endif

kinc_g4_texture_init(&texture, width, height, KINC_IMAGE_FORMAT_RGBA32);
kinc_internal_g1_tex_width = texture.tex_width;
Expand All @@ -93,7 +108,11 @@ void kinc_g1_init(int width, int height) {
float xAspect = (float)width / texture.tex_width;
float yAspect = (float)height / texture.tex_height;

#ifdef KINC_KONG
kinc_g4_vertex_buffer_init(&vb, 4, &kinc_g1_vertex_in_structure, KINC_G4_USAGE_STATIC, 0);
#else
kinc_g4_vertex_buffer_init(&vb, 4, &structure, KINC_G4_USAGE_STATIC, 0);
#endif
float *v = kinc_g4_vertex_buffer_lock_all(&vb);
{
int i = 0;
Expand Down
Loading

0 comments on commit 8fafd9f

Please sign in to comment.