commit fbf395541a5c1e625573cd1f6456da920eb10bdd
parent c8cf9116a390cb64705ccaf31bbceddae559bebb
Author: Kyle Milz <kyle@0x30.net>
Date: Tue, 10 Oct 2017 00:49:40 -0600
bin: wrap and rename demo-shader
Diffstat:
9 files changed, 273 insertions(+), 266 deletions(-)
diff --git a/bin/Jamfile b/bin/Jamfile
@@ -56,12 +56,12 @@ Main citrun_inst : $(INST_SRCS) ;
# citrun_gl, citrun_gltest
#
GL_SRCS =
- demo-shader.cc
gl_atlas.cc
gl_buffer.cc
gl_font.cc
gl_main.cc
gl_runtime.cc
+ gl_shader.cc
gl_state.cc
gl_view.cc
matrix4x4.c ;
diff --git a/bin/demo-shader.cc b/bin/demo-shader.cc
@@ -1,211 +0,0 @@
-/*
- * Copyright 2012 Google, Inc. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Google Author(s): Behdad Esfahbod
- */
-#include <assert.h>
-#include <GL/glew.h>
-
-#include "demo-shader.h"
-
-#include "demo_atlas_glsl.h"
-#include "demo_vshader_glsl.h"
-#include "demo_fshader_glsl.h"
-
-
-
-static unsigned int
-glyph_encode (unsigned int atlas_x , /* 7 bits */
- unsigned int atlas_y, /* 7 bits */
- unsigned int corner_x, /* 1 bit */
- unsigned int corner_y, /* 1 bit */
- unsigned int nominal_w, /* 6 bits */
- unsigned int nominal_h /* 6 bits */)
-{
- assert (0 == (atlas_x & ~0x7F));
- assert (0 == (atlas_y & ~0x7F));
- assert (0 == (corner_x & ~1));
- assert (0 == (corner_y & ~1));
- assert (0 == (nominal_w & ~0x3F));
- assert (0 == (nominal_h & ~0x3F));
-
- unsigned int x = (((atlas_x << 6) | nominal_w) << 1) | corner_x;
- unsigned int y = (((atlas_y << 6) | nominal_h) << 1) | corner_y;
-
- return (x << 16) | y;
-}
-
-static void
-glyph_vertex_encode (double x, double y,
- unsigned int corner_x, unsigned int corner_y,
- const citrun::glyph_info_t *gi,
- glyph_vertex_t *v)
-{
- unsigned int encoded = glyph_encode (gi->atlas_x, gi->atlas_y,
- corner_x, corner_y,
- gi->nominal_w, gi->nominal_h);
- v->x = x;
- v->y = y;
- v->g16hi = encoded >> 16;
- v->g16lo = encoded & 0xFFFF;
-}
-
-void
-demo_shader_add_glyph_vertices (const glyphy_point_t &p,
- double font_size,
- citrun::glyph_info_t *gi,
- std::vector<glyph_vertex_t> *vertices,
- glyphy_extents_t *extents)
-{
- if (gi->is_empty)
- return;
-
- glyph_vertex_t v[4];
-
-#define ENCODE_CORNER(_cx, _cy) \
- do { \
- double _vx = p.x + font_size * ((1-_cx) * gi->extents.min_x + _cx * gi->extents.max_x); \
- double _vy = p.y - font_size * ((1-_cy) * gi->extents.min_y + _cy * gi->extents.max_y); \
- glyph_vertex_encode (_vx, _vy, _cx, _cy, gi, &v[_cx * 2 + _cy]); \
- } while (0)
- ENCODE_CORNER (0, 0);
- ENCODE_CORNER (0, 1);
- ENCODE_CORNER (1, 0);
- ENCODE_CORNER (1, 1);
-#undef ENCODE_CORNER
-
- vertices->push_back (v[0]);
- vertices->push_back (v[1]);
- vertices->push_back (v[2]);
-
- vertices->push_back (v[1]);
- vertices->push_back (v[2]);
- vertices->push_back (v[3]);
-
- if (extents) {
- glyphy_extents_clear (extents);
- for (unsigned int i = 0; i < 4; i++) {
- glyphy_point_t p = {v[i].x, v[i].y};
- glyphy_extents_add (extents, &p);
- }
- }
-}
-
-
-
-
-static GLuint
-compile_shader (GLenum type,
- GLsizei count,
- const GLchar** sources)
-{
- TRACE();
-
- GLuint shader;
- GLint compiled;
-
- if (!(shader = glCreateShader (type)))
- return shader;
-
- glShaderSource (shader, count, sources, 0);
- glCompileShader (shader);
-
- glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
- if (!compiled) {
- GLint info_len = 0;
- LOGW ("%s shader failed to compile\n",
- type == GL_VERTEX_SHADER ? "Vertex" : "Fragment");
- glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &info_len);
-
- if (info_len > 0) {
- char *info_log = (char*) malloc (info_len);
- glGetShaderInfoLog (shader, info_len, NULL, info_log);
-
- LOGW ("%s\n", info_log);
- free (info_log);
- }
-
- abort ();
- }
-
- return shader;
-}
-
-static GLuint
-link_program (GLuint vshader,
- GLuint fshader)
-{
- TRACE();
-
- GLuint program;
- GLint linked;
-
- program = glCreateProgram ();
- glAttachShader (program, vshader);
- glAttachShader (program, fshader);
- glLinkProgram (program);
- glDeleteShader (vshader);
- glDeleteShader (fshader);
-
- glGetProgramiv (program, GL_LINK_STATUS, &linked);
- if (!linked) {
- GLint info_len = 0;
- LOGW ("Program failed to link\n");
- glGetProgramiv (program, GL_INFO_LOG_LENGTH, &info_len);
-
- if (info_len > 0) {
- char *info_log = (char*) malloc (info_len);
- glGetProgramInfoLog (program, info_len, NULL, info_log);
-
- LOGW ("%s\n", info_log);
- free (info_log);
- }
-
- abort ();
- }
-
- return program;
-}
-
-#ifdef GL_ES_VERSION_2_0
-# define GLSL_HEADER_STRING \
- "#extension GL_OES_standard_derivatives : enable\n" \
- "precision highp float;\n" \
- "precision highp int;\n"
-#else
-# define GLSL_HEADER_STRING \
- "#version 110\n"
-#endif
-
-GLuint
-demo_shader_create_program (void)
-{
- TRACE();
-
- GLuint vshader, fshader, program;
- const GLchar *vshader_sources[] = {GLSL_HEADER_STRING,
- demo_vshader_glsl};
- vshader = compile_shader (GL_VERTEX_SHADER, ARRAY_LEN (vshader_sources), vshader_sources);
- const GLchar *fshader_sources[] = {GLSL_HEADER_STRING,
- demo_atlas_glsl,
- glyphy_common_shader_source (),
- "#define GLYPHY_SDF_PSEUDO_DISTANCE 1\n",
- glyphy_sdf_shader_source (),
- demo_fshader_glsl};
- fshader = compile_shader (GL_FRAGMENT_SHADER, ARRAY_LEN (fshader_sources), fshader_sources);
-
- program = link_program (vshader, fshader);
- return program;
-}
diff --git a/bin/demo-shader.h b/bin/demo-shader.h
@@ -1,49 +0,0 @@
-/*
- * Copyright 2012 Google, Inc. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Google Author(s): Behdad Esfahbod
- */
-
-#ifndef DEMO_SHADERS_H
-#define DEMO_SHADERS_H
-
-#include <vector>
-
-#include "demo-common.h"
-#include "gl_font.h" // citrun::glyph_info_t
-
-
-struct glyph_vertex_t {
- /* Position */
- GLfloat x;
- GLfloat y;
- /* Glyph info */
- GLfloat g16hi;
- GLfloat g16lo;
-};
-
-void
-demo_shader_add_glyph_vertices (const glyphy_point_t &p,
- double font_size,
- citrun::glyph_info_t *gi,
- std::vector<glyph_vertex_t> *vertices,
- glyphy_extents_t *extents);
-
-
-GLuint
-demo_shader_create_program (void);
-
-
-#endif /* DEMO_SHADERS_H */
diff --git a/bin/gl_buffer.cc b/bin/gl_buffer.cc
@@ -123,7 +123,7 @@ citrun::gl_buffer::add_text(const char *utf8, citrun::gl_font &font, double font
/* Update ink extents */
glyphy_extents_t m_ink_extents;
- demo_shader_add_glyph_vertices(m_cursor, font_size, &gi, &m_vertices, &m_ink_extents);
+ shader.add_glyph_vertices(m_cursor, font_size, &gi, &m_vertices, &m_ink_extents);
glyphy_extents_extend(&m_ink_extents, &m_ink_extents);
/* Update logical extents */
diff --git a/bin/gl_buffer.h b/bin/gl_buffer.h
@@ -23,7 +23,7 @@
#include "demo-common.h"
#include "gl_font.h" // citrun::gl_font
-#include "demo-shader.h"
+#include "gl_shader.h" // citrun::glyph_vertex_t
namespace citrun {
@@ -32,12 +32,14 @@ class gl_buffer
{
unsigned int m_refcount;
glyphy_point_t m_cursor;
- std::vector<glyph_vertex_t> m_vertices;
+ std::vector<citrun::glyph_vertex_t> m_vertices;
glyphy_extents_t m_ink_extents;
glyphy_extents_t m_logical_extents;
bool m_dirty;
GLuint m_buf_name;
+ citrun::gl_shader shader;
+
public:
gl_buffer();
diff --git a/bin/gl_shader.cc b/bin/gl_shader.cc
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2012 Google, Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Google Author(s): Behdad Esfahbod
+ */
+#include <assert.h>
+#include <GL/glew.h>
+
+#include "gl_shader.h"
+
+#include "demo_atlas_glsl.h"
+#include "demo_vshader_glsl.h"
+#include "demo_fshader_glsl.h"
+
+
+citrun::gl_shader::gl_shader()
+{
+}
+
+static unsigned int
+glyph_encode (unsigned int atlas_x , /* 7 bits */
+ unsigned int atlas_y, /* 7 bits */
+ unsigned int corner_x, /* 1 bit */
+ unsigned int corner_y, /* 1 bit */
+ unsigned int nominal_w, /* 6 bits */
+ unsigned int nominal_h /* 6 bits */)
+{
+ assert (0 == (atlas_x & ~0x7F));
+ assert (0 == (atlas_y & ~0x7F));
+ assert (0 == (corner_x & ~1));
+ assert (0 == (corner_y & ~1));
+ assert (0 == (nominal_w & ~0x3F));
+ assert (0 == (nominal_h & ~0x3F));
+
+ unsigned int x = (((atlas_x << 6) | nominal_w) << 1) | corner_x;
+ unsigned int y = (((atlas_y << 6) | nominal_h) << 1) | corner_y;
+
+ return (x << 16) | y;
+}
+
+static void
+glyph_vertex_encode (double x, double y,
+ unsigned int corner_x, unsigned int corner_y,
+ const citrun::glyph_info_t *gi,
+ citrun::glyph_vertex_t *v)
+{
+ unsigned int encoded = glyph_encode (gi->atlas_x, gi->atlas_y,
+ corner_x, corner_y,
+ gi->nominal_w, gi->nominal_h);
+ v->x = x;
+ v->y = y;
+ v->g16hi = encoded >> 16;
+ v->g16lo = encoded & 0xFFFF;
+}
+
+void
+citrun::gl_shader::add_glyph_vertices(const glyphy_point_t &p, double font_size,
+ citrun::glyph_info_t *gi, std::vector<glyph_vertex_t> *vertices,
+ glyphy_extents_t *extents)
+{
+ if (gi->is_empty)
+ return;
+
+ glyph_vertex_t v[4];
+
+#define ENCODE_CORNER(_cx, _cy) \
+ do { \
+ double _vx = p.x + font_size * ((1-_cx) * gi->extents.min_x + _cx * gi->extents.max_x); \
+ double _vy = p.y - font_size * ((1-_cy) * gi->extents.min_y + _cy * gi->extents.max_y); \
+ glyph_vertex_encode (_vx, _vy, _cx, _cy, gi, &v[_cx * 2 + _cy]); \
+ } while (0)
+ ENCODE_CORNER (0, 0);
+ ENCODE_CORNER (0, 1);
+ ENCODE_CORNER (1, 0);
+ ENCODE_CORNER (1, 1);
+#undef ENCODE_CORNER
+
+ vertices->push_back (v[0]);
+ vertices->push_back (v[1]);
+ vertices->push_back (v[2]);
+
+ vertices->push_back (v[1]);
+ vertices->push_back (v[2]);
+ vertices->push_back (v[3]);
+
+ if (extents) {
+ glyphy_extents_clear (extents);
+ for (unsigned int i = 0; i < 4; i++) {
+ glyphy_point_t p = {v[i].x, v[i].y};
+ glyphy_extents_add (extents, &p);
+ }
+ }
+}
+
+
+
+
+static GLuint
+compile_shader (GLenum type,
+ GLsizei count,
+ const GLchar** sources)
+{
+ TRACE();
+
+ GLuint shader;
+ GLint compiled;
+
+ if (!(shader = glCreateShader (type)))
+ return shader;
+
+ glShaderSource (shader, count, sources, 0);
+ glCompileShader (shader);
+
+ glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
+ if (!compiled) {
+ GLint info_len = 0;
+ LOGW ("%s shader failed to compile\n",
+ type == GL_VERTEX_SHADER ? "Vertex" : "Fragment");
+ glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &info_len);
+
+ if (info_len > 0) {
+ char *info_log = (char*) malloc (info_len);
+ glGetShaderInfoLog (shader, info_len, NULL, info_log);
+
+ LOGW ("%s\n", info_log);
+ free (info_log);
+ }
+
+ abort ();
+ }
+
+ return shader;
+}
+
+static GLuint
+link_program (GLuint vshader,
+ GLuint fshader)
+{
+ TRACE();
+
+ GLuint program;
+ GLint linked;
+
+ program = glCreateProgram ();
+ glAttachShader (program, vshader);
+ glAttachShader (program, fshader);
+ glLinkProgram (program);
+ glDeleteShader (vshader);
+ glDeleteShader (fshader);
+
+ glGetProgramiv (program, GL_LINK_STATUS, &linked);
+ if (!linked) {
+ GLint info_len = 0;
+ LOGW ("Program failed to link\n");
+ glGetProgramiv (program, GL_INFO_LOG_LENGTH, &info_len);
+
+ if (info_len > 0) {
+ char *info_log = (char*) malloc (info_len);
+ glGetProgramInfoLog (program, info_len, NULL, info_log);
+
+ LOGW ("%s\n", info_log);
+ free (info_log);
+ }
+
+ abort ();
+ }
+
+ return program;
+}
+
+#ifdef GL_ES_VERSION_2_0
+# define GLSL_HEADER_STRING \
+ "#extension GL_OES_standard_derivatives : enable\n" \
+ "precision highp float;\n" \
+ "precision highp int;\n"
+#else
+# define GLSL_HEADER_STRING \
+ "#version 110\n"
+#endif
+
+GLuint
+citrun::gl_shader::create_program()
+{
+ TRACE();
+
+ GLuint vshader, fshader, program;
+ const GLchar *vshader_sources[] = {GLSL_HEADER_STRING,
+ demo_vshader_glsl};
+ vshader = compile_shader (GL_VERTEX_SHADER, ARRAY_LEN (vshader_sources), vshader_sources);
+ const GLchar *fshader_sources[] = {GLSL_HEADER_STRING,
+ demo_atlas_glsl,
+ glyphy_common_shader_source (),
+ "#define GLYPHY_SDF_PSEUDO_DISTANCE 1\n",
+ glyphy_sdf_shader_source (),
+ demo_fshader_glsl};
+ fshader = compile_shader (GL_FRAGMENT_SHADER, ARRAY_LEN (fshader_sources), fshader_sources);
+
+ program = link_program (vshader, fshader);
+ return program;
+}
diff --git a/bin/gl_shader.h b/bin/gl_shader.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2012 Google, Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Google Author(s): Behdad Esfahbod
+ */
+#ifndef DEMO_SHADERS_H
+#define DEMO_SHADERS_H
+
+#include <vector>
+
+#include "demo-common.h"
+#include "gl_font.h" // citrun::glyph_info_t
+
+
+namespace citrun {
+
+struct glyph_vertex_t {
+ /* Position */
+ GLfloat x;
+ GLfloat y;
+ /* Glyph info */
+ GLfloat g16hi;
+ GLfloat g16lo;
+};
+
+class gl_shader {
+public:
+ gl_shader();
+
+ void add_glyph_vertices (const glyphy_point_t &,
+ double,
+ citrun::glyph_info_t *,
+ std::vector<glyph_vertex_t> *,
+ glyphy_extents_t *);
+ GLuint create_program();
+};
+
+} // namespace citrun
+
+#endif /* DEMO_SHADERS_H */
diff --git a/bin/gl_state.cc b/bin/gl_state.cc
@@ -19,7 +19,7 @@
citrun::gl_state::gl_state() :
- program(demo_shader_create_program()),
+ program(shader.create_program()),
atlas(2048, 1024, 64, 8),
u_debug(false),
u_contrast(1.0),
diff --git a/bin/gl_state.h b/bin/gl_state.h
@@ -20,12 +20,13 @@
#include "demo-common.h"
#include "gl_atlas.h" // citrun::gl_atlas
-#include "demo-shader.h"
+#include "gl_shader.h"
namespace citrun {
class gl_state {
+ citrun::gl_shader shader;
GLuint program;
citrun::gl_atlas atlas;