Skip to content

Commit

Permalink
Include shaders in the binary (#40)
Browse files Browse the repository at this point in the history
* include-shaders: this is about right

* include-shaders: this works!

* include-shaders: remove debug stuff

* include-shaders: cannot remove that cache buster for now

* include-shaders: move size2str into Manager as static

* include-shaders: use static const shaders

* include-shaders: remove dead platform code

* include-shaders: disable cache buster for now

* include-shaders: rename vertex to vertex_3d

* include-shaders: re-enable cache buster
  • Loading branch information
fenollp authored Apr 13, 2017
1 parent 74c2935 commit b376fd9
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 139 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ endif()
# SOURCES
#-----------------------------------------------------------------------------

add_subdirectory(${PROJECT_SOURCE_DIR}/src/resources)

# to add an include path
include_directories(
${PROJECT_SOURCE_DIR}/src/include
${PROJECT_SOURCE_DIR}/src/common
${CMAKE_CURRENT_BINARY_DIR}/src/resources
)

set(SOURCES
src/common/platform/platform.cc

src/miners/main.cc
src/miners/Arguments.cc
src/miners/Algorithm.cc
Expand Down Expand Up @@ -102,6 +103,8 @@ endif(APPLE)
# create your executable with
add_executable(miners ${GUI_TYPE} ${SOURCES})

add_dependencies(miners shaders_out)

# create a library with
# add_library(my_lib SHARED ${SOURCES})

Expand All @@ -116,9 +119,6 @@ target_link_libraries(miners
string(REPLACE ";" " " glfw_static_ldflags "${GLFW_STATIC_LDFLAGS}")
set_property(TARGET miners APPEND_STRING PROPERTY LINK_FLAGS "${glfw_static_ldflags}")

add_custom_command(TARGET miners POST_BUILD
COMMAND ln -sf ${PROJECT_SOURCE_DIR}/src/resources .)

#-----------------------------------------------------------------------------
# PACKAGING
#-----------------------------------------------------------------------------
Expand Down
13 changes: 0 additions & 13 deletions src/common/platform.hpp

This file was deleted.

111 changes: 0 additions & 111 deletions src/common/platform/platform.cc

This file was deleted.

12 changes: 12 additions & 0 deletions src/include/Manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public:
Arguments* args() { return args_; }
Scene* scene() { return scene_; }

public:
static std::string /// 991337 --> "991,337"
size2str (size_t size) {
std::string str = std::to_string(size);
int pos = str.length() - 3;
while (pos > 0) {
str.insert(pos, ",");
pos -= 3;
}
return str;
}

protected:
bool fullscreen_;
Arguments* args_;
Expand Down
4 changes: 2 additions & 2 deletions src/miners/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>

#include <platform.hpp>
#include <Manager.hh>

#include <Algorithm.hh>
#include <Algo2DEntropy.hh>
Expand Down Expand Up @@ -44,7 +44,7 @@ Algorithm::loadDataRange(const DataRange& range, size_t& size)
size = std::min<size_t>(range.size(), max_data_size_);
else
size = range.size();
std::cerr << "read " << size2str(size) << " bytes" << std::endl;
std::cerr << "read " << Manager::size2str(size) << " bytes" << std::endl;
return loader_->dataChunk(range.begin, size);
}

Expand Down
6 changes: 3 additions & 3 deletions src/miners/Scene2D.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <iostream>
#include <stdexcept>

#include <platform.hpp>
#include <shaders.hh>

#include <Scene2D.hh>
#include <Manager.hh>

void
Scene2D::load_shaders() {
const std::vector<tdogl::Shader> shaders{
tdogl::Shader::shaderFromFile(ResourcePath("vertex_2d.glsl"), GL_VERTEX_SHADER),
tdogl::Shader::shaderFromFile(ResourcePath("fragment.glsl"), GL_FRAGMENT_SHADER)
tdogl::Shader(shader__vertex_2d, GL_VERTEX_SHADER),
tdogl::Shader(shader__fragment, GL_FRAGMENT_SHADER)
};
program_ = new tdogl::Program(shaders);
}
Expand Down
10 changes: 5 additions & 5 deletions src/miners/Scene3D.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include <stdexcept>
#include <algorithm>

#include <platform.hpp>
#include <shaders.hh>

#include <Scene3D.hh>
#include <Manager.hh>

void
Scene3D::load_shaders() {
const std::vector<tdogl::Shader> shaders{
tdogl::Shader::shaderFromFile(ResourcePath("vertex.glsl"), GL_VERTEX_SHADER),
tdogl::Shader::shaderFromFile(ResourcePath("fragment.glsl"), GL_FRAGMENT_SHADER)
tdogl::Shader(shader__vertex_3d, GL_VERTEX_SHADER),
tdogl::Shader(shader__fragment, GL_FRAGMENT_SHADER)
};
ctx_.program = new tdogl::Program(shaders);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ Scene3D::reload()
ctx_.reset_points();
algo->apply(ctx_.vertices, ctx_.colors, ctx_.selected, ctx_.width, ctx_.height, ctx_.depth)
|| std::cerr << "!apply" << std::endl;
std::cout << "#points: " << size2str(ctx_.selected.size()) << std::endl;
std::cout << "#points: " << Manager::size2str(ctx_.selected.size()) << std::endl;
ctx_.selected.shrink_to_fit();
load_buffers();
glBindVertexArray(ctx_.vao);
Expand All @@ -97,7 +97,7 @@ Scene3D::load(Algorithm* algorithm)
load_shaders();
algo->apply(ctx_.vertices, ctx_.colors, ctx_.selected, ctx_.width, ctx_.height, ctx_.depth)
|| std::cerr << "!apply" << std::endl;
std::cout << "#points: " << size2str(ctx_.selected.size()) << std::endl;
std::cout << "#points: " << Manager::size2str(ctx_.selected.size()) << std::endl;
ctx_.selected.shrink_to_fit();
load_buffers();

Expand Down
8 changes: 8 additions & 0 deletions src/resources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_custom_command(
OUTPUT shaders.hh
cache-buster.hh
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/pack_GLSLs.sh ${CMAKE_CURRENT_BINARY_DIR}/shaders.hh
)

set(GENERATED_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(shaders_out ${GLSLs} DEPENDS shaders.hh)
24 changes: 24 additions & 0 deletions src/resources/pack_GLSLs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Combine surrounding GLSL files into C++ data

[[ $# -ne 1 ]] && echo "Usage: $0 <path to target header file>" && exit 1

hh="$1"
hh_dir="$(dirname "$0")"

pack_GLSL() {
local glsl="$1"
local name="$(basename "$glsl" .glsl)"
echo >>"$hh"
echo "static const std::string shader__${name} = R\"(\\" >>"$hh"
cat "$glsl" >>"$hh"
echo ')";' >>"$hh"
echo >>"$hh"
}

echo '#pragma once' >"$hh"
echo '#include <string>' >>"$hh"
for glsl in "$hh_dir"/*.glsl; do
pack_GLSL "$glsl"
done
File renamed without changes.

0 comments on commit b376fd9

Please sign in to comment.