From 1da96e0dbe95f5078623e2b0469152692833a096 Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Tue, 22 Nov 2022 23:02:54 -0500 Subject: file io, compression --- main.cpp | 181 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 108 insertions(+), 73 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 2d5b501..9d8c968 100644 --- a/main.cpp +++ b/main.cpp @@ -41,12 +41,18 @@ #define STB_IMAGE_RESIZE_IMPLEMENTATION #include "lib/stb_image_resize.h" +extern "C" { +#include "lib/miniz.h" +#include "lib/miniz.c" +} + #include "defines.h" #include "my_math.h" #include "structs.h" - -#include "memory.h" +#if STABLE #include "stable_diffusion.h" +#endif +#include "memory.h" #include "main.h" #include "debug.h" @@ -66,17 +72,20 @@ static uint64 BitmapBlockSize; static instruction_mode InstructionMode = instruction_mode_scalar; static uint32 RandomGlobalIncrement = 0; -#if SD +#if STABLE #include "lib/base64.c" #include -#include "stable_diffusion.cpp" #endif #include "memory.cpp" #include "undo.cpp" +#include "io.cpp" #include "strings.cpp" #include "threading.cpp" #include "createcalls.cpp" +#if STABLE +#include "stable_diffusion.cpp" +#endif // #include "ffmpeg_backend.cpp" #include "my_imgui_widgets.cpp" #include "prenderer.cpp" @@ -173,17 +182,18 @@ Main_InputTest(project_data *File, project_state *State, memory *Memory, ui *UI, ImGui_DebugUndoTree(Memory, State); } +#if 0 if (State->Initializing == 3) { Source_UICreateButton(File, State, Memory, Sorted.CompArray, Sorted.LayerArray); block_layer *Layera = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 2); - Layera->x.IsToggled = true; - Layera->y.IsToggled = true; + // Layera->x.IsToggled = true; + // Layera->y.IsToggled = true; block_layer *Layerb = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 1); - Layerb->x.IsToggled = true; - Layerb->y.IsToggled = true; + // Layerb->x.IsToggled = true; + // Layerb->y.IsToggled = true; block_layer *Layerc = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 0); - Layerc->x.IsToggled = true; - Layerc->y.IsToggled = true; + // Layerc->x.IsToggled = true; + // Layerc->y.IsToggled = true; // Layer_Select(Memory, State, 0); // Layer_Select(Memory, State, 1); // Layer_Select(Memory, State, 2); @@ -194,9 +204,16 @@ Main_InputTest(project_data *File, project_state *State, memory *Memory, ui *UI, // Layer_DeselectAll(Memory, State, File->Layer_Count); block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 0); + block_layer *Layer2 = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 1); + block_layer *Layer3 = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, 2); Layer_Select(Memory, State, 0); - History_Entry_Commit(Memory, "Add keyframe"); + // History_Entry_Commit(Memory, "Add keyframe"); property_channel *Property = &Layer->x; + Layer->scale.CurrentValue = 0.3; + Layer2->scale.CurrentValue = 0.3; + Layer3->scale.CurrentValue = 0.3; + /* + State->UpdateFrame = true; { bezier_point Point = { 1, {1, 0, -5, 0, 5, 0}, interpolation_type_bezier, 0, {0, 0, 0}, 0 }; Bezier_Add(Memory, Property, Point); @@ -210,7 +227,6 @@ Main_InputTest(project_data *File, project_state *State, memory *Memory, ui *UI, Bezier_Add(Memory, Property, Point); } History_Entry_End(Memory); - /* Property = &Layer->y; { bezier_point Point = { 1, {10, 50, -5, 0, 5, 0}, interpolation_type_bezier, 0, {0, 0, 0}, 0 }; @@ -249,15 +265,16 @@ Main_InputTest(project_data *File, project_state *State, memory *Memory, ui *UI, History_Entry_End(Memory); */ } +#endif ImGui_Viewport(File, State, UI, Memory, io, textureID, Sorted.CompArray, Sorted.LayerArray); ImGui_Timeline(File, State, Memory, UI, io, Sorted.CompArray, Sorted.LayerArray, Sorted.PropertyInfo, Sorted.PropertyArray); ImGui_File(File, State, Memory, io, Sorted.CompArray, Sorted.LayerArray); ImGui_PropertiesPanel(File, State, UI, Memory, io); ImGui_ColorPanel(File, State, UI, Memory, io); -#if SD - ImGui_SD_Prompt(File, State, UI, Memory, io); - ImGui_SD_Thumbnail(File, State, UI, Memory, io, Sorted.SourceArray, Sorted.TempSourceCount, textureID); +#if STABLE + ImGui_SD_Prompt(File, State, UI, Memory, io, Sorted.CompArray, Sorted.LayerArray); + ImGui_SD_Thumbnail(File, State, UI, Memory, io, Sorted.CompArray, Sorted.LayerArray, Sorted.SourceArray, Sorted.TempSourceCount); #endif ImGui_Menu(File, State, UI, Memory, io); @@ -308,48 +325,50 @@ Render_Comp(project_data *File, project_state *State, memory *Memory, ImGuiIO io if (Layer->Frame_Start <= Frame_Current && Layer->Frame_End > Frame_Current && Layer->IsVisible) { - int32 Offset = (State->Interact_Active == interact_type_keyframe_move) ? (int32)State->Interact_Offset[0] : 0; - for (int h = 0; h < AmountOf(Layer->Property); h++) { - property_channel *Property = &Layer->Property[h]; - sorted_property_info *InfoLocation = SortedPropertyInfo + (Index_Physical * 7) + h; - uint16 *ArrayLocation = SortedPropertyArray + (Index_Physical * 7 * MAX_KEYFRAMES_PER_BLOCK) + (h * MAX_KEYFRAMES_PER_BLOCK); - if (Property->Block_Bezier_Count) { - real32 MinY, MaxY; - Property_MinMax_Y(Memory, State, Property, InfoLocation, &MinY, &MaxY); - real32 Y_Increment = 1 / (MaxY - MinY); - v2 FirstPointPos[3]; - bezier_point *FirstPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[0]); - Bezier_EvaluateValue(State, FirstPointAddress, FirstPointPos); - v2 LastPointPos[3]; - bezier_point *LastPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[Property->Keyframe_Count - 1]); - Bezier_EvaluateValue(State, LastPointAddress, LastPointPos); - if (FirstPointPos[0].x >= Frame_Current) { - Property->CurrentValue = FirstPointPos[0].y; - } else if (LastPointPos[0].x <= Frame_Current) { - Property->CurrentValue = LastPointPos[0].y; - } else { - int KeyframeIndex = 0; - for (;;) { + if (State->UpdateKeyframes) { + int32 Offset = (State->Interact_Active == interact_type_keyframe_move) ? (int32)State->Interact_Offset[0] : 0; + for (int h = 0; h < AmountOf(Layer->Property); h++) { + property_channel *Property = &Layer->Property[h]; + sorted_property_info *InfoLocation = Property_GetSortedInfo(SortedPropertyInfo, i, h); + uint16 *ArrayLocation = Property_GetSortedArray(SortedPropertyArray, i, h); + if (Property->Block_Bezier_Count) { + real32 MinY, MaxY; + Property_MinMax_Y(Memory, State, Property, InfoLocation, &MinY, &MaxY); + real32 Y_Increment = 1 / (MaxY - MinY); + v2 FirstPointPos[3]; + bezier_point *FirstPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[0]); + Bezier_EvaluateValue(State, FirstPointAddress, FirstPointPos); + v2 LastPointPos[3]; + bezier_point *LastPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[Property->Keyframe_Count - 1]); + Bezier_EvaluateValue(State, LastPointAddress, LastPointPos); + if (FirstPointPos[0].x >= Frame_Current) { + Property->CurrentValue = FirstPointPos[0].y; + } else if (LastPointPos[0].x <= Frame_Current) { + Property->CurrentValue = LastPointPos[0].y; + } else { + int KeyframeIndex = 0; + for (;;) { + v2 PointPos[3]; + bezier_point *PointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex + 1]); + Bezier_EvaluateValue(State, PointAddress, PointPos, 1, Y_Increment); + if (PointPos[0].x >= Frame_Current) + break; + KeyframeIndex++; + } v2 PointPos[3]; - bezier_point *PointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex + 1]); + bezier_point *PointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex]); Bezier_EvaluateValue(State, PointAddress, PointPos, 1, Y_Increment); - if (PointPos[0].x >= Frame_Current) - break; - KeyframeIndex++; - } - v2 PointPos[3]; - bezier_point *PointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex]); - Bezier_EvaluateValue(State, PointAddress, PointPos, 1, Y_Increment); - v2 NextPointPos[3]; - bezier_point *NextPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex + 1]); - Bezier_EvaluateValue(State, NextPointAddress, NextPointPos, 1, Y_Increment); - if (PointAddress->Type == interpolation_type_hold) { - Property->CurrentValue = PointPos[0].y; - } else if (PointAddress->Type == interpolation_type_linear && NextPointAddress->Type == interpolation_type_linear) { - real32 Ratio = (Frame_Current - PointPos[0].x) / (NextPointPos[0].x - PointPos[0].x); - Property->CurrentValue = PointPos[0].y + ((NextPointPos[0].y - PointPos[0].y) * Ratio); - } else { - Property->CurrentValue = Bezier_SolveYForX(PointPos[0], PointPos[0] + PointPos[2], NextPointPos[0] + NextPointPos[1], NextPointPos[0], Frame_Current); + v2 NextPointPos[3]; + bezier_point *NextPointAddress = Bezier_LookupAddress(Memory, Property, ArrayLocation[KeyframeIndex + 1]); + Bezier_EvaluateValue(State, NextPointAddress, NextPointPos, 1, Y_Increment); + if (PointAddress->Type == interpolation_type_hold) { + Property->CurrentValue = PointPos[0].y; + } else if (PointAddress->Type == interpolation_type_linear && NextPointAddress->Type == interpolation_type_linear) { + real32 Ratio = (Frame_Current - PointPos[0].x) / (NextPointPos[0].x - PointPos[0].x); + Property->CurrentValue = PointPos[0].y + ((NextPointPos[0].y - PointPos[0].y) * Ratio); + } else { + Property->CurrentValue = Bezier_SolveYForX(PointPos[0], PointPos[0] + PointPos[2], NextPointPos[0] + NextPointPos[1], NextPointPos[0], Frame_Current); + } } } } @@ -357,21 +376,31 @@ Render_Comp(project_data *File, project_state *State, memory *Memory, ImGuiIO io layer_bitmap_state *BitmapState = &State->Render.Bitmap[Index_Physical]; void *BitmapAddress = NULL; + int Width = 0, Height = 0, BytesPerPixel = 0; uint64 ScratchActive = 0; if (!Layer->IsPrecomp) { - block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Layer->Block_Source_Index); - if (Source->Type == source_type_principal) { + block_source *Source; + if (State->PreviewSource == -1) { + Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Layer->Block_Source_Index); + } else { + Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, State->PreviewSource); + } + Width = Source->Width; + Height = Source->Height; + BytesPerPixel = Source->BytesPerPixel; + if (Source->Type == source_type_principal || Source->Type == source_type_principal_temp) { if (State->Interact_Active == interact_type_brush && State->Brush.LayerToPaint_Index == Index_Physical) { + Assert(Source->Type == source_type_principal); void *SourceBitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index, 0); - rectangle RenderRegion = { 0, 0, Source->Width, Source->Height }; + ScratchActive = Source->Width * Source->Height * Source->BytesPerPixel; + void *SecondSourceBitmap = Memory_PushScratch(Memory, ScratchActive); + Memory_Copy((uint8 *)SecondSourceBitmap, (uint8 *)SourceBitmapAddress, ScratchActive); // TODO(fox): Do all these extra precomputes really make a difference in the renderer? + rectangle RenderRegion = { 0, 0, Source->Width, Source->Height }; direct_info Info = { (real32)Source->Width, (real32)Source->Height, (real32)Source->BytesPerPixel, (real32)Source->Width * Source->BytesPerPixel, Bitmap_ByteInfo(Source->BytesPerPixel), blend_normal, RenderRegion, State->Brush.TransientBitmap}; - ScratchActive = Source->Width * Source->Height * Source->BytesPerPixel; - void *SecondSourceBitmap = Memory_PushScratch(Memory, ScratchActive); - Memory_Copy((uint8 *)SecondSourceBitmap, (uint8 *)SourceBitmapAddress, ScratchActive); - Render_Main((void *)&Info, SecondSourceBitmap, render_type_notransform, RenderRegion); + Render_Main((void *)&Info, SecondSourceBitmap, render_type_notransform, State->Brush.CacheBounds); BitmapAddress = SecondSourceBitmap; } else { BitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index, 0); @@ -404,6 +433,9 @@ Render_Comp(project_data *File, project_state *State, memory *Memory, ImGuiIO io block_composition *Precomp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, Layer->Block_Source_Index); BitmapAddress = Render_Comp(File, State, Memory, io, SortedCompArray, SortedLayerArray, SortedPropertyInfo, SortedPropertyArray, Layer->Block_Source_Index, (int32)Layer->time.CurrentValue); + Width = Precomp->Width; + Height = Precomp->Height; + BytesPerPixel = Precomp->BytesPerPixel; } Assert(BitmapAddress); @@ -421,7 +453,7 @@ Render_Comp(project_data *File, project_state *State, memory *Memory, ImGuiIO io } */ - transform_info T = Transform_Calculate(State, Memory, File, Layer, Comp); + transform_info T = Transform_Calculate(State, Memory, File, Layer, Comp, Width, Height, BytesPerPixel); T.SourceBuffer = BitmapAddress; rectangle RenderRegion = {0, 0, Comp->Width, Comp->Height}; @@ -438,8 +470,6 @@ Render_Comp(project_data *File, project_state *State, memory *Memory, ImGuiIO io static void Main_Renderer(project_data *File, project_state *State, memory *Memory, SDL_Window *window, GLuint textureID, ImGuiIO io) { - State->UpdateFrame = false; - block_composition *MainComp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, File->PrincipalCompIndex); sorted_file Sorted = File_Sort_Push(File, State, Memory); @@ -459,6 +489,9 @@ Main_Renderer(project_data *File, project_state *State, memory *Memory, SDL_Wind glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, MainComp->Width, MainComp->Height, GL_RGBA, ByteFlag2, MainCompBuffer); // TODO(fox): garbage collect AV state! + + State->UpdateFrame = false; + State->UpdateKeyframes = false; } int main(int argc, char *argv[]) { @@ -553,21 +586,21 @@ int main(int argc, char *argv[]) { File->Comp_Count = 1; -#if 1 +#if 0 { - uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/a.jpg"); + uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/t_a.png"); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(&Memory, F_Sources, 0); Source->IsSelected = true; } { - uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/b.jpg"); + uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/t_b.png"); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(&Memory, F_Sources, 1); Source->IsSelected = true; } { - uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/c.jpg"); + uint16 SourceIndex = Source_Generate(File, State, &Memory, (void *)"../asset/t_c.png"); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(&Memory, F_Sources, 2); Source->IsSelected = true; } @@ -678,11 +711,12 @@ int main(int argc, char *argv[]) { Brush_CalcBitmapAlphaFromSize(&Memory, &State->Brush, 4); State_BindBrushTexture(&Memory, &State->Brush, 4); -#if SD + // File_Open(File, State, &Memory, "test"); + +#if STABLE curl_global_init(CURL_GLOBAL_ALL); curl_state MainHandle = {}; curl_state ProgHandle = {}; - uint32 Inc = 0; #endif while (State->IsRunning) @@ -690,9 +724,9 @@ int main(int argc, char *argv[]) { // State->Interact_Active = interact_type_layer_move; // State->Interact_Offset[1] = -3.0f; -#if SD +#if STABLE if (State->CurlActive) { - Curl_Main(); + Curl_Main(File, State, &Memory, &MainHandle, &ProgHandle); } #endif @@ -702,6 +736,7 @@ int main(int argc, char *argv[]) { block_composition *MainComp = (block_composition *)Memory_Block_AddressAtIndex(&Memory, F_Precomps, File->PrincipalCompIndex); State->Frame_Current = ((State->Frame_Current + 1) >= MainComp->Frame_Count) ? 0 : State->Frame_Current + 1; State->UpdateFrame = true; + State->UpdateKeyframes = true; } if (State->UpdateFrame) { -- cgit v1.2.3