From 6e040b045ab1d94a877ab6f72f431c9b64e1121c Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 13 Feb 2018 18:18:04 +0100 Subject: GPU: add offscreen buffer drawing utility functions. --- source/blender/gpu/GPU_framebuffer.h | 6 +++-- source/blender/gpu/GPU_viewport.h | 1 + source/blender/gpu/intern/gpu_framebuffer.c | 42 +++++++++++++++++++++-------- source/blender/gpu/intern/gpu_viewport.c | 22 ++++++++++----- 4 files changed, 52 insertions(+), 19 deletions(-) (limited to 'source/blender/gpu') diff --git a/source/blender/gpu/GPU_framebuffer.h b/source/blender/gpu/GPU_framebuffer.h index c58d98c201e..31f24aa7c2e 100644 --- a/source/blender/gpu/GPU_framebuffer.h +++ b/source/blender/gpu/GPU_framebuffer.h @@ -83,14 +83,16 @@ void GPU_framebuffer_recursive_downsample( * - wrapper around framebuffer and texture for simple offscreen drawing * - changes size if graphics card can't support it */ -GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, bool high_bitdepth, char err_out[256]); +GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, + bool depth, bool high_bitdepth, char err_out[256]); void GPU_offscreen_free(GPUOffScreen *ofs); void GPU_offscreen_bind(GPUOffScreen *ofs, bool save); void GPU_offscreen_unbind(GPUOffScreen *ofs, bool restore); void GPU_offscreen_read_pixels(GPUOffScreen *ofs, int type, void *pixels); +void GPU_offscreen_blit(GPUOffScreen *ofs, int x, int y); int GPU_offscreen_width(const GPUOffScreen *ofs); int GPU_offscreen_height(const GPUOffScreen *ofs); -int GPU_offscreen_color_texture(const GPUOffScreen *ofs); +struct GPUTexture *GPU_offscreen_color_texture(const GPUOffScreen *ofs); void GPU_offscreen_viewport_data_get( GPUOffScreen *ofs, diff --git a/source/blender/gpu/GPU_viewport.h b/source/blender/gpu/GPU_viewport.h index 580ff64befb..dff7e278ae1 100644 --- a/source/blender/gpu/GPU_viewport.h +++ b/source/blender/gpu/GPU_viewport.h @@ -98,6 +98,7 @@ typedef struct ViewportEngineData_Info { GPUViewport *GPU_viewport_create(void); void GPU_viewport_bind(GPUViewport *viewport, const rcti *rect); void GPU_viewport_unbind(GPUViewport *viewport); +void GPU_viewport_draw_to_screen(GPUViewport *viewport, const rcti *rect); void GPU_viewport_free(GPUViewport *viewport); GPUViewport *GPU_viewport_create_from_offscreen(struct GPUOffScreen *ofs); diff --git a/source/blender/gpu/intern/gpu_framebuffer.c b/source/blender/gpu/intern/gpu_framebuffer.c index 7b1f5c80246..2c38319906c 100644 --- a/source/blender/gpu/intern/gpu_framebuffer.c +++ b/source/blender/gpu/intern/gpu_framebuffer.c @@ -641,7 +641,7 @@ struct GPUOffScreen { GPUTexture *depth; }; -GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, bool high_bitdepth, char err_out[256]) +GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, bool depth, bool high_bitdepth, char err_out[256]) { GPUOffScreen *ofs; @@ -663,15 +663,17 @@ GPUOffScreen *GPU_offscreen_create(int width, int height, int samples, bool high } } - ofs->depth = GPU_texture_create_depth_with_stencil_multisample(width, height, samples, err_out); - if (!ofs->depth) { - GPU_offscreen_free(ofs); - return NULL; - } + if (depth) { + ofs->depth = GPU_texture_create_depth_with_stencil_multisample(width, height, samples, err_out); + if (!ofs->depth) { + GPU_offscreen_free(ofs); + return NULL; + } - if (!GPU_framebuffer_texture_attach(ofs->fb, ofs->depth, 0, 0)) { - GPU_offscreen_free(ofs); - return NULL; + if (!GPU_framebuffer_texture_attach(ofs->fb, ofs->depth, 0, 0)) { + GPU_offscreen_free(ofs); + return NULL; + } } if (high_bitdepth) { @@ -731,6 +733,24 @@ void GPU_offscreen_unbind(GPUOffScreen *ofs, bool restore) glEnable(GL_SCISSOR_TEST); } +void GPU_offscreen_blit(GPUOffScreen *ofs, int x, int y) +{ + const int w = GPU_texture_width(ofs->color); + const int h = GPU_texture_height(ofs->color); + + glBindFramebuffer(GL_READ_FRAMEBUFFER, ofs->fb->object); + GLenum status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER); + + if (status == GL_FRAMEBUFFER_COMPLETE) { + glBlitFramebuffer(0, 0, w, h, x, y, x + w, y + h, GL_COLOR_BUFFER_BIT, GL_NEAREST); + } + else { + gpu_print_framebuffer_error(status, NULL); + } + + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); +} + void GPU_offscreen_read_pixels(GPUOffScreen *ofs, int type, void *pixels) { const int w = GPU_texture_width(ofs->color); @@ -818,9 +838,9 @@ int GPU_offscreen_height(const GPUOffScreen *ofs) return GPU_texture_height(ofs->color); } -int GPU_offscreen_color_texture(const GPUOffScreen *ofs) +GPUTexture *GPU_offscreen_color_texture(const GPUOffScreen *ofs) { - return GPU_texture_opengl_bindcode(ofs->color); + return ofs->color; } /* only to be used by viewport code! */ diff --git a/source/blender/gpu/intern/gpu_viewport.c b/source/blender/gpu/intern/gpu_viewport.c index 3ef53b3a6c3..fc045805874 100644 --- a/source/blender/gpu/intern/gpu_viewport.c +++ b/source/blender/gpu/intern/gpu_viewport.c @@ -474,7 +474,7 @@ cleanup: GPU_framebuffer_slots_bind(dfbl->default_fb, 0); } -static void draw_ofs_to_screen(GPUViewport *viewport) +static void draw_ofs_to_screen(GPUViewport *viewport, const rcti *rect) { DefaultTextureList *dtxl = viewport->txl; @@ -483,6 +483,9 @@ static void draw_ofs_to_screen(GPUViewport *viewport) const float w = (float)GPU_texture_width(color); const float h = (float)GPU_texture_height(color); + BLI_assert(w == BLI_rcti_size_x(rect) + 1); + BLI_assert(h == BLI_rcti_size_y(rect) + 1); + Gwn_VertFormat *format = immVertexFormat(); unsigned int texcoord = GWN_vertformat_attr_add(format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); @@ -495,16 +498,16 @@ static void draw_ofs_to_screen(GPUViewport *viewport) immBegin(GWN_PRIM_TRI_STRIP, 4); immAttrib2f(texcoord, 0.0f, 0.0f); - immVertex2f(pos, 0.0f, 0.0f); + immVertex2f(pos, rect->xmin, rect->ymin); immAttrib2f(texcoord, 1.0f, 0.0f); - immVertex2f(pos, w, 0.0f); + immVertex2f(pos, rect->xmin + w, rect->ymin); immAttrib2f(texcoord, 0.0f, 1.0f); - immVertex2f(pos, 0.0f, h); + immVertex2f(pos, rect->xmin, rect->ymin + h); immAttrib2f(texcoord, 1.0f, 1.0f); - immVertex2f(pos, w, h); + immVertex2f(pos, rect->xmin + w, rect->ymin + h); immEnd(); @@ -523,9 +526,16 @@ void GPU_viewport_unbind(GPUViewport *viewport) glEnable(GL_SCISSOR_TEST); glDisable(GL_DEPTH_TEST); + } +} +void GPU_viewport_draw_to_screen(GPUViewport *viewport, const rcti *rect) +{ + DefaultFramebufferList *dfbl = viewport->fbl; + + if (dfbl->default_fb) { /* This might be bandwidth limiting */ - draw_ofs_to_screen(viewport); + draw_ofs_to_screen(viewport, rect); } } -- cgit v1.2.3