fractorium/Source/EmberCommon/SimpleGlob.h

960 lines
27 KiB
C
Raw Normal View History

/*! @file SimpleGlob.h
2014-12-07 03:59:26 -05:00
@version 3.6
@brief A cross-platform file globbing library providing the ability to
expand wildcards in command-line arguments to a list of all matching
files. It is designed explicitly to be portable to any platform and has
been tested on Windows and Linux. See CSimpleGlobTempl for the class
definition.
@section features FEATURES
- MIT Licence allows free use in all software (including GPL and
commercial)
- multi-platform (Windows 95/98/ME/NT/2K/XP, Linux, Unix)
- supports most of the standard linux glob() options
- recognition of a forward paths as equivalent to a backward slash
on Windows. e.g. "c:/path/foo*" is equivalent to "c:\path\foo*".
- implemented with only a single C++ header file
- char, wchar_t and Windows TCHAR in the same program
- complete working examples included
- compiles cleanly at warning level 4 (Windows/VC.NET 2003),
warning level 3 (Windows/VC6) and -Wall (Linux/gcc)
@section usage USAGE
The SimpleGlob class is used by following these steps:
<ol>
<li> Include the SimpleGlob.h header file
<pre>
\#include "SimpleGlob.h"
</pre>
<li> Instantiate a CSimpleGlob object supplying the appropriate flags.
2014-12-07 03:59:26 -05:00
<pre>
@link CSimpleGlobTempl CSimpleGlob @endlink glob(FLAGS);
</pre>
<li> Add all file specifications to the glob class.
2014-12-07 03:59:26 -05:00
<pre>
glob.Add("file*");
glob.Add(argc, argv);
</pre>
<li> Process all files with File(), Files() and FileCount()
2014-12-07 03:59:26 -05:00
<pre>
for (int n = 0; n < glob.FileCount(); ++n) {
ProcessFile(glob.File(n));
}
</pre>
2014-12-07 03:59:26 -05:00
</ol>
2014-12-07 03:59:26 -05:00
@section licence MIT LICENCE
<pre>
2014-12-07 03:59:26 -05:00
The licence text below is the boilerplate "MIT Licence" used from:
http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2006-2013, Brodie Thiesfield
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</pre>
*/
#ifndef INCLUDED_SimpleGlob
#define INCLUDED_SimpleGlob
/*! @brief The operation of SimpleGlob is fine-tuned via the use of a
2014-12-07 03:59:26 -05:00
combination of the following flags.
2014-12-07 03:59:26 -05:00
The flags may be passed at initialization of the class and used for every
filespec added, or alternatively they may optionally be specified in the
call to Add() and be different for each filespec.
2014-12-07 03:59:26 -05:00
@param SG_GLOB_ERR
Return upon read error (e.g. directory does not have read permission)
2014-12-07 03:59:26 -05:00
@param SG_GLOB_MARK
Append a slash (backslash in Windows) to every path which corresponds
to a directory
2014-12-07 03:59:26 -05:00
@param SG_GLOB_NOSORT
By default, files are returned in sorted into string order. With this
flag, no sorting is done. This is not compatible with
SG_GLOB_FULLSORT.
2014-12-07 03:59:26 -05:00
@param SG_GLOB_FULLSORT
By default, files are sorted in groups belonging to each filespec that
was added. For example if the filespec "b*" was added before the
filespec "a*" then the argv array will contain all b* files sorted in
order, followed by all a* files sorted in order. If this flag is
specified, the entire array will be sorted ignoring the filespec
groups.
2014-12-07 03:59:26 -05:00
@param SG_GLOB_NOCHECK
If the pattern doesn't match anything, return the original pattern.
2014-12-07 03:59:26 -05:00
@param SG_GLOB_TILDE
Tilde expansion is carried out (on Unix platforms)
2014-12-07 03:59:26 -05:00
@param SG_GLOB_ONLYDIR
Return only directories which match (not compatible with
SG_GLOB_ONLYFILE)
2014-12-07 03:59:26 -05:00
@param SG_GLOB_ONLYFILE
Return only files which match (not compatible with SG_GLOB_ONLYDIR)
2014-12-07 03:59:26 -05:00
@param SG_GLOB_NODOT
Do not return the "." or ".." special directories.
*/
enum SG_Flags {
2014-12-07 03:59:26 -05:00
SG_GLOB_ERR = 1 << 0,
SG_GLOB_MARK = 1 << 1,
SG_GLOB_NOSORT = 1 << 2,
SG_GLOB_NOCHECK = 1 << 3,
SG_GLOB_TILDE = 1 << 4,
SG_GLOB_ONLYDIR = 1 << 5,
SG_GLOB_ONLYFILE = 1 << 6,
SG_GLOB_NODOT = 1 << 7,
SG_GLOB_FULLSORT = 1 << 8
};
/*! @brief Error return codes */
enum SG_Error {
2014-12-07 03:59:26 -05:00
SG_SUCCESS = 0,
SG_ERR_NOMATCH = 1,
SG_ERR_MEMORY = -1,
SG_ERR_FAILURE = -2
};
// ---------------------------------------------------------------------------
// Platform dependent implementations
// if we aren't on Windows and we have ICU available, then enable ICU
// by default. Define this to 0 to intentially disable it.
#ifndef SG_HAVE_ICU
# if !defined(_WIN32) && defined(USTRING_H)
# define SG_HAVE_ICU 1
# else
# define SG_HAVE_ICU 0
# endif
#endif
// don't include this in documentation as it isn't relevant
#ifndef DOXYGEN
// on Windows we want to use MBCS aware string functions and mimic the
// Unix glob functionality. On Unix we just use glob.
#ifdef _WIN32
# include <mbstring.h>
# define sg_strchr ::_mbschr
# define sg_strrchr ::_mbsrchr
# define sg_strlen ::_mbslen
# if __STDC_WANT_SECURE_LIB__
# define sg_strcpy_s(a,n,b) ::_mbscpy_s(a,n,b)
# else
# define sg_strcpy_s(a,n,b) ::_mbscpy(a,b)
# endif
# define sg_strcmp ::_mbscmp
# define sg_strcasecmp ::_mbsicmp
# define SOCHAR_T byte
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <glob.h>
# include <limits.h>
# define MAX_PATH PATH_MAX
# define sg_strchr ::strchr
# define sg_strrchr ::strrchr
# define sg_strlen ::strlen
# define sg_strcpy_s(a,n,b) ::strcpy(a,b)
# define sg_strcmp ::strcmp
# define sg_strcasecmp ::strcasecmp
# define SOCHAR_T char
#endif
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
// use assertions to test the input data
#ifdef _DEBUG
# ifdef _MSC_VER
# include <crtdbg.h>
# define SG_ASSERT(b) _ASSERTE(b)
# else
# include <assert.h>
# define SG_ASSERT(b) assert(b)
# endif
#else
# define SG_ASSERT(b)
#endif
/*! @brief String manipulation functions. */
class SimpleGlobUtil
{
public:
2014-12-07 03:59:26 -05:00
static const char * strchr(const char *s, char c) {
return reinterpret_cast<const char*>(sg_strchr(reinterpret_cast<const SOCHAR_T *>(s), c));
}
static const wchar_t * strchr(const wchar_t *s, wchar_t c) {
return ::wcschr(s, c);
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static const UChar * strchr(const UChar *s, UChar c) {
return ::u_strchr(s, c);
}
#endif
2014-12-07 03:59:26 -05:00
static const char * strrchr(const char *s, char c) {
return reinterpret_cast<const char*>(sg_strrchr(reinterpret_cast<const SOCHAR_T *>(s), c));
}
static const wchar_t * strrchr(const wchar_t *s, wchar_t c) {
return ::wcsrchr(s, c);
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static const UChar * strrchr(const UChar *s, UChar c) {
return ::u_strrchr(s, c);
}
#endif
2014-12-07 03:59:26 -05:00
// Note: char strlen returns number of bytes, not characters
static size_t strlen(const char *s) { return ::strlen(s); }
static size_t strlen(const wchar_t *s) { return ::wcslen(s); }
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static size_t strlen(const UChar *s) { return ::u_strlen(s); }
#endif
2014-12-07 03:59:26 -05:00
static void strcpy_s(char *dst, size_t n, const char *src) {
(void) n;
sg_strcpy_s(reinterpret_cast<SOCHAR_T *>(dst), n, reinterpret_cast<const SOCHAR_T *>(src));
}
static void strcpy_s(wchar_t *dst, size_t n, const wchar_t *src) {
# if __STDC_WANT_SECURE_LIB__
2014-12-07 03:59:26 -05:00
::wcscpy_s(dst, n, src);
#else
2014-12-07 03:59:26 -05:00
(void) n;
::wcscpy(dst, src);
#endif
2014-12-07 03:59:26 -05:00
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static void strcpy_s(UChar *dst, size_t n, const UChar *src) {
::u_strncpy(dst, src, n);
}
#endif
2014-12-07 03:59:26 -05:00
static int strcmp(const char *s1, const char *s2) {
return sg_strcmp(reinterpret_cast<const SOCHAR_T *>(s1), reinterpret_cast<const SOCHAR_T *>(s2));
}
static int strcmp(const wchar_t *s1, const wchar_t *s2) {
return ::wcscmp(s1, s2);
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static int strcmp(const UChar *s1, const UChar *s2) {
return ::u_strcmp(s1, s2);
}
#endif
2014-12-07 03:59:26 -05:00
static int strcasecmp(const char *s1, const char *s2) {
return sg_strcasecmp(reinterpret_cast<const SOCHAR_T *>(s1), reinterpret_cast<const SOCHAR_T *>(s2));
}
#if _WIN32
2014-12-07 03:59:26 -05:00
static int strcasecmp(const wchar_t *s1, const wchar_t *s2) {
return ::_wcsicmp(s1, s2);
}
#endif // _WIN32
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
static int strcasecmp(const UChar *s1, const UChar *s2) {
return u_strcasecmp(s1, s2, 0);
}
#endif
};
enum SG_FileType {
2014-12-07 03:59:26 -05:00
SG_FILETYPE_INVALID,
SG_FILETYPE_FILE,
SG_FILETYPE_DIR
};
#ifdef _WIN32
#ifndef INVALID_FILE_ATTRIBUTES
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif
#define SG_PATH_CHAR '\\'
/*! @brief Windows glob implementation. */
template<class SOCHAR>
struct SimpleGlobBase
{
2014-12-07 03:59:26 -05:00
SimpleGlobBase() : m_hFind(INVALID_HANDLE_VALUE) { }
int FindFirstFileS(const char * a_pszFileSpec, uint) {
m_hFind = FindFirstFileA(a_pszFileSpec, &m_oFindDataA);
if (m_hFind != INVALID_HANDLE_VALUE) {
return SG_SUCCESS;
}
DWORD dwErr = GetLastError();
if (dwErr == ERROR_FILE_NOT_FOUND) {
return SG_ERR_NOMATCH;
}
return SG_ERR_FAILURE;
}
int FindFirstFileS(const wchar_t * a_pszFileSpec, uint) {
m_hFind = FindFirstFileW(a_pszFileSpec, &m_oFindDataW);
if (m_hFind != INVALID_HANDLE_VALUE) {
return SG_SUCCESS;
}
DWORD dwErr = GetLastError();
if (dwErr == ERROR_FILE_NOT_FOUND) {
return SG_ERR_NOMATCH;
}
return SG_ERR_FAILURE;
}
bool FindNextFileS(char) {
return FindNextFileA(m_hFind, &m_oFindDataA) != FALSE;
}
bool FindNextFileS(wchar_t) {
return FindNextFileW(m_hFind, &m_oFindDataW) != FALSE;
}
void FindDone() {
FindClose(m_hFind);
}
const char * GetFileNameS(char) const {
return m_oFindDataA.cFileName;
}
const wchar_t * GetFileNameS(wchar_t) const {
return m_oFindDataW.cFileName;
}
bool IsDirS(char) const {
return this->GetFileTypeS(m_oFindDataA.dwFileAttributes) == SG_FILETYPE_DIR;
}
bool IsDirS(wchar_t) const {
return this->GetFileTypeS(m_oFindDataW.dwFileAttributes) == SG_FILETYPE_DIR;
}
SG_FileType GetFileTypeS(const char * a_pszPath) {
return this->GetFileTypeS(GetFileAttributesA(a_pszPath));
}
SG_FileType GetFileTypeS(const wchar_t * a_pszPath) {
return this->GetFileTypeS(GetFileAttributesW(a_pszPath));
}
SG_FileType GetFileTypeS(DWORD a_dwAttribs) const {
if (a_dwAttribs == INVALID_FILE_ATTRIBUTES) {
return SG_FILETYPE_INVALID;
}
if (a_dwAttribs & FILE_ATTRIBUTE_DIRECTORY) {
return SG_FILETYPE_DIR;
}
return SG_FILETYPE_FILE;
}
private:
2014-12-07 03:59:26 -05:00
HANDLE m_hFind;
WIN32_FIND_DATAA m_oFindDataA;
WIN32_FIND_DATAW m_oFindDataW;
};
#else // !_WIN32
#define SG_PATH_CHAR '/'
/*! @brief Unix glob implementation. */
template<class SOCHAR>
struct SimpleGlobBase
{
2014-12-07 03:59:26 -05:00
SimpleGlobBase() {
memset(&m_glob, 0, sizeof(m_glob));
m_uiCurr = size_t(-1);
}
~SimpleGlobBase() {
globfree(&m_glob);
}
void FilePrep() {
m_bIsDir = false;
size_t len = strlen(m_glob.gl_pathv[m_uiCurr]);
if (m_glob.gl_pathv[m_uiCurr][len-1] == '/') {
m_bIsDir = true;
m_glob.gl_pathv[m_uiCurr][len-1] = 0;
}
}
int FindFirstFileS(const char * a_pszFileSpec, uint a_uiFlags) {
int nFlags = GLOB_MARK | GLOB_NOSORT;
if (a_uiFlags & SG_GLOB_ERR) nFlags |= GLOB_ERR;
if (a_uiFlags & SG_GLOB_TILDE) nFlags |= GLOB_TILDE;
int rc = glob(a_pszFileSpec, nFlags, nullptr, &m_glob);
if (rc == GLOB_NOSPACE) return SG_ERR_MEMORY;
if (rc == GLOB_ABORTED) return SG_ERR_FAILURE;
if (rc == GLOB_NOMATCH) return SG_ERR_NOMATCH;
m_uiCurr = 0;
FilePrep();
return SG_SUCCESS;
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
int FindFirstFileS(const UChar * a_pszFileSpec, uint a_uiFlags) {
char buf[PATH_MAX] = { 0 };
UErrorCode status = U_ZERO_ERROR;
u_strToUTF8(buf, sizeof(buf), nullptr, a_pszFileSpec, -1, &status);
if (U_FAILURE(status)) return SG_ERR_FAILURE;
return this->FindFirstFileS(buf, a_uiFlags);
}
#endif
2014-12-07 03:59:26 -05:00
bool FindNextFileS(char) {
SG_ASSERT(m_uiCurr != (size_t)-1);
if (++m_uiCurr >= m_glob.gl_pathc) {
return false;
}
FilePrep();
return true;
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
bool FindNextFileS(UChar) {
return this->FindNextFileS((char)0);
}
#endif
2014-12-07 03:59:26 -05:00
void FindDone() {
globfree(&m_glob);
memset(&m_glob, 0, sizeof(m_glob));
m_uiCurr = size_t(-1);
}
2014-12-07 03:59:26 -05:00
const char * GetFileNameS(char) const {
SG_ASSERT(m_uiCurr != size_t(-1));
return m_glob.gl_pathv[m_uiCurr];
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
const UChar * GetFileNameS(UChar) const {
const char * pszFile = this->GetFileNameS((char)0);
if (!pszFile) return nullptr;
UErrorCode status = U_ZERO_ERROR;
memset(m_szBuf, 0, sizeof(m_szBuf));
u_strFromUTF8(m_szBuf, PATH_MAX, nullptr, pszFile, -1, &status);
if (U_FAILURE(status)) return nullptr;
return m_szBuf;
}
#endif
2014-12-07 03:59:26 -05:00
bool IsDirS(char) const {
SG_ASSERT(m_uiCurr != size_t(-1));
return m_bIsDir;
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
bool IsDirS(UChar) const {
return this->IsDirS((char)0);
}
#endif
2014-12-07 03:59:26 -05:00
SG_FileType GetFileTypeS(const char * a_pszPath) const {
struct stat sb;
if (0 != stat(a_pszPath, &sb)) {
return SG_FILETYPE_INVALID;
}
if (S_ISDIR(sb.st_mode)) {
return SG_FILETYPE_DIR;
}
if (S_ISREG(sb.st_mode)) {
return SG_FILETYPE_FILE;
}
return SG_FILETYPE_INVALID;
}
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
SG_FileType GetFileTypeS(const UChar * a_pszPath) const {
char buf[PATH_MAX] = { 0 };
UErrorCode status = U_ZERO_ERROR;
u_strToUTF8(buf, sizeof(buf), nullptr, a_pszPath, -1, &status);
if (U_FAILURE(status)) return SG_FILETYPE_INVALID;
return this->GetFileTypeS(buf);
}
#endif
private:
2014-12-07 03:59:26 -05:00
glob_t m_glob;
size_t m_uiCurr;
bool m_bIsDir;
#if SG_HAVE_ICU
2014-12-07 03:59:26 -05:00
mutable UChar m_szBuf[PATH_MAX];
#endif
};
#endif // _WIN32
#endif // DOXYGEN
// ---------------------------------------------------------------------------
// MAIN TEMPLATE CLASS
// ---------------------------------------------------------------------------
/*! @brief Implementation of the SimpleGlob class */
template<class SOCHAR>
class CSimpleGlobTempl : private SimpleGlobBase<SOCHAR>
{
public:
2014-12-07 03:59:26 -05:00
/*! @brief Initialize the class.
@param a_uiFlags Combination of SG_GLOB flags.
@param a_nReservedSlots Number of slots in the argv array that
should be reserved. In the returned array these slots
argv[0] ... argv[a_nReservedSlots-1] will be left empty for
the caller to fill in.
*/
CSimpleGlobTempl(uint a_uiFlags = 0, int a_nReservedSlots = 0);
/*! @brief Deallocate all memory buffers. */
~CSimpleGlobTempl();
/*! @brief Initialize (or re-initialize) the class in preparation for
adding new filespecs.
All existing files are cleared. Note that allocated memory is only
deallocated at object destruction.
@param a_uiFlags Combination of SG_GLOB flags.
@param a_nReservedSlots Number of slots in the argv array that
should be reserved. In the returned array these slots
argv[0] ... argv[a_nReservedSlots-1] will be left empty for
the caller to fill in.
*/
int Init(uint a_uiFlags = 0, int a_nReservedSlots = 0);
/*! @brief Add a new filespec to the glob.
The filesystem will be immediately scanned for all matching files and
directories and they will be added to the glob.
@param a_pszFileSpec Filespec to add to the glob.
@return SG_SUCCESS Matching files were added to the glob.
@return SG_ERR_NOMATCH Nothing matched the pattern. To ignore this
error compare return value to >= SG_SUCCESS.
@return SG_ERR_MEMORY Out of memory failure.
@return SG_ERR_FAILURE General failure.
*/
int Add(const SOCHAR *a_pszFileSpec);
/*! @brief Add an array of filespec to the glob.
The filesystem will be immediately scanned for all matching files and
directories in each filespec and they will be added to the glob.
@param a_nCount Number of filespec in the array.
@param a_rgpszFileSpec Array of filespec to add to the glob.
@return SG_SUCCESS Matching files were added to the glob.
@return SG_ERR_NOMATCH Nothing matched the pattern. To ignore this
error compare return value to >= SG_SUCCESS.
@return SG_ERR_MEMORY Out of memory failure.
@return SG_ERR_FAILURE General failure.
*/
int Add(int a_nCount, const SOCHAR * const * a_rgpszFileSpec);
/*! @brief Return the number of files in the argv array.
*/
inline int FileCount() const { return m_nArgsLen; }
/*! @brief Return the full argv array. */
inline SOCHAR ** Files() {
SetArgvArrayType(POINTERS);
return m_rgpArgs;
}
/*! @brief Return the a single file. */
inline SOCHAR * File(int n) {
SG_ASSERT(n >= 0 && n < m_nArgsLen);
return Files()[n];
}
private:
2014-12-07 03:59:26 -05:00
CSimpleGlobTempl(const CSimpleGlobTempl &); // disabled
CSimpleGlobTempl & operator=(const CSimpleGlobTempl &); // disabled
--User changes -Add support for multiple GPU devices. --These options are present in the command line and in Fractorium. -Change scheme of specifying devices from platform,device to just total device index. --Single number on the command line. --Change from combo boxes for device selection to a table of all devices in Fractorium. -Temporal samples defaults to 100 instead of 1000 which was needless overkill. --Bug fixes -EmberAnimate, EmberRender, FractoriumSettings, FinalRenderDialog: Fix wrong order of arguments to Clamp() when assigning thread priority. -VariationsDC.h: Fix NVidia OpenCL compilation error in DCTriangleVariation. -FractoriumXformsColor.cpp: Checking for null pixmap pointer is not enough, must also check if the underlying buffer is null via call to QPixmap::isNull(). --Code changes -Ember.h: Add case for FLAME_MOTION_NONE and default in ApplyFlameMotion(). -EmberMotion.h: Call base constructor. -EmberPch.h: #pragma once only on Windows. -EmberToXml.h: --Handle different types of exceptions. --Add default cases to ToString(). -Isaac.h: Remove unused variable in constructor. -Point.h: Call base constructor in Color(). -Renderer.h/cpp: --Add bool to Alloc() to only allocate memory for the histogram. Needed for multi-GPU. --Make CoordMap() return a const ref, not a pointer. -SheepTools.h: --Use 64-bit types like the rest of the code already does. --Fix some comment misspellings. -Timing.h: Make BeginTime(), EndTime(), ElapsedTime() and Format() be const functions. -Utils.h: --Add new functions Equal() and Split(). --Handle more exception types in ReadFile(). --Get rid of most legacy blending of C and C++ argument parsing. -XmlToEmber.h: --Get rid of most legacy blending of C and C++ code from flam3. --Remove some unused variables. -EmberAnimate: --Support multi-GPU processing that alternates full frames between devices. --Use OpenCLInfo instead of OpenCLWrapper for --openclinfo option. --Remove bucketT template parameter, and hard code float in its place. --If a render fails, exit since there is no point in continuing an animation with a missing frame. --Pass variables to threaded save better, which most likely fixes a very subtle bug that existed before. --Remove some unused variables. -EmberGenome, EmberRender: --Support multi-GPU processing that alternates full frames between devices. --Use OpenCLInfo instead of OpenCLWrapper for --openclinfo option. --Remove bucketT template parameter, and hard code float in its place. -EmberRender: --Support multi-GPU processing that alternates full frames between devices. --Use OpenCLInfo instead of OpenCLWrapper for --openclinfo option. --Remove bucketT template parameter, and hard code float in its place. --Only print values when not rendering with OpenCL, since they're always 0 in that case. -EmberCLPch.h: --#pragma once only on Windows. --#include <atomic>. -IterOpenCLKernelCreator.h: Add new kernel for summing two histograms. This is needed for multi-GPU. -OpenCLWrapper.h: --Move all OpenCL info related code into its own class OpenCLInfo. --Add members to cache the values of global memory size and max allocation size. -RendererCL.h/cpp: --Redesign to accomodate multi-GPU. --Constructor now takes a vector of devices. --Remove DumpErrorReport() function, it's handled in the base. --ClearBuffer(), ReadPoints(), WritePoints(), ReadHist() and WriteHist() now optionally take a device index as a parameter. --MakeDmap() override and m_DmapCL member removed because it no longer applies since the histogram is always float since the last commit. --Add new function SumDeviceHist() to sum histograms from two devices by first copying to a temporary on the host, then a temporary on the device, then summing. --m_Calls member removed, as it's now per-device. --OpenCLWrapper removed. --m_Seeds member is now a vector of vector of seeds, to accomodate a separate and different array of seeds for each device. --Added member m_Devices, a vector of unique_ptr of RendererCLDevice. -EmberCommon.h --Added Devices() function to convert from a vector of device indices to a vector of platform,device indices. --Changed CreateRenderer() to accept a vector of devices to create a single RendererCL which will split work across multiple devices. --Added CreateRenderers() function to accept a vector of devices to create multiple RendererCL, each which will render on a single device. --Add more comments to some existing functions. -EmberCommonPch.h: #pragma once only on Windows. -EmberOptions.h: --Remove --platform option, it's just sequential device number now with the --device option. --Make --out be OPT_USE_RENDER instead of OPT_RENDER_ANIM since it's an error condition when animating. It makes no sense to write all frames to a single image. --Add Devices() function to parse comma separated --device option string and return a vector of device indices. --Make int and uint types be 64-bit, so intmax_t and size_t. --Make better use of macros. -JpegUtils.h: Make string parameters to WriteJpeg() and WritePng() be const ref. -All project files: Turn off buffer security check option in Visual Studio (/Gs-) -deployment.pri: Remove the line OTHER_FILES +=, it's pointless and was causing problems. -Ember.pro, EmberCL.pro: Add CONFIG += plugin, otherwise it wouldn't link. -EmberCL.pro: Add new files for multi-GPU support. -build_all.sh: use -j4 and QMAKE=${QMAKE:/usr/bin/qmake} -shared_settings.pri: -Add version string. -Remove old DESTDIR definitions. -Add the following lines or else nothing would build: CONFIG(release, debug|release) { CONFIG += warn_off DESTDIR = ../../../Bin/release } CONFIG(debug, debug|release) { DESTDIR = ../../../Bin/debug } QMAKE_POST_LINK += $$quote(cp --update ../../../Data/flam3-palettes.xml $${DESTDIR}$$escape_expand(\n\t)) LIBS += -L/usr/lib -lpthread -AboutDialog.ui: Another futile attempt to make it look correct on Linux. -FinalRenderDialog.h/cpp: --Add support for multi-GPU. --Change from combo boxes for device selection to a table of all devices. --Ensure device selection makes sense. --Remove "FinalRender" prefix of various function names, it's implied given the context. -FinalRenderEmberController.h/cpp: --Add support for multi-GPU. --Change m_FinishedImageCount to be atomic. --Move CancelRender() from the base to FinalRenderEmberController<T>. --Refactor RenderComplete() to omit any progress related functionality or image saving since it can be potentially ran in a thread. --Consolidate setting various renderer fields into SyncGuiToRenderer(). -Fractorium.cpp: Allow for resizing of the options dialog to show the entire device table. -FractoriumCommon.h: Add various functions to handle a table showing the available OpenCL devices on the system. -FractoriumEmberController.h/cpp: Remove m_FinalImageIndex, it's no longer needed. -FractoriumRender.cpp: Scale the interactive sub batch count and quality by the number of devices used. -FractoriumSettings.h/cpp: --Temporal samples defaults to 100 instead of 1000 which was needless overkill. --Add multi-GPU support, remove old device,platform pair. -FractoriumToolbar.cpp: Disable OpenCL toolbar button if there are no devices present on the system. -FractoriumOptionsDialog.h/cpp: --Add support for multi-GPU. --Consolidate more assignments in DataToGui(). --Enable/disable CPU/OpenCL items in response to OpenCL checkbox event. -Misc: Convert almost everything to size_t for unsigned, intmax_t for signed.
2015-09-12 21:33:45 -04:00
/*! @brief The argv array has its members stored as either an offset into
2014-12-07 03:59:26 -05:00
the string buffer, or as pointers to their string in the buffer. The
offsets are used because if the string buffer is dynamically resized,
all pointers into that buffer would become invalid.
*/
enum ARG_ARRAY_TYPE { OFFSETS, POINTERS };
2014-12-07 03:59:26 -05:00
/*! @brief Change the type of data stored in the argv array. */
void SetArgvArrayType(ARG_ARRAY_TYPE a_nNewType);
2014-12-07 03:59:26 -05:00
/*! @brief Add a filename to the array if it passes all requirements. */
int AppendName(const SOCHAR *a_pszFileName, bool a_bIsDir);
2014-12-07 03:59:26 -05:00
/*! @brief Grow the argv array to the required size. */
bool GrowArgvArray(int a_nNewLen);
2014-12-07 03:59:26 -05:00
/*! @brief Grow the string buffer to the required size. */
bool GrowStringBuffer(size_t a_uiMinSize);
2014-12-07 03:59:26 -05:00
/*! @brief Compare two (possible nullptr) strings */
static int fileSortCompare(const void *a1, const void *a2);
private:
2014-12-07 03:59:26 -05:00
uint m_uiFlags;
ARG_ARRAY_TYPE m_nArgArrayType; //!< argv is indexes or pointers
SOCHAR ** m_rgpArgs; //!< argv
int m_nReservedSlots; //!< # client slots in argv array
int m_nArgsSize; //!< allocated size of array
int m_nArgsLen; //!< used length
SOCHAR * m_pBuffer; //!< argv string buffer
size_t m_uiBufferSize; //!< allocated size of buffer
size_t m_uiBufferLen; //!< used length of buffer
SOCHAR m_szPathPrefix[MAX_PATH]; //!< wildcard path prefix
};
// ---------------------------------------------------------------------------
// IMPLEMENTATION
// ---------------------------------------------------------------------------
template<class SOCHAR>
CSimpleGlobTempl<SOCHAR>::CSimpleGlobTempl(
2014-12-07 03:59:26 -05:00
uint a_uiFlags,
int a_nReservedSlots
)
{
2014-12-07 03:59:26 -05:00
m_rgpArgs = nullptr;
m_nArgsSize = 0;
m_pBuffer = nullptr;
m_uiBufferSize = 0;
2014-12-07 03:59:26 -05:00
Init(a_uiFlags, a_nReservedSlots);
}
template<class SOCHAR>
CSimpleGlobTempl<SOCHAR>::~CSimpleGlobTempl()
{
2014-12-07 03:59:26 -05:00
if (m_rgpArgs) free(m_rgpArgs);
if (m_pBuffer) free(m_pBuffer);
}
template<class SOCHAR>
int
CSimpleGlobTempl<SOCHAR>::Init(
2014-12-07 03:59:26 -05:00
uint a_uiFlags,
int a_nReservedSlots
)
{
2014-12-07 03:59:26 -05:00
m_nArgArrayType = POINTERS;
m_uiFlags = a_uiFlags;
m_nArgsLen = a_nReservedSlots;
m_nReservedSlots = a_nReservedSlots;
m_uiBufferLen = 0;
if (m_nReservedSlots > 0) {
if (!GrowArgvArray(m_nReservedSlots)) {
return SG_ERR_MEMORY;
}
for (int n = 0; n < m_nReservedSlots; ++n) {
m_rgpArgs[n] = nullptr;
}
}
return SG_SUCCESS;
}
template<class SOCHAR>
int
CSimpleGlobTempl<SOCHAR>::Add(
2014-12-07 03:59:26 -05:00
const SOCHAR *a_pszFileSpec
)
{
#ifdef _WIN32
2014-12-07 03:59:26 -05:00
// Windows FindFirst/FindNext recognizes forward slash as the same as
// backward slash and follows the directories. We need to do the same
// when calculating the prefix and when we have no wildcards.
SOCHAR szFileSpec[MAX_PATH];
SimpleGlobUtil::strcpy_s(szFileSpec, MAX_PATH, a_pszFileSpec);
const SOCHAR * pszPath = SimpleGlobUtil::strchr(szFileSpec, '/');
while (pszPath) {
szFileSpec[pszPath - szFileSpec] = SG_PATH_CHAR;
pszPath = SimpleGlobUtil::strchr(pszPath + 1, '/');
}
a_pszFileSpec = szFileSpec;
#endif
2014-12-07 03:59:26 -05:00
// if this doesn't contain wildcards then we can just add it directly
m_szPathPrefix[0] = 0;
if (!SimpleGlobUtil::strchr(a_pszFileSpec, '*') &&
!SimpleGlobUtil::strchr(a_pszFileSpec, '?'))
{
SG_FileType nType = this->GetFileTypeS(a_pszFileSpec);
if (nType == SG_FILETYPE_INVALID) {
if (m_uiFlags & SG_GLOB_NOCHECK) {
return AppendName(a_pszFileSpec, false);
}
return SG_ERR_NOMATCH;
}
return AppendName(a_pszFileSpec, nType == SG_FILETYPE_DIR);
}
#ifdef _WIN32
2014-12-07 03:59:26 -05:00
// Windows doesn't return the directory with the filename, so we need to
// extract the path from the search string ourselves and prefix it to the
// filename we get back.
const SOCHAR * pszFilename =
SimpleGlobUtil::strrchr(a_pszFileSpec, SG_PATH_CHAR);
if (pszFilename) {
SimpleGlobUtil::strcpy_s(m_szPathPrefix, MAX_PATH, a_pszFileSpec);
m_szPathPrefix[pszFilename - a_pszFileSpec + 1] = 0;
}
#endif
2014-12-07 03:59:26 -05:00
// search for the first match on the file
int rc = this->FindFirstFileS(a_pszFileSpec, m_uiFlags);
if (rc != SG_SUCCESS) {
if (rc == SG_ERR_NOMATCH && (m_uiFlags & SG_GLOB_NOCHECK)) {
int ok = AppendName(a_pszFileSpec, false);
if (ok != SG_SUCCESS) rc = ok;
}
return rc;
}
// add it and find all subsequent matches
int nError, nStartLen = m_nArgsLen;
bool bSuccess;
do {
nError = AppendName(this->GetFileNameS(SOCHAR(0)), this->IsDirS(SOCHAR(0)));
bSuccess = this->FindNextFileS(SOCHAR(0));
}
while (nError == SG_SUCCESS && bSuccess);
SimpleGlobBase<SOCHAR>::FindDone();
// sort these files if required
if (m_nArgsLen > nStartLen && !(m_uiFlags & SG_GLOB_NOSORT)) {
if (m_uiFlags & SG_GLOB_FULLSORT) {
nStartLen = m_nReservedSlots;
}
SetArgvArrayType(POINTERS);
qsort(
m_rgpArgs + nStartLen,
m_nArgsLen - nStartLen,
sizeof(m_rgpArgs[0]), fileSortCompare);
}
return nError;
}
template<class SOCHAR>
int
CSimpleGlobTempl<SOCHAR>::Add(
2014-12-07 03:59:26 -05:00
int a_nCount,
const SOCHAR * const * a_rgpszFileSpec
)
{
2014-12-07 03:59:26 -05:00
int nResult;
for (int n = 0; n < a_nCount; ++n) {
nResult = Add(a_rgpszFileSpec[n]);
if (nResult != SG_SUCCESS) {
return nResult;
}
}
return SG_SUCCESS;
}
template<class SOCHAR>
int
CSimpleGlobTempl<SOCHAR>::AppendName(
2014-12-07 03:59:26 -05:00
const SOCHAR * a_pszFileName,
bool a_bIsDir
)
{
2014-12-07 03:59:26 -05:00
// we need the argv array as offsets in case we resize it
SetArgvArrayType(OFFSETS);
// check for special cases which cause us to ignore this entry
if ((m_uiFlags & SG_GLOB_ONLYDIR) && !a_bIsDir) {
return SG_SUCCESS;
}
if ((m_uiFlags & SG_GLOB_ONLYFILE) && a_bIsDir) {
return SG_SUCCESS;
}
if ((m_uiFlags & SG_GLOB_NODOT) && a_bIsDir) {
if (a_pszFileName[0] == '.') {
if (a_pszFileName[1] == '\0') {
return SG_SUCCESS;
}
if (a_pszFileName[1] == '.' && a_pszFileName[2] == '\0') {
return SG_SUCCESS;
}
}
}
// ensure that we have enough room in the argv array
if (!GrowArgvArray(m_nArgsLen + 1)) {
return SG_ERR_MEMORY;
}
// ensure that we have enough room in the string buffer (+1 for null)
size_t uiPrefixLen = SimpleGlobUtil::strlen(m_szPathPrefix);
size_t uiLen = uiPrefixLen + SimpleGlobUtil::strlen(a_pszFileName) + 1;
if (a_bIsDir && (m_uiFlags & SG_GLOB_MARK) == SG_GLOB_MARK) {
++uiLen; // need space for the backslash
}
if (!GrowStringBuffer(m_uiBufferLen + uiLen)) {
return SG_ERR_MEMORY;
}
// add this entry. m_uiBufferLen is offset from beginning of buffer.
m_rgpArgs[m_nArgsLen++] = reinterpret_cast<SOCHAR*>(m_uiBufferLen);
SimpleGlobUtil::strcpy_s(m_pBuffer + m_uiBufferLen,
m_uiBufferSize - m_uiBufferLen, m_szPathPrefix);
SimpleGlobUtil::strcpy_s(m_pBuffer + m_uiBufferLen + uiPrefixLen,
m_uiBufferSize - m_uiBufferLen - uiPrefixLen, a_pszFileName);
m_uiBufferLen += uiLen;
// add the directory slash if desired
if (a_bIsDir && (m_uiFlags & SG_GLOB_MARK) == SG_GLOB_MARK) {
const static SOCHAR szDirSlash[] = { SG_PATH_CHAR, 0 };
SimpleGlobUtil::strcpy_s(m_pBuffer + m_uiBufferLen - 2,
m_uiBufferSize - (m_uiBufferLen - 2), szDirSlash);
}
return SG_SUCCESS;
}
template<class SOCHAR>
void
CSimpleGlobTempl<SOCHAR>::SetArgvArrayType(
2014-12-07 03:59:26 -05:00
ARG_ARRAY_TYPE a_nNewType
)
{
2014-12-07 03:59:26 -05:00
if (m_nArgArrayType == a_nNewType) return;
if (a_nNewType == POINTERS) {
SG_ASSERT(m_nArgArrayType == OFFSETS);
for (int n = 0; n < m_nArgsLen; ++n) {
m_rgpArgs[n] = (m_rgpArgs[n] == reinterpret_cast<SOCHAR*>(-1)) ?
nullptr : m_pBuffer + size_t(m_rgpArgs[n]);
}
}
else {
SG_ASSERT(a_nNewType == OFFSETS);
SG_ASSERT(m_nArgArrayType == POINTERS);
for (int n = 0; n < m_nArgsLen; ++n) {
m_rgpArgs[n] = (m_rgpArgs[n] == nullptr) ?
reinterpret_cast<SOCHAR*>(-1) : reinterpret_cast<SOCHAR*>(m_rgpArgs[n] - m_pBuffer);
}
}
m_nArgArrayType = a_nNewType;
}
template<class SOCHAR>
bool
CSimpleGlobTempl<SOCHAR>::GrowArgvArray(
2014-12-07 03:59:26 -05:00
int a_nNewLen
)
{
2014-12-07 03:59:26 -05:00
if (a_nNewLen >= m_nArgsSize) {
static const int SG_ARGV_INITIAL_SIZE = 32;
int nNewSize = (m_nArgsSize > 0) ?
m_nArgsSize * 2 : SG_ARGV_INITIAL_SIZE;
while (a_nNewLen >= nNewSize) {
nNewSize *= 2;
}
void * pNewBuffer = realloc(m_rgpArgs, nNewSize * sizeof(SOCHAR*));
if (!pNewBuffer) return false;
m_nArgsSize = nNewSize;
m_rgpArgs = reinterpret_cast<SOCHAR**>(pNewBuffer);
}
return true;
}
template<class SOCHAR>
bool
CSimpleGlobTempl<SOCHAR>::GrowStringBuffer(
2014-12-07 03:59:26 -05:00
size_t a_uiMinSize
)
{
2014-12-07 03:59:26 -05:00
if (a_uiMinSize >= m_uiBufferSize) {
static const int SG_BUFFER_INITIAL_SIZE = 1024;
size_t uiNewSize = (m_uiBufferSize > 0) ?
m_uiBufferSize * 2 : SG_BUFFER_INITIAL_SIZE;
while (a_uiMinSize >= uiNewSize) {
uiNewSize *= 2;
}
void * pNewBuffer = realloc(m_pBuffer, uiNewSize * sizeof(SOCHAR));
if (!pNewBuffer) return false;
m_uiBufferSize = uiNewSize;
m_pBuffer = reinterpret_cast<SOCHAR*>(pNewBuffer);
}
return true;
}
template<class SOCHAR>
int
CSimpleGlobTempl<SOCHAR>::fileSortCompare(
2014-12-07 03:59:26 -05:00
const void *a1,
const void *a2
)
{
2014-12-07 03:59:26 -05:00
const SOCHAR * s1 = *(reinterpret_cast<const SOCHAR **>(a1));
const SOCHAR * s2 = *(reinterpret_cast<const SOCHAR **>(a2));
if (s1 && s2) {
return SimpleGlobUtil::strcasecmp(s1, s2);
}
// nullptr sorts first
return s1 == s2 ? 0 : (s1 ? 1 : -1);
}
// ---------------------------------------------------------------------------
// TYPE DEFINITIONS
// ---------------------------------------------------------------------------
/*! @brief ASCII/MBCS version of CSimpleGlob */
typedef CSimpleGlobTempl<char> CSimpleGlobA;
/*! @brief wchar_t version of CSimpleGlob */
typedef CSimpleGlobTempl<wchar_t> CSimpleGlobW;
#if SG_HAVE_ICU
/*! @brief UChar version of CSimpleGlob */
typedef CSimpleGlobTempl<UChar> CSimpleGlobU;
#endif
#ifdef _UNICODE
/*! @brief TCHAR version dependent on if _UNICODE is defined */
# if SG_HAVE_ICU
# define CSimpleGlob CSimpleGlobU
# else
# define CSimpleGlob CSimpleGlobW
# endif
#else
/*! @brief TCHAR version dependent on if _UNICODE is defined */
# define CSimpleGlob CSimpleGlobA
#endif
#endif // INCLUDED_SimpleGlob