Skip to content

Commit 7872f93

Browse files
soswowzachlewis
authored andcommitted
Use R"()" syntax for glsl shader strings for better readability (AcademySoftwareFoundation#4795)
Small PR to improve redability of shader code parts. Signed-off-by: Aleksandr Motsjonov <[email protected]>
1 parent 9cfac79 commit 7872f93

File tree

1 file changed

+131
-120
lines changed

1 file changed

+131
-120
lines changed

src/iv/ivgl.cpp

Lines changed: 131 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,18 @@ const char*
191191
IvGL::color_func_shader_text()
192192
{
193193
// clang-format off
194-
return
195-
"uniform float gain;\n"
196-
"uniform float gamma;\n"
197-
"\n"
198-
"vec4 ColorFunc(vec4 C)\n"
199-
"{\n"
200-
" C.xyz *= gain;\n"
201-
" float invgamma = 1.0/gamma;\n"
202-
" C.xyz = pow (C.xyz, vec3 (invgamma, invgamma, invgamma));\n"
203-
" return C;\n"
204-
"}\n";
194+
return R"(
195+
uniform float gain;
196+
uniform float gamma;
197+
198+
vec4 ColorFunc(vec4 C)
199+
{
200+
C.xyz *= gain;
201+
float invgamma = 1.0/gamma;
202+
C.xyz = pow (C.xyz, vec3 (invgamma, invgamma, invgamma));
203+
return C;
204+
}
205+
)";
205206
// clang-format on
206207
}
207208

@@ -235,13 +236,14 @@ IvGL::create_shaders(void)
235236

236237
if (!m_vertex_shader) {
237238
// clang-format off
238-
static const GLchar* vertex_source =
239-
"varying vec2 vTexCoord;\n"
240-
"void main ()\n"
241-
"{\n"
242-
" vTexCoord = gl_MultiTexCoord0.xy;\n"
243-
" gl_Position = ftransform();\n"
244-
"}\n";
239+
static const GLchar* vertex_source = R"(
240+
varying vec2 vTexCoord;
241+
void main ()
242+
{
243+
vTexCoord = gl_MultiTexCoord0.xy;
244+
gl_Position = ftransform();
245+
}
246+
)";
245247
// clang-format on
246248

247249
m_vertex_shader = glCreateShader(GL_VERTEX_SHADER);
@@ -258,109 +260,118 @@ IvGL::create_shaders(void)
258260
}
259261

