KHR_debug_emulator.cc 32.6 KB
Newer Older
1

Robert Menzel's avatar
Robert Menzel committed
2
// include your OpenGL header here:
3 4
#include <ACGL/OpenGL/GL.hh>

Robert Menzel's avatar
Robert Menzel committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * Simulates KHR_debug
 *
 * Use-cases:
 * ----------
 *
 * Use this as a fallback on systems that don't implement KHR_debug for debugging only,
 * don't use this code in shipping release builds - it will slow down the application!
 * Using a debug callback instead of lots of glGetError() calls should work fine,
 * MessageControl, DebugGroups and DebugLabels are only implemented as fallbacks, in case
 * you have to rely on those features, you want to reimplement them in a more efficient way.
 *
 *
 * Known restrictions:
 * -------------------
 *
 *    Wrong behavior:
 *    ---------------
 *
 *    * Does not support multiple OpenGL contexts, all errors from all contexts are mixed.
 *      All settings (including the debug callback) are set for all contexts.
 *
 *    * glObjectLabel and glObjectPtrLabel do not check if the object to label exists and thus
 *      will not generate a GL_INVALID_VALUE.
 *
 *    * glObjectLabel can label GL_DISPLAY_LIST even in Core profiles.
 *
 *    Inefficiency:
 *    -------------
 *
 *    * Using this, the number of GL calls doubles as each call will get followed by a glGetError.
 *    * This will also force OpenGL to run synchronous which will reduce the performance!
 *    * ObjectLabels are implemented inefficiently and are not used internally. The functionality is
 *      only present to be compatible with KHR_debug.
 *    * DebugGroups and glDebugMessageControl are not efficiently implemented.
 *    * Some memory will not get released.
 *
 *    Implementation dependent limits:
 *    --------------------------------
 *
 *    * GL_MAX_DEBUG_MESSAGE_LENGTH and Gl_MAX_LABEL_LENGTH are arbitrary and can be changed.
 *    * GL_MAX_DEBUG_GROUP_STACK_DEPTH is set to the lowest allowed value of 64 but can be changed
 *    * GL_DEBUG_LOGGED_MESSAGES is set to 1 - increasing this will be more work.
 *
 *    * This implementation always behaves synchronous, even if GL_DEBUG_OUTPUT_SYNCHRONOUS is
 *      disabled (the default btw.). This is legal by the spec.
 */
52 53 54 55 56

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

57 58 59 60 61 62 63
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// global variables and implementation dependent limits:
///
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
64

65 66 67 68 69 70 71 72 73 74 75 76
// GL state:
GLDEBUGPROC KHR_DEBUG_EMULATOR_callback      = NULL;
const void *KHR_DEBUG_EMULATOR_userParam     = NULL;
int         KHR_DEBUG_EMULATOR_isEnabled     = 1;
int         KHR_DEBUG_EMULATOR_isSynchronous = 0;
GLenum      KHR_DEBUG_EMULATOR_lastGLError   = GL_NO_ERROR;

// limits:
const int   KHR_DEBUG_EMULATOR_MAX_DEBUG_MESSAGE_LENGTH    = 256;
const int   KHR_DEBUG_EMULATOR_MAX_DEBUG_LOGGED_MESSAGES   = 1;
#define     KHR_DEBUG_EMULATOR_MAX_DEBUG_GROUP_STACK_DEPTH   64
const int   KHR_DEBUG_EMULATOR_MAX_LABEL_LENGTH            = 256;
77 78 79 80 81 82 83 84 85


// extern declarations of internal original OpenGL calls (unwrapped)
extern GLenum    (CODEGEN_FUNCPTR *_original_glGetError)();
extern GLvoid    (CODEGEN_FUNCPTR *_original_glEnable)( GLenum );
extern GLvoid    (CODEGEN_FUNCPTR *_original_glDisable)( GLenum );
extern GLboolean (CODEGEN_FUNCPTR *_original_glIsEnabled)( GLenum );
extern GLvoid    (CODEGEN_FUNCPTR *_original_glGetIntegerv)( GLenum, GLint * );
extern GLvoid    (CODEGEN_FUNCPTR *_original_glGetPointerv)( GLenum, GLvoid ** );
86 87
// not used in here, just used to check if glClear redirects to the original function ;-)
extern GLvoid    (CODEGEN_FUNCPTR *_original_glClear)(GLbitfield);
88

89
// shortcut to add OpenGL errors detected by this emulation:
90 91
#define INSERT_API_ERROR( e, m ) KHR_DEBUG_EMULATOR_lastGLError = e; KHR_DEBUG_EMULATOR_DebugMessageInsert_internal(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, e, GL_DEBUG_SEVERITY_HIGH, -1, m );

