44 using namespace KIGFX;
47 m_initialized( false ),
51 m_curFbo( DIRECT_RENDERING ),
66 catch(
const std::runtime_error& exc )
68 wxLogError( wxT(
"Run time exception `%s` occurred in OPENGL_COMPOSITOR destructor." ),
101 m_antialiasing = std::make_unique<ANTIALIASING_SUPERSAMPLING>(
this );
109 assert( dims.
x != 0 && dims.
y != 0 );
112 glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxBufSize );
115 if( dims.
x > (
unsigned) maxBufSize || dims.
y >= (
unsigned) maxBufSize )
116 throw std::runtime_error(
"Requested render buffer size is not supported" );
121 checkGlError(
"generating framebuffer", __FILE__, __LINE__ );
127 checkGlError(
"generating renderbuffer", __FILE__, __LINE__ );
129 checkGlError(
"binding renderbuffer", __FILE__, __LINE__ );
131 glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, dims.
x, dims.
y );
132 checkGlError(
"creating renderbuffer storage", __FILE__, __LINE__ );
133 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT,
135 checkGlError(
"attaching renderbuffer", __FILE__, __LINE__ );
168 int maxBuffers, maxTextureSize;
171 glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, (GLint*) &maxBuffers );
175 throw std::runtime_error(
"Cannot create more framebuffers. OpenGL rendering backend " 176 "requires at least 3 framebuffers. You may try to update/change " 177 "your graphic drivers." );
180 glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*) &maxTextureSize );
182 if( maxTextureSize < (
int) aDimensions.
x || maxTextureSize < (
int) aDimensions.
y )
184 throw std::runtime_error(
"Requested texture size is not supported. " 185 "Could not create a buffer." );
189 GLuint attachmentPoint = GL_COLOR_ATTACHMENT0 +
usedBuffers();
190 GLuint textureTarget;
193 glActiveTexture( GL_TEXTURE0 );
194 glGenTextures( 1, &textureTarget );
195 checkGlError(
"generating framebuffer texture target", __FILE__, __LINE__ );
196 glBindTexture( GL_TEXTURE_2D, textureTarget );
197 checkGlError(
"binding framebuffer texture target", __FILE__, __LINE__ );
200 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
201 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, aDimensions.
x, aDimensions.
y, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE,
nullptr );
203 checkGlError(
"creating framebuffer texture", __FILE__, __LINE__ );
204 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
205 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
209 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachmentPoint, GL_TEXTURE_2D, textureTarget,
213 GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
215 if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
219 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
220 throw std::runtime_error(
"The framebuffer attachment points are incomplete." );
222 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
223 throw std::runtime_error(
"No images attached to the framebuffer." );
225 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
226 throw std::runtime_error(
"The framebuffer does not have at least one " 227 "image attached to it." );
229 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
230 throw std::runtime_error(
"The framebuffer read buffer is incomplete." );
232 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
233 throw std::runtime_error(
"The combination of internal formats of the attached " 234 "images violates an implementation-dependent set of " 237 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
238 throw std::runtime_error(
"GL_RENDERBUFFER_SAMPLES is not the same for " 239 "all attached renderbuffers" );
241 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
242 throw std::runtime_error(
"Framebuffer incomplete layer targets errors." );
244 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
245 throw std::runtime_error(
"Framebuffer attachments have different dimensions" );
248 throw std::runtime_error(
"Unknown error occurred when creating the framebuffer." );
258 OPENGL_BUFFER buffer = { aDimensions, textureTarget, attachmentPoint };
267 wxASSERT( aBufferHandle > 0 && aBufferHandle <=
usedBuffers() );
268 return m_buffers[aBufferHandle - 1].textureTarget;
285 checkGlError(
"setting draw buffer", __FILE__, __LINE__ );
301 glClearColor( aColor.
r, aColor.
g, aColor.
b, 0.0f );
302 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
327 wxASSERT( aSourceHandle != 0 && aSourceHandle <=
usedBuffers() );
334 glDisable( GL_DEPTH_TEST );
335 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
338 glEnable( GL_TEXTURE_2D );
339 glBindTexture( GL_TEXTURE_2D,
m_buffers[aSourceHandle - 1].textureTarget );
342 glMatrixMode( GL_MODELVIEW );
345 glMatrixMode( GL_PROJECTION );
349 glBegin( GL_TRIANGLES );
350 glTexCoord2f( 0.0f, 1.0f );
351 glVertex2f( -1.0f, 1.0f );
352 glTexCoord2f( 0.0f, 0.0f );
353 glVertex2f( -1.0f, -1.0f );
354 glTexCoord2f( 1.0f, 1.0f );
355 glVertex2f( 1.0f, 1.0f );
357 glTexCoord2f( 1.0f, 1.0f );
358 glVertex2f( 1.0f, 1.0f );
359 glTexCoord2f( 0.0f, 0.0f );
360 glVertex2f( -1.0f, -1.0f );
361 glTexCoord2f( 1.0f, 0.0f );
362 glVertex2f( 1.0f, -1.0f );
366 glMatrixMode( GL_MODELVIEW );
384 glBindFramebufferEXT( GL_FRAMEBUFFER, aFb );
385 checkGlError(
"switching framebuffer", __FILE__, __LINE__ );
398 glDeleteTextures( 1, &buffer.textureTarget );
402 if( glDeleteFramebuffersEXT )
403 glDeleteFramebuffersEXT( 1, &
m_mainFbo );
405 if( glDeleteRenderbuffersEXT )
unsigned int m_curBuffer
Currently used buffer handle.
VECTOR2U GetScreenSize() const
virtual void DrawBuffer(unsigned int aBufferHandle) override
Draw the selected buffer to the output buffer.
The Cairo implementation of the graphics abstraction layer.
int GetAntialiasSupersamplingFactor() const
virtual void Begin() override
Call this at the beginning of each frame.
unsigned int m_width
Width of the buffer (in pixels)
void bindFb(unsigned int aFb)
Binds a specific Framebuffer Object.
static const COLOR4D BLACK
OPENGL_ANTIALIASING_MODE GetAntialiasingMode() const
unsigned int usedBuffers()
Returns number of used buffers.
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
bool m_initialized
Initialization status flag.
static const unsigned int DIRECT_RENDERING
Handle multitarget rendering (ie.
std::unique_ptr< OPENGL_PRESENTOR > m_antialiasing
OPENGL_ANTIALIASING_MODE m_currentAntialiasingMode
void SetAntialiasingMode(OPENGL_ANTIALIASING_MODE aMode)
void clean()
Perform freeing of resources.
VECTOR2< double > VECTOR2D
virtual void Present() override
Call this to present the output buffer to the screen.
virtual void ClearBuffer(const COLOR4D &aColor) override
Clear the selected buffer (set by the SetBuffer() function).
virtual void Resize(unsigned int aWidth, unsigned int aHeight) override
Clear the state of COMPOSITOR, so it has to be reinitialized again with the new dimensions.
GLuint m_curFbo
Store the used FBO name in case there was more than one compositor used.
virtual void SetBuffer(unsigned int aBufferHandle) override
Set the selected buffer as the rendering target.
GLuint m_mainFbo
Main FBO handle (storing all target textures)
GLuint m_depthBuffer
Depth buffer handle.
virtual void Initialize() override
Perform primary initialization, necessary to use the object.
OPENGL_BUFFERS m_buffers
Stores information about initialized buffers.
virtual ~OPENGL_COMPOSITOR()
GLenum GetBufferTexture(unsigned int aBufferHandle)
virtual unsigned int CreateBuffer() override
Prepare a new buffer that may be used as a rendering target.
unsigned int m_height
Height of the buffer (in pixels)
VECTOR2D GetAntialiasRenderingOffset() const
A color representation with 4 components: red, green, blue, alpha.