Browse Source

* implemented examples for linux

master
bergmann89 10 years ago
parent
commit
67050e1de8
11 changed files with 340 additions and 68 deletions
  1. +8
    -2
      library/header/examples/c++/Makefile
  2. +14
    -2
      library/header/examples/c++/example.cpp
  3. +8
    -2
      library/header/examples/c/Makefile
  4. +14
    -2
      library/header/examples/c/example.c
  5. +14
    -1
      library/header/examples/fpc/example.lpr
  6. +20
    -5
      library/header/libShaderFile.h
  7. +46
    -35
      library/header/libShaderFile.hpp
  8. +47
    -14
      library/header/ulibShaderFile.pas
  9. +165
    -1
      library/libShaderFile.lpi
  10. +1
    -1
      uengShaderFileExpression.pas
  11. +3
    -3
      uengShaderFileTypes.pas

+ 8
- 2
library/header/examples/c++/Makefile View File

@@ -1,5 +1,11 @@
ifeq ($(OS),Windows_NT)
LDFLAGS=
else
LDFLAGS=-ldl
endif

all: example.cpp ../../libShaderFile.h
g++ -o example example.cpp
g++ -o example example.cpp $(LDFLAGS)

clean:
rm -rf *.exe
rm -rf *.exe

+ 14
- 2
library/header/examples/c++/example.cpp View File

@@ -1,6 +1,18 @@
#include <iostream>
#include "../../libShaderFile.hpp"

#if _WIN64
static const char* LibName = "..\..\..\libShaderFile-x86_64-win64.dll";
#elif _WIN32
static const char* LibName = "..\..\..\libShaderFile-i386-win32.dll";
#elif __linux__ && (__amd64 || __x86_64 || _M_AMD64 || __ppc64__)
static const char* LibName = "../../../libShaderFile-x86_64-linux.so";
#elif __linux__ && (__i386 || _X86_)
static const char* LibName = "../../../libShaderFile-i386-linux.so";
#else
# error 'unknown operation system'
#endif

int main(int argc, char **argv)
{
try
@@ -12,7 +24,7 @@ int main(int argc, char **argv)
}
// initialize library
lsf::Library lib("../../../libShaderFile-i386-win32.dll");
lsf::Library lib(LibName);
// create and load shader file
lsf::ShaderFile shaderFile(lib);
@@ -39,4 +51,4 @@ int main(int argc, char **argv)
std::cout << "error: " << ex.what() << std::endl;
return 2;
}
}
}

+ 8
- 2
library/header/examples/c/Makefile View File

@@ -1,5 +1,11 @@
ifeq ($(OS),Windows_NT)
LDFLAGS=
else
LDFLAGS=-ldl
endif

all: example.c ../../libShaderFile.h
gcc -o example example.c
gcc -g -o example example.c $(LDFLAGS)

clean:
rm -rf *.exe
rm -rf *.exe

+ 14
- 2
library/header/examples/c/example.c View File

@@ -1,6 +1,18 @@
#include <stdio.h>
#include "../../libShaderFile.h"

#if _WIN64
# define LIB_NAME "..\..\..\libShaderFile-x86_64-win64.dll"
#elif _WIN32
# define LIB_NAME "..\..\..\libShaderFile-i386-win32.dll"
#elif __linux__ && (__amd64 || __x86_64 || _M_AMD64 || __ppc64__)
# define LIB_NAME "../../../libShaderFile-x86_64-linux.so"
#elif __linux__ && (__i386 || _X86_)
# define LIB_NAME "../../../libShaderFile-i386-linux.so"
#else
# error 'unknown operation system'
#endif

int main(int argc, char **argv)
{
lsf_shader_file_handle_t sfHandle = 0;
@@ -15,7 +27,7 @@ int main(int argc, char **argv)
return 1;
}
lsf_init("../../../libShaderFile-i386-win32.dll");
lsf_init(LIB_NAME);
// create shader file
sfHandle = lsf_shader_file_create();
@@ -84,4 +96,4 @@ cleanup_lib:
printf("warning: could finish library: %d\n", err);
return ret;
}
}

+ 14
- 1
library/header/examples/fpc/example.lpr View File

@@ -4,6 +4,19 @@ uses
sysutils, variants,
ulibShaderFile;

