initial import of files

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1801 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
blaszijk
2011-08-15 22:08:43 +00:00
parent 8c5783c3d5
commit ca7613fadd
7 changed files with 3810 additions and 0 deletions

View File

@ -0,0 +1,224 @@
//
// Utility functions for compiling shaders and programs
//
// Author: Evan Hart
// Copyright (c) NVIDIA Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////
unit nvShaderUtils;
interface
{ $define NV_REPORT_COMPILE_ERRORS}
uses
Classes, SysUtils, GL, GLext;
function CompileGLSLShader(target: GLenum; shader: string): GLuint;
function CompileGLSLShaderFromFile(target: GLenum; filename: string): GLuint;
function LinkGLSLProgram(vertexShader: GLuint; fragmentShader: GLuint): GLuint;
function LinkGLSLProgram(vertexShader: GLuint; geometryShader: GLuint; inputType: GLint; vertexOut: GLint; outputType: GLint; fragmentShader: GLuint): GLuint;
function CompileASMShader(program_type: GLenum; code: string): GLuint;
function CompileASMShaderFromFile(target: GLenum; filename: string): GLuint;
implementation
function CompileGLSLShader(target: GLenum; shader: string): GLuint;
var
_object: GLuint;
compiled: GLint;
temp: string;
begin
_object := glCreateShader(target);
if _object = 0 then
begin
Result := _object;
exit;
end;
glShaderSource(_object, 1, @shader, nil);
glCompileShader(_object);
//check if shader compiled
compiled := 0;
glGetShaderiv(_object, GL_COMPILE_STATUS, @compiled);
if compiled = 0 then
begin
{$ifdef NV_REPORT_COMPILE_ERRORS}
temp := '';
glGetShaderInfoLog(_object, 256, nil, @temp[1]);
writeln(stderr, 'Compile failed:');
writeln(stderr, temp);
{$endif}
glDeleteShader(_object);
Result := 0;
exit;
end;
Result := _object;
end;
//
//
////////////////////////////////////////////////////////////
function CompileGLSLShaderFromFile(target: GLenum; filename: string): GLuint;
var
_object: GLuint;
s: TStrings;
begin
//must read files as binary to prevent problems from newline translation
try
try
s := TStringList.Create;
s.LoadFromFile(filename);
_object := CompileGLSLShader(target, s.Text);
Result := _object;
exit;
finally
FreeAndNil(s);
end;
except
Result := 0;
exit;
end;
end;
// Create a program composed of vertex and fragment shaders.
function LinkGLSLProgram(vertexShader: GLuint; fragmentShader: GLuint): GLuint;
var
_program: GLuint;
infoLogLength: GLint;
infoLog: string;
charsWritten: GLint;
linkSucceed: GLint;
begin
_program := glCreateProgram();
glAttachShader(_program, vertexShader);
glAttachShader(_program, fragmentShader);
glLinkProgram(_program);
{$ifdef NV_REPORT_COMPILE_ERRORS}
// Get error log.
glGetProgramiv(_program, GL_INFO_LOG_LENGTH, @infoLogLength);
infoLog := StringOfChar(' ',infoLogLength);
glGetProgramInfoLog(_program, infoLogLength, @charsWritten, @infoLog[1]);
writeln(stdout, infoLog);
{$endif}
// Test linker result.
linkSucceed := GL_FALSE;
glGetProgramiv(_program, GL_LINK_STATUS, @linkSucceed);
if linkSucceed = GL_FALSE then
begin
glDeleteProgram(_program);
Result := 0;
exit;
end;
Result := _program;
end;
// Create a program composed of vertex, geometry and fragment shaders.
function LinkGLSLProgram(vertexShader: GLuint; geometryShader: GLuint; inputType: GLint; vertexOut: GLint; outputType: GLint; fragmentShader: GLuint): GLuint;
var
_program: GLuint;
charsWritten: GLint;
infoLogLength: GLint;
infoLog: string;
linkSucceed: GLint;
begin
_program := glCreateProgram();
glAttachShader(_program, vertexShader);
glAttachShader(_program, geometryShader);
{$note fix this}
//glProgramParameteriEXT(_program,GL_GEOMETRY_INPUT_TYPE_EXT,inputType);
//glProgramParameteriEXT(_program,GL_GEOMETRY_VERTICES_OUT_EXT,vertexOut);
//glProgramParameteriEXT(_program,GL_GEOMETRY_OUTPUT_TYPE_EXT,outputType);
glAttachShader(_program, fragmentShader);
glLinkProgram(_program);
{$ifdef NV_REPORT_COMPILE_ERRORS}
//Get error log.
glGetProgramiv(_program, GL_INFO_LOG_LENGTH, @infoLogLength);
infoLog := StringOfChar(' ',infoLogLength);
glGetProgramInfoLog(_program, infoLogLength, @charsWritten, @infoLog[1]);
writeln(stdout, infoLog);
{$endif}
//Test linker result.
linkSucceed := GL_FALSE;
glGetProgramiv(_program, GL_LINK_STATUS, @linkSucceed);
if linkSucceed = GL_FALSE then
begin
glDeleteProgram(_program);
Result := 0;
exit;
end;
Result := _program;
end;
//
//
////////////////////////////////////////////////////////////
function CompileASMShader(program_type: GLenum; code: string): GLuint;
var
error_string: string;
program_id: GLuint;
error_pos: GLint;
begin
glGenProgramsARB(1, @program_id);
glBindProgramARB(program_type, program_id);
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB, length(code), @code[1]);
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, @error_pos);
if error_pos <> -1 then
begin
{$ifdef NV_REPORT_COMPILE_ERRORS}
error_string := glGetString(GL_PROGRAM_ERROR_STRING_ARB);
writeln(stderr, 'Program error at position: ', error_pos);
writeln(stderr, error_string);
{$endif}
Result := 0;
exit;
end;
Result := program_id;
end;
//
//
////////////////////////////////////////////////////////////
function CompileASMShaderFromFile(target: GLenum; filename: string): GLuint;
var
size: integer;
program_id: GLuint;
s: TStrings;
begin
//must read files as binary to prevent problems from newline translation
try
try
s := TStringList.Create;
s.LoadFromFile(filename);
size := Length(s.Text);
program_id := CompileASMShader(target, s.Text);
Result := program_id;
exit;
finally
FreeAndNil(s);
end;
except
Result := 0;
exit;
end;
end;
end.

