Move all the shaders to glsl files (and fix the processor more)

This commit is contained in:
Marek Roszko 2022-05-12 23:37:29 -04:00
parent 239af40256
commit 8288f24264
14 changed files with 113 additions and 240 deletions

View File

@ -1,7 +1,7 @@
file( READ ${SOURCE} SOURCE_TEXT )
file( SIZE ${SOURCE} SOURCE_FILESIZE )
set( MAX_BYTES_PER_LITERAL 16380 )
set( MAX_BYTES_PER_LITERAL 16000 )
math(EXPR NUMBER_LITERALS "${SOURCE_FILESIZE}/${MAX_BYTES_PER_LITERAL}")
@ -18,21 +18,28 @@ namespace BUILTIN_SHADERS {
")
MATH(EXPR LAST_LITERAL_ITER "${LITERAL_ITER}-1")
MATH(EXPR LAST_LITERAL_ITER "${NUMBER_LITERALS}-1")
foreach(LITERAL_ITER RANGE 0 ${NUMBER_LITERALS} 1)
set( CHUNK_TEXT "" )
MATH(EXPR TEXT_OFFSET "${LITERAL_ITER}*${MAX_BYTES_PER_LITERAL}")
string(SUBSTRING "${SOURCE_TEXT}" ${TEXT_OFFSET} ${MAX_BYTES_PER_LITERAL} CHUNK_TEXT)
foreach(LITERAL_ITER RANGE ${NUMBER_LITERALS})
set( outCppText
"
${outCppText}
const char ${OUTVARNAME}_p${LITERAL_ITER}[] = R\"SHADER_SOURCE(
${SOURCE_TEXT}
${CHUNK_TEXT}
)SHADER_SOURCE\";
" )
set( outCppTextStdString "${outCppTextStdString} std::string(${OUTVARNAME}_p${LITERAL_ITER})")
if( ${LITERAL_ITER} LESS ${LAST_LITERAL_ITER})
if( ${LITERAL_ITER} LESS_EQUAL ${LAST_LITERAL_ITER})
set( outCppTextStdString " ${outCppTextStdString} +")
endif()
endforeach()

View File

@ -21,7 +21,7 @@ set( GAL_SRCS
# OpenGL GAL
opengl/opengl_gal.cpp
opengl/gl_resources.cpp
opengl/gl_builtin_shaders.cpp
opengl/shader.cpp
opengl/vertex_item.cpp
opengl/vertex_container.cpp
@ -70,7 +70,7 @@ function( add_shader outTarget inFile shaderName )
-DDESTINATION_HEADER_DIR="${CMAKE_BINARY_DIR}/include/gal/shaders/"
-DOUTCPPFILE="${outCppName}"
-DOUTHEADERFILE="${outHeaderName}"
-DOUTVARNAME="${shaderName}_shader"
-DOUTVARNAME="${shaderName}"
-P ${CMAKE_MODULE_PATH}/BuildSteps/CreateShaderCpp.cmake
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/shaders/${inFile}
${CMAKE_MODULE_PATH}/BuildSteps/CreateShaderCpp.cmake
@ -82,3 +82,11 @@ endfunction()
add_shader( gal kicad_frag.glsl glsl_kicad_frag )
add_shader( gal kicad_vert.glsl glsl_kicad_vert )
add_shader( gal smaa_base.glsl glsl_smaa_base )
add_shader( gal smaa_pass_1_frag_color.glsl glsl_smaa_pass_1_frag_color )
add_shader( gal smaa_pass_1_frag_luma.glsl glsl_smaa_pass_1_frag_luma )
add_shader( gal smaa_pass_1_vert.glsl glsl_smaa_pass_1_vert )
add_shader( gal smaa_pass_2_frag.glsl glsl_smaa_pass_2_frag )
add_shader( gal smaa_pass_2_vert.glsl glsl_smaa_pass_2_vert )
add_shader( gal smaa_pass_3_frag.glsl glsl_smaa_pass_3_frag )
add_shader( gal smaa_pass_3_vert.glsl glsl_smaa_pass_3_vert )

View File

@ -29,7 +29,14 @@
#include <memory>
#include <tuple>
#include "gl_builtin_shaders.h"
#include <glsl_smaa_base.h>
#include <glsl_smaa_pass_1_frag_color.h>
#include <glsl_smaa_pass_1_frag_luma.h>
#include <glsl_smaa_pass_1_vert.h>
#include <glsl_smaa_pass_2_frag.h>
#include <glsl_smaa_pass_2_vert.h>
#include <glsl_smaa_pass_3_frag.h>
#include <glsl_smaa_pass_3_vert.h>
#include "SmaaAreaTex.h"
#include "SmaaSearchTex.h"
@ -259,7 +266,7 @@ void ANTIALIASING_SMAA::loadShaders()
// Edge Detection: In Eeschema, when a single pixel line changes color, edge detection using
// color is too aggressive and leads to a white spot at the transition point
std::string quality_string;
const char* edge_detect_shader;
std::string edge_detect_shader;
// trades imperfect AA of shallow angles for a near artifact-free reproduction of fine features
// jaggies are smoothed over max 5px (original step + 2px in both directions)
@ -268,7 +275,7 @@ void ANTIALIASING_SMAA::loadShaders()
"#define SMAA_MAX_SEARCH_STEPS_DIAG 2\n"
"#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 1.5\n"
"#define SMAA_CORNER_ROUNDING 0\n";
edge_detect_shader = BUILTIN_SHADERS::smaa_pass_1_fragment_shader_luma;
edge_detect_shader = BUILTIN_SHADERS::glsl_smaa_pass_1_frag_luma;
// set up shaders
std::string vert_preamble( R"SHADER(
@ -287,19 +294,15 @@ uniform vec4 SMAA_RT_METRICS;
uniform vec4 SMAA_RT_METRICS;
)SHADER" );
std::string smaa_source = std::string( BUILTIN_SHADERS::smaa_base_shader_p1 )
+ std::string( BUILTIN_SHADERS::smaa_base_shader_p2 )
+ std::string( BUILTIN_SHADERS::smaa_base_shader_p3 )
+ std::string( BUILTIN_SHADERS::smaa_base_shader_p4 );
//
// Set up pass 1 Shader
//
pass_1_shader = std::make_unique<SHADER>();
pass_1_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_VERTEX, vert_preamble, quality_string,
smaa_source, BUILTIN_SHADERS::smaa_pass_1_vertex_shader );
BUILTIN_SHADERS::glsl_smaa_base,
BUILTIN_SHADERS::glsl_smaa_pass_1_vert );
pass_1_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_FRAGMENT, frag_preamble,
quality_string, smaa_source, edge_detect_shader );
quality_string, BUILTIN_SHADERS::glsl_smaa_base, edge_detect_shader );
pass_1_shader->Link();
checkGlError( "linking pass 1 shader", __FILE__, __LINE__ );
@ -320,10 +323,11 @@ uniform vec4 SMAA_RT_METRICS;
//
pass_2_shader = std::make_unique<SHADER>();
pass_2_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_VERTEX, vert_preamble, quality_string,
smaa_source, BUILTIN_SHADERS::smaa_pass_2_vertex_shader );
BUILTIN_SHADERS::glsl_smaa_base,
BUILTIN_SHADERS::glsl_smaa_pass_2_vert );
pass_2_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_FRAGMENT, frag_preamble,
quality_string, smaa_source,
BUILTIN_SHADERS::smaa_pass_2_fragment_shader );
quality_string, BUILTIN_SHADERS::glsl_smaa_base,
BUILTIN_SHADERS::glsl_smaa_pass_2_frag );
pass_2_shader->Link();
checkGlError( "linking pass 2 shader", __FILE__, __LINE__ );
@ -352,10 +356,11 @@ uniform vec4 SMAA_RT_METRICS;
//
pass_3_shader = std::make_unique<SHADER>();
pass_3_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_VERTEX, vert_preamble, quality_string,
smaa_source, BUILTIN_SHADERS::smaa_pass_3_vertex_shader );
BUILTIN_SHADERS::glsl_smaa_base,
BUILTIN_SHADERS::glsl_smaa_pass_3_vert );
pass_3_shader->LoadShaderFromStrings( KIGFX::SHADER_TYPE_FRAGMENT, frag_preamble,
quality_string, smaa_source,
BUILTIN_SHADERS::smaa_pass_3_fragment_shader );
quality_string, BUILTIN_SHADERS::glsl_smaa_base,
BUILTIN_SHADERS::glsl_smaa_pass_3_frag );
pass_3_shader->Link();
GLint smaaP3ColorTexParameter = pass_3_shader->AddParameter( "colorTex" );

View File

@ -1,50 +0,0 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef GAL_GL_BUILTIN_SHADERS_H__
#define GAL_GL_BUILTIN_SHADERS_H__
namespace KIGFX {
namespace BUILTIN_SHADERS {
extern const char ssaa_x4_vertex_shader[];
extern const char ssaa_x4_fragment_shader[];
extern const char smaa_base_shader_p1[];
extern const char smaa_base_shader_p2[];
extern const char smaa_base_shader_p3[];
extern const char smaa_base_shader_p4[];
extern const char smaa_pass_1_vertex_shader[];
extern const char smaa_pass_1_fragment_shader_luma[];
extern const char smaa_pass_1_fragment_shader_color[];
extern const char smaa_pass_2_vertex_shader[];
extern const char smaa_pass_2_fragment_shader[];
extern const char smaa_pass_3_vertex_shader[];
extern const char smaa_pass_3_fragment_shader[];
}
}
#endif

View File

@ -2303,14 +2303,14 @@ void OPENGL_GAL::init()
// Prepare shaders
if( !m_shader->IsLinked()
&& !m_shader->LoadShaderFromStrings( SHADER_TYPE_VERTEX,
BUILTIN_SHADERS::glsl_kicad_vert_shader ) )
BUILTIN_SHADERS::glsl_kicad_vert ) )
{
throw std::runtime_error( "Cannot compile vertex shader!" );
}
if( !m_shader->IsLinked()
&& !m_shader->LoadShaderFromStrings( SHADER_TYPE_FRAGMENT,
BUILTIN_SHADERS::glsl_kicad_frag_shader ) )
BUILTIN_SHADERS::glsl_kicad_frag ) )
{
throw std::runtime_error( "Cannot compile fragment shader!" );
}

