From 2e0e12140e95be18abf3cf7e54230ef22f410fbd Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Tue, 23 Aug 2022 14:40:02 -0400 Subject: effects can use gl state --- build.sh | 4 +- createcalls.cpp | 14 ++++--- effects.cpp | 44 ++++----------------- effects.h | 9 ++--- effects_gl.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++ effects_software.cpp | 2 +- gl_calls.cpp | 108 ++++++++++++++++++++++++++++++++++++++++----------- main.cpp | 1 - 8 files changed, 209 insertions(+), 74 deletions(-) create mode 100644 effects_gl.cpp diff --git a/build.sh b/build.sh index a41428b..61109c7 100755 --- a/build.sh +++ b/build.sh @@ -1,8 +1,8 @@ #!/bin/bash -OPTIMIZATION="-O2" # Enable optimization. +OPTIMIZATION="-g" # Enable optimization. DEBUG=1 # Compile with debug UI. -IMGUI=1 # Compile ImGui libs. Our custom ImGui functions still compile on zero. +IMGUI=0 # Compile ImGui libs. Our custom ImGui functions still compile on zero. THREADED=1 # Compile with threading. Useful to disable when stepping through the renderer. WINDOWS=0 # Compile for Windows with Mingw. ARM=0 # Compile on ARM machines. diff --git a/createcalls.cpp b/createcalls.cpp index f8c166e..193deee 100644 --- a/createcalls.cpp +++ b/createcalls.cpp @@ -237,10 +237,12 @@ Layer_UpdateBitmap(project_data *File, project_layer *Layer, memory *Memory, int Bitmap_StencilAlpha(Bitmap->Data, DestBuffer, BytesPerPixel, UnpackedSize); } - for (int i = 0; i < Layer->NumberOfEffects; i++) - { - if (Layer->Effect[i]->IsActive) - Layer->Effect[i]->func(Source, BitmapInfo, Memory, Layer->Effect[i]->Property); + if (Layer->NumberOfEffects) { + for (int i = 0; i < Layer->NumberOfEffects; i++) + { + if (Layer->Effect[i]->IsActive) + Layer->Effect[i]->func(Source, BitmapInfo, Memory, Layer->Effect[i]->Property); + } } #endif } @@ -288,8 +290,8 @@ LoadTestFootage(project_data *File, project_state *State, memory *Memory) SourceString = String_GenerateFromChar(Memory, "../asset/p.mp4"); Source_Generate(File, Memory, SourceString); - // Layer_CreateFromSource(File, State, Memory, &File->Source[0]); - // SelectLayer(File->Layer[0], State, 0); + Layer_CreateFromSource(File, State, Memory, &File->Source[0]); + SelectLayer(File->Layer[0], State, 0); // property_channel *Property = &File->Layer[0]->x; // for (int i = 0; i < 16; i++) // Keyframe_Insert(Property, Memory, i*2, i*2*100); diff --git a/effects.cpp b/effects.cpp index 9de8008..2844d23 100644 --- a/effects.cpp +++ b/effects.cpp @@ -1,62 +1,34 @@ #include "effects.h" + #include "effects_software.cpp" +//#include "effects_gl.cpp" + +static void Effect_GL_Levels(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]); +static void Effect_GL_GaussianBlur(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]); static void Effect_DrawColor(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) { - Effect_DrawColor_Software(Source, BitmapInfo, Memory, Property); + Effect_Software_DrawColor(Source, BitmapInfo, Memory, Property); } static void Effect_Levels(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) { -#if 0 - real32 All_Start = Property[0].CurrentValue.f; - real32 All_Mid = Property[1].CurrentValue.f; - real32 All_End = Property[2].CurrentValue.f; - v4 Start = Property[3].CurrentValue.col; - v4 Mid = Property[4].CurrentValue.col; - v4 End = Property[5].CurrentValue.col; if (!BitmapInfo->HistogramVals) { uint64 Size = Bitmap_CalcUnpackedBytes(Source->Info.Width, Source->Info.Height, Source->Info.BytesPerPixel); BitmapInfo->HistogramVals = AllocateMemory(Memory, (sizeof(uint32) * 5 * 256), P_MiscCache); Bitmap_CalcHistogram(BitmapInfo->HistogramVals, BitmapInfo->BitmapBuffer, Source->Info.BytesPerPixel, Size); } - Assert(&BitmapInfo->Test); - gl_effect_layer Test = BitmapInfo->Test; - // glBindRenderbuffer(GL_RENDERBUFFER, Test.RBO); - /* - glUseProgram(TGL.ShaderProgram); - - int vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "Start"); - glUniform1f(vertexColorLocation, All_Start); - vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "Mid"); - glUniform1f(vertexColorLocation, All_Mid); - vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "End"); - glUniform1f(vertexColorLocation, All_End); - vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "StartCol"); - glUniform4f(vertexColorLocation, Start.r, Start.g, Start.b, Start.a); - vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "MidCol"); - glUniform4f(vertexColorLocation, Mid.r, Mid.g, Mid.b, Mid.a); - vertexColorLocation = glGetUniformLocation(TGL.ShaderProgram, "EndCol"); - glUniform4f(vertexColorLocation, End.r, End.g, End.b, End.a); - */ - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - - glReadBuffer(GL_COLOR_ATTACHMENT0); - uint16 Width = Source->Info.Width; - uint16 Height = Source->Info.Height; - uint8 *Data = (uint8 *)BitmapInfo->BitmapBuffer; - glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Data[0]); - glBindFramebuffer(GL_FRAMEBUFFER, 0); -#endif + Effect_GL_Levels(Source, BitmapInfo, Memory, Property); } static void Effect_GaussianBlur(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) { + Effect_GL_GaussianBlur(Source, BitmapInfo, Memory, Property); #if 0 real32 Radius = Property[0].CurrentValue.f; diff --git a/effects.h b/effects.h index fdd311e..d84bc25 100644 --- a/effects.h +++ b/effects.h @@ -31,18 +31,14 @@ struct effect { void (*func)(source *, layer_bitmap_info *, memory *, property_channel []); uint16 NumberOfProperties; effect_display_type DisplayType; - - struct gl_effect { - uint32 ShaderProgram; - }; - - gl_effect GL_Effect; property_channel Property[MAX_PROPERTIES_PER_EFFECT]; bool32 UIIsCollapsed = 0; bool32 IsActive = 1; }; +// TODO(fox): It'd probably be easier if we just switched to constructors at some point. + static effect_header EffectList[] { { "Solid Color", @@ -69,3 +65,4 @@ static effect_header EffectList[] { } } }; + diff --git a/effects_gl.cpp b/effects_gl.cpp new file mode 100644 index 0000000..2c1435b --- /dev/null +++ b/effects_gl.cpp @@ -0,0 +1,101 @@ +static uint16 Effect_GL_InitShader(int EffectIndex); +static uint16 GL_EffectShaderProgram[512]; +static void GL_BindDefaultVertexArrays(); +static void GL_InitTexture(gl_effect_layer *Test); + +void Effect_GL_Levels(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) +{ + real32 All_Start = Property[0].CurrentValue.f; + real32 All_Mid = Property[1].CurrentValue.f; + real32 All_End = Property[2].CurrentValue.f; + v4 Start = Property[3].CurrentValue.col; + v4 Mid = Property[4].CurrentValue.col; + v4 End = Property[5].CurrentValue.col; + + if (!BitmapInfo->Test.Initialized) { + GL_InitTexture(&BitmapInfo->Test); + } + + gl_effect_layer Test = BitmapInfo->Test; + uint8 *Data = (uint8 *)BitmapInfo->BitmapBuffer; + uint16 Width = Source->Info.Width; + uint16 Height = Source->Info.Height; + + glBindTexture(GL_TEXTURE_2D, Test.Texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, Data); + + glBindFramebuffer(GL_FRAMEBUFFER, Test.FramebufferObject); + glBindRenderbuffer(GL_RENDERBUFFER, Test.Color_Renderbuffer); + + uint16 ShaderProgram = Effect_GL_InitShader(1); + + GL_BindDefaultVertexArrays(); + + int vertexColorLocation = glGetUniformLocation(ShaderProgram, "Start"); + glUniform1f(vertexColorLocation, All_Start); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "Mid"); + glUniform1f(vertexColorLocation, All_Mid); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "End"); + glUniform1f(vertexColorLocation, All_End); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "StartCol"); + glUniform4f(vertexColorLocation, Start.r, Start.g, Start.b, Start.a); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "MidCol"); + glUniform4f(vertexColorLocation, Mid.r, Mid.g, Mid.b, Mid.a); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "EndCol"); + glUniform4f(vertexColorLocation, End.r, End.g, End.b, End.a); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + + glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Data[0]); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + +} + +void Effect_GL_GaussianBlur(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) +{ + real32 Radius = Property[0].CurrentValue.f; + + if (!BitmapInfo->Test.Initialized) { + GL_InitTexture(&BitmapInfo->Test); + } + + gl_effect_layer Test = BitmapInfo->Test; + uint8 *Data = (uint8 *)BitmapInfo->BitmapBuffer; + uint16 Width = Source->Info.Width; + uint16 Height = Source->Info.Height; + + glBindTexture(GL_TEXTURE_2D, Test.Texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, Data); + + glBindFramebuffer(GL_FRAMEBUFFER, Test.FramebufferObject); + glBindRenderbuffer(GL_RENDERBUFFER, Test.Color_Renderbuffer); + + uint16 ShaderProgram = Effect_GL_InitShader(2); + + GL_BindDefaultVertexArrays(); + + int vertexColorLocation = glGetUniformLocation(ShaderProgram, "Radius"); + glUniform1f(vertexColorLocation, Radius + 1.60f); + vertexColorLocation = glGetUniformLocation(ShaderProgram, "Direction"); + glUniform2f(vertexColorLocation, 1.0f, 0.0f); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Data[0]); + + // This blur works in two passes. + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, Data); + + vertexColorLocation = glGetUniformLocation(ShaderProgram, "Direction"); + glUniform2f(vertexColorLocation, 0.0f, 1.0f); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Data[0]); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + +} diff --git a/effects_software.cpp b/effects_software.cpp index 5b9e4d9..8451f6d 100644 --- a/effects_software.cpp +++ b/effects_software.cpp @@ -1,5 +1,5 @@ static void -Effect_DrawColor_Software(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) +Effect_Software_DrawColor(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, property_channel Property[]) { v4 FloatColor = Property[0].CurrentValue.col; blend_mode BlendMode = Property[1].CurrentValue.blendmode; diff --git a/gl_calls.cpp b/gl_calls.cpp index 8a7b905..d791a9c 100644 --- a/gl_calls.cpp +++ b/gl_calls.cpp @@ -1,6 +1,5 @@ #include "gl_calls.h" - const char *DefaultVertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "layout (location = 1) in vec2 aTexCoord;\n" @@ -30,8 +29,8 @@ const char *DefaultFragmentShaderSource = "#version 330 core\n" " FragColor = vec4(vec3(1.0f), Col.a);\n" "}\n" "}\0"; -#if 0 -const char *fragmentShaderSource = "#version 330 core\n" +const char *FragmentShaderEffectSource[] = {"", +"#version 330 core\n" "out vec4 FragColor;\n" "in vec2 TexCoord;\n" "uniform float Start;\n" @@ -47,12 +46,13 @@ const char *fragmentShaderSource = "#version 330 core\n" // individual channels "vec4 ColorI = pow(OutCol, MidCol);\n" "vec4 ValI = 1.0f / (EndCol - StartCol) * (ColorI - StartCol);\n" -// global channel +// global channel (doesn't affect alpha) "vec4 ColorG = pow(ValI, vec4(Mid));\n" "vec4 ValG = 1.0f / (End - Start) * (ColorG - Start);\n" +"ValG = vec4(ValG.rgb, ValI.a);\n" "FragColor = clamp(ValG, 0.0f, 1.0f);\n" -"}\0"; -const char *fragmentShaderSource = "#version 330 core\n" +"}\0", +"#version 330 core\n" "uniform float Radius;\n" "uniform vec2 Direction;\n" "uniform sampler2D ourTexture;\n" @@ -74,10 +74,13 @@ const char *fragmentShaderSource = "#version 330 core\n" "}\n" "void main(void) {\n" " gl_FragColor = blur(ourTexture, TexCoord, vec2(1280, 720), Direction);\n" -"}\0"; -#endif +"}\0" +}; + +#include "effects_gl.cpp" + static void GL_InitDefaultShader() { DefaultVertexShader = glCreateShader(GL_VERTEX_SHADER); @@ -182,7 +185,7 @@ GL_UpdateTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height, glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (Multisample) { - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, Width, Height, GL_TRUE); + // glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, Width, Height, GL_TRUE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); glBindRenderbuffer(GL_RENDERBUFFER, Test->Color_Renderbuffer); @@ -191,6 +194,8 @@ GL_UpdateTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height, glBindRenderbuffer(GL_RENDERBUFFER, (GLuint)Test->Stencil_Renderbuffer ); glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_STENCIL_INDEX8, Width, Height ); } else { + // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, + // GL_UNSIGNED_BYTE, Data); glBindTexture(GL_TEXTURE_2D, 0); glBindRenderbuffer(GL_RENDERBUFFER, Test->Color_Renderbuffer); @@ -211,12 +216,24 @@ GL_UpdateTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height, Assert(0); } - // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, - // GL_UNSIGNED_BYTE, Data); - glBindFramebuffer(GL_FRAMEBUFFER, 0); } +static void +GL_BindDefaultVertexArrays() +{ + // Switch to main buffer + glBindBuffer(GL_ARRAY_BUFFER, DefaultVerts.VertexBufferObject); + glBufferData(GL_ARRAY_BUFFER, sizeof(GL_DefaultVertices), GL_DefaultVertices, GL_STATIC_DRAW); + // position attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + // texture coordinate (note the last parameter's offset) + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + +} + static void GL_RasterizeShape(project_layer *Layer, mask *Mask) { @@ -283,16 +300,7 @@ GL_RasterizeShape(project_layer *Layer, mask *Mask) glUniform1i(VertexMode, 0); glUniform1i(FragmentMode, 1); - - // // Switch to main buffer - glBindBuffer(GL_ARRAY_BUFFER, DefaultVerts.VertexBufferObject); - glBufferData(GL_ARRAY_BUFFER, sizeof(GL_DefaultVertices), GL_DefaultVertices, GL_STATIC_DRAW); - // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - // texture coordinate (note the last parameter's offset) - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(1); + GL_BindDefaultVertexArrays(); glStencilFunc(GL_NOTEQUAL, 0, 0xFF); glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); @@ -312,3 +320,59 @@ GL_RasterizeShape(project_layer *Layer, mask *Mask) glBindFramebuffer(GL_FRAMEBUFFER, 0); } + +static uint16 +Effect_GL_InitShader(int EffectIndex) +{ + uint16 *ShaderProgram = &GL_EffectShaderProgram[EffectIndex]; + + // this is safe to do, riiight + if (*ShaderProgram) { + glUseProgram(*ShaderProgram); + return *ShaderProgram; + } + + glShaderSource(DefaultVertexShader, 1, &DefaultVertexShaderSource, NULL); + glCompileShader(DefaultVertexShader); + + int success; + char infoLog[512]; + glGetShaderiv(DefaultVertexShader, GL_COMPILE_STATUS, &success); + + if(!success) + { + glGetShaderInfoLog(DefaultVertexShader, 512, NULL, infoLog); + printf("Vertex shader fail:\n %s", infoLog); + } + + uint32 FragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + + glShaderSource(FragmentShader, 1, &FragmentShaderEffectSource[EffectIndex], NULL); + glCompileShader(FragmentShader); + + glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &success); + + if(!success) + { + glGetShaderInfoLog(FragmentShader, 512, NULL, infoLog); + printf("Fragment shader fail:\n %s", infoLog); + } + + *ShaderProgram = glCreateProgram(); + + glAttachShader(*ShaderProgram, DefaultVertexShader); + glAttachShader(*ShaderProgram, FragmentShader); + glLinkProgram(*ShaderProgram); + + glGetProgramiv(*ShaderProgram, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(*ShaderProgram, 512, NULL, infoLog); + printf("Shader linkage fail:\n %s", infoLog); + } + + glDeleteShader(FragmentShader); + + glUseProgram(*ShaderProgram); + + return *ShaderProgram; +} diff --git a/main.cpp b/main.cpp index 7cb0288..85f1ed4 100644 --- a/main.cpp +++ b/main.cpp @@ -47,7 +47,6 @@ SDL_atomic_t CompletedEntries; static bool32 IsRendering = false; static instruction_mode InstructionMode = instruction_mode_scalar; static uint32 RandomGlobalIncrement = 0; -static bool32 test = 1; render_entry Entries[256]; -- cgit v1.2.3