Advertisement
Ancurio

ARB_copy_buffer weird behavior

May 2nd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. #include <GL/glew.h>
  5. #include <SDL2/SDL.h>
  6.  
  7. static SDL_Window *win;
  8. static SDL_GLContext *ctx;
  9.  
  10. void setupGL()
  11. {
  12.     SDL_Init(SDL_INIT_VIDEO);
  13.     win = SDL_CreateWindow("CopyBufferBug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64, 64, SDL_WINDOW_OPENGL);
  14.     ctx = SDL_GL_CreateContext(win);
  15.     glewInit();
  16. }
  17.  
  18. static void teardownGL()
  19. {
  20.     SDL_GL_DeleteContext(ctx);
  21.     SDL_DestroyWindow(win);
  22.  
  23.     SDL_Quit();
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28.     setupGL();
  29.  
  30.     /* These don't matter I think */
  31.     #define BLOCK_SIZE 128
  32.     #define BUFFER1_SIZE BLOCK_SIZE
  33.     #define BUFFER2_SIZE BLOCK_SIZE
  34.     #define BUFFER1_TARGET GL_COPY_READ_BUFFER
  35.     #define BUFFER2_TARGET GL_COPY_WRITE_BUFFER
  36.     #define BUFFER1_USAGE GL_DYNAMIC_DRAW
  37.     #define BUFFER2_USAGE GL_DYNAMIC_DRAW
  38.  
  39.     GLuint buffers[2];
  40.     glGenBuffers(2, buffers);
  41.  
  42.     /* We allocate both buffers with undefined memory */
  43.     glBindBuffer(BUFFER1_TARGET, buffers[0]);
  44.     glBufferData(BUFFER1_TARGET, BUFFER1_SIZE, 0, BUFFER1_USAGE);
  45.  
  46.     glBindBuffer(BUFFER2_TARGET, buffers[1]);
  47.     glBufferData(BUFFER2_TARGET, BUFFER2_SIZE, 0, BUFFER2_USAGE);
  48.  
  49.     /* Then copy (undefined) bytes from the first into the second buffer */
  50.     /* Note: If I comment this line out, everything works */
  51.     glCopyBufferSubData(BUFFER1_TARGET, BUFFER2_TARGET, 0, 0, BUFFER1_SIZE);
  52.  
  53.     /* Generate random string */
  54.     FILE *rand = fopen("/dev/urandom", "r");
  55.     char data[BLOCK_SIZE];
  56.     fread(data, 1, sizeof(data), rand);
  57.     fclose(rand);
  58.  
  59.     /* We fill the second buffer with defined data */
  60.     /* Note: If I execute this call twice (just copy paste the line), everything works */
  61.     glBufferSubData(BUFFER2_TARGET, 0, sizeof(data), data);
  62.  
  63.     /* Then download it again to compare its contents against client buffer */
  64.     char data2[BLOCK_SIZE];
  65.     glGetBufferSubData(BUFFER2_TARGET, 0, sizeof(data2), data2);
  66.  
  67.     if (memcmp(data, data2, sizeof(data)))
  68.         printf("Data does NOT match up!\n");
  69.     else
  70.         printf("Data matches.\n");
  71.  
  72.     glDeleteBuffers(2, buffers);
  73.  
  74.     teardownGL();
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement