| 1 |
{%MainUnit castleinternalglutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Part of CastleGLUtils unit: dealing with OpenGL errors. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ OpenGL error. |
| 23 |
Usually OpenGL error indicates a bug in our code. |
| 24 |
Invalid uniform types for GLSL uniforms (including in user provided shaders) |
| 25 |
can also result in this error. |
| 26 |
|
| 27 |
Note: @link(EOpenGLOutOfMemoryError) may happen regardless |
| 28 |
of what we do, and depends on the device. |
| 29 |
|
| 30 |
You can call it with overloaded ancestor Create / CreateFmt that take just string, |
| 31 |
not ErrorCode: TGLenum, this is a feature. } |
| 32 |
EOpenGLError = class(Exception) |
| 33 |
public |
| 34 |
constructor Create(const AErrorCode: TGLenum; const AdditionalComment: string = ''); overload; |
| 35 |
end; |
| 36 |
|
| 37 |
{ GPU memory is not sufficient. |
| 38 |
|
| 39 |
When programming for platforms with limited GPU memory (Android, iOS...) |
| 40 |
you should prepare to handle EOpenGLOutOfMemoryError (corresponding to |
| 41 |
GL_OUT_OF_MEMORY). This can always happen for large GPU data, |
| 42 |
and you should be prepared to capture it (around loading your data) |
| 43 |
and display some nice information for user. |
| 44 |
Alternatively, you can leave GLOutOfMemoryError = @false, |
| 45 |
and then EOpenGLOutOfMemoryError will not happen, but you risk all kinds |
| 46 |
of rendering artifacts. } |
| 47 |
EOpenGLOutOfMemoryError = class(EOpenGLError) |
| 48 |
end; |
| 49 |
|
| 50 |
var |
| 51 |
{ When GPU runs out of memory, raise exception (EOpenGLOutOfMemoryError) |
| 52 |
or merely make a warning. Merely making a warning is very risky (you risk all kinds |
| 53 |
of rendering artifacts), but sometimes the rendering is actually smooth |
| 54 |
even though GPU complains. } |
| 55 |
GLOutOfMemoryError: boolean = false; |
| 56 |
|
| 57 |
procedure GLOutOfMemory(const AdditionalComment: string = ''); |
| 58 |
|
| 59 |
function GLErrorString(const ErrorCode: TGLenum; const AdditionalComment: string = ''): string; |
| 60 |
|
| 61 |
{ Check are any OpenGL errors recorded (in glGetError). |
| 62 |
If there are errors, our behavior depends on whether we were compiled |
| 63 |
with -dRELEASE. With -dRELEASE, we make WritelnWarning. This way eventual |
| 64 |
errors in release builds don't completely abort your program. |
| 65 |
|
| 66 |
Note that the behavior on GL_OUT_OF_MEMORY is different. |
| 67 |
-dRELEASE does not matter here. Only GLOutOfMemoryError boolean dictates |
| 68 |
if we should raise an exception or merely make warning, regardless of -dRELEASE |
| 69 |
state. |
| 70 |
|
| 71 |
Without -dRELEASE, we raise EOpenGLError. So a developer is strongly |
| 72 |
suggested to fix the code to not produce OpenGL errors, never ever. |
| 73 |
|
| 74 |
@raises EOpenGLError } |
| 75 |
procedure CheckGLErrors(const AdditionalComment: string = ''); |
| 76 |
|
| 77 |
{$endif read_interface} |
| 78 |
|
| 79 |
{$ifdef read_implementation} |
| 80 |
|
| 81 |
function GLErrorString(const ErrorCode: TGLenum; const AdditionalComment: string): string; |
| 82 |
var |
| 83 |
S: string; |
| 84 |
begin |
| 85 |
{ Do not use gluErrorString, not available in OpenGL ES. |
| 86 |
Error descriptions below from |
| 87 |
http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetError.xml } |
| 88 |
case ErrorCode of |
| 89 |
GL_NO_ERROR: S := 'No error has been recorded.'; |
| 90 |
GL_INVALID_ENUM: S := 'An unacceptable value is specified for an enumerated argument.'; |
| 91 |
GL_INVALID_VALUE: S := 'A numeric argument is out of range.'; |
| 92 |
GL_INVALID_OPERATION: S := 'The specified operation is not allowed in the current state.'; |
| 93 |
{$ifndef OpenGLES} |
| 94 |
GL_STACK_OVERFLOW: S := 'Stack overflow'; |
| 95 |
GL_STACK_UNDERFLOW: S := 'Stack underflow'; |
| 96 |
{$endif} |
| 97 |
GL_INVALID_FRAMEBUFFER_OPERATION: S := 'The command is trying to render to or read from the framebuffer while the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE).'; |
| 98 |
GL_OUT_OF_MEMORY: S := 'There is not enough memory left to execute the command.'; |
| 99 |
else S := 'Unknown error.'; |
| 100 |
end; |
| 101 |
|
| 102 |
if AdditionalComment <> '' then |
| 103 |
Result := AdditionalComment + nl else |
| 104 |
Result := ''; |
| 105 |
Result := Result + Format('OpenGL error (%d): %s', [ErrorCode, S]); |
| 106 |
end; |
| 107 |
|
| 108 |
constructor EOpenGLError.Create(const AErrorCode: TGLenum; const AdditionalComment: string); |
| 109 |
begin |
| 110 |
inherited Create(GLErrorString(AErrorCode, AdditionalComment)); |
| 111 |
end; |
| 112 |
|
| 113 |
procedure GLOutOfMemory(const AdditionalComment: string); |
| 114 |
const |
| 115 |
ErrorCode = GL_OUT_OF_MEMORY; |
| 116 |
begin |
| 117 |
if GLOutOfMemoryError then |
| 118 |
raise EOpenGLOutOfMemoryError.Create(ErrorCode, AdditionalComment) else |
| 119 |
WritelnWarning('OpenGL', GLErrorString(ErrorCode, AdditionalComment)); |
| 120 |
end; |
| 121 |
|
| 122 |
procedure CheckGLErrors(const AdditionalComment: string); |
| 123 |
var |
| 124 |
ErrorCode: TGLenum; |
| 125 |
begin |
| 126 |
ErrorCode := glGetError(); |
| 127 |
if ErrorCode <> GL_NO_ERROR then |
| 128 |
begin |
| 129 |
if ErrorCode = GL_OUT_OF_MEMORY then |
| 130 |
GLOutOfMemory(AdditionalComment) else |
| 131 |
{$ifdef RELEASE} |
| 132 |
WritelnWarning('OpenGL', GLErrorString(ErrorCode, AdditionalComment)); |
| 133 |
{$else} |
| 134 |
raise EOpenGLError.Create(ErrorCode, AdditionalComment); |
| 135 |
{$endif} |
| 136 |
end; |
| 137 |
end; |
| 138 |
|
| 139 |
{$endif read_implementation} |