Skip to content
This repository
tree: 227bf34aa4
Fetching contributors…

Cannot retrieve contributors at this time

executable file 302 lines (232 sloc) 7.652 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/*
Copyright (c) 2011 NVIDIA Corporation
Copyright (c) 2011-2012 Cass Everitt
Copyright (c) 2012 Scott Nations
Copyright (c) 2012 Mathias Schott
Copyright (c) 2012 Nigel Stewart
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef REGAL_UTIL_H
#define REGAL_UTIL_H

#include <cstdlib>
#include <cstdio>

#include <limits>

#include "RegalSystem.h"

// Visual Studio with /W4 complains about unused function
// parameters.

#ifndef UNUSED_PARAMETER
#ifdef _MSC_VER
#define UNUSED_PARAMETER(p) (p)
#else
#define UNUSED_PARAMETER(p)
#endif
#endif

#define REGAL_QUOTE(str) #str
#define REGAL_EQUOTE(str) REGAL_QUOTE(str)

#define REGAL_NAMESPACE_INTERNAL Regal

// REGAL_GLOBAL_BEGIN and REGAL_GLOBAL_END for scoping externals
// such as C, C++ and boost includes.

// VC8, VC9 - C4127: conditional expression is constant in std::list
// VC10 - C4512:
// VC9, VC10 - C4996: 'vsprintf': This function or variable may be unsafe

#ifdef _MSC_VER
# define REGAL_GLOBAL_BEGIN \
__pragma(pack(push)) \
__pragma(pack(8)) \
__pragma(warning(push)) \
__pragma(warning(disable : 4127)) \
__pragma(warning(disable : 4512)) \
__pragma(warning(disable : 4996))
# define REGAL_GLOBAL_END \
__pragma(warning(pop)) \
__pragma(pack(pop))
#else
# define REGAL_GLOBAL_BEGIN
# define REGAL_GLOBAL_END
#endif

// REGAL_NAMESPACE_BEGIN and REGAL_NAMESPACE_END for scoping Regal internals

#ifdef _MSC_VER
# define REGAL_NAMESPACE_BEGIN \
__pragma(pack(push)) \
__pragma(pack(8)) \
__pragma(warning(push)) \
__pragma(warning(disable : 4996)) \
namespace REGAL_NAMESPACE_INTERNAL \
{
# define REGAL_NAMESPACE_END \
} \
__pragma(warning(pop)) \
__pragma(pack(pop))
#else
# define REGAL_NAMESPACE_BEGIN \
namespace REGAL_NAMESPACE_INTERNAL \
{
# define REGAL_NAMESPACE_END \
}
#endif

// Compile-time configuration
// - Emulation not forced by default
// - All emulation layers enabled by default

#ifndef REGAL_FORCE_EMULATION
#define REGAL_FORCE_EMULATION 0
#endif

// Debug dispatch disabled by default in
// release mode

#ifndef REGAL_DEBUG
#ifdef NDEBUG
#define REGAL_DEBUG 0
#else
#define REGAL_DEBUG 1
#endif
#endif

// Error dispatch disabled by default in
// release mode

#ifndef REGAL_ERROR
#ifdef NDEBUG
#define REGAL_ERROR 0
#else
#define REGAL_ERROR 1
#endif
#endif

// Emulation dispatch enabled by default

#ifndef REGAL_EMULATION
#define REGAL_EMULATION 1
#endif

// Driver dispatch enabled by default

#ifndef REGAL_DRIVER
#define REGAL_DRIVER 1
#endif

#ifndef REGAL_LOG
#ifdef NDEBUG
#define REGAL_LOG 0
#else
#define REGAL_LOG 1
#endif
#endif

#ifndef REGAL_EMU_PPA
#define REGAL_EMU_PPA 1
#endif

#ifndef REGAL_EMU_OBJ
#define REGAL_EMU_OBJ 1
#endif

#ifndef REGAL_EMU_BIN
#define REGAL_EMU_BIN 1
#endif

#ifndef REGAL_EMU_DSA
#define REGAL_EMU_DSA 1
#endif

#ifndef REGAL_EMU_IFF
#define REGAL_EMU_IFF 1
#endif

#ifndef REGAL_EMU_VAO
#define REGAL_EMU_VAO 1
#endif

//

#ifndef REGAL_STATIC_ES2
#define REGAL_STATIC_ES2 0
#endif

#ifndef REGAL_STATIC_EGL
#define REGAL_STATIC_EGL 0
#endif

// AssertFunction depends on Error log, but
// ideally we wouldn't depend on RegalLog.h here

REGAL_GLOBAL_BEGIN

#include <string>

#include "RegalLog.h"

REGAL_GLOBAL_END

REGAL_NAMESPACE_BEGIN

inline const char * GetEnv(const char * const varname)
{
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4996)
#endif

    return std::getenv(varname);

#ifdef _MSC_VER
#pragma warning (pop)
#endif
}

//
// RegalCheckGLError
//

#ifdef NDEBUG
# define RegalCheckGLError( ctx )
#else
# define RegalCheckGLError( ctx ) RegalCheckForGLErrors( (ctx) )
#endif

//
// RegalAssert
//

#if defined(NDEBUG) && !defined(REGAL_NO_ASSERT)
# define REGAL_NO_ASSERT
#endif

#ifdef REGAL_NO_ASSERT
# define RegalAssert( foo )
#else
# define RegalAssert( foo ) if (!(foo)) ::REGAL_NAMESPACE_INTERNAL::AssertFunction( __FILE__ , __LINE__ , #foo);
#endif

#ifndef REGAL_NO_ASSERT
void AssertFunction(const char *file, const std::size_t line, const char *expr);
#endif

//
//
//

struct RegalContext;

void RegalCheckForGLErrors( RegalContext * ctx );

void *GetProcAddress(const char *entry);

template <typename T>
T *GetProcAddress(T *&f, const char *entry )
{
  f = reinterpret_cast<T *>(GetProcAddress(entry));
  return f;
}

// Lightweight boost::algorithm::string::starts_with

inline bool starts_with(const std::string &input, const std::string &test) { return std::strncmp(input.c_str(),test.c_str(),test.length())==0; }
inline bool starts_with(const std::string &input, const char * const test) { return std::strncmp(input.c_str(),test, strlen(test) )==0; }

// ToFloat for integer -> float

template <typename T> inline float ToFloat(const bool normalize, const T v ) {
  UNUSED_PARAMETER(normalize); return float(v);
}

template <> inline float ToFloat(const bool normalize, const signed char v ) {
  return normalize ? float(v)/float(std::numeric_limits<signed char>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const unsigned char v ) {
  return normalize ? float(v)/float(std::numeric_limits<unsigned char>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const signed short v ) {
  return normalize ? float(v)/float(std::numeric_limits<signed short>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const unsigned short v ) {
  return normalize ? float(v)/float(std::numeric_limits<unsigned short>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const signed int v ) {
  return normalize ? float(v)/float(std::numeric_limits<signed int>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const unsigned int v ) {
  return normalize ? float(v)/float(std::numeric_limits<unsigned int>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const signed long int v ) {
  return normalize ? float(v)/float(std::numeric_limits<signed long int>::max()) : float(v);
}

template <> inline float ToFloat(const bool normalize, const unsigned long int v ) {
  return normalize ? float(v)/float(std::numeric_limits<unsigned long int>::max()) : float(v);
}

REGAL_NAMESPACE_END

#endif
Something went wrong with that request. Please try again.