92 93
//
// support to query debug messages (only one):
94
struct DebugMessage {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    GLenum       source;
    GLenum       type;
    GLuint       id;
    GLenum       severity;
    GLsizei      length;
    const GLchar *buf;
};
DebugMessage g_LastDebugMessage;
GLboolean    g_LastDebugMessageEmpty = true;

//
// support for debug message control
// NOTE: mixing rules for specific IDs and all IDs was probably a bad idea
typedef struct DebugMessageControlRule
{
110 111 112
    GLenum source;
    GLenum type;
    GLenum severity;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    GLuint id;
    GLboolean allIDs; // if set, ignore id
    GLboolean enabled;

    GLuint debugGroup;

    DebugMessageControlRule *previousRule;
    DebugMessageControlRule *nextRule;
} DebugMessageControlRule;

// yes, I'm aware that this will never get cleaned up completely :-(
DebugMessageControlRule *g_FirstDebugMessageControlRule = NULL;
DebugMessageControlRule *g_LastDebugMessageControlRule  = NULL;
GLuint g_DebugGroupNumber = 0; // 0 == default group

//
// support for ObjectLabels
// All known labels are stored in a double-linked list. Not pretty but should work as a
// compatable fallback.
typedef struct ObjectLabel {
    GLenum  identifier;
    GLuint  name;
135
    GLsizei length;
136
    GLchar  *label;
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    ObjectLabel *nextLabel;
    ObjectLabel *previousLabel;
} ObjectLabel;
ObjectLabel *g_ObjectLabels = NULL;

//
// support for ObjectPtrLabels
// The same idea as for ObjectLabels.
typedef struct ObjectPtrLabel {
    const void * ptr;
    GLsizei length;
    GLchar * label;

    ObjectPtrLabel *nextLabel;
    ObjectPtrLabel *previousLabel;
} ObjectPtrLabel;
ObjectPtrLabel *g_ObjectPtrLabels = NULL;

//
// Support for DebugGroups
// There is a maximum number of groups, so just use a big array. Element 0, the default
// group is never used.
struct DebugGroupDescription {
    GLenum  source;
    GLuint  id;
    GLsizei length;
    GLchar  *message;
};
DebugGroupDescription g_DebugGroupDescriptions[ KHR_DEBUG_EMULATOR_MAX_DEBUG_GROUP_STACK_DEPTH ];
167 168 169 170 171 172 173 174 175

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Replacements of the extension functions:
///
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

176 177 178 179 180 181 182
///////////////////////////
///   glDebugMessageInsert
///////////////////////////
//
// most work is done in KHR_DEBUG_EMULATOR_DebugMessageInsert_internal, but called from the application some more strict
// checks have to be performed.
//
183 184
GLboolean isValidSeverity( GLenum e );
GLboolean isValidType( GLenum e );
185
void APIENTRY KHR_DEBUG_EMULATOR_DebugMessageInsert_internal(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * buf);
186
// returns true if the message should not get ignored by the rules defined by glDebugMessageControl:
187 188
GLboolean shouldMessageGetProcessed( GLenum source, GLenum type, GLuint id, GLenum severity );

189
void APIENTRY KHR_DEBUG_EMULATOR_DebugMessageInsert(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * buf)
190 191 192 193 194 195 196 197 198 199 200
{
    if (KHR_DEBUG_EMULATOR_isEnabled == 0) return;

    // calls from the application are a bit more restricted in the types of errors they are allowed to generate:
    if ((source != GL_DEBUG_SOURCE_APPLICATION) && (source != GL_DEBUG_SOURCE_THIRD_PARTY)) {
        INSERT_API_ERROR( GL_INVALID_ENUM, "invalid enum in glDebugMessageInsert: source has to be GL_DEBUG_SOURCE_APPLICATION or GL_DEBUG_SOURCE_THIRD_PARTY" );
        return;
    }
    KHR_DEBUG_EMULATOR_DebugMessageInsert_internal( source, type, id, severity, length, buf );
}

201
void APIENTRY KHR_DEBUG_EMULATOR_DebugMessageInsert_internal(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * buf)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
{
    if (KHR_DEBUG_EMULATOR_isEnabled == 0) return;

    if ( !isValidSeverity( severity ) ) {
        INSERT_API_ERROR( GL_INVALID_ENUM , "invalid enum in glDebugMessageInsert: severity is invalid" );
        return;
    }
    if ( !isValidType( type ) ) {
        INSERT_API_ERROR( GL_INVALID_ENUM , "invalid enum in glDebugMessageInsert: type is invalid" );
        return;
    }

    // length can be -1 which means that buf is 0 terminated.
    // however, the messages created should always set length to the number of chars in the message (excluding the trailing 0)
    if (length < 0) {
        length = strlen( buf );
    }

    if (length > KHR_DEBUG_EMULATOR_MAX_DEBUG_MESSAGE_LENGTH) {
        INSERT_API_ERROR( GL_INVALID_VALUE , "invalid value in glDebugMessageInsert: message is too long" );
        return;
    }

    // there might be rules inserted by glDebugMessageControl to mute this message:
    if ( !shouldMessageGetProcessed(source, type, id, severity) ) {
        return;
    }

    if (KHR_DEBUG_EMULATOR_callback) {
        KHR_DEBUG_EMULATOR_callback( source, type, id, severity, length, buf, KHR_DEBUG_EMULATOR_userParam );
    } else {
        g_LastDebugMessageEmpty = false;
        g_LastDebugMessage.source = source;
        g_LastDebugMessage.type   = type;
        g_LastDebugMessage.id     = id;
        g_LastDebugMessage.severity = severity;
        g_LastDebugMessage.length = length;
        g_LastDebugMessage.buf    = buf;
    }
}

243 244 245 246

/////////////////////////////
///   glDebugMessageCallback
/////////////////////////////
247 248
GLboolean debugContextIsSimulated();

249
void APIENTRY KHR_DEBUG_EMULATOR_DebugMessageCallback(GLDEBUGPROC callback, const void * userParam)
250 251 252
{
    KHR_DEBUG_EMULATOR_callback  = callback;
    KHR_DEBUG_EMULATOR_userParam = userParam;
253 254 255 256 257

    if (debugContextIsSimulated()) {
        // we will never get here if the debug context is real in the first place!
        KHR_DEBUG_EMULATOR_DebugMessageInsert_internal( GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_NOTIFICATION, -1, "Note: Application is not running in a real debug context, debug context behaviour is only simulated in a slow way!" );
    }
258 259 260
}


261 262 263
/////////////////////////////
///   glDebugMessageControl
/////////////////////////////
264

265 266 267 268 269 270
// true if a given rule matches the message:
GLboolean ruleApplies( DebugMessageControlRule *rule, GLenum source, GLenum type, GLuint id, GLenum severity );
// check if the current rules allow this message:
GLboolean shouldMessageGetProcessed( GLenum source, GLenum type, GLuint id, GLenum severity );
// adds a new rule:
void addRule( DebugMessageControlRule *newRule );
271

272
void APIENTRY KHR_DEBUG_EMULATOR_DebugMessageControl(GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint * ids, GLboolean enabled)
273 274 275 276 277 278
{
    DebugMessageControlRule *rule = (DebugMessageControlRule*) malloc( sizeof(DebugMessageControlRule) );
    rule->source   = source;
    rule->type     = type;
    rule->severity = severity;
    rule->enabled  = enabled;
279
    rule->debugGroup = g_DebugGroupNumber;
280 281 282 283 284 285 286 287 288 289 290

    if (count == 0) {
        // ID-agnostic rule
        rule->allIDs = true;
        rule->id       = 0;

        addRule( rule );
    } else {
        // rules for specific IDs
        rule->allIDs = false;

291 292 293 294 295
        if (source == GL_DONT_CARE || type == GL_DONT_CARE || severity != GL_DONT_CARE) {
            // see KHR_debug 5.5.4
            INSERT_API_ERROR( GL_INVALID_OPERATION , "invalid operation in glDebugMessageControl: if an ID is specified, source and type have to be specified as well but severity has to be GL_DONT_CARE" );
        }

296 297 298 299 300 301 302
        for (int i = 0; i < count; ++i) {
            rule->id = ids[i];
            addRule( rule );
        }
    }
}

303 304 305
/////////////////////////////
///   glGetDebugMessageLog
/////////////////////////////
306
GLuint APIENTRY KHR_DEBUG_EMULATOR_GetDebugMessageLog(GLuint count, GLsizei bufsize, GLenum * sources, GLenum * types, GLuint * ids, GLenum * severities, GLsizei * lengths, GLchar * messageLog)
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
    if (bufsize < 0 && messageLog != NULL) {
        INSERT_API_ERROR( GL_INVALID_VALUE , "invalid value in glGetDebugMessageLog: bufsize < 0 and messageLog != NULL" );
        return 0;
    }

    if (g_LastDebugMessageEmpty || count == 0) return 0;

    if (types)      types[0]      = g_LastDebugMessage.type;
    if (sources)    sources[0]    = g_LastDebugMessage.source;
    if (ids)        ids[0]        = g_LastDebugMessage.id;
    if (severities) severities[0] = g_LastDebugMessage.severity;
    if (lengths)    lengths[0]    = g_LastDebugMessage.length;

    // length is without the 0-termination
    if (bufsize <= g_LastDebugMessage.length) {
        // won't fit, don't return the error :-(
        // 6.1.15 of KHR_debug
        return 0;
    }

    strncpy( messageLog, g_LastDebugMessage.buf, bufsize );
    messageLog[bufsize-1] = 0;

    g_LastDebugMessageEmpty = true;
    return 1;
}

Robert Menzel's avatar
Robert Menzel committed
335 336 337 338 339 340 341
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Handling of ObjectLabels:
/// ( not very pretty, just for compatibility )
///
///////////////////////////////////////////////////////////////////////////////////////////////////

342 343 344 345 346 347 348
/////////////////////////////
///   glObjectLabel
/////////////////////////////
///
GLboolean isValidObjectLabelNamespace( GLenum e );
ObjectLabel *findObjectLabel(GLenum identifier, GLuint name);
void insertObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar * label);
349

350 351 352
//
// TODO: check if name is a valid object, emit an INVALID_VALUE otherwise
//
353
void APIENTRY KHR_DEBUG_EMULATOR_ObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar * label)
354
{
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    if ( !isValidObjectLabelNamespace(identifier) ) {
        INSERT_API_ERROR( GL_INVALID_ENUM, "invalid enum in glObjectLabel" );
        return;
    }

    if (length < 0) {
        length = strlen( label );
    }

    if (length >= KHR_DEBUG_EMULATOR_MAX_LABEL_LENGTH) {
        INSERT_API_ERROR( GL_INVALID_VALUE, "invalid value in glObjectLabel: label too long" );
        return;
    }

    ObjectLabel *thelabel = findObjectLabel( identifier, name );

    if (thelabel == NULL) {
        insertObjectLabel( identifier, name, length, label );
    } else {
        // update
        if (length == 0) {
            // delete
            ObjectLabel *pre = thelabel->previousLabel;
            ObjectLabel *next = thelabel->nextLabel;

            if (pre) pre->nextLabel = next;
            if (next) next->previousLabel = pre;
            if (!pre) g_ObjectLabels = next; // this was the first label

            free( (void*) thelabel->label );
            free( (void*) thelabel );
        } else {
            // update name & length:
            if (length < 0) {
                length = strlen( label );
            }
            free( (void*) thelabel->label );
            thelabel->label      = (GLchar *) malloc( length+1 );
            strncpy( thelabel->label , label, length );
            thelabel->label[ length ] = 0;
            thelabel->length = length;
        }
    }
398 399
}

400 401 402
/////////////////////////////
///   glGetObjectLabel
/////////////////////////////
403
void APIENTRY KHR_DEBUG_EMULATOR_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize, GLsizei * length, GLchar * label)
404
{
405 406 407 408 409
    if ( !isValidObjectLabelNamespace(identifier) ) {
        INSERT_API_ERROR( GL_INVALID_ENUM, "invalid enum in glObjectGetLabel" );
        return;
    }

410 411 412 413 414 415 416 417 418
    ObjectLabel *thelabel = findObjectLabel( identifier, name );
    if (thelabel == NULL) {
        // label not found:
        label[0] = 0;
        *length = 0;
        return;
    }

    size_t size = thelabel->length + 1;
419
    if ((size_t)bufSize <= size) size = bufSize-1;
420 421 422 423 424 425 426 427

    if (label != NULL) {
        strncpy( label, thelabel->label, size );
        label[size] = 0;
    }
    *length = size;
}

Robert Menzel's avatar
Robert Menzel committed
428 429 430 431 432 433
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Handling of ObjectPtrLabels:
/// ( not very pretty, just for compatibility )
///
///////////////////////////////////////////////////////////////////////////////////////////////////
434

435 436 437 438 439
/////////////////////////////
///   glObjectPtrLabel
/////////////////////////////
ObjectPtrLabel *findObjectPtrLabel(const void * ptr);
void insertObjectPtrLabel(const void * ptr, GLsizei length, const GLchar * label);
440

441
void APIENTRY KHR_DEBUG_EMULATOR_ObjectPtrLabel(const void * ptr, GLsizei length, const GLchar * label)
442
{
443 444 445
    if (length < 0) {
        length = strlen( label );
    }
446

447 448 449
    if (length >= KHR_DEBUG_EMULATOR_MAX_LABEL_LENGTH) {
        INSERT_API_ERROR( GL_INVALID_VALUE, "invalid value in glObjectPtrLabel: label too long" );
        return;
450 451
    }

452
    ObjectPtrLabel *thelabel = findObjectPtrLabel( ptr );
453

454 455 456 457 458 459 460 461
    if (thelabel == NULL) {
        insertObjectPtrLabel( ptr, length, label );
    } else {
        // update
        if (length == 0) {
            // delete
            ObjectPtrLabel *pre  = thelabel->previousLabel;
            ObjectPtrLabel *next = thelabel->nextLabel;
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

            if (pre) pre->nextLabel = next;
            if (next) next->previousLabel = pre;
            if (!pre) g_ObjectPtrLabels = next; // this was the first label

            free( (void*) thelabel->label );
            free( (void*) thelabel );
        } else {
            // update name & length:
            if (length < 0) {
                length = strlen( label );
            }
            free( (void*) thelabel->label );
            thelabel->label      = (GLchar *) malloc( length+1 );
            strncpy( thelabel->label , label, length );
            thelabel->label[ length ] = 0;
            thelabel->length = length;
        }
    }
481 482
}