View File

@ -0,0 +1,96 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<General>
<Flags>
<SaveClosedFiles Value="False"/>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<Units Count="3">
<Unit0>
<Filename Value="example.pp"/>
<IsPartOfProject Value="True"/>
<UnitName Value="example"/>
</Unit0>
<Unit1>
<Filename Value="..\nvglwidgets.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="nvGLWidgets"/>
</Unit1>
<Unit2>
<Filename Value="..\..\nvglutils\nvShaderUtils.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="nvShaderUtils"/>
</Unit2>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="example"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="..;..\..\nvglutils"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<AllowLabel Value="False"/>
<CPPInline Value="False"/>
</SyntaxOptions>
</Parsing>
<Linking>
<Debugging>
<UseLineInfoUnit Value="False"/>
</Debugging>
<LinkSmart Value="True"/>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,191 @@
program example;
{$mode objfpc}
{$H+}
uses
Classes, SysUtils,
GLut, GL, GLu,
nvWidgets, nvGlutWidgets, nvGLWidgets, nvShaderUtils;
type
UIOption = (
OPTION_DIFF,
OPTION_DXT5_YCOCG,
OPTION_COMPRESS,
OPTION_ANIMATE,
OPTION_THUMBNAIL,
OPTION_COUNT);
var
options: array [UIOption] of boolean;
ui: GlutUIContext;
win_w: integer = 512;
win_h: integer = 512;
errorScale: double = 4;
compressionRate: double = 1;
texture: GLuint = 0;
procedure closeapp;
begin
FreeAndNil(ui);
halt(0);
end;
procedure idle; cdecl;
begin
glutPostRedisplay;
end;
procedure key(k: byte; x: integer; y: integer); cdecl;
begin
ui.keyboard(k, x, y);
case k of
27, Ord('q'):
closeapp;
end;
end;
procedure special(key: integer; x: integer; y: integer); cdecl;
begin
ui.specialKeyboard(key, x, y);
end;
procedure resize(w: integer; h: integer); cdecl;
begin
ui.reshape(w, h);
glViewport(0, 0, w, h);
win_w := w;
win_h := h;
end;
procedure mouse(button: integer; state: integer; x: integer; y: integer); cdecl;
begin
ui.mouse(button, state, x, y);
end;
procedure motion(x: integer; y: integer); cdecl;
begin
ui.mouseMotion(x, y);
end;
procedure doUI;
const
formatLabel: array [0..1] of string = ('YCoCg-DXT5', 'DXT1');
var
none: Rect;
formatIdx: integer;
Text: string;
textureRect: Rect;
begin
none.Rect(0, 0);
ui._begin;
ui.beginGroup(GroupFlags_GrowDownFromLeft);
ui.doCheckButton(none, 'Enable compression', options[OPTION_COMPRESS]);
if options[OPTION_COMPRESS] then
begin
ui.beginGroup(GroupFlags_GrowLeftFromTop or GroupFlags_LayoutNoMargin);
ui.doCheckButton(none, 'Show difference', options[OPTION_DIFF]);
if options[OPTION_DIFF] then
ui.doHorizontalSlider(none, 1, 16, errorScale);
ui.endGroup;
ui.beginGroup(GroupFlags_GrowLeftFromTop);
ui.doLabel(none, 'Format');
if options[OPTION_DXT5_YCOCG] then
formatIdx := 0
else
formatIdx := 1;
ui.doComboBox(none, 2, formatLabel, formatIdx);
options[OPTION_DXT5_YCOCG] := formatIdx = 0;
ui.endGroup;
end;
ui.doCheckButton(none, 'Display dummy texture', options[OPTION_THUMBNAIL]);
if options[OPTION_THUMBNAIL] then
begin
textureRect.Rect(0, 0, 100, 100);
ui.doTextureView(textureRect, texture, textureRect);
end;
ui.endGroup;
if options[OPTION_COMPRESS] then
begin
ui.beginGroup(GroupFlags_GrowDownFromRight);
if ui.doButton(none, 'Benchmark') then
begin
// doBenchmark = true;
end;
if compressionRate <> 0 then
begin
Text := Format('%.2d Mpixels/sec', [100]);
ui.doLabel(none, Text);
end;
ui.endGroup;
end;
// Pass non-ui mouse events to the manipulator
//updateManipulator(ui, manipulator);
ui._end;
end;
procedure display; cdecl;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
doUI;
glutSwapBuffers;
end;
var
texture_data: array [0..3] of cardinal = ($FFFF0000, $FF0000FF, $FF00FF00, $FF00FF00);
begin
glutInit(@argc, argv);
glutInitWindowSize(win_w, win_h);
glutInitDisplayMode(GLUT_DOUBLE or GLUT_DEPTH or GLUT_RGB);
glutCreateWindow('UI example');
ui := GlutUIContext.Create;
if not ui.init(win_w, win_h) then
begin
writeln('UI initialization failed');
closeapp;
end;
glutReportErrors;
glGenTextures(1, @texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, @texture_data);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
glutDisplayFunc(@display);
glutMouseFunc(@mouse);
glutMotionFunc(@motion);
glutPassiveMotionFunc(@motion);
glutIdleFunc(@idle);
glutKeyboardFunc(@key);
glutSpecialFunc(@special);
glutReshapeFunc(@resize);
glutMainLoop;
end.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,222 @@
//
// nvGlutWidgets
//
// Adaptor classes to integrate the nvWidgets UI library with the GLUT windowing
// toolkit. The adaptors convert native GLUT UI data to native nvWidgets data. All
// adaptor classes are implemented as in-line code in this header. The adaptor
// defaults to using the standard OpenGL painter implementation.
//
// Author: Ignacio Castano, Samuel Gateau, Evan Hart
// Email: sdkfeedback@nvidia.com
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////
unit nvGlutWidgets;
{$mode objfpc}{$H+}
interface
uses
SysUtils, nvWidgets;
type
{ GlutUIContext }
GlutUIContext = class(UIContext)
public
// Default UI constructor
// Creates private OpenGL painter
//////////////////////////////////////////////////////////////////
constructor Create;
// Alternate UI constructor
// Allows for overriding the standard painter
//////////////////////////////////////////////////////////////////
constructor Create(painter: UIPainter);
// UI destructor
// Destroy painter if it is private
//////////////////////////////////////////////////////////////////
destructor Destroy; override;
// One time initialization
//////////////////////////////////////////////////////////////////
function init(w, h: integer): boolean;
// UI method for processing GLUT mouse button events
// Call this method from the glutMouseFunc callback, the
// modifier parameter maps to glutGetModifiers.
//////////////////////////////////////////////////////////////////
procedure mouse(button, state, modifier, x, y: integer);
procedure mouse(button, state, x, y: integer);
// UI method for processing key events
// Call this method from the glutReshapeFunc callback
//////////////////////////////////////////////////////////////////
procedure specialKeyboard(k, x, y: integer);
private
m_ownPainter: boolean;
// Translate non-ascii keys from GLUT to nvWidgets
//////////////////////////////////////////////////////////////////
function translateKey(k: integer): byte;
end;
implementation
uses
GLut, GLext, nvGLWidgets;
{ GlutUIContext }
constructor GlutUIContext.Create;
begin
inherited Create(GLUIPainter.Create);
m_ownPainter := True;
end;
constructor GlutUIContext.Create(painter: UIPainter);
begin
inherited Create(painter);
m_ownPainter := False;
end;
destructor GlutUIContext.Destroy;
var
painter: UIPainter;
begin
if m_ownPainter then
begin
painter := getPainter;
FreeAndNil(painter);
end;
inherited;
end;
function GlutUIContext.init(w, h: integer): boolean;
begin
Result := False;
Load_GL_version_2_0;
if not glext_ExtensionSupported('GL_ARB_vertex_program', '') or
not glext_ExtensionSupported('GL_ARB_fragment_program', '') then
begin
Result := False;
exit;
end;
reshape(w, h);
Result := True;
end;
procedure GlutUIContext.mouse(button, state, modifier, x, y: integer);
var
modifierMask: integer = 0;
begin
if button = GLUT_LEFT_BUTTON then
button := MouseButton_Left
else
if button = GLUT_MIDDLE_BUTTON then
button := MouseButton_Middle
else
if button = GLUT_RIGHT_BUTTON then
button := MouseButton_Right;
if (modifier and GLUT_ACTIVE_ALT) = 1 then
modifierMask := modifierMask or (ButtonFlags_Alt);
if (modifier and GLUT_ACTIVE_SHIFT) = 1 then
modifierMask := modifierMask or (ButtonFlags_Shift);
if (modifier and GLUT_ACTIVE_CTRL) = 1 then
modifierMask := modifierMask or (ButtonFlags_Ctrl);
if state = GLUT_DOWN then
state := 1
else
state := 0;
inherited mouse(button, state, modifierMask, x, y);
end;
procedure GlutUIContext.mouse(button, state, x, y: integer);
begin
mouse(button, state, glutGetModifiers(), x, y);
end;
procedure GlutUIContext.specialKeyboard(k, x, y: integer);
begin
inherited keyboard(translateKey(k), x, y);
end;
function GlutUIContext.translateKey(k: integer): byte;
begin
case k of
GLUT_KEY_F1:
Result := Key_F1;
GLUT_KEY_F2:
Result := Key_F2;
GLUT_KEY_F3:
Result := Key_F3;
GLUT_KEY_F4:
Result := Key_F4;
GLUT_KEY_F5:
Result := Key_F5;
GLUT_KEY_F6:
Result := Key_F6;
GLUT_KEY_F7:
Result := Key_F7;
GLUT_KEY_F8:
Result := Key_F8;
GLUT_KEY_F9:
Result := Key_F9;
GLUT_KEY_F10:
Result := Key_F10;
GLUT_KEY_F11:
Result := Key_F11;
GLUT_KEY_F12:
Result := Key_F12;
GLUT_KEY_LEFT:
Result := Key_Left;
GLUT_KEY_UP:
Result := Key_Up;
GLUT_KEY_RIGHT:
Result := Key_Right;
GLUT_KEY_DOWN:
Result := Key_Down;
GLUT_KEY_PAGE_UP:
Result := Key_PageUp;
GLUT_KEY_PAGE_DOWN:
Result := Key_PageDown;
GLUT_KEY_HOME:
Result := Key_Home;
GLUT_KEY_END:
Result := Key_End;
GLUT_KEY_INSERT:
Result := Key_Insert;
else
Result := 0;
end;
end;
end.

File diff suppressed because it is too large Load Diff