38#include <glsl_xor_diff_frag.h>
39#include <glsl_xor_diff_vert.h>
76 catch(
const std::runtime_error& exc )
78 wxLogError( wxT(
"Run time exception `%s` occurred in OPENGL_COMPOSITOR destructor." ),
111 m_antialiasing = std::make_unique<ANTIALIASING_SUPERSAMPLING>(
this );
119 assert( dims.
x != 0 && dims.
y != 0 );
122 glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxBufSize );
124 if( dims.
x < 0 || dims.
y < 0 || dims.
x > maxBufSize || dims.
y >= maxBufSize )
125 throw std::runtime_error(
"Requested render buffer size is not supported" );
130 checkGlError(
"generating framebuffer", __FILE__, __LINE__ );
136 checkGlError(
"generating renderbuffer", __FILE__, __LINE__ );
138 checkGlError(
"binding renderbuffer", __FILE__, __LINE__ );
140 glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, dims.
x, dims.
y );
141 checkGlError(
"creating renderbuffer storage", __FILE__, __LINE__ );
142 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT,
144 checkGlError(
"attaching renderbuffer", __FILE__, __LINE__ );
177 int maxBuffers, maxTextureSize;
180 glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, (GLint*) &maxBuffers );
184 throw std::runtime_error(
"Cannot create more framebuffers. OpenGL rendering backend requires at "
185 "least 3 framebuffers. You may try to update/change your graphic drivers." );
188 glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*) &maxTextureSize );
190 if( maxTextureSize < (
int) aDimensions.
x || maxTextureSize < (
int) aDimensions.
y )
192 throw std::runtime_error(
"Requested texture size is not supported. Could not create a buffer." );
196 GLuint attachmentPoint = GL_COLOR_ATTACHMENT0 +
usedBuffers();
197 GLuint textureTarget;
200 glActiveTexture( GL_TEXTURE0 );
201 glGenTextures( 1, &textureTarget );
202 checkGlError(
"generating framebuffer texture target", __FILE__, __LINE__ );
203 glBindTexture( GL_TEXTURE_2D, textureTarget );
204 checkGlError(
"binding framebuffer texture target", __FILE__, __LINE__ );
207 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
208 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, aDimensions.
x, aDimensions.
y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr );
209 checkGlError(
"creating framebuffer texture", __FILE__, __LINE__ );
210 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
211 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
215 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachmentPoint, GL_TEXTURE_2D, textureTarget, 0 );
218 GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
220 if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
224 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
225 throw std::runtime_error(
"The framebuffer attachment points are incomplete." );
227 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
228 throw std::runtime_error(
"No images attached to the framebuffer." );
230 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
231 throw std::runtime_error(
"The framebuffer does not have at least one image attached to it." );
233 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
234 throw std::runtime_error(
"The framebuffer read buffer is incomplete." );
236 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
237 throw std::runtime_error(
"The combination of internal formats of the attached images violates "
238 "an implementation-dependent set of restrictions." );
240 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
241 throw std::runtime_error(
"GL_RENDERBUFFER_SAMPLES is not the same for all attached renderbuffers" );
243 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
244 throw std::runtime_error(
"Framebuffer incomplete layer targets errors." );
246 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
247 throw std::runtime_error(
"Framebuffer attachments have different dimensions" );
250 throw std::runtime_error(
"Unknown error occurred when creating the framebuffer." );
260 OPENGL_BUFFER buffer = { aDimensions, textureTarget, attachmentPoint };
269 wxCHECK( aBufferHandle > 0 && aBufferHandle <=
usedBuffers(), 0 );
270 return m_buffers[aBufferHandle - 1].textureTarget;
286 checkGlError(
"setting draw buffer", __FILE__, __LINE__ );
302 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
309 wxASSERT(
m_width <=
static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
310 wxASSERT(
m_height <=
static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
312 return {
static_cast<coord_t
>(
m_width ),
static_cast<coord_t
>(
m_height ) };
337 glDisable( GL_DEPTH_TEST );
338 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
341 glEnable( GL_TEXTURE_2D );
342 glBindTexture( GL_TEXTURE_2D,
m_buffers[aSourceHandle - 1].textureTarget );
345 glMatrixMode( GL_MODELVIEW );
348 glMatrixMode( GL_PROJECTION );
352 glBegin( GL_TRIANGLES );
353 glTexCoord2f( 0.0f, 1.0f );
354 glVertex2f( -1.0f, 1.0f );
355 glTexCoord2f( 0.0f, 0.0f );
356 glVertex2f( -1.0f, -1.0f );
357 glTexCoord2f( 1.0f, 1.0f );
358 glVertex2f( 1.0f, 1.0f );
360 glTexCoord2f( 1.0f, 1.0f );
361 glVertex2f( 1.0f, 1.0f );
362 glTexCoord2f( 0.0f, 0.0f );
363 glVertex2f( -1.0f, -1.0f );
364 glTexCoord2f( 1.0f, 0.0f );
365 glVertex2f( 1.0f, -1.0f );
369 glMatrixMode( GL_MODELVIEW );
387 glBindFramebufferEXT( GL_FRAMEBUFFER, aFb );
388 checkGlError(
"switching framebuffer", __FILE__, __LINE__ );
401 glDeleteTextures( 1, &buffer.textureTarget );
405 if( glDeleteFramebuffersEXT )
406 glDeleteFramebuffersEXT( 1, &
m_mainFbo );
408 if( glDeleteRenderbuffersEXT )
445 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader() called, initialized=%d" ),
450 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): already initialized, returning" ) );
454 const char* vertexShaderSrc = BUILTIN_SHADERS::glsl_xor_diff_vert.c_str();
455 const char* fragmentShaderSrc = BUILTIN_SHADERS::glsl_xor_diff_frag.c_str();
457 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): creating shaders" ) );
458 wxLogTrace(
traceGalXorMode, wxT(
"Vertex shader source:\n%s" ), vertexShaderSrc );
459 wxLogTrace(
traceGalXorMode, wxT(
"Fragment shader source:\n%s" ), fragmentShaderSrc );
461 GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
462 GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
464 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): vertexShader=%u, fragmentShader=%u" ),
465 vertexShader, fragmentShader );
468 glShaderSource( vertexShader, 1, &vertexShaderSrc,
nullptr );
469 glCompileShader( vertexShader );
472 glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &compiled );
474 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): vertex shader compile status=%d" ), compiled );
479 glGetShaderiv( vertexShader, GL_INFO_LOG_LENGTH, &logLen );
483 char* log =
new char[logLen];
484 glGetShaderInfoLog( vertexShader, logLen,
nullptr, log );
485 wxLogTrace(
traceGalXorMode, wxT(
"Vertex shader compile error:\n%s" ), log );
489 glDeleteShader( vertexShader );
490 glDeleteShader( fragmentShader );
495 glShaderSource( fragmentShader, 1, &fragmentShaderSrc,
nullptr );
496 glCompileShader( fragmentShader );
498 glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &compiled );
500 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): fragment shader compile status=%d" ), compiled );
505 glGetShaderiv( fragmentShader, GL_INFO_LOG_LENGTH, &logLen );
509 char* log =
new char[logLen];
510 glGetShaderInfoLog( fragmentShader, logLen,
nullptr, log );
511 wxLogTrace(
traceGalXorMode, wxT(
"Fragment shader compile error:\n%s" ), log );
515 glDeleteShader( vertexShader );
516 glDeleteShader( fragmentShader );
531 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): link status=%d" ), linked );
534 glDeleteShader( vertexShader );
535 glDeleteShader( fragmentShader );
544 char* log =
new char[logLen];
546 wxLogTrace(
traceGalXorMode, wxT(
"Shader program link error:\n%s" ), log );
559 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): srcTexUniform=%d, dstTexUniform=%d" ),
563 wxLogTrace(
traceGalXorMode, wxT(
"initDifferenceShader(): SUCCESS" ) );
570 glMatrixMode( GL_MODELVIEW );
573 glMatrixMode( GL_PROJECTION );
577 glBegin( GL_TRIANGLES );
578 glTexCoord2f( 0.0f, 1.0f );
579 glVertex2f( -1.0f, 1.0f );
580 glTexCoord2f( 0.0f, 0.0f );
581 glVertex2f( -1.0f, -1.0f );
582 glTexCoord2f( 1.0f, 1.0f );
583 glVertex2f( 1.0f, 1.0f );
585 glTexCoord2f( 1.0f, 1.0f );
586 glVertex2f( 1.0f, 1.0f );
587 glTexCoord2f( 0.0f, 0.0f );
588 glVertex2f( -1.0f, -1.0f );
589 glTexCoord2f( 1.0f, 0.0f );
590 glVertex2f( 1.0f, -1.0f );
594 glMatrixMode( GL_MODELVIEW );
601 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference() called: src=%u, dst=%u" ),
602 aSourceHandle, aDestHandle );
603 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): initialized=%d, usedBuffers=%u" ),
607 wxCHECK( aDestHandle != 0 && aDestHandle <=
usedBuffers(), );
612 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): shader init FAILED, falling back" ) );
618 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): shader initialized OK" ) );
621 GLuint srcTexture =
m_buffers[aSourceHandle - 1].textureTarget;
622 GLuint dstTexture =
m_buffers[aDestHandle - 1].textureTarget;
624 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): srcTexture=%u, dstTexture=%u" ),
625 srcTexture, dstTexture );
630 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): buffer dims=%dx%d" ), dims.
x, dims.
y );
634 glGenTextures( 1, &dstTempTex );
635 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): created dstTempTex=%u" ), dstTempTex );
637 glActiveTexture( GL_TEXTURE1 );
638 glBindTexture( GL_TEXTURE_2D, dstTempTex );
639 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
640 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
641 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, dims.
x, dims.
y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr );
642 checkGlError(
"allocating dst temp texture", __FILE__, __LINE__ );
646 GLenum destAttachment =
m_buffers[aDestHandle - 1].attachmentPoint;
647 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): dest attachment point=0x%X" ), destAttachment );
649 glReadBuffer( destAttachment );
650 checkGlError(
"setting read buffer for dst copy", __FILE__, __LINE__ );
652 glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, dims.
x, dims.
y );
653 checkGlError(
"copying dest to temp texture", __FILE__, __LINE__ );
655 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): copied dest buffer to dstTempTex" ) );
659 glDisable( GL_DEPTH_TEST );
660 glDisable( GL_BLEND );
664 checkGlError(
"using difference shader program", __FILE__, __LINE__ );
666 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): activated shader program=%u" ),
670 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): binding srcTexture=%u to unit 0" ), srcTexture );
672 glActiveTexture( GL_TEXTURE0 );
673 glBindTexture( GL_TEXTURE_2D, srcTexture );
675 checkGlError(
"binding source texture for XOR mode", __FILE__, __LINE__ );
678 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): binding dstTempTex=%u to unit 1" ), dstTempTex );
680 glActiveTexture( GL_TEXTURE1 );
681 glBindTexture( GL_TEXTURE_2D, dstTempTex );
683 checkGlError(
"binding dest texture for XOR mode", __FILE__, __LINE__ );
685 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): drawing fullscreen quad" ) );
689 glActiveTexture( GL_TEXTURE0 );
693 checkGlError(
"drawing fullscreen quad for XOR mode", __FILE__, __LINE__ );
695 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): cleanup" ) );
699 glDeleteTextures( 1, &dstTempTex );
701 glActiveTexture( GL_TEXTURE0 );
702 glEnable( GL_BLEND );
703 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
705 wxLogTrace(
traceGalXorMode, wxT(
"DrawBufferDifference(): COMPLETE" ) );
A color representation with 4 components: red, green, blue, alpha.
static const COLOR4D BLACK
unsigned int m_height
Height of the buffer (in pixels)
unsigned int m_width
Width of the buffer (in pixels)
GAL_ANTIALIASING_MODE m_currentAntialiasingMode
virtual ~OPENGL_COMPOSITOR()
VECTOR2D GetAntialiasRenderingOffset() const
virtual void Begin() override
Call this at the beginning of each frame.
unsigned int usedBuffers()
Returns number of used buffers.
void SetAntialiasingMode(GAL_ANTIALIASING_MODE aMode)
static const unsigned int DIRECT_RENDERING
VECTOR2I GetScreenSize() const
void clean()
Perform freeing of resources.
GLint m_diffDstTexUniform
Destination texture uniform location.
bool initDifferenceShader()
Initialize the difference shader program.
virtual void Present() override
Call this to present the output buffer to the screen.
bool m_initialized
Initialization status flag.
GLint m_diffSrcTexUniform
Source texture uniform location.
void bindFb(unsigned int aFb)
Binds a specific Framebuffer Object.
GAL_ANTIALIASING_MODE GetAntialiasingMode() const
GLuint m_curFbo
Store the used FBO name in case there was more than one compositor used.
virtual void Initialize() override
Perform primary initialization, necessary to use the object.
void drawFullScreenQuad()
Draw a fullscreen quad for compositing.
void DrawBufferDifference(unsigned int aSourceHandle, unsigned int aDestHandle)
Draw buffer with difference blending (XOR-style for gerbview).
GLuint m_depthBuffer
Depth buffer handle.
virtual void ClearBuffer(const COLOR4D &aColor) override
Clear the selected buffer (set by the SetBuffer() function).
std::unique_ptr< OPENGL_PRESENTOR > m_antialiasing
GLuint m_differenceShader
Difference shader program.
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.
OPENGL_BUFFERS m_buffers
Stores information about initialized buffers.
bool m_differenceShaderInitialized
virtual void DrawBuffer(unsigned int aBufferHandle) override
Draw the selected buffer to the output buffer.
GLuint m_mainFbo
Main FBO handle (storing all target textures)
int GetAntialiasSupersamplingFactor() const
unsigned int m_curBuffer
Currently used buffer handle.
GLenum GetBufferTexture(unsigned int aBufferHandle)
virtual void SetBuffer(unsigned int aBufferHandle) override
Set the selected buffer as the rendering target.
virtual unsigned int CreateBuffer() override
Prepare a new buffer that may be used as a rendering target.
The Cairo implementation of the graphics abstraction layer.
static const wxChar *const traceGalXorMode
Handle multitarget rendering (ie.
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D