483 484 485
/////////////////////////////
///   glGetObjectPtrLabel
/////////////////////////////
486
void APIENTRY KHR_DEBUG_EMULATOR_GetObjectPtrLabel(const void * ptr, GLsizei bufSize, GLsizei * length, GLchar * label)
487
{
488 489 490 491 492 493 494 495 496
    ObjectPtrLabel *thelabel = findObjectPtrLabel( ptr );
    if (thelabel == NULL) {
        // label not found:
        label[0] = 0;
        *length = 0;
        return;
    }

    size_t size = thelabel->length + 1;
497
    if ((size_t)bufSize <= size) size = bufSize-1;
498 499 500 501 502 503

    if (label != NULL) {
        strncpy( label, thelabel->label, size );
        label[size] = 0;
    }
    *length = size;
504 505
}

Robert Menzel's avatar
Robert Menzel committed
506 507 508 509 510 511
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Handling of DebugGroups
///
///////////////////////////////////////////////////////////////////////////////////////////////////

512 513 514
/////////////////////////////
///   glPopDebugGroup
/////////////////////////////
515
void APIENTRY KHR_DEBUG_EMULATOR_PopDebugGroup()
516
{
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    if (g_DebugGroupNumber == 0) {
        INSERT_API_ERROR( GL_STACK_UNDERFLOW, "stack underflow in glPushDebugGroup: can't pop default group" );
        return;
    }

    //
    // delete all rules from the poped group:
    while (g_LastDebugMessageControlRule && g_LastDebugMessageControlRule->debugGroup >= g_DebugGroupNumber) {

        DebugMessageControlRule *ruleToDelete = g_LastDebugMessageControlRule;

        // set new last rule:
        g_LastDebugMessageControlRule = g_LastDebugMessageControlRule->previousRule;
        if (g_LastDebugMessageControlRule != NULL) {
            g_LastDebugMessageControlRule->nextRule = NULL;
        } else {
            g_FirstDebugMessageControlRule = NULL;
        }

        // now delete the rule:
        free( (void*) ruleToDelete );
    }

    GLenum source    = g_DebugGroupDescriptions[g_DebugGroupNumber].source;
    GLuint id        = g_DebugGroupDescriptions[g_DebugGroupNumber].id;
    GLsizei length   = g_DebugGroupDescriptions[g_DebugGroupNumber].length;
    GLchar * message = g_DebugGroupDescriptions[g_DebugGroupNumber].message;

    // goodby message:
    KHR_DEBUG_EMULATOR_DebugMessageInsert_internal( source, GL_DEBUG_TYPE_POP_GROUP, id, GL_DEBUG_SEVERITY_NOTIFICATION, length, message );
    // clean up:
    free( (void*) g_DebugGroupDescriptions[g_DebugGroupNumber].message );
    g_DebugGroupDescriptions[g_DebugGroupNumber].message = NULL;

    g_DebugGroupNumber--;
552 553
}