260262
// clang-format off
261-
static const GLchar* fragment_source =
262-
"uniform sampler2D imgtex;\n"
263-
"varying vec2 vTexCoord;\n"
264-
"uniform int startchannel;\n"
265-
"uniform int colormode;\n"
263+
static const GLchar* fragment_source = R"(
264+
uniform sampler2D imgtex;
265+
varying vec2 vTexCoord;
266+
uniform int startchannel;
267+
uniform int colormode;
266268
// Remember, if imgchannels == 2, second channel would be channel 4 (a).
267-
"uniform int imgchannels;\n"
268-
"uniform int pixelview;\n"
269-
"uniform int linearinterp;\n"
270-
"uniform int width;\n"
271-
"uniform int height;\n"
272-
"vec4 rgba_mode (vec4 C)\n"
273-
"{\n"
274-
" if (imgchannels <= 2) {\n"
275-
" if (startchannel == 1)\n"
276-
" return vec4(C.aaa, 1.0);\n"
277-
" return C.rrra;\n"
278-
" }\n"
279-
" return C;\n"
280-
"}\n"
281-
"vec4 rgb_mode (vec4 C)\n"
282-
"{\n"
283-
" if (imgchannels <= 2) {\n"
284-
" if (startchannel == 1)\n"
285-
" return vec4(C.aaa, 1.0);\n"
286-
" return vec4 (C.rrr, 1.0);\n"
287-
" }\n"
288-
" float C2[4];\n"
289-
" C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;\n"
290-
" return vec4 (C2[startchannel], C2[startchannel+1], C2[startchannel+2], 1.0);\n"
291-
"}\n"
292-
"vec4 singlechannel_mode (vec4 C)\n"
293-
"{\n"
294-
" float C2[4];\n"
295-
" C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;\n"
296-
" if (startchannel > imgchannels)\n"
297-
" return vec4 (0.0,0.0,0.0,1.0);\n"
298-
" return vec4 (C2[startchannel], C2[startchannel], C2[startchannel], 1.0);\n"
299-
"}\n"
300-
"vec4 luminance_mode (vec4 C)\n"
301-
"{\n"
302-
" if (imgchannels <= 2)\n"
303-
" return vec4 (C.rrr, C.a);\n"
304-
" float lum = dot (C.rgb, vec3(0.2126, 0.7152, 0.0722));\n"
305-
" return vec4 (lum, lum, lum, C.a);\n"
306-
"}\n"
307-
"float heat_red(float x)\n"
308-
"{\n"
309-
" return clamp (mix(0.0, 1.0, (x-0.35)/(0.66-0.35)), 0.0, 1.0) -\n"
310-
" clamp (mix(0.0, 0.5, (x-0.89)/(1.0-0.89)), 0.0, 1.0);\n"
311-
"}\n"
312-
"float heat_green(float x)\n"
313-
"{\n"
314-
" return clamp (mix(0.0, 1.0, (x-0.125)/(0.375-0.125)), 0.0, 1.0) -\n"
315-
" clamp (mix(0.0, 1.0, (x-0.64)/(0.91-0.64)), 0.0, 1.0);\n"
316-
"}\n"
317-
"vec4 heatmap_mode (vec4 C)\n"
318-
"{\n"
319-
" float C2[4];\n"
320-
" C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;\n"
321-
" return vec4(heat_red(C2[startchannel]),\n"
322-
" heat_green(C2[startchannel]),\n"
323-
" heat_red(1.0-C2[startchannel]),\n"
324-
" 1.0);\n"
325-
"}\n"
326-
"void main ()\n"
327-
"{\n"
328-
" vec2 st = vTexCoord;\n"
329-
" float black = 0.0;\n"
330-
" if (pixelview != 0 || linearinterp == 0) {\n"
331-
" vec2 wh = vec2(width,height);\n"
332-
" vec2 onehalf = vec2(0.5,0.5);\n"
333-
" vec2 st_res = st * wh /* + onehalf */ ;\n"
334-
" vec2 st_pix = floor (st_res);\n"
335-
" vec2 st_rem = st_res - st_pix;\n"
336-
" st = (st_pix + onehalf) / wh;\n"
337-
" if (pixelview != 0) {\n"
338-
" if (st.x < 0.0 || st.x >= 1.0 || \n"
339-
" st.y < 0.0 || st.y >= 1.0 || \n"
340-
" st_rem.x < 0.05 || st_rem.x >= 0.95 || \n"
341-
" st_rem.y < 0.05 || st_rem.y >= 0.95)\n"
342-
" black = 1.0;\n"
343-
" }\n"
344-
" }\n"
345-
" vec4 C = texture2D (imgtex, st);\n"
346-
" C = mix (C, vec4(0.05,0.05,0.05,1.0), black);\n"
347-
" if (startchannel < 0)\n"
348-
" C = vec4(0.0,0.0,0.0,1.0);\n"
349-
" else if (colormode == 0)\n" // RGBA
350-
" C = rgba_mode (C);\n"
351-
" else if (colormode == 1)\n" // RGB (i.e., ignore alpha).
352-
" C = rgb_mode (C);\n"
353-
" else if (colormode == 2)\n" // Single channel.
354-
" C = singlechannel_mode (C);\n"
355-
" else if (colormode == 3)\n" // Luminance.
356-
" C = luminance_mode (C);\n"
357-
" else if (colormode == 4)\n" // Heatmap.
358-
" C = heatmap_mode (C);\n"
359-
" if (pixelview != 0)\n"
360-
" C.a = 1.0;\n"
361-
" C = ColorFunc(C);\n"
362-
" gl_FragColor = C;\n"
363-
"}\n";
269+
uniform int imgchannels;
270+
uniform int pixelview;
271+
uniform int linearinterp;
272+
uniform int width;
273+
uniform int height;
274+
275+
vec4 rgba_mode (vec4 C)
276+
{
277+
if (imgchannels <= 2) {
278+
if (startchannel == 1)
279+
return vec4(C.aaa, 1.0);
280+
return C.rrra;
281+
}
282+
return C;
283+
}
284+
285+
vec4 rgb_mode (vec4 C)
286+
{
287+
if (imgchannels <= 2) {
288+
if (startchannel == 1)
289+
return vec4(C.aaa, 1.0);
290+
return vec4 (C.rrr, 1.0);
291+
}
292+
float C2[4];
293+
C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;
294+
return vec4 (C2[startchannel], C2[startchannel+1], C2[startchannel+2], 1.0);
295+
}
296+
297+
vec4 singlechannel_mode (vec4 C)
298+
{
299+
float C2[4];
300+
C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;
301+
if (startchannel > imgchannels)
302+
return vec4 (0.0,0.0,0.0,1.0);
303+
return vec4 (C2[startchannel], C2[startchannel], C2[startchannel], 1.0);
304+
}
305+
306+
vec4 luminance_mode (vec4 C)
307+
{
308+
if (imgchannels <= 2)
309+
return vec4 (C.rrr, C.a);
310+
float lum = dot (C.rgb, vec3(0.2126, 0.7152, 0.0722));
311+
return vec4 (lum, lum, lum, C.a);
312+
}
313+
314+
float heat_red(float x)
315+
{
316+
return clamp (mix(0.0, 1.0, (x-0.35)/(0.66-0.35)), 0.0, 1.0) -
317+
clamp (mix(0.0, 0.5, (x-0.89)/(1.0-0.89)), 0.0, 1.0);
318+
}
319+
320+
float heat_green(float x)
321+
{
322+
return clamp (mix(0.0, 1.0, (x-0.125)/(0.375-0.125)), 0.0, 1.0) -
323+
clamp (mix(0.0, 1.0, (x-0.64)/(0.91-0.64)), 0.0, 1.0);
324+
}
325+
326+
vec4 heatmap_mode (vec4 C)
327+
{
328+
float C2[4];
329+
C2[0]=C.x; C2[1]=C.y; C2[2]=C.z; C2[3]=C.w;
330+
return vec4(heat_red(C2[startchannel]),
331+
heat_green(C2[startchannel]),
332+
heat_red(1.0-C2[startchannel]),
333+
1.0);
334+
}
335+
336+
void main ()
337+
{
338+
vec2 st = vTexCoord;
339+
float black = 0.0;
340+
if (pixelview != 0 || linearinterp == 0) {
341+
vec2 wh = vec2(width,height);
342+
vec2 onehalf = vec2(0.5,0.5);
343+
vec2 st_res = st * wh /* + onehalf */ ;
344+
vec2 st_pix = floor (st_res);
345+
vec2 st_rem = st_res - st_pix;
346+
st = (st_pix + onehalf) / wh;
347+
if (pixelview != 0) {
348+
if (st.x < 0.0 || st.x >= 1.0 ||
349+
st.y < 0.0 || st.y >= 1.0 ||
350+
st_rem.x < 0.05 || st_rem.x >= 0.95 ||
351+
st_rem.y < 0.05 || st_rem.y >= 0.95)
352+
black = 1.0;
353+
}
354+
}
355+
vec4 C = texture2D (imgtex, st);
356+
C = mix (C, vec4(0.05,0.05,0.05,1.0), black);
357+
if (startchannel < 0)
358+
C = vec4(0.0,0.0,0.0,1.0);
359+
else if (colormode == 0) // RGBA
360+
C = rgba_mode (C);
361+
else if (colormode == 1) // RGB (i.e., ignore alpha).
362+
C = rgb_mode (C);
363+
else if (colormode == 2) // Single channel.
364+
C = singlechannel_mode (C);
365+
else if (colormode == 3) // Luminance.
366+
C = luminance_mode (C);
367+
else if (colormode == 4) // Heatmap.
368+
C = heatmap_mode (C);
369+
if (pixelview != 0)
370+
C.a = 1.0;
371+
C = ColorFunc(C);
372+
gl_FragColor = C;
373+
}
374+
)";
364375
// clang-format on
365376

366377
const char* fragment_sources[] = { "#version 120\n", color_shader,

0 commit comments

Comments
 (0)