This commit is contained in:
luboslenco
2025-04-10 15:42:15 +02:00
parent 6be2105514
commit 07ad07e02b
3 changed files with 18 additions and 348 deletions
+9 -119
View File
@@ -995,59 +995,8 @@ void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipe) {
}
}
static ShaderConstant findConstant(iron_gpu_shader_t *shader, const char *name) {
if (shader != NULL) {
for (int i = 0; i < MAX_SHADER_THING; ++i) {
if (strcmp(shader->impl.constants[i].name, name) == 0) {
return shader->impl.constants[i];
}
}
}
ShaderConstant constant;
constant.name[0] = 0;
constant.offset = -1;
return constant;
}
static ShaderTexture findTexture(iron_gpu_shader_t *shader, const char *name) {
for (int i = 0; i < MAX_SHADER_THING; ++i) {
if (strcmp(shader->impl.textures[i].name, name) == 0) {
return shader->impl.textures[i];
}
}
ShaderTexture texture;
texture.name[0] = 0;
texture.texture = -1;
return texture;
}
static ShaderAttribute findAttribute(iron_gpu_shader_t *shader, const char *name) {
for (int i = 0; i < MAX_SHADER_THING; ++i) {
if (strcmp(shader->impl.attributes[i].name, name) == 0) {
return shader->impl.attributes[i];
}
}
ShaderAttribute attribute;
attribute.name[0] = 0;
attribute.attribute = -1;
return attribute;
}
iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(struct iron_gpu_pipeline *pipe, const char *name) {
iron_gpu_constant_location_t location;
{
ShaderConstant constant = findConstant(pipe->vertex_shader, name);
location.impl.vertexOffset = constant.offset;
}
{
ShaderConstant constant = findConstant(pipe->fragment_shader, name);
location.impl.fragmentOffset = constant.offset;
}
return location;
}
@@ -1056,15 +1005,6 @@ iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *
for (int i = 0; i < IRON_GPU_SHADER_TYPE_COUNT; ++i) {
unit.stages[i] = -1;
}
ShaderTexture vertexTexture = findTexture(pipe->vertex_shader, name);
if (vertexTexture.texture != -1) {
unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] = vertexTexture.texture;
}
else {
ShaderTexture fragmentTexture = findTexture(pipe->fragment_shader, name);
unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] = fragmentTexture.texture;
}
return unit;
}
@@ -1092,12 +1032,7 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipe) {
for (int i = 0; i < pipe->input_layout->size; ++i) {
vertexDesc[i].SemanticName = "TEXCOORD";
vertexDesc[i].SemanticIndex = findAttribute(pipe->vertex_shader, pipe->input_layout->elements[i].name).attribute;
if (pipe->kong) {
vertexDesc[i].SemanticIndex = i;
}
vertexDesc[i].SemanticIndex = i;
vertexDesc[i].InputSlot = 0;
vertexDesc[i].AlignedByteOffset = (i == 0) ? 0 : D3D12_APPEND_ALIGNED_ELEMENT;
vertexDesc[i].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
@@ -1250,55 +1185,10 @@ void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *_data, size_t l
memset(shader->impl.attributes, 0, sizeof(shader->impl.attributes));
memset(shader->impl.textures, 0, sizeof(shader->impl.textures));
unsigned index = 0;
uint8_t *data = (uint8_t *)_data;
int attributesCount = data[index++];
for (int i = 0; i < attributesCount; ++i) {
char name[64];
for (unsigned i2 = 0; i2 < 63; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
strcpy(shader->impl.attributes[i].name, name);
shader->impl.attributes[i].attribute = data[index++];
}
uint8_t texCount = data[index++];
for (unsigned i = 0; i < texCount; ++i) {
char name[64];
for (unsigned i2 = 0; i2 < 63; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
strcpy(shader->impl.textures[i].name, name);
shader->impl.textures[i].texture = data[index++];
}
shader->impl.texturesCount = texCount;
uint8_t constantCount = data[index++];
for (unsigned i = 0; i < constantCount; ++i) {
char name[64];
for (unsigned i2 = 0; i2 < 63; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
ShaderConstant constant;
memcpy(&constant.offset, &data[index], sizeof(constant.offset));
index += 4;
// memcpy(&constant.size, &data[index], sizeof(constant.size));
index += 4;
index += 2; // columns and rows
strcpy(constant.name, name);
shader->impl.constants[i] = constant;
}
shader->impl.length = (int)length - index;
shader->impl.length = (int)length;
shader->impl.data = (uint8_t *)malloc(shader->impl.length);
memcpy(shader->impl.data, &data[index], shader->impl.length);
memcpy(shader->impl.data, data, shader->impl.length);
}
void iron_gpu_shader_destroy(iron_gpu_shader_t *shader) {
@@ -2033,13 +1923,13 @@ void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int count, bool gpuMe
}
void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) {
if (buffer->impl.index_buffer != NULL) {
buffer->impl.index_buffer->lpVtbl->Release(buffer->impl.index_buffer);
buffer->impl.index_buffer = NULL;
}
// if (buffer->impl.index_buffer != NULL) {
// buffer->impl.index_buffer->lpVtbl->Release(buffer->impl.index_buffer);
// buffer->impl.index_buffer = NULL;
// }
buffer->impl.upload_buffer->lpVtbl->Release(buffer->impl.upload_buffer);
buffer->impl.upload_buffer = NULL;
// buffer->impl.upload_buffer->lpVtbl->Release(buffer->impl.upload_buffer);
// buffer->impl.upload_buffer = NULL;
}
static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) {
+2 -117
View File
@@ -976,17 +976,6 @@ iron_gpu_shader_t *gpu_create_shader(buffer_t *data, i32 shader_type) {
return shader;
}
static void write_attrib(char *file, int *output_len, const char *attrib, int index) {
if (index > -1) {
strcpy(file + (*output_len), attrib);
(*output_len) += strlen(attrib);
file[(*output_len)] = 0;
(*output_len) += 1;
file[(*output_len)] = index;
(*output_len) += 1;
}
}
#include "../../sources/libs/kong/analyzer.h"
#include "../../sources/libs/kong/compiler.h"
#include "../../sources/libs/kong/disasm.h"
@@ -1058,114 +1047,10 @@ iron_gpu_shader_t *gpu_create_shader_from_source(string_t *source, iron_gpu_shad
return NULL;
}
ID3D11ShaderReflection *reflector = NULL;
D3DReflect(shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), shader_buffer->lpVtbl->GetBufferSize(shader_buffer), &IID_ID3D11ShaderReflection, (void **)&reflector);
int size = shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
char *file = malloc(size * 2);
int output_len = 0;
if (shader_type == IRON_GPU_SHADER_TYPE_VERTEX) {
bool has_col = strstr(temp_string_s, " col:") != NULL || strstr(temp_string_s, " col :") != NULL;
bool has_nor = strstr(temp_string_s, " nor:") != NULL || strstr(temp_string_s, " nor :") != NULL;
bool has_pos = strstr(temp_string_s, " pos:") != NULL || strstr(temp_string_s, " pos :") != NULL;
bool has_tex = strstr(temp_string_s, " tex:") != NULL || strstr(temp_string_s, " tex :") != NULL;
int icol = -1;
int inor = -1;
int ipos = -1;
int itex = -1;
int index = 0;
if (has_col) icol = index++;
if (has_nor) inor = index++;
if (has_pos) ipos = index++;
if (has_tex) itex = index++;
file[output_len] = (char)index;
output_len += 1;
write_attrib(file, &output_len, "col", icol);
write_attrib(file, &output_len, "nor", inor);
write_attrib(file, &output_len, "pos", ipos);
write_attrib(file, &output_len, "tex", itex);
}
else {
file[output_len] = 0;
output_len += 1;
}
D3D11_SHADER_DESC desc;
reflector->lpVtbl->GetDesc(reflector, &desc);
file[output_len] = desc.BoundResources;
output_len += 1;
for (int i = 0; i < desc.BoundResources; ++i) {
D3D11_SHADER_INPUT_BIND_DESC bindDesc;
reflector->lpVtbl->GetResourceBindingDesc(reflector, i, &bindDesc);
strcpy(file + output_len, bindDesc.Name);
output_len += strlen(bindDesc.Name);
file[output_len] = 0;
output_len += 1;
file[output_len] = bindDesc.BindPoint;
output_len += 1;
}
ID3D11ShaderReflectionConstantBuffer *constants = reflector->lpVtbl->GetConstantBufferByName(reflector, "$Globals");
D3D11_SHADER_BUFFER_DESC buffer_desc;
hr = constants->lpVtbl->GetDesc(constants, &buffer_desc);
if (hr == S_OK) {
file[output_len] = buffer_desc.Variables;
output_len += 1;
for (int i = 0; i < buffer_desc.Variables; ++i) {
ID3D11ShaderReflectionVariable *variable = constants->lpVtbl->GetVariableByIndex(constants, i);
D3D11_SHADER_VARIABLE_DESC variable_desc;
hr = variable->lpVtbl->GetDesc(variable, &variable_desc);
if (hr == S_OK) {
strcpy(file + output_len, variable_desc.Name);
output_len += strlen(variable_desc.Name);
file[output_len] = 0;
output_len += 1;
*(uint32_t *)(file + output_len) = variable_desc.StartOffset;
output_len += 4;
*(uint32_t *)(file + output_len) = variable_desc.Size;
output_len += 4;
D3D11_SHADER_TYPE_DESC type_desc;
ID3D11ShaderReflectionType *type = variable->lpVtbl->GetType(variable);
hr = type->lpVtbl->GetDesc(type, &type_desc);
if (hr == S_OK) {
file[output_len] = type_desc.Columns;
output_len += 1;
file[output_len] = type_desc.Rows;
output_len += 1;
}
else {
file[output_len] = 0;
output_len += 1;
file[output_len] = 0;
output_len += 1;
}
}
}
}
else {
file[output_len] = 0;
output_len += 1;
}
memcpy(file + output_len, (char *)shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), shader_buffer->lpVtbl->GetBufferSize(shader_buffer));
output_len += shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
shader_buffer->lpVtbl->Release(shader_buffer);
reflector->lpVtbl->Release(reflector);
shader = (iron_gpu_shader_t *)malloc(sizeof(iron_gpu_shader_t));
iron_gpu_shader_init(shader, file, (int)output_len, shader_type);
free(file);
iron_gpu_shader_init(shader, (char *)shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), size, shader_type);
shader_buffer->lpVtbl->Release(shader_buffer);
#elif defined(IRON_METAL)
+7 -112
View File
@@ -1,5 +1,5 @@
// Simple text processing tool for converting shaders at runtime.
// kong wrapper
#include <stdio.h>
#include <stdlib.h>
@@ -28,17 +28,6 @@
#include <d3d11.h>
#include <D3Dcompiler.h>
static void write_attrib(char *file, int *output_len, const char *attrib, int index) {
if (index > -1) {
strcpy(file + (*output_len), attrib);
(*output_len) += strlen(attrib);
file[(*output_len)] = 0;
(*output_len) += 1;
file[(*output_len)] = index;
(*output_len) += 1;
}
}
char *hlsl_to_bin(char *source, char *shader_type, char *to) {
char *type;
if (string_equals(shader_type, "vert")) {
@@ -58,108 +47,11 @@ char *hlsl_to_bin(char *source, char *shader_type, char *to) {
return NULL;
}
ID3D11ShaderReflection *reflector = NULL;
D3DReflect(shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), shader_buffer->lpVtbl->GetBufferSize(shader_buffer), &IID_ID3D11ShaderReflection, (void **)&reflector);
int len = shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
char *file = malloc(len * 2);
int output_len = 0;
bool has_col = strstr(source, " col:") != NULL || strstr(source, " col :") != NULL;
bool has_nor = strstr(source, " nor:") != NULL || strstr(source, " nor :") != NULL;
bool has_pos = strstr(source, " pos:") != NULL || strstr(source, " pos :") != NULL;
bool has_tex = strstr(source, " tex:") != NULL || strstr(source, " tex :") != NULL;
int icol = -1;
int inor = -1;
int ipos = -1;
int itex = -1;
int index = 0;
if (has_col) icol = index++;
if (has_nor) inor = index++;
if (has_pos) ipos = index++;
if (has_tex) itex = index++;
file[output_len] = (char)index;
output_len += 1;
write_attrib(file, &output_len, "col", icol);
write_attrib(file, &output_len, "nor", inor);
write_attrib(file, &output_len, "pos", ipos);
write_attrib(file, &output_len, "tex", itex);
D3D11_SHADER_DESC desc;
reflector->lpVtbl->GetDesc(reflector, &desc);
file[output_len] = desc.BoundResources;
output_len += 1;
for (int i = 0; i < desc.BoundResources; ++i) {
D3D11_SHADER_INPUT_BIND_DESC bindDesc;
reflector->lpVtbl->GetResourceBindingDesc(reflector, i, &bindDesc);
strcpy(file + output_len, bindDesc.Name);
output_len += strlen(bindDesc.Name);
file[output_len] = 0;
output_len += 1;
file[output_len] = bindDesc.BindPoint;
output_len += 1;
}
ID3D11ShaderReflectionConstantBuffer *constants = reflector->lpVtbl->GetConstantBufferByName(reflector, "$Globals");
D3D11_SHADER_BUFFER_DESC buffer_desc;
hr = constants->lpVtbl->GetDesc(constants, &buffer_desc);
if (hr == S_OK) {
file[output_len] = buffer_desc.Variables;
output_len += 1;
for (int i = 0; i < buffer_desc.Variables; ++i) {
ID3D11ShaderReflectionVariable *variable = constants->lpVtbl->GetVariableByIndex(constants, i);
D3D11_SHADER_VARIABLE_DESC variable_desc;
hr = variable->lpVtbl->GetDesc(variable, &variable_desc);
if (hr == S_OK) {
strcpy(file + output_len, variable_desc.Name);
output_len += strlen(variable_desc.Name);
file[output_len] = 0;
output_len += 1;
*(uint32_t *)(file + output_len) = variable_desc.StartOffset;
output_len += 4;
*(uint32_t *)(file + output_len) = variable_desc.Size;
output_len += 4;
D3D11_SHADER_TYPE_DESC type_desc;
ID3D11ShaderReflectionType *type = variable->lpVtbl->GetType(variable);
hr = type->lpVtbl->GetDesc(type, &type_desc);
if (hr == S_OK) {
file[output_len] = type_desc.Columns;
output_len += 1;
file[output_len] = type_desc.Rows;
output_len += 1;
}
else {
file[output_len] = 0;
output_len += 1;
file[output_len] = 0;
output_len += 1;
}
}
}
}
else {
file[output_len] = 0;
output_len += 1;
}
memcpy(file + output_len, (char *)shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), shader_buffer->lpVtbl->GetBufferSize(shader_buffer));
output_len += shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
shader_buffer->lpVtbl->Release(shader_buffer);
reflector->lpVtbl->Release(reflector);
FILE *fp = fopen(to, "wb");
fwrite(file, 1, output_len, fp);
int len = shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
fwrite((char *)shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), 1, len, fp);
fclose(fp);
free(file);
shader_buffer->lpVtbl->Release(shader_buffer);
}
void hlslbin(const char *from, const char *to) {
@@ -222,6 +114,9 @@ void kong_compile(const char *from, const char *to) {
}
analyze();
// vulkan
// transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE);
char output[512];
strcpy(output, to);