Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.[oa]
*.exe
test/libmatrix_test
test/libmatrix_test.exe
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
CXXFLAGS = -Wall -Werror -pedantic -O3
COMMON_FLAGS = -std=gnu++26 -Wall -Werror -pedantic -march=native -O3
ifeq ($(shell uname -m), x86_64)
COMMON_FLAGS += -mfpmath=sse
endif
CXXFLAGS ?= $(COMMON_FLAGS)
LIBMATRIX = libmatrix.a
LIBSRCS = mat.cc program.cc log.cc util.cc shader-source.cc
LIBOBJS = $(LIBSRCS:.cc=.o)
Expand Down
45 changes: 41 additions & 4 deletions log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// Alexandros Frantzis <alexandros.frantzis@linaro.org>
// Jesse Barker <jesse.barker@linaro.org>
//
#include <unistd.h>
#include <cstdio>
#include <cstdarg>
#include <string>
Expand All @@ -22,6 +21,13 @@
#include <android/log.h>
#endif

#ifdef _WIN32
// On windows 'isatty' is found in <io.h>
#include <io.h>
#else
#include <unistd.h>
#endif

using std::string;

const string Log::continuation_prefix("\x10");
Expand All @@ -33,6 +39,7 @@ static const string terminal_color_normal("\033[0m");
static const string terminal_color_red("\033[1;31m");
static const string terminal_color_cyan("\033[36m");
static const string terminal_color_yellow("\033[33m");
static const string terminal_color_magenta("\033[35m");
static const string empty;

static void
Expand Down Expand Up @@ -96,6 +103,16 @@ print_prefixed_message(std::ostream& stream, const string& color, const string&
delete[] buf;
}

#ifdef ANDROID
static void
android_vlog(int prio, const char *tag, const char *fmt, va_list ap)
{
va_list aq;
va_copy(aq, ap);
__android_log_vprint(prio, tag, fmt, aq);
va_end(aq);
}
#endif

void
Log::info(const char *fmt, ...)
Expand All @@ -110,7 +127,7 @@ Log::info(const char *fmt, ...)
const string& color(do_debug_ ? infocolor : empty);
print_prefixed_message(std::cout, color, prefix, fmt, ap);
#else
__android_log_vprint(ANDROID_LOG_INFO, appname_.c_str(), fmt, ap);
android_vlog(ANDROID_LOG_INFO, appname_.c_str(), fmt, ap);
#endif

if (extra_out_)
Expand All @@ -132,7 +149,7 @@ Log::debug(const char *fmt, ...)
static const string& dbgcolor(isatty(fileno(stdout)) ? terminal_color_yellow : empty);
print_prefixed_message(std::cout, dbgcolor, dbgprefix, fmt, ap);
#else
__android_log_vprint(ANDROID_LOG_DEBUG, appname_.c_str(), fmt, ap);
android_vlog(ANDROID_LOG_DEBUG, appname_.c_str(), fmt, ap);
#endif

if (extra_out_)
Expand All @@ -152,7 +169,7 @@ Log::error(const char *fmt, ...)
static const string& errcolor(isatty(fileno(stderr)) ? terminal_color_red : empty);
print_prefixed_message(std::cerr, errcolor, errprefix, fmt, ap);
#else
__android_log_vprint(ANDROID_LOG_ERROR, appname_.c_str(), fmt, ap);
android_vlog(ANDROID_LOG_ERROR, appname_.c_str(), fmt, ap);
#endif

if (extra_out_)
Expand All @@ -161,6 +178,26 @@ Log::error(const char *fmt, ...)
va_end(ap);
}

void
Log::warning(const char *fmt, ...)
{
static const string warnprefix("Warning");
va_list ap;
va_start(ap, fmt);

#ifndef ANDROID
static const string& warncolor(isatty(fileno(stderr)) ? terminal_color_magenta : empty);
print_prefixed_message(std::cerr, warncolor, warnprefix, fmt, ap);
#else
android_vlog(ANDROID_LOG_WARN, appname_.c_str(), fmt, ap);
#endif

if (extra_out_)
print_prefixed_message(*extra_out_, empty, warnprefix, fmt, ap);

va_end(ap);
}