View File

@ -1,52 +1,3 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "gl_builtin_shaders.h"
namespace KIGFX {
namespace BUILTIN_SHADERS {
const char ssaa_x4_fragment_shader[] = R"SHADER_SOURCE(
#version 120
varying vec2 texcoord;
uniform sampler2D source;
void main()
{
float step_x = dFdx(texcoord.x)/4.;
float step_y = dFdy(texcoord.y)/4.;
vec4 q00 = texture2D( source, texcoord + vec2(-step_x, -step_y) );
vec4 q01 = texture2D( source, texcoord + vec2( step_x, -step_y) );
vec4 q10 = texture2D( source, texcoord + vec2(-step_x, step_y) );
vec4 q11 = texture2D( source, texcoord + vec2( step_x, step_y) );
gl_FragColor = (q00+q01+q10+q11)/4;
}
)SHADER_SOURCE";
const char smaa_base_shader_p1[] = R"SHADER_SOURCE(
/**
* Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
@ -416,9 +367,7 @@ const char smaa_base_shader_p1[] = R"SHADER_SOURCE(
#ifndef SMAA_MAX_SEARCH_STEPS
#define SMAA_MAX_SEARCH_STEPS 16
#endif
)SHADER_SOURCE";
const char smaa_base_shader_p2[] = R"SHADER_SOURCE(
/**
* SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
* diagonal pattern searches, at each side of the pixel. In this case we jump
@ -871,9 +820,7 @@ float2 SMAAColorEdgeDetectionPS(float2 texcoord,
return edges;
}
)SHADER_SOURCE";
extern const char smaa_base_shader_p3[] = R"SHADER_SOURCE(
/**
* Depth Edge Detection
*/
@ -1255,9 +1202,6 @@ float4 SMAABlendingWeightCalculationPS(float2 texcoord,
// SMAAArea below needs a sqrt, as the areas texture is compressed
// quadratically:
float2 sqrt_d = sqrt(d);
)SHADER_SOURCE";
extern const char smaa_base_shader_p4[] = R"SHADER_SOURCE(
// Fetch the right crossing edges:
float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r;
@ -1426,109 +1370,3 @@ void SMAASeparatePS(float4 position,
//-----------------------------------------------------------------------------
#endif // SMAA_INCLUDE_PS
)SHADER_SOURCE";
const char smaa_pass_1_vertex_shader[] = R"SHADER_SOURCE(
varying vec4 offset[3];
varying vec2 texcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAAEdgeDetectionVS( texcoord, offset);
gl_Position = ftransform();
}
)SHADER_SOURCE";
const char smaa_pass_1_fragment_shader_luma[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec4 offset[3];
uniform sampler2D colorTex;
void main()
{
gl_FragColor.xy = SMAALumaEdgeDetectionPS(texcoord, offset, colorTex).xy;
}
)SHADER_SOURCE";
const char smaa_pass_1_fragment_shader_color[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec4 offset[3];
uniform sampler2D colorTex;
void main()
{
gl_FragColor.xy = SMAAColorEdgeDetectionPS(texcoord, offset, colorTex).xy;
}
)SHADER_SOURCE";
const char smaa_pass_2_vertex_shader[] = R"SHADER_SOURCE(
varying vec4 offset[3];
varying vec2 texcoord;
varying vec2 pixcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAABlendingWeightCalculationVS( texcoord, pixcoord, offset );
gl_Position = ftransform();
}
)SHADER_SOURCE";
const char smaa_pass_2_fragment_shader[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec2 pixcoord;
varying vec4 offset[3];
uniform sampler2D edgesTex;
uniform sampler2D areaTex;
uniform sampler2D searchTex;
void main()
{
gl_FragColor = SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex, vec4(0.,0.,0.,0.));
}
)SHADER_SOURCE";
const char smaa_pass_3_vertex_shader[] = R"SHADER_SOURCE(
varying vec4 offset;
varying vec2 texcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAANeighborhoodBlendingVS( texcoord, offset );
gl_Position = ftransform();
}
)SHADER_SOURCE";
const char smaa_pass_3_fragment_shader[] = R"SHADER_SOURCE(
varying vec2 texcoord;
varying vec4 offset;
uniform sampler2D colorTex;
uniform sampler2D blendTex;
void main()
{
gl_FragColor = SMAANeighborhoodBlendingPS(texcoord, offset, colorTex, blendTex);
}
)SHADER_SOURCE";
}
}