const
{$IF DEFINED(WIN32)}
LibName = '..\..\..\libShaderFile-i386-win32.dll';
{$ELSEIF DEFINED(WIN64)}
LibName = '..\..\..\libShaderFile-x86_64-win64.dll';
{$ELSEIF DEFINED(LINUX) AND DEFINED(CPU32)}
LibName = '../../../libShaderFile-i386-linux.so';
{$ELSEIF DEFINED(LINUX) AND DEFINED(CPU64)}
LibName = '../../../libShaderFile-x86_64-linux.so';
{$ELSE}
{$ERROR 'unknown operation system'}
{$IFEND}

var
i: Integer;
s: String;
@@ -11,7 +24,7 @@ var
Generator: TlsfGenerator;

begin
lsf_init(ExtractFilePath(ParamStr(0)) + '..\..\..\libShaderFile-x86_64-win64.dll');
lsf_init(ExtractFilePath(ParamStr(0)) + LibName);
try
if (ParamCount < 2) then begin
WriteLn('error: expected input file and generator/class name as parameter');


+ 20
- 5
library/header/libShaderFile.h View File

@@ -36,7 +36,11 @@
#define LSF_ERR_INVALID_METHOD_NAME 0x00002002
#define LSF_ERR_UNKNOWN 0xFFFFFFFF

#define WINAPI __stdcall
#if __MINGW32__
# define WINAPI __stdcall
#else
# define WINAPI
#endif

typedef uint32_t lsf_error_code_t;
typedef uint32_t lsf_log_level_t;
@@ -158,7 +162,7 @@ int lsf_finish(void);
# include <stdio.h>
#endif

#if defined(WIN32) || defined(WIN64)
#if WIN32 || WIN64 || _WIN32 || _WIN64
# include <windows.h>

typedef HMODULE lib_handle_t;
@@ -172,9 +176,20 @@ void* get_addr(lib_handle_t handle, const char* name)
int close_lib(lib_handle_t handle)
{ return FreeLibrary(handle); };
#elif defined(LINUX)
#elif LINUX || __linux__
# include <dlfcn.h>
# error "linux is not supported yet"

typedef void* lib_handle_t;

lib_handle_t open_lib(const char* name)
{ return dlopen(name, RTLD_LAZY); };
void* get_addr(lib_handle_t handle, const char* name)
{ return dlsym(handle, name); };

int close_lib(lib_handle_t handle)
{ return !dlclose(handle); };

#else
# error "unknown operation system"
#endif
@@ -244,4 +259,4 @@ int lsf_finish(void)
return err;
}

#endif /* LIB_SHADER_FILE_H */
#endif /* LIB_SHADER_FILE_H */

+ 46
- 35
library/header/libShaderFile.hpp View File

@@ -6,14 +6,20 @@
#include <vector>
#include <stdint.h>

#if defined(WIN32) || defined(WIN64)
#if WIN32 || WIN64 || _WIN32 || _WIN64
# include <windows.h>
#elif defined(LINUX)
#elif LINUX || __linux__
# include <dlfcn.h>
#else
# error "unknown operation system"
#endif

#if __MINGW32__
# define WINAPI __stdcall
#else
# define WINAPI
#endif

/**********************************************************************************************************************************/
/* public interface */
/**********************************************************************************************************************************/
@@ -62,10 +68,10 @@ namespace lsf
friend class Generator;
public:
#if defined(WIN32) || defined(WIN64)
#if WIN32 || WIN64 || _WIN32 || _WIN64
typedef HMODULE Handle; //!< shader file handle
#elif defined(LINUX)
#error "linux is not supported yet"
#elif LINUX || __linux__
typedef void* Handle; //!< shader file handle
#else
#error "unknown operation system"
#endif
@@ -149,7 +155,7 @@ namespace lsf
* @param logLevel log level of the log message
* @param msg log message
* @param userargs user defined arguments */
static void __stdcall logCallback(const LogLevel loglevel, const char* msg, void* userargs);
static void WINAPI logCallback(const LogLevel loglevel, const char* msg, void* userargs);
};
/** class to manage a shader code generator */
@@ -253,23 +259,28 @@ namespace lsf
/**********************************************************************************************************************************/
/* private implementation */
/**********************************************************************************************************************************/
#if defined(WIN32) || defined(WIN64)
#if WIN32 || WIN64 || _WIN32 || _WIN64
lsf::Library::Handle libOpen(const char* name)
{ return LoadLibrary(name); };