void
Log::flush()
{
Expand Down
2 changes: 2 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Log
static void debug(const char *fmt, ...);
// Emit an error message
static void error(const char *fmt, ...);
// Emit a warning message
static void warning(const char *fmt, ...);
// Explicit flush of the log buffer
static void flush();
// A prefix constant that informs the logging infrastructure that the log
Expand Down
26 changes: 23 additions & 3 deletions mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include <iostream>
#include <iomanip>
#include "vec.h"
#ifndef USE_EXCEPTIONS
// If we're not throwing exceptions, we'll need the logger to make sure the
// caller is informed of errors.
#include "log.h"
#endif // USE_EXCEPTIONS

namespace LibMatrix
{
Expand Down Expand Up @@ -103,12 +108,17 @@ class tmat2
//
// NOTE: If this is non-invertible, we will
// throw to avoid undefined behavior.
tmat2& inverse() throw(std::runtime_error)
tmat2& inverse()
{
T d(determinant());
if (d == static_cast<T>(0))
{
#ifdef USE_EXCEPTIONS
throw std::runtime_error("Matrix is noninvertible!!!!");
#else // !USE_EXCEPTIONS
Log::error("Matrix is noninvertible!!!!\n");
return *this;
#endif // USE_EXCEPTIONS
}
T c0r0(m_[3] / d);
T c0r1(-m_[1] / d);
Expand Down Expand Up @@ -397,12 +407,17 @@ class tmat3
//
// NOTE: If this is non-invertible, we will
// throw to avoid undefined behavior.
tmat3& inverse() throw(std::runtime_error)
tmat3& inverse()
{
T d(determinant());
if (d == static_cast<T>(0))
{
#ifdef USE_EXCEPTIONS
throw std::runtime_error("Matrix is noninvertible!!!!");
#else // !USE_EXCEPTIONS
Log::error("Matrix is noninvertible!!!!\n");
return *this;
#endif // USE_EXCEPTIONS
}
tmat2<T> minor0(m_[4], m_[5], m_[7], m_[8]);
tmat2<T> minor1(m_[7], m_[8], m_[1], m_[2]);
Expand Down Expand Up @@ -771,12 +786,17 @@ class tmat4
//
// NOTE: If this is non-invertible, we will
// throw to avoid undefined behavior.
tmat4& inverse() throw(std::runtime_error)
tmat4& inverse()
{
T d(determinant());
if (d == static_cast<T>(0))
{
#ifdef USE_EXCEPTIONS
throw std::runtime_error("Matrix is noninvertible!!!!");
#else // !USE_EXCEPTIONS
Log::error("Matrix is noninvertible!!!!\n");
return *this;
#endif // USE_EXCEPTIONS
}
tmat3<T> minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]);
tmat3<T> minor1(m_[1], m_[2], m_[3], m_[13], m_[14], m_[15], m_[9], m_[10], m_[11]);
Expand Down
2 changes: 1 addition & 1 deletion program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Program::addShader(unsigned int type, const string& source)
}

shader.attach(handle_);
shaders_.push_back(shader);
shaders_.push_back(std::move(shader));
return;
}

Expand Down
8 changes: 8 additions & 0 deletions program.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <vector>
#include <map>
#include <utility>
#include "mat.h"

// Simple shader container. Abstracts all of the OpenGL bits, but leaves
Expand All @@ -35,6 +36,13 @@ class Shader
message_(shader.message_),
ready_(shader.ready_),
valid_(shader.valid_) {}
Shader(Shader&& shader) noexcept:
handle_(std::exchange(shader.handle_, 0)),
type_(std::exchange(shader.type_, 0)),
source_(std::move(shader.source_)),
message_(std::move(shader.message_)),
ready_(std::exchange(shader.ready_, false)),
valid_(std::exchange(shader.valid_, false)) {}
Shader(unsigned int type, const std::string& source);
~Shader();

Expand Down
58 changes: 53 additions & 5 deletions shader-source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@
#include "vec.h"
#include "util.h"

namespace
{

bool is_valid_precision_value(ShaderSource::PrecisionValue precision_value)
{
switch(precision_value) {
case ShaderSource::PrecisionValueLow:
case ShaderSource::PrecisionValueMedium:
case ShaderSource::PrecisionValueHigh:
case ShaderSource::PrecisionValueDefault:
return true;
default:
return false;
}
}

bool is_valid_shader_type(ShaderSource::ShaderType shader_type)
{
switch(shader_type) {
case ShaderSource::ShaderTypeVertex:
case ShaderSource::ShaderTypeFragment:
case ShaderSource::ShaderTypeUnknown:
return true;
default:
return false;
}
}

}

/**
* Holds default precision values for all shader types
* (even the unknown type, which is hardwired to default precision values)
Expand All @@ -34,7 +64,7 @@ ShaderSource::default_precision_(ShaderSource::ShaderTypeUnknown + 1);
bool
ShaderSource::load_file(const std::string& filename, std::string& str)
{
std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename));
std::unique_ptr<std::istream> is_ptr(Util::get_resource(filename));
std::istream& inputFile(*is_ptr);

if (!inputFile)
Expand Down Expand Up @@ -421,7 +451,9 @@ ShaderSource::emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue
ss << "#endif" << std::endl;
}
}
else if (val >= 0 && val < ShaderSource::PrecisionValueDefault) {
else if (is_valid_precision_value(val) &&
val != ShaderSource::PrecisionValueDefault)
{
ss << "precision " << precision_map[val] << " ";
ss << type_str << ";" << std::endl;
}
Expand Down Expand Up @@ -456,6 +488,22 @@ ShaderSource::str()
precision = default_precision(type_);

/* Create the precision statements */
std::stringstream precision_macros_ss;

precision_macros_ss << "#if defined(GL_ES)";
if (type_ == ShaderSource::ShaderTypeFragment)
precision_macros_ss << " && defined(GL_FRAGMENT_PRECISION_HIGH)";
precision_macros_ss << std::endl;
precision_macros_ss << "#define HIGHP_OR_DEFAULT highp" << std::endl;
precision_macros_ss << "#else" << std::endl;
precision_macros_ss << "#define HIGHP_OR_DEFAULT" << std::endl;
precision_macros_ss << "#endif" << std::endl;
precision_macros_ss << "#if defined(GL_ES)" << std::endl;
precision_macros_ss << "#define MEDIUMP_OR_DEFAULT mediump" << std::endl;
precision_macros_ss << "#else" << std::endl;
precision_macros_ss << "#define MEDIUMP_OR_DEFAULT" << std::endl;
precision_macros_ss << "#endif" << std::endl;

std::stringstream ss;

emit_precision(ss, precision.int_precision, "int");
Expand All @@ -469,7 +517,7 @@ ShaderSource::str()
precision_str.insert(precision_str.size(), "#endif\n");
}

return precision_str + source_.str();
return precision_macros_ss.str() + precision_str + source_.str();
}

/**
Expand Down Expand Up @@ -512,7 +560,7 @@ void
ShaderSource::default_precision(const ShaderSource::Precision& precision,
ShaderSource::ShaderType type)
{
if (type < 0 || type > ShaderSource::ShaderTypeUnknown)
if (!is_valid_shader_type(type))
type = ShaderSource::ShaderTypeUnknown;

if (type == ShaderSource::ShaderTypeUnknown) {
Expand All @@ -537,7 +585,7 @@ ShaderSource::default_precision(const ShaderSource::Precision& precision,
const ShaderSource::Precision&
ShaderSource::default_precision(ShaderSource::ShaderType type)
{
if (type < 0 || type > ShaderSource::ShaderTypeUnknown)
if (!is_valid_shader_type(type))
type = ShaderSource::ShaderTypeUnknown;

return default_precision_[type];
Expand Down
Loading