554 555 556
/////////////////////////////
///   glPushDebugGroup
/////////////////////////////
557
void APIENTRY KHR_DEBUG_EMULATOR_PushDebugGroup(GLenum source, GLuint id, GLsizei length, const GLchar * message)
558
{
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    if ((source != GL_DEBUG_SOURCE_APPLICATION) && (source != GL_DEBUG_SOURCE_THIRD_PARTY)) {
        INSERT_API_ERROR( GL_INVALID_ENUM, "invalid enum in glPushDebugGroup: source has to be GL_DEBUG_SOURCE_APPLICATION or GL_DEBUG_SOURCE_THIRD_PARTY" );
        return;
    }
    if (g_DebugGroupNumber >= KHR_DEBUG_EMULATOR_MAX_DEBUG_GROUP_STACK_DEPTH-1) {
        INSERT_API_ERROR( GL_STACK_OVERFLOW, "stack overflow in glPushDebugGroup: already too many groups pushed" );
        return;
    }

    KHR_DEBUG_EMULATOR_DebugMessageInsert_internal( source, GL_DEBUG_TYPE_PUSH_GROUP, id, GL_DEBUG_SEVERITY_NOTIFICATION, length, message );

    g_DebugGroupNumber++;

    if (length < 0) {
        length = strlen( message );
    }

    g_DebugGroupDescriptions[g_DebugGroupNumber].id      = id;
    g_DebugGroupDescriptions[g_DebugGroupNumber].source  = source;
    g_DebugGroupDescriptions[g_DebugGroupNumber].length  = length;
    g_DebugGroupDescriptions[g_DebugGroupNumber].message = (GLchar *) malloc( length+1 );

    strncpy( g_DebugGroupDescriptions[g_DebugGroupNumber].message, message, length );
    g_DebugGroupDescriptions[g_DebugGroupNumber].message[ length ] = 0;
583 584 585 586 587 588 589 590 591 592 593 594
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Replacements for normal GL functions that have to behave a bit differently when KHR_debug
/// is present:
///
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

595
void APIENTRY KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
596

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
//
// glGetError
//   needs to return the last error even if this KHR_debug emulator already got the error code
//
GLenum KHR_DEBUG_EMULATOR_GetError() {
    // if there was an error, report it. if not report the last global error
    // which might got set by the automatic error checks
    GLenum currentError = _original_glGetError();
    if ( currentError == GL_NO_ERROR ) {
            currentError = KHR_DEBUG_EMULATOR_lastGLError;
    }
    KHR_DEBUG_EMULATOR_lastGLError = GL_NO_ERROR;
    return currentError;
}

//
// glDisable | glEnable | glIsEnabled
//   need to recognize GL_DEBUG_OUTPUT & GL_DEBUG_OUTPUT_SYNCHRONOUS
//
616
void APIENTRY KHR_DEBUG_EMULATOR_Disable(GLenum cap){
617 618 619 620 621 622 623 624 625 626 627 628
  if (cap == GL_DEBUG_OUTPUT) {
    KHR_DEBUG_EMULATOR_isEnabled = 0;
    return;
  }
  if (cap == GL_DEBUG_OUTPUT_SYNCHRONOUS) {
    KHR_DEBUG_EMULATOR_isSynchronous = 0;
    return;
  }
   _original_glDisable(cap);
   KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
}

629
void APIENTRY KHR_DEBUG_EMULATOR_Enable(GLenum cap){
630 631 632 633 634 635 636 637 638 639 640 641 642
  if (cap == GL_DEBUG_OUTPUT) {
    KHR_DEBUG_EMULATOR_isEnabled = 1;
    return;
  }
  if (cap == GL_DEBUG_OUTPUT_SYNCHRONOUS) {
    KHR_DEBUG_EMULATOR_isSynchronous = 1;
    return;
  }

   _original_glEnable(cap);
  KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
}

643
GLboolean APIENTRY KHR_DEBUG_EMULATOR_IsEnabled(GLenum cap){
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  if (cap == GL_DEBUG_OUTPUT) {
    return (KHR_DEBUG_EMULATOR_isEnabled == 1);
  }
  if (cap == GL_DEBUG_OUTPUT_SYNCHRONOUS) {
    return (KHR_DEBUG_EMULATOR_isSynchronous == 1);
  }

   GLboolean returnValue = _original_glIsEnabled(cap);
  KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
  return returnValue;
}

//
// glGetIntegerv
//   needs to recognize a few new tokens
//
660
void APIENTRY KHR_DEBUG_EMULATOR_GetIntegerv(GLenum pname, GLint * params){
Robert Menzel's avatar
Robert Menzel committed
661 662
    if (pname == GL_CONTEXT_FLAGS) {
        _original_glGetIntegerv(pname, params);
663 664 665 666 667

        if (debugContextIsSimulated()) {
            // debug context simulation
            *params |= GL_CONTEXT_FLAG_DEBUG_BIT; // we make this a debug context ;-)
        }
Robert Menzel's avatar
Robert Menzel committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    } else if (pname == GL_MAX_DEBUG_MESSAGE_LENGTH) {
        *params = KHR_DEBUG_EMULATOR_MAX_DEBUG_MESSAGE_LENGTH;
    } else if (pname == GL_MAX_DEBUG_LOGGED_MESSAGES) {
        *params = KHR_DEBUG_EMULATOR_MAX_DEBUG_LOGGED_MESSAGES;
    } else if (pname == GL_MAX_DEBUG_GROUP_STACK_DEPTH) {
        *params = KHR_DEBUG_EMULATOR_MAX_DEBUG_GROUP_STACK_DEPTH;
    } else if (pname == GL_MAX_LABEL_LENGTH) {
        *params = KHR_DEBUG_EMULATOR_MAX_LABEL_LENGTH;
    } else if (pname == GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH) {
        if (g_LastDebugMessageEmpty) {
            *params = 0;
        } else {
            *params = g_LastDebugMessage.length;
        }
    } else {
        _original_glGetIntegerv(pname, params);
    }
    KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
686 687 688 689 690 691
}

//
// glGetPointerv
//   needs to recognize GL_DEBUG_CALLBACK_FUNCTION & GL_DEBUG_CALLBACK_USER_PARAM
//
692
void APIENTRY KHR_DEBUG_EMULATOR_GetPointerv( GLenum pname, GLvoid ** params ){
693 694 695 696 697 698 699 700 701 702

  if (pname == GL_DEBUG_CALLBACK_FUNCTION) {
    *params = (GLvoid*) KHR_DEBUG_EMULATOR_callback;
  } else if (pname == GL_DEBUG_CALLBACK_USER_PARAM) {
    *params = (GLvoid*) KHR_DEBUG_EMULATOR_userParam;
  } else {
    _original_glGetPointerv( pname, params );
    KHR_DEBUG_EMULATOR_CHECK_GL_ERROR();
  }
}
703 704 705 706 707 708 709

///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Generating errors:
///
///////////////////////////////////////////////////////////////////////////////////////////////////

710
const char* APIENTRY KHR_DEBUG_EMULATOR_GET_ERROR_STRING( GLenum _errorCode )
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
{
    if      (_errorCode == GL_INVALID_ENUM)                  { return (char*) "OpenGL error: GL_INVALID_ENUM"; }
    else if (_errorCode == GL_INVALID_VALUE)                 { return (char*) "OpenGL error: GL_INVALID_VALUE"; }
    else if (_errorCode == GL_INVALID_OPERATION)             { return (char*) "OpenGL error: GL_INVALID_OPERATION"; }
    else if (_errorCode == GL_INVALID_FRAMEBUFFER_OPERATION) { return (char*) "OpenGL error: GL_INVALID_FRAMEBUFFER_OPERATION"; }
    else if (_errorCode == GL_OUT_OF_MEMORY)                 { return (char*) "OpenGL error: GL_OUT_OF_MEMORY"; }
    else if (_errorCode == GL_NO_ERROR)                      { return (char*) "OpenGL error: GL_NO_ERROR"; }
    else if (_errorCode == GL_STACK_UNDERFLOW)               { return (char*) "OpenGL error: GL_STACK_UNDERFLOW"; }
    else if (_errorCode == GL_STACK_OVERFLOW)                { return (char*) "OpenGL error: GL_STACK_OVERFLOW"; }
    else {
        return (char*) "Unknown OpenGL error";
    }
}

// internal error check that gets triggered after every GL call
// * check for errors, if there was one, trigger a debug message but store the error code to fake the original glGetError behavior
727
void APIENTRY KHR_DEBUG_EMULATOR_CHECK_GL_ERROR() {
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    //printf("check error\n");
    GLenum currentError = _original_glGetError();
    if ( currentError != GL_NO_ERROR ) {
        KHR_DEBUG_EMULATOR_lastGLError = currentError;
        KHR_DEBUG_EMULATOR_DebugMessageInsert_internal( GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, currentError, GL_DEBUG_SEVERITY_HIGH, -1, KHR_DEBUG_EMULATOR_GET_ERROR_STRING( currentError ) );
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Other helping functions:
///
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

744 745 746 747 748 749 750 751 752
GLboolean debugContextIsSimulated()
{
    // Test if we simulate a debug context:
    // if the externally visible glClear is also the original function, we don't
    // simulate a debug context. Note that if they are the same, it doesn't mean there
    // is a real debug context, just that we don't mess with the original context!
    return (_original_glClear != glClear);
}

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
GLboolean isValidSeverity( GLenum e )
{
    if ((e == GL_DEBUG_SEVERITY_HIGH) || (e == GL_DEBUG_SEVERITY_MEDIUM) || (e == GL_DEBUG_SEVERITY_LOW) || (e == GL_DEBUG_SEVERITY_NOTIFICATION)) return true;
    return false;
}

GLboolean isValidType( GLenum e )
{
    if (  (e == GL_DEBUG_TYPE_ERROR)       || (e == GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR) || (e == GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR)
       || (e == GL_DEBUG_TYPE_PERFORMANCE) || (e == GL_DEBUG_TYPE_PORTABILITY)         || (e == GL_DEBUG_TYPE_OTHER)
       || (e == GL_DEBUG_TYPE_MARKER)      || (e == GL_DEBUG_TYPE_PUSH_GROUP)          || (e == GL_DEBUG_TYPE_POP_GROUP)) return true;
    return false;
}

GLboolean isValidSource( GLenum e )
{
    if (  (e == GL_DEBUG_SOURCE_API)         || (e == GL_DEBUG_SOURCE_SHADER_COMPILER) || (e == GL_DEBUG_SOURCE_WINDOW_SYSTEM)
       || (e == GL_DEBUG_SOURCE_THIRD_PARTY) || (e == GL_DEBUG_SOURCE_APPLICATION)     || (e == GL_DEBUG_SOURCE_OTHER)) return true;
    return false;
}

GLboolean ruleApplies( DebugMessageControlRule *rule, GLenum source, GLenum type, GLuint id, GLenum severity )
{
    if ( ( rule->allIDs   != true )         && ( rule->id       != id       ) ) return false; // ID mismatch
    if ( ( rule->source   != GL_DONT_CARE ) && ( rule->source   != source   ) ) return false; // source mismatch
    if ( ( rule->type     != GL_DONT_CARE ) && ( rule->type     != type     ) ) return false; // type mismatch
    if ( ( rule->severity != GL_DONT_CARE ) && ( rule->severity != severity ) ) return false; // severity mismatch

    return true;
}

GLboolean shouldMessageGetProcessed( GLenum source, GLenum type, GLuint id, GLenum severity )
{
    // check from the newest to the oldest rule,
    // first one to be applyable to this message defines if it gets processed:

    DebugMessageControlRule *ruleToCheck = g_LastDebugMessageControlRule;

    while (ruleToCheck != NULL) {
        if ( ruleApplies( ruleToCheck, source, type, id, severity) ) {
            return ruleToCheck->enabled;
        }
        ruleToCheck = ruleToCheck->previousRule;
    }

    // no matching rule found, apply default behavior:
    if (severity == GL_DEBUG_SEVERITY_LOW) {
        return false;
    }
    return true;
}

void addRule( DebugMessageControlRule *newRule )
{
    newRule->nextRule     = NULL;
    newRule->previousRule = NULL;

    if (g_FirstDebugMessageControlRule == NULL) {
        // first rule to insert:
        g_FirstDebugMessageControlRule = newRule;
        g_LastDebugMessageControlRule  = newRule;
        return;
    }

    g_LastDebugMessageControlRule->nextRule = newRule;
    newRule->previousRule = g_LastDebugMessageControlRule;
    g_LastDebugMessageControlRule = newRule;
}


#ifndef GL_TRANSFORM_FEEDBACK
#define GL_TRANSFORM_FEEDBACK 0x8E22
#endif

#ifndef GL_DISPLAY_LIST
#define GL_DISPLAY_LIST 0x82E7
#endif

GLboolean isValidObjectLabelNamespace( GLenum e )
{
    if (e == GL_BUFFER || e == GL_SHADER || e == GL_PROGRAM || e == GL_VERTEX_ARRAY
            || e == GL_QUERY || e == GL_PROGRAM_PIPELINE || e == GL_TRANSFORM_FEEDBACK
            || e == GL_SAMPLER || e == GL_TEXTURE || e == GL_RENDERBUFFER || e == GL_FRAMEBUFFER
            || e == GL_DISPLAY_LIST ) {
        return true;
    }

    return false;
}

ObjectLabel *findObjectLabel(GLenum identifier, GLuint name)
{
    ObjectLabel *l = g_ObjectLabels;

    while ( l != NULL ) {

        if (l->identifier == identifier && l->name == name) return l;

        l = l->nextLabel;
    }

    // none found:
    return NULL;
}

void insertObjectLabel(GLenum identifier, GLuint name, GLsizei length, const GLchar * label)
{
    ObjectLabel *l = (ObjectLabel *) malloc( sizeof(ObjectLabel) );

    l->identifier = identifier;
    l->name       = name;
    l->length     = length;
    l->label      = (GLchar *) malloc( length+1 );
    l->nextLabel  = NULL;
    l->previousLabel = NULL;

    strncpy( l->label , label, length );
    l->label[ length ] = 0;

    // find last label
    ObjectLabel *last = g_ObjectLabels;

    if (last == NULL) {
        // first label:
        g_ObjectLabels = l;
        return;
    }

    while ( last->nextLabel != NULL ) { last = last->nextLabel; }

    last->nextLabel = l;
    l->previousLabel = last;
}

ObjectPtrLabel *findObjectPtrLabel(const void * ptr)
{
    ObjectPtrLabel *l = g_ObjectPtrLabels;

    while ( l != NULL ) {

        if (l->ptr == ptr) return l;

        l = l->nextLabel;
    }

    // none found:
    return NULL;
}

void insertObjectPtrLabel(const void * ptr, GLsizei length, const GLchar * label)
{
    ObjectPtrLabel *l = (ObjectPtrLabel *) malloc( sizeof(ObjectPtrLabel) );

    l->ptr        = ptr;
    l->length     = length;
    l->label      = (GLchar *) malloc( length+1 );
    l->nextLabel  = NULL;
    l->previousLabel = NULL;

    strncpy( l->label , label, length );
    l->label[ length ] = 0;

    // find last label
    ObjectPtrLabel *last = g_ObjectPtrLabels;

    if (last == NULL) {
        // first label:
        g_ObjectPtrLabels = l;
        return;
    }

    while ( last->nextLabel != NULL ) { last = last->nextLabel; }

    last->nextLabel = l;
    l->previousLabel = last;
}