template <typename T>
T getAddr(lsf::Library::Handle handle, const char* name)
{
FARPROC tmp = GetProcAddress(handle, name);
return reinterpret_cast<T>(tmp);
};
{ return reinterpret_cast<T>(GetProcAddress(handle, name)); };

int libClose(lsf::Library::Handle handle)
{ return FreeLibrary(handle); };

#elif defined(LINUX)
# include <dlfcn.h>
# error "linux is not supported yet"
#elif LINUX || __linux__
lsf::Library::Handle libOpen(const char* name)
{ return dlopen(name, RTLD_LAZY); };

template <typename T>
T getAddr(lsf::Library::Handle handle, const char* name)
{ return reinterpret_cast<T>(dlsym(handle, name)); };

int libClose(lsf::Library::Handle handle)
{ return !dlclose(handle); };

#else
# error "unknown operation system"
#endif
@@ -277,28 +288,28 @@ int libClose(lsf::Library::Handle handle)
/* Library::Impl ******************************************************************************************************************/
struct lsf::Library::Impl
{
typedef ShaderFile::Handle (__stdcall *lsf_shader_file_create_t) ();
typedef void (__stdcall *lsf_shader_file_log_callback_t) (const LogLevel loglevel, const char* msg, void* userargs);
typedef ErrorCode (__stdcall *lsf_shader_file_set_log_callback_t) (const ShaderFile::Handle handle, const lsf_shader_file_log_callback_t callback, void* userargs);
typedef ErrorCode (__stdcall *lsf_shader_file_load_from_file_t) (const ShaderFile::Handle handle, const char* filename);
typedef ErrorCode (__stdcall *lsf_shader_file_save_to_file_t) (const ShaderFile::Handle handle, const char* filename);
typedef const char* (__stdcall *lsf_shader_file_get_generator_names_t) (const ShaderFile::Handle handle);
typedef ErrorCode (__stdcall *lsf_shader_file_destroy_t) (const ShaderFile::Handle handle);
typedef ShaderFile::Handle (WINAPI *lsf_shader_file_create_t) ();
typedef void (WINAPI *lsf_shader_file_log_callback_t) (const LogLevel loglevel, const char* msg, void* userargs);
typedef ErrorCode (WINAPI *lsf_shader_file_set_log_callback_t) (const ShaderFile::Handle handle, const lsf_shader_file_log_callback_t callback, void* userargs);
typedef ErrorCode (WINAPI *lsf_shader_file_load_from_file_t) (const ShaderFile::Handle handle, const char* filename);
typedef ErrorCode (WINAPI *lsf_shader_file_save_to_file_t) (const ShaderFile::Handle handle, const char* filename);
typedef const char* (WINAPI *lsf_shader_file_get_generator_names_t) (const ShaderFile::Handle handle);
typedef ErrorCode (WINAPI *lsf_shader_file_destroy_t) (const ShaderFile::Handle handle);
typedef Generator::Handle (__stdcall *lsf_generator_create_t) (const ShaderFile::Handle handle, const char* name);
typedef const char* (__stdcall *lsf_generator_get_property_names_t) (const Generator::Handle handle);
typedef const char* (__stdcall *lsf_generator_get_property_t) (const Generator::Handle handle, const int index);
typedef const char* (__stdcall *lsf_generator_get_property_by_name_t) (const Generator::Handle handle, const char* name);
typedef ErrorCode (__stdcall *lsf_generator_set_property_t) (const Generator::Handle handle, const int index, const char* value);
typedef ErrorCode (__stdcall *lsf_generator_set_property_by_name_t) (const Generator::Handle handle, const char* name, const char* value);
typedef const char* (__stdcall *lsf_generator_generate_code_t) (const Generator::Handle handle);
typedef ErrorCode (__stdcall *lsf_generator_destroy_t) (const Generator::Handle handle);
typedef ErrorCode (__stdcall *lsf_init_t) ();
typedef ErrorCode (__stdcall *lsf_get_last_error_code_t) ();
typedef const char* (__stdcall *lsf_get_last_error_msg_t) ();
typedef const char* (__stdcall *lsf_get_last_error_trace_t) ();
typedef ErrorCode (__stdcall *lsf_finish_t) ();
typedef Generator::Handle (WINAPI *lsf_generator_create_t) (const ShaderFile::Handle handle, const char* name);
typedef const char* (WINAPI *lsf_generator_get_property_names_t) (const Generator::Handle handle);
typedef const char* (WINAPI *lsf_generator_get_property_t) (const Generator::Handle handle, const int index);
typedef const char* (WINAPI *lsf_generator_get_property_by_name_t) (const Generator::Handle handle, const char* name);
typedef ErrorCode (WINAPI *lsf_generator_set_property_t) (const Generator::Handle handle, const int index, const char* value);
typedef ErrorCode (WINAPI *lsf_generator_set_property_by_name_t) (const Generator::Handle handle, const char* name, const char* value);
typedef const char* (WINAPI *lsf_generator_generate_code_t) (const Generator::Handle handle);
typedef ErrorCode (WINAPI *lsf_generator_destroy_t) (const Generator::Handle handle);
typedef ErrorCode (WINAPI *lsf_init_t) ();
typedef ErrorCode (WINAPI *lsf_get_last_error_code_t) ();
typedef const char* (WINAPI *lsf_get_last_error_msg_t) ();
typedef const char* (WINAPI *lsf_get_last_error_trace_t) ();
typedef ErrorCode (WINAPI *lsf_finish_t) ();

const Library& library;



+ 47
- 14
library/header/ulibShaderFile.pas View File

@@ -21,6 +21,7 @@ type

{$Z4}
TlsfErrorCode = (
errUnknown = -1,
errNone = $00000000,
errNotInit = $00000001,
errInvalidHandleShaderFile = $00000010,
@@ -43,8 +44,7 @@ type
errShaderPart = $0000100b,
errInvalidLibraryName = $00002000,
errInvalidLibraryHandle = $00002001,
errInvalidMethodName = $00002002,
errUnknown = -1
errInvalidMethodName = $00002002
);

TlsfShaderFileHandle = Pointer;
@@ -152,19 +152,22 @@ procedure lsf_finish;

implementation

uses
{$IF DEFINED(WIN32) OR DEFINED(WIN64)}
uses
windows;

type
TLibHandle = HMODULE;

function LibOpen(const aLibName: String; out aErrorCode: Cardinal): TLibHandle;
const
InvalidLibHandle: TLibHandle = 0;

function LibOpen(const aLibName: String; out aError: String): TLibHandle;
begin
result := LoadLibraryA(PAnsiChar(AnsiString(aLibName)));
if (result = 0)
then aErrorCode := GetLastError()
else aErrorCode := 0;
then aError := GetLastError()
else aError := '';
end;

function GetAddr(const aLibHandle: TLibHandle; const aName: String): Pointer;
@@ -172,10 +175,40 @@ begin
result := GetProcAddress(aLibHandle, PAnsiChar(AnsiString(aName)));
end;

function LibClose(const aLibHandle: TLibHandle): Boolean;
procedure LibClose(const aLibHandle: TLibHandle);
begin
result := FreeLibrary(aLibHandle);
FreeLibrary(aLibHandle);
end;

{$ELSEIF DEFINED(LINUX)}
uses
dl;

type
TLibHandle = Pointer;

const
InvalidLibHandle: TLibHandle = nil;

function LibOpen(const aLibName: String; out aError: String): TLibHandle;
begin
dlerror();
result := dlopen(PChar(aLibName), RTLD_LAZY);
if (result = InvalidLibHandle)
then aError := dlerror()
else aError := '';
end;

function GetAddr(const aLibHandle: TLibHandle; const aName: String): Pointer;
begin
result := dlsym(aLibHandle, PChar(aName));
end;

procedure LibClose(const aLibHandle: TLibHandle);
begin
dlclose(aLibHandle);
end;

{$ELSE}
{$ERROR 'unknown operation system'}
{$IFEND}
@@ -196,12 +229,12 @@ procedure lsf_init(const aLibName: String);
end;

var
e: Cardinal;
eMsg: String;
err: TlsfErrorCode;
begin
libHandle := LibOpen(aLibName, e);
if (libHandle = 0) then
raise Exception.CreateFmt('unable to load library (%d): %s', [e, aLibName]);
libHandle := LibOpen(aLibName, eMsg);
if (libHandle = InvalidLibHandle) then
raise Exception.Create('unable to load library: ' + eMsg);

lsf_ShaderFile_create := Tlsf_ShaderFile_create( LoadProc('lsf_ShaderFile_create'));
lsf_ShaderFile_setLogCallback := Tlsf_ShaderFile_setLogCallback( LoadProc('lsf_ShaderFile_setLogCallback'));
@@ -243,9 +276,9 @@ begin
lsf_getLastErrorCode := nil;
lsf_getLastErrorMsg := nil;
lsf_getLastErrorTrace := nil;
if (libHandle <> 0) then begin
if (libHandle <> InvalidLibHandle) then begin
LibClose(libHandle);
libHandle := 0;
libHandle := InvalidLibHandle;
end;
end;



+ 165
- 1
library/libShaderFile.lpi View File

@@ -20,7 +20,7 @@
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="4">
<BuildModes Count="8">
<Item1 Name="Win32Debug" Default="True"/>
<Item2 Name="Win32Release">
<CompilerOptions>
@@ -141,6 +141,170 @@
</Other>
</CompilerOptions>
</Item4>
<Item5 Name="Linux32Debug">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="libShaderFile-$(TargetCPU)-$(TargetOS)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<IncludeAssertionCode Value="True"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<RelocatableUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<TargetCPU Value="i386"/>
<TargetOS Value="linux"/>
</CodeGeneration>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf2Set"/>
<UseHeaptrc Value="True"/>
<UseExternalDbgSyms Value="True"/>
</Debugging>
<Options>
<ExecutableType Value="Library"/>
</Options>
</Linking>
<Other>
<CompilerMessages>
<IgnoredMessages idx5024="True"/>
</CompilerMessages>
</Other>
</CompilerOptions>
</Item5>
<Item6 Name="Linux32Release">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="libShaderFile-$(TargetCPU)-$(TargetOS)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<RelocatableUnit Value="True"/>
<TargetCPU Value="i386"/>
<TargetOS Value="linux"/>
<Optimizations>
<OptimizationLevel Value="3"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
</Debugging>
<LinkSmart Value="True"/>
<Options>
<ExecutableType Value="Library"/>
</Options>
</Linking>
<Other>
<CompilerMessages>
<IgnoredMessages idx5024="True"/>
</CompilerMessages>
</Other>
</CompilerOptions>
</Item6>
<Item7 Name="Linux64Debug">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="libShaderFile-$(TargetCPU)-$(TargetOS)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<IncludeAssertionCode Value="True"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<RelocatableUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<TargetCPU Value="x86_64"/>
<TargetOS Value="linux"/>
</CodeGeneration>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf2Set"/>
<UseHeaptrc Value="True"/>
<UseExternalDbgSyms Value="True"/>
</Debugging>
<Options>
<ExecutableType Value="Library"/>
</Options>
</Linking>
<Other>
<CompilerMessages>
<IgnoredMessages idx5024="True"/>
</CompilerMessages>
</Other>
</CompilerOptions>
</Item7>
<Item8 Name="Linux64Release">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="libShaderFile-$(TargetCPU)-$(TargetOS)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<RelocatableUnit Value="True"/>
<TargetCPU Value="x86_64"/>
<TargetOS Value="linux"/>
<Optimizations>
<OptimizationLevel Value="3"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
</Debugging>
<LinkSmart Value="True"/>
<Options>
<ExecutableType Value="Library"/>
</Options>
</Linking>
<Other>
<CompilerMessages>
<IgnoredMessages idx5024="True"/>
</CompilerMessages>
</Other>
</CompilerOptions>
</Item8>
</BuildModes>
<PublishOptions>
<Version Value="2"/>


+ 1
- 1
uengShaderFileExpression.pas View File

@@ -554,7 +554,7 @@ begin
result := (fParamPos < fParams.Count);
if result
then aParam := fParams[fParamPos]
else FillByte(aParam, SizeOf(aParam), 0);
else FillByte(aParam{%H-}, SizeOf(aParam), 0);
end;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


+ 3
- 3
uengShaderFileTypes.pas View File

@@ -225,19 +225,19 @@ uses
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TIntfObjNoRefCount//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TIntfObjNoRefCount.QueryInterface(constref iid: tguid; out obj): longint; stdcall;
function TIntfObjNoRefCount.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
if getinterface(iid,obj)
then result := S_OK
else result := longint(E_NOINTERFACE);
end;

function TIntfObjNoRefCount._AddRef: longint; stdcall;
function TIntfObjNoRefCount._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
result := InterLockedIncrement(fRefCount);
end;

function TIntfObjNoRefCount._Release: longint; stdcall;
function TIntfObjNoRefCount._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
result := InterLockedDecrement(fRefCount);
end;


Loading…
Cancel
Save