Now we store height in TextureDataS. + wrote codegen for writing texture to png file

This commit is contained in:
Андреев Григорий 2025-09-24 17:49:59 +03:00
parent c8ae4ab017
commit ad5b3c58d6
6 changed files with 60 additions and 14 deletions

View File

@ -14,7 +14,7 @@ color_type_name_in_png color_types_names_in_png[3] = {
{4, cstr("PNG_COLOR_TYPE_RGBA")},
};
NODISCARD VecU8 generate_margaret_png_texture_data_methods(SpanU8 format_signature, int depth, int channel_count) {
NODISCARD VecU8 generate_margaret_png_texture_data_methods(SpanU8 format_signature, S64 depth, int channel_count) {
if (depth != 8)
abortf("Please no");
for (size_t i = 0; i < ARRAY_SIZE(color_types_names_in_png); i++) {
@ -27,6 +27,55 @@ NODISCARD VecU8 generate_margaret_png_texture_data_methods(SpanU8 format_signatu
VecU8 g_tex = VecU8_fmt("TextureData%s", format_signature);
SpanU8 tex = VecU8_to_span(&g_tex);
VecU8_append_vec(&res, VecU8_fmt(
"ResultVoidOrVecU8 %s_write_to_png(const %s* self, SpanU8 filename) {\n" /* tex, tex */
SPACE "VecU8 nt_filename = VecU8_fmt(\"%%s%%c\", filename, 0);\n"
SPACE "FILE *fp = fopen((CSTR)nt_filename.buf, \"wb\");\n"
SPACE "VecU8_drop(nt_filename);\n"
SPACE "if (!fp) {\n"
SPACE SPACE "return (ResultVoidOrVecU8){.variant = Result_Err, .err = VecU8_fmt(\"Unable to open file %%s\", filename)};\n"
SPACE "}\n"
SPACE "png_structp pngshka = png_create_write_struct(PNG_LIBPNG_VER_STRING,\n"
SPACE SPACE "NULL, margaret_libpng_h_error_cb, margaret_libpng_h_warning_cb);\n"
SPACE "if (!pngshka)\n"
SPACE SPACE "abortf(\"png_create_write_struct\");\n"
SPACE "png_infop info = png_create_info_struct(pngshka);\n"
SPACE "if (!info)\n"
SPACE SPACE"abortf(\"png_create_info_struct\");\n"
SPACE "png_bytep* row_pointers = NULL;\n"
SPACE "if (setjmp(png_jmpbuf(pngshka))){\n"
SPACE SPACE "png_destroy_write_struct(&pngshka, &info);\n"
SPACE SPACE "fclose(fp);\n"
SPACE SPACE "free(row_pointers);\n"
SPACE SPACE "return (ResultVoidOrVecU8){.variant = Result_Err, .err = VecU8_from_cstr(\"Some png error happened\")};\n"
SPACE "}\n"
SPACE "png_init_io(pngshka, fp);\n"
SPACE "U32 width = self->width;\n"
SPACE "U32 height = self->height;\n"
SPACE "png_set_IHDR(pngshka, info, width, height, %i, %s,\n" /* depth, color_type */
SPACE SPACE "PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);\n"
SPACE "row_pointers = calloc(height, sizeof(row_pointers));\n"
SPACE "for (U32 y = 0; y < height; y++) {\n"
SPACE SPACE "row_pointers[height - 1 - y] = (png_bytep)((%s_at(self, 0, y)));\n" /* tex */
SPACE "}\n"
SPACE "png_set_rows(pngshka, info, row_pointers);\n"
SPACE "png_write_png(pngshka, info, 0, NULL);\n"
SPACE "/* No more errors */\n"
SPACE "free(row_pointers);\n"
SPACE "png_destroy_write_struct(&pngshka, &info);\n"
SPACE "fclose(fp);\n"
SPACE "return (ResultVoidOrVecU8){.variant = Result_Ok};\n"
"}\n\n"
"/* Aborts on error */\n"
"void %s_write_to_png_nofail(const %s* self, SpanU8 filename) {\n" /* tex, tex*/
SPACE "ResultVoidOrVecU8 res = %s_write_to_png(self, filename);\n" /* tex */
SPACE "if (res.variant == Result_Err) {\n"
SPACE SPACE "SpanU8_fprint(VecU8_to_span(&res.err), stderr);\n"
SPACE SPACE "abortf(\" %s_write_to_png\\n\");\n" /* tex */
SPACE "}\n"
"}\n",
tex, tex, depth, color_type, tex, tex, tex, tex, tex));
/* Non clonable structure */
VecU8_append_vec(&res, VecU8_fmt(
"typedef struct {\n"
@ -66,7 +115,7 @@ void generate_margaret_png_pixel_masses_header() {
SPACE "printf(\"[.] %s\\n\", warning);\n"
"}\n\n"));
VecU8_append_vec(&header.result, generate_margaret_png_texture_data_methods(cstr("TextureDataR8G8B8A8"), 8, 4));
VecU8_append_vec(&header.result, generate_margaret_png_texture_data_methods(cstr("R8G8B8A8"), 8, 4));
finish_header(header);
}

View File

@ -25,6 +25,7 @@ NODISCARD VecU8 generate_texture_data_struct_and_necc_methods(SpanU8 tex, SpanU8
"typedef struct {\n"
SPACE "%s pixels;\n"
SPACE "size_t width;\n"
SPACE "size_t height;\n"
"} %s;\n\n", pixvec, tex);
/* Method _new() */
VecU8_append_vec(&res, VecU8_fmt(
@ -37,11 +38,6 @@ NODISCARD VecU8 generate_texture_data_struct_and_necc_methods(SpanU8 tex, SpanU8
"void %s_drop(%s self) {\n"
SPACE "%s_drop(self.pixels);\n"
"}\n\n", tex, tex, pixvec));
/* Method _get_height() */
VecU8_append_vec(&res, VecU8_fmt(
"size_t %s_get_height(const %s* self) {\n"
SPACE "return self->pixels.len / self->width;\n"
"}\n\n", tex, tex));
/* Methods _at and _cat */
VecU8_append_vec(&res, generate_texture_data_method_at(tex, pixvec, memb, false));
VecU8_append_vec(&res, generate_texture_data_method_at(tex, pixvec, memb, true));

View File

@ -42,12 +42,13 @@ NODISCARD VecU8 generate_trait_table_structure(NamedTraitDefRecordRef trait){
}
typedef struct {
NamedTraitDefRecordRef trait;
bool box;
bool ref;
bool mut_ref;
} trait_wrapper_boil_options;
NODISCARD VecU8 generate_trait_wrapper_boilerplate(NamedTraitDefRecordRef trait, trait_wrapper_boil_options op) {
NODISCARD VecU8 generate_trait_wrapper_boilerplate(trait_wrapper_boil_options op) {
VecU8 res = VecU8_new();
// todo: write it
return res;

View File

@ -4,7 +4,7 @@
#include "../../../gen/l1/pixel_masses.h"
#include "../../l1/core/util.h"
#include "../../l1/core/VecU8_as_str.h"
#include "../../../gen/l1/ResultVoidOrVecU8"
#include "../../../gen/l1/ResultVoidOrVecU8.h"
#include <png.h>
// todo: generate all of this automaticcally
@ -43,7 +43,7 @@ ResultVoidOrVecU8 TextureDataR8G8B8A8_write_to_png(const TextureDataR8G8B8A8* se
png_init_io(pngshka, fp);
U32 width = self->width;
U32 height = TextureDataR8G8B8A8_get_height(self);
U32 height = self->height;
png_set_IHDR(pngshka, info, width, height, 8, PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

View File

@ -6,7 +6,7 @@
void TextureDataR8G8B8A8_print(const TextureDataR8G8B8A8* self) {
U64 width = self->width;
U64 height = TextureDataR8G8B8A8_get_height(self);
// U64 height = ;
U64 cell_width = MAX_U64(1, width / 350);
U64 cell_height = MAX_U64(1, cell_width * 14 / 8);
for (U64 CY = 0; CY < height; CY += cell_height) {
@ -34,7 +34,7 @@ void TextureDataR8G8B8A8_print(const TextureDataR8G8B8A8* self) {
/* Fixes several of my generated textures */
NODISCARD TextureDataR8G8B8A8 TextureDataR8G8B8A8_expand_nontransparent_1px(const TextureDataR8G8B8A8* self) {
S32 width = (S32)self->width;
S32 height = (S32)TextureDataR8G8B8A8_get_height(self);
S32 height = (S32)self->height;
TextureDataR8G8B8A8 res = TextureDataR8G8B8A8_new(width, height);
// S32 chain[9][2] = {{0, 0}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
for (S32 y = 0; y < height; y++) {

View File

@ -1862,9 +1862,9 @@ int main() {
vk_ctx->device_zbuffer_image = margaret_prep_image_mem_info_of_zbuffer(
MAX_WIN_WIDTH, MAX_WIN_HEIGHT, zbuffer_format.some);
vk_ctx->device_cyl_1_diffuse_texture = margaret_prep_image_mem_info_of_gpu_texture_srgba(
vk_ctx->cyl_1_diffuse_tex.width, TextureDataR8G8B8A8_get_height(&vk_ctx->cyl_1_diffuse_tex));
vk_ctx->cyl_1_diffuse_tex.width, &vk_ctx->cyl_1_diffuse_tex.height);
vk_ctx->device_cyl_1_normal_texture = margaret_prep_image_mem_info_of_gpu_texture_unorm_32(
vk_ctx->cyl_1_normal_tex.width, TextureDataR8G8B8A8_get_height(&vk_ctx->cyl_1_normal_tex));
vk_ctx->cyl_1_normal_tex.width, &vk_ctx->cyl_1_normal_tex.height);
PtrMargaretImageInMemoryInfo device_mem_images_SPAN[] = {
&vk_ctx->device_IT1_image, &vk_ctx->device_zbuffer_image, &vk_ctx->device_cyl_1_diffuse_texture,