citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit c8cf9116a390cb64705ccaf31bbceddae559bebb
parent 4e595e8ab657f1831215cab2df3135ec9885d359
Author: Kyle Milz <kyle@0x30.net>
Date:   Tue, 10 Oct 2017 00:27:33 -0600

bin: wrap and rename demo-atlas

Diffstat:
Mbin/Jamfile | 2+-
Dbin/demo-atlas.cc | 148-------------------------------------------------------------------------------
Dbin/demo-atlas.h | 54------------------------------------------------------
Abin/gl_atlas.cc | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abin/gl_atlas.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Mbin/gl_font.cc | 9++++-----
Mbin/gl_font.h | 8++++----
Mbin/gl_state.cc | 7+++----
Mbin/gl_state.h | 6+++---
9 files changed, 174 insertions(+), 219 deletions(-)

diff --git a/bin/Jamfile b/bin/Jamfile @@ -56,8 +56,8 @@ Main citrun_inst : $(INST_SRCS) ; # citrun_gl, citrun_gltest # GL_SRCS = - demo-atlas.cc demo-shader.cc + gl_atlas.cc gl_buffer.cc gl_font.cc gl_main.cc diff --git a/bin/demo-atlas.cc b/bin/demo-atlas.cc @@ -1,148 +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 <err.h> -#include "demo-atlas.h" - -#define gl(name) \ - for (GLint __ee, __ii = 0; \ - __ii < 1; \ - (__ii++, \ - (__ee = glGetError()) && \ - (fprintf (stderr, "gl" #name " failed with error %04X on line %d\n", __ee, __LINE__), abort (), 0))) \ - gl##name - -struct demo_atlas_t { - unsigned int refcount; - - GLuint tex_unit; - GLuint tex_name; - GLuint tex_w; - GLuint tex_h; - GLuint item_w; - GLuint item_h_q; /* height quantum */ - GLuint cursor_x; - GLuint cursor_y; -}; - - -demo_atlas_t * -demo_atlas_create (unsigned int w, - unsigned int h, - unsigned int item_w, - unsigned int item_h_quantum) -{ - TRACE(); - - demo_atlas_t *at = (demo_atlas_t *) calloc (1, sizeof (demo_atlas_t)); - at->refcount = 1; - - glGetIntegerv (GL_ACTIVE_TEXTURE, (GLint *) &at->tex_unit); - glGenTextures (1, &at->tex_name); - at->tex_w = w; - at->tex_h = h; - at->item_w = item_w; - at->item_h_q = item_h_quantum; - at->cursor_x = 0; - at->cursor_y = 0; - - demo_atlas_bind_texture (at); - - glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - gl(TexImage2D) (GL_TEXTURE_2D, 0, GL_RGBA, at->tex_w, at->tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - - return at; -} - -demo_atlas_t * -demo_atlas_reference (demo_atlas_t *at) -{ - if (at) at->refcount++; - return at; -} - -void -demo_atlas_destroy (demo_atlas_t *at) -{ - if (!at || --at->refcount) - return; - - glDeleteTextures (1, &at->tex_name); - free (at); -} - -void -demo_atlas_bind_texture (demo_atlas_t *at) -{ - glActiveTexture (at->tex_unit); - glBindTexture (GL_TEXTURE_2D, at->tex_name); -} - -void -demo_atlas_set_uniforms (demo_atlas_t *at) -{ - GLuint program; - glGetIntegerv (GL_CURRENT_PROGRAM, (GLint *) &program); - - glUniform4i (glGetUniformLocation (program, "u_atlas_info"), - at->tex_w, at->tex_h, at->item_w, at->item_h_q); - glUniform1i (glGetUniformLocation (program, "u_atlas_tex"), at->tex_unit - GL_TEXTURE0); -} - -void -demo_atlas_alloc (demo_atlas_t *at, - glyphy_rgba_t *data, - unsigned int len, - unsigned int *px, - unsigned int *py) -{ - GLuint w, h, x, y; - - w = at->item_w; - h = (len + w - 1) / w; - - if (at->cursor_y + h > at->tex_h) { - /* Go to next column */ - at->cursor_x += at->item_w; - at->cursor_y = 0; - } - - if (at->cursor_x + w <= at->tex_w && - at->cursor_y + h <= at->tex_h) - { - x = at->cursor_x; - y = at->cursor_y; - at->cursor_y += (h + at->item_h_q - 1) & ~(at->item_h_q - 1); - } else - errx(1, "Ran out of atlas memory"); - - demo_atlas_bind_texture (at); - if (w * h == len) - gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data); - else { - gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y, w, h - 1, GL_RGBA, GL_UNSIGNED_BYTE, data); - /* Upload the last row separately */ - gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y + h - 1, len - (w * (h - 1)), 1, GL_RGBA, GL_UNSIGNED_BYTE, - data + w * (h - 1)); - } - - *px = x / at->item_w; - *py = y / at->item_h_q; -} diff --git a/bin/demo-atlas.h b/bin/demo-atlas.h @@ -1,54 +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, Maysum Panju - */ - -#ifndef DEMO_ATLAS_H -#define DEMO_ATLAS_H - -#include "demo-common.h" - - -typedef struct demo_atlas_t demo_atlas_t; - -demo_atlas_t * -demo_atlas_create (unsigned int w, - unsigned int h, - unsigned int item_w, - unsigned int item_h_quantum); - -demo_atlas_t * -demo_atlas_reference (demo_atlas_t *at); - -void -demo_atlas_destroy (demo_atlas_t *at); - - -void -demo_atlas_alloc (demo_atlas_t *at, - glyphy_rgba_t *data, - unsigned int len, - unsigned int *px, - unsigned int *py); - -void -demo_atlas_bind_texture (demo_atlas_t *at); - -void -demo_atlas_set_uniforms (demo_atlas_t *at); - - -#endif /* DEMO_ATLAS_H */ diff --git a/bin/gl_atlas.cc b/bin/gl_atlas.cc @@ -0,0 +1,112 @@ +/* + * 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 <err.h> + +#include "gl_atlas.h" // citrun::gl_atlas + +#define gl(name) \ + for (GLint __ee, __ii = 0; \ + __ii < 1; \ + (__ii++, \ + (__ee = glGetError()) && \ + (fprintf (stderr, "gl" #name " failed with error %04X on line %d\n", __ee, __LINE__), abort (), 0))) \ + gl##name + + +citrun::gl_atlas::gl_atlas(unsigned int w, unsigned int h, + unsigned int _item_w, unsigned int item_h_quantum) : + tex_w(w), + tex_h(h), + item_w(_item_w), + item_h_q(item_h_quantum), + cursor_x(0), + cursor_y(0) +{ + TRACE(); + + glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *) &tex_unit); + glGenTextures(1, &tex_name); + + bind_texture(); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + gl(TexImage2D) (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); +} + +citrun::gl_atlas::~gl_atlas() +{ + glDeleteTextures(1, &tex_name); +} + +void +citrun::gl_atlas::bind_texture() +{ + glActiveTexture(tex_unit); + glBindTexture(GL_TEXTURE_2D, tex_name); +} + +void +citrun::gl_atlas::set_uniforms() +{ + GLuint program; + glGetIntegerv (GL_CURRENT_PROGRAM, (GLint *) &program); + + glUniform4i(glGetUniformLocation(program, "u_atlas_info"), + tex_w, tex_h, item_w, item_h_q); + glUniform1i(glGetUniformLocation(program, "u_atlas_tex"), tex_unit - GL_TEXTURE0); +} + +void +citrun::gl_atlas::alloc(glyphy_rgba_t *data, unsigned int len, + unsigned int *px, unsigned int *py) +{ + GLuint w, h, x, y; + + w = item_w; + h = (len + w - 1) / w; + + if (cursor_y + h > tex_h) { + /* Go to next column */ + cursor_x += item_w; + cursor_y = 0; + } + + if (cursor_x + w <= tex_w && cursor_y + h <= tex_h) + { + x = cursor_x; + y = cursor_y; + cursor_y += (h + item_h_q - 1) & ~(item_h_q - 1); + } else + errx(1, "Ran out of atlas memory"); + + bind_texture(); + if (w * h == len) + gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data); + else { + gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y, w, h - 1, GL_RGBA, GL_UNSIGNED_BYTE, data); + /* Upload the last row separately */ + gl(TexSubImage2D) (GL_TEXTURE_2D, 0, x, y + h - 1, len - (w * (h - 1)), 1, GL_RGBA, GL_UNSIGNED_BYTE, + data + w * (h - 1)); + } + + *px = x / item_w; + *py = y / item_h_q; +} diff --git a/bin/gl_atlas.h b/bin/gl_atlas.h @@ -0,0 +1,47 @@ +/* + * 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, Maysum Panju + */ +#ifndef DEMO_ATLAS_H +#define DEMO_ATLAS_H + +#include "demo-common.h" + + +namespace citrun { + +class gl_atlas { + GLuint tex_unit; + GLuint tex_name; + GLuint tex_w; + GLuint tex_h; + GLuint item_w; + GLuint item_h_q; /* height quantum */ + GLuint cursor_x; + GLuint cursor_y; + +public: + gl_atlas(unsigned int, unsigned int, unsigned int, unsigned int); + ~gl_atlas(); + + void alloc(glyphy_rgba_t *, unsigned int, unsigned int *, unsigned int *); + void bind_texture(); + void set_uniforms(); +}; + +} // namespace citrun + +#endif /* DEMO_ATLAS_H */ diff --git a/bin/gl_font.cc b/bin/gl_font.cc @@ -25,14 +25,14 @@ #include "glyphy/glyphy-freetype.h" -citrun::gl_font::gl_font(std::string const &font_path, demo_atlas_t *at) : +citrun::gl_font::gl_font(std::string const &font_path, citrun::gl_atlas &at) : face(NULL), num_glyphs(0), sum_error(0), sum_endpoints(0), sum_fetch(0), sum_bytes(0), - atlas(demo_atlas_reference(at)), + atlas(at), acc(glyphy_arc_accumulator_create()) { FT_Init_FreeType(&ft_library); @@ -42,7 +42,6 @@ citrun::gl_font::gl_font(std::string const &font_path, demo_atlas_t *at) : citrun::gl_font::~gl_font() { glyphy_arc_accumulator_destroy(acc); - demo_atlas_destroy(atlas); } FT_Face @@ -51,7 +50,7 @@ citrun::gl_font::get_face() const return face; } -demo_atlas_t * +citrun::gl_atlas & citrun::gl_font::get_atlas() { return atlas; @@ -182,7 +181,7 @@ citrun::gl_font::_upload_glyph(unsigned int glyph_index, glyph_info->is_empty = glyphy_extents_is_empty (&glyph_info->extents); if (!glyph_info->is_empty) - demo_atlas_alloc (atlas, buffer, output_len, + atlas.alloc(buffer, output_len, &glyph_info->atlas_x, &glyph_info->atlas_y); } diff --git a/bin/gl_font.h b/bin/gl_font.h @@ -22,7 +22,7 @@ #include <unordered_map> #include "demo-common.h" -#include "demo-atlas.h" +#include "gl_atlas.h" #include <ft2build.h> #include FT_FREETYPE_H @@ -53,7 +53,7 @@ class gl_font { double sum_fetch; unsigned int sum_bytes; - demo_atlas_t *atlas; + citrun::gl_atlas &atlas; glyphy_arc_accumulator_t *acc; void _upload_glyph(unsigned int, glyph_info_t *); @@ -67,11 +67,11 @@ class gl_font { glyphy_extents_t *, double *); public: - gl_font(std::string const&, demo_atlas_t *atlas); + gl_font(std::string const&, citrun::gl_atlas &); ~gl_font(); FT_Face get_face() const; - demo_atlas_t *get_atlas(); + citrun::gl_atlas &get_atlas(); void lookup_glyph(unsigned int, glyph_info_t *); void print_stats(); }; diff --git a/bin/gl_state.cc b/bin/gl_state.cc @@ -20,7 +20,7 @@ citrun::gl_state::gl_state() : program(demo_shader_create_program()), - atlas(demo_atlas_create(2048, 1024, 64, 8)), + atlas(2048, 1024, 64, 8), u_debug(false), u_contrast(1.0), u_gamma_adjust(1.0), @@ -33,7 +33,6 @@ citrun::gl_state::gl_state() : citrun::gl_state::~gl_state() { - demo_atlas_destroy(atlas); glDeleteProgram(program); } @@ -52,7 +51,7 @@ citrun::gl_state::setup() { glUseProgram(program); - demo_atlas_set_uniforms(atlas); + atlas.set_uniforms(); SET_UNIFORM(u_debug, u_debug); SET_UNIFORM(u_contrast, u_contrast); @@ -65,7 +64,7 @@ citrun::gl_state::setup() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } -demo_atlas_t * +citrun::gl_atlas & citrun::gl_state::get_atlas() { return atlas; diff --git a/bin/gl_state.h b/bin/gl_state.h @@ -19,7 +19,7 @@ #define DEMO_GLSTATE_H #include "demo-common.h" -#include "demo-atlas.h" +#include "gl_atlas.h" // citrun::gl_atlas #include "demo-shader.h" @@ -27,7 +27,7 @@ namespace citrun { class gl_state { GLuint program; - demo_atlas_t *atlas; + citrun::gl_atlas atlas; /* Uniforms */ double u_debug; @@ -41,7 +41,7 @@ public: ~gl_state(); void setup(); - demo_atlas_t *get_atlas(); + citrun::gl_atlas &get_atlas(); void scale_gamma_adjust(double); void scale_contrast(double); void toggle_debug();