View File

@ -0,0 +1,8 @@
varying vec2 texcoord;
varying vec4 offset[3];
uniform sampler2D colorTex;
void main()
{
gl_FragColor.xy = SMAAColorEdgeDetectionPS(texcoord, offset, colorTex).xy;
}

View File

@ -0,0 +1,8 @@
varying vec2 texcoord;
varying vec4 offset[3];
uniform sampler2D colorTex;
void main()
{
gl_FragColor.xy = SMAALumaEdgeDetectionPS(texcoord, offset, colorTex).xy;
}

View File

@ -0,0 +1,10 @@
varying vec4 offset[3];
varying vec2 texcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAAEdgeDetectionVS( texcoord, offset);
gl_Position = ftransform();
}

View File

@ -0,0 +1,11 @@
varying vec2 texcoord;
varying vec2 pixcoord;
varying vec4 offset[3];
uniform sampler2D edgesTex;
uniform sampler2D areaTex;
uniform sampler2D searchTex;
void main()
{
gl_FragColor = SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex, vec4(0.,0.,0.,0.));
}

View File

@ -0,0 +1,10 @@
varying vec4 offset[3];
varying vec2 texcoord;
varying vec2 pixcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAABlendingWeightCalculationVS( texcoord, pixcoord, offset );
gl_Position = ftransform();
}

View File

@ -0,0 +1,9 @@
varying vec2 texcoord;
varying vec4 offset;
uniform sampler2D colorTex;
uniform sampler2D blendTex;
void main()
{
gl_FragColor = SMAANeighborhoodBlendingPS(texcoord, offset, colorTex, blendTex);
}

View File

@ -0,0 +1,9 @@
varying vec4 offset;
varying vec2 texcoord;
void main()
{
texcoord = gl_MultiTexCoord0.st;
SMAANeighborhoodBlendingVS( texcoord, offset );
gl_Position = ftransform();
}

View File

@ -41,7 +41,7 @@
namespace KIGFX
{
///< Possible types of shaders (keep consistent with the actual shader source in
///< gl_builtin_shaders.cpp).
///< kicad_vert.glsl and kicad_frag.glsl).
enum SHADER_MODE
{
SHADER_NONE = 0,