this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added GPU_SetDepthTest() and GPU_SetDepthWrite(). Only context targets have depth buffer attachments so far, so image targets won't do depth testing.

Added depth-test program.

Added near and far clipping plane values to GPU_Camera. This is passed straight into the ortho call when flushing, so a function to explicitly set them might be good (to hide/enforce the flush). Changed the default z of the camera to 0.

+190 -10
+14 -1
include/SDL_gpu.h
··· 305 305 float x, y, z; 306 306 float angle; 307 307 float zoom; 308 + float near, far; // z clipping planes 308 309 } GPU_Camera; 309 310 310 311 ··· 416 417 /*! Perspective and object viewing transforms. */ 417 418 GPU_Camera camera; 418 419 GPU_bool use_camera; 420 + 421 + GPU_bool use_depth_test; 422 + GPU_bool use_depth_write; 419 423 420 424 /*! Renderer context data. NULL if the target does not represent a window or rendering context. */ 421 425 GPU_Context* context; ··· 958 962 /*! Returns the current line thickness value. */ 959 963 DECLSPEC float SDLCALL GPU_GetLineThickness(void); 960 964 965 + 961 966 // End of ContextControls 962 967 /*! @} */ 963 968 ··· 1004 1009 /*! Resets the given target's viewport to the entire target area. */ 1005 1010 DECLSPEC void SDLCALL GPU_UnsetViewport(GPU_Target* target); 1006 1011 1007 - /*! \return A GPU_Camera with position (0, 0, -10), angle of 0, and zoom of 1. */ 1012 + /*! \return A GPU_Camera with position (0, 0, 0), angle of 0, zoom of 1, and near/far clipping planes of -100 and 100. */ 1008 1013 DECLSPEC GPU_Camera SDLCALL GPU_GetDefaultCamera(void); 1009 1014 1010 1015 /*! \return The camera of the given render target. If target is NULL, returns the default camera. */ ··· 1021 1026 1022 1027 /*! Returns 1 if the camera transforms are enabled, 0 otherwise. */ 1023 1028 DECLSPEC GPU_bool SDLCALL GPU_IsCameraEnabled(GPU_Target* target); 1029 + 1030 + /*! Enables or disables the depth test, which will skip drawing pixels/fragments behind other fragments. Disabled by default. 1031 + * This has implications for alpha blending, where compositing might not work correctly depending on render order. 1032 + */ 1033 + DECLSPEC void SDLCALL GPU_SetDepthTest(GPU_Target* target, GPU_bool enable); 1034 + 1035 + /*! Enables or disables writing the depth (effective view z-coordinate) of new pixels to the depth buffer. Enabled by default, but you must call GPU_SetDepthTest() to use it. */ 1036 + DECLSPEC void SDLCALL GPU_SetDepthWrite(GPU_Target* target, GPU_bool enable); 1024 1037 1025 1038 /*! \return The RGBA color of a pixel. */ 1026 1039 DECLSPEC SDL_Color SDLCALL GPU_GetPixel(GPU_Target* target, Sint16 x, Sint16 y);
+3
include/SDL_gpu_GLES_1.h
··· 67 67 GPU_Camera last_camera; 68 68 GPU_bool last_camera_inverted; 69 69 70 + GPU_bool last_depth_test; 71 + GPU_bool last_depth_write; 72 + 70 73 GPU_Image* last_image; 71 74 GPU_Target* last_target; 72 75 float* blit_buffer; // Holds sets of 4 vertices and 4 tex coords interleaved (e.g. [x0, y0, z0, s0, t0, ...]).
+3
include/SDL_gpu_GLES_2.h
··· 122 122 GPU_Camera last_camera; 123 123 GPU_bool last_camera_inverted; 124 124 125 + GPU_bool last_depth_test; 126 + GPU_bool last_depth_write; 127 + 125 128 GPU_Image* last_image; 126 129 GPU_Target* last_target; 127 130 float* blit_buffer; // Holds sets of 4 vertices, each with interleaved position, tex coords, and colors (e.g. [x0, y0, z0, s0, t0, r0, g0, b0, a0, ...]).
+3
include/SDL_gpu_GLES_3.h
··· 126 126 GPU_Camera last_camera; 127 127 GPU_bool last_camera_inverted; 128 128 129 + GPU_bool last_depth_test; 130 + GPU_bool last_depth_write; 131 + 129 132 GPU_Image* last_image; 130 133 GPU_Target* last_target; 131 134 float* blit_buffer; // Holds sets of 4 vertices, each with interleaved position, tex coords, and colors (e.g. [x0, y0, z0, s0, t0, r0, g0, b0, a0, ...]).
+3
include/SDL_gpu_OpenGL_1.h
··· 232 232 GPU_Camera last_camera; 233 233 GPU_bool last_camera_inverted; 234 234 235 + GPU_bool last_depth_test; 236 + GPU_bool last_depth_write; 237 + 235 238 GPU_Image* last_image; 236 239 GPU_Target* last_target; 237 240 float* blit_buffer; // Holds sets of 4 vertices and 4 tex coords interleaved (e.g. [x0, y0, z0, s0, t0, ...]).
+3
include/SDL_gpu_OpenGL_1_BASE.h
··· 47 47 GPU_Camera last_camera; 48 48 GPU_bool last_camera_inverted; 49 49 50 + GPU_bool last_depth_test; 51 + GPU_bool last_depth_write; 52 + 50 53 GPU_Image* last_image; 51 54 GPU_Target* last_target; 52 55 float* blit_buffer; // Holds sets of 4 vertices and 4 tex coords interleaved (e.g. [x0, y0, z0, s0, t0, ...]).
+3
include/SDL_gpu_OpenGL_2.h
··· 103 103 GPU_Camera last_camera; 104 104 GPU_bool last_camera_inverted; 105 105 106 + GPU_bool last_depth_test; 107 + GPU_bool last_depth_write; 108 + 106 109 GPU_Image* last_image; 107 110 GPU_Target* last_target; 108 111 float* blit_buffer; // Holds sets of 4 vertices and 4 tex coords interleaved (e.g. [x0, y0, z0, s0, t0, ...]).
+3
include/SDL_gpu_OpenGL_3.h
··· 166 166 GPU_Camera last_camera; 167 167 GPU_bool last_camera_inverted; 168 168 169 + GPU_bool last_depth_test; 170 + GPU_bool last_depth_write; 171 + 169 172 GPU_Image* last_image; 170 173 GPU_Target* last_target; 171 174 float* blit_buffer; // Holds sets of 4 vertices, each with interleaved position, tex coords, and colors (e.g. [x0, y0, z0, s0, t0, r0, g0, b0, a0, ...]).
+3
include/SDL_gpu_OpenGL_4.h
··· 104 104 GPU_Camera last_camera; 105 105 GPU_bool last_camera_inverted; 106 106 107 + GPU_bool last_depth_test; 108 + GPU_bool last_depth_write; 109 + 107 110 GPU_Image* last_image; 108 111 GPU_Target* last_target; 109 112 float* blit_buffer; // Holds sets of 4 vertices, each with interleaved position, tex coords, and colors (e.g. [x0, y0, z0, s0, t0, r0, g0, b0, a0, ...]).
+14 -1
src/SDL_gpu.c
··· 526 526 #endif 527 527 } 528 528 529 + 530 + void GPU_SetDepthTest(GPU_Target* target, GPU_bool enable) 531 + { 532 + if(target != NULL) 533 + target->use_depth_test = enable; 534 + } 535 + 536 + void GPU_SetDepthWrite(GPU_Target* target, GPU_bool enable) 537 + { 538 + if(target != NULL) 539 + target->use_depth_write = enable; 540 + } 541 + 529 542 GPU_bool GPU_SetWindowResolution(Uint16 w, Uint16 h) 530 543 { 531 544 if(_gpu_current_renderer == NULL || _gpu_current_renderer->current_context_target == NULL || w == 0 || h == 0) ··· 866 879 867 880 GPU_Camera GPU_GetDefaultCamera(void) 868 881 { 869 - GPU_Camera cam = {0.0f, 0.0f, -10.0f, 0.0f, 1.0f}; 882 + GPU_Camera cam = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -100.0f, 100.0f}; 870 883 return cam; 871 884 } 872 885
+46 -8
src/renderer_GL_common.inl
··· 822 822 glDisable(GL_SCISSOR_TEST); 823 823 } 824 824 825 + 826 + static void changeDepthTest(GPU_Renderer* renderer, GPU_bool enable) 827 + { 828 + GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data; 829 + if(cdata->last_depth_test == enable) 830 + return; 831 + 832 + cdata->last_depth_test = enable; 833 + if(enable) 834 + glEnable(GL_DEPTH_TEST); 835 + else 836 + glDisable(GL_DEPTH_TEST); 837 + glEnable(GL_ALPHA_TEST); 838 + } 839 + 840 + static void changeDepthWrite(GPU_Renderer* renderer, GPU_bool enable) 841 + { 842 + GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data; 843 + if(cdata->last_depth_write == enable) 844 + return; 845 + 846 + cdata->last_depth_write = enable; 847 + glDepthMask(enable); 848 + } 849 + 825 850 static void prepareToRenderToTarget(GPU_Renderer* renderer, GPU_Target* target) 826 851 { 827 852 // Set up the camera 828 853 renderer->impl->SetCamera(renderer, target, &target->camera); 854 + changeDepthTest(renderer, target->use_depth_test); 855 + changeDepthWrite(renderer, target->use_depth_write); 829 856 } 830 857 831 858 ··· 1100 1127 } 1101 1128 } 1102 1129 1103 - static void get_camera_matrix(float* result, GPU_Camera camera) 1130 + static void get_camera_matrix(float* result) 1104 1131 { 1105 1132 GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)GPU_GetContextTarget()->context->data; 1106 1133 GPU_Target* target = cdata->last_target; ··· 1111 1138 1112 1139 // Now multiply in the projection part 1113 1140 if(!invert ^ GPU_GetCoordinateMode()) 1114 - GPU_MatrixOrtho(result, target->camera.x, target->w + target->camera.x, target->h + target->camera.y, target->camera.y, -100.0f, 100.0f); 1141 + GPU_MatrixOrtho(result, target->camera.x, target->w + target->camera.x, target->h + target->camera.y, target->camera.y, target->camera.near, target->camera.far); 1115 1142 else 1116 - GPU_MatrixOrtho(result, target->camera.x, target->w + target->camera.x, target->camera.y, target->h + target->camera.y, -100.0f, 100.0f); // Special inverted orthographic projection because tex coords are inverted already for render-to-texture 1143 + GPU_MatrixOrtho(result, target->camera.x, target->w + target->camera.x, target->camera.y, target->h + target->camera.y, target->camera.near, target->camera.far); // Special inverted orthographic projection because tex coords are inverted already for render-to-texture 1117 1144 1118 1145 // First the modelview part 1119 1146 offsetX = target->w/2.0f; ··· 1133 1160 #ifdef SDL_GPU_APPLY_TRANSFORMS_TO_GL_STACK 1134 1161 static void applyTransforms(void) 1135 1162 { 1136 - GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)GPU_GetContextTarget()->context->data; 1137 1163 float* p = GPU_GetProjection(); 1138 1164 float* m = GPU_GetModelView(); 1139 1165 1140 1166 float cam_matrix[16]; 1141 - get_camera_matrix(cam_matrix, cdata->last_camera); 1167 + get_camera_matrix(cam_matrix); 1142 1168 1143 1169 GPU_MultiplyAndAssign(m, cam_matrix); 1144 1170 ··· 1445 1471 GPU_InitMatrixStack(&target->context->modelview_matrix); 1446 1472 1447 1473 target->context->matrix_mode = GPU_MODELVIEW; 1448 - 1474 + 1449 1475 cdata->last_image = NULL; 1450 1476 cdata->last_target = NULL; 1451 1477 // Initialize the blit buffer ··· 1536 1562 target->viewport = GPU_MakeRect(0, 0, target->context->drawable_w, target->context->drawable_h); 1537 1563 target->camera = GPU_GetDefaultCamera(); 1538 1564 target->use_camera = GPU_TRUE; 1565 + 1566 + target->use_depth_test = GPU_FALSE; 1567 + target->use_depth_write = GPU_TRUE; 1539 1568 1540 1569 target->context->line_thickness = 1.0f; 1541 1570 target->context->use_texturing = GPU_TRUE; ··· 1553 1582 cdata->last_viewport = target->viewport; 1554 1583 cdata->last_camera = target->camera; // Redundant due to applyTargetCamera(), below 1555 1584 cdata->last_camera_inverted = GPU_FALSE; 1585 + 1586 + cdata->last_depth_test = GPU_FALSE; 1587 + cdata->last_depth_write = GPU_TRUE; 1556 1588 1557 1589 #ifdef SDL_GPU_USE_OPENGL 1558 1590 glewExperimental = GL_TRUE; // Force GLEW to get exported functions instead of checking via extension string ··· 1904 1936 1905 1937 forceChangeBlendMode(renderer, cdata->last_blend_mode); 1906 1938 1939 + if(cdata->last_depth_test) 1940 + glEnable(GL_DEPTH_TEST); 1941 + else 1942 + glDisable(GL_DEPTH_TEST); 1943 + 1944 + glDepthMask(cdata->last_depth_write); 1945 + 1907 1946 forceChangeViewport(target, target->viewport); 1908 1947 1909 1948 if(cdata->last_image != NULL) ··· 4668 4707 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4669 4708 static void gpu_upload_modelviewprojection(GPU_Target* dest, GPU_Context* context) 4670 4709 { 4671 - GPU_CONTEXT_DATA* cdata = (GPU_CONTEXT_DATA*)context->data; 4672 4710 if(context->current_shader_block.modelViewProjection_loc >= 0) 4673 4711 { 4674 4712 float p[16]; ··· 4681 4719 if(dest->use_camera) 4682 4720 { 4683 4721 float cam_matrix[16]; 4684 - get_camera_matrix(cam_matrix, cdata->last_camera); 4722 + get_camera_matrix(cam_matrix); 4685 4723 4686 4724 GPU_MultiplyAndAssign(cam_matrix, p); 4687 4725 GPU_MatrixCopy(p, cam_matrix);
+3
tests/CMakeLists.txt
··· 142 142 add_executable(reinit-test reinit/main.c) 143 143 target_link_libraries (reinit-test ${TEST_LIBS}) 144 144 145 + add_executable(depth-test depth/main.c) 146 + target_link_libraries (depth-test ${TEST_LIBS}) 147 + 145 148 add_executable(video-test video/main.c) 146 149 target_link_libraries (video-test ${TEST_LIBS})
+89
tests/depth/main.c
··· 1 + #include "SDL.h" 2 + #include "SDL_gpu.h" 3 + #include <math.h> 4 + #include "common.h" 5 + 6 + #define IMAGE_FILE "data/test3.png" 7 + 8 + int main(int argc, char* argv[]) 9 + { 10 + GPU_Target* screen; 11 + 12 + printRenderers(); 13 + 14 + screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); 15 + if(screen == NULL) 16 + { 17 + GPU_LogError("Failed to init SDL_gpu.\n"); 18 + return -1; 19 + } 20 + 21 + printCurrentRenderer(); 22 + 23 + { 24 + Uint32 startTime; 25 + long frameCount; 26 + Uint8 done; 27 + SDL_Event event; 28 + GPU_Image* image; 29 + 30 + image = GPU_LoadImage(IMAGE_FILE); 31 + if(image == NULL) 32 + { 33 + GPU_LogError("Failed to load image.\n"); 34 + return -1; 35 + } 36 + 37 + startTime = SDL_GetTicks(); 38 + frameCount = 0; 39 + 40 + GPU_SetDepthTest(screen, 1); 41 + 42 + done = 0; 43 + while(!done) 44 + { 45 + while(SDL_PollEvent(&event)) 46 + { 47 + if(event.type == SDL_QUIT) 48 + done = 1; 49 + else if(event.type == SDL_KEYDOWN) 50 + { 51 + if(event.key.keysym.sym == SDLK_ESCAPE) 52 + done = 1; 53 + } 54 + } 55 + 56 + GPU_ClearRGB(screen, 200, 200, 200); 57 + 58 + // Images drawn left to right, but layered alternating. Positive z values are on top. 59 + GPU_Blit(image, NULL, screen, 150, 300); 60 + 61 + GPU_PushMatrix(); 62 + GPU_Translate(0, 0, 5); 63 + GPU_Blit(image, NULL, screen, 300, 300); 64 + 65 + GPU_Translate(0, 0, -10); 66 + GPU_Blit(image, NULL, screen, 450, 300); 67 + 68 + GPU_Translate(0, 0, 10); 69 + GPU_Blit(image, NULL, screen, 600, 300); 70 + GPU_PopMatrix(); 71 + 72 + GPU_Flip(screen); 73 + 74 + frameCount++; 75 + if(frameCount%500 == 0) 76 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 77 + } 78 + 79 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 80 + 81 + GPU_FreeImage(image); 82 + } 83 + 84 + GPU_Quit(); 85 + 86 + return 0; 87 + } 88 + 89 +