2016-10-13 04:22:28 +00:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2006 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file blender/gpu/intern/gpu_viewport.c
|
|
|
|
* \ingroup gpu
|
|
|
|
*
|
|
|
|
* System that manages viewport drawing.
|
|
|
|
*/
|
|
|
|
|
2016-10-21 20:50:00 +00:00
|
|
|
#include "GPU_glew.h"
|
|
|
|
#include "GPU_immediate.h"
|
2016-10-13 04:22:28 +00:00
|
|
|
#include "GPU_viewport.h"
|
2016-10-21 20:50:00 +00:00
|
|
|
#include "GPU_texture.h"
|
2016-10-13 04:22:28 +00:00
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
struct GPUViewport {
|
|
|
|
float pad[4];
|
2016-10-21 20:50:00 +00:00
|
|
|
|
|
|
|
/* debug */
|
|
|
|
GPUTexture *debug_depth;
|
|
|
|
int debug_width, debug_height;
|
2016-10-13 04:22:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GPUViewport *GPU_viewport_create(void)
|
|
|
|
{
|
|
|
|
GPUViewport *viewport = MEM_callocN(sizeof(GPUViewport), "GPUViewport");
|
|
|
|
return viewport;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_viewport_free(GPUViewport *viewport)
|
|
|
|
{
|
2016-10-21 20:50:00 +00:00
|
|
|
GPU_viewport_debug_depth_free(viewport);
|
2016-10-13 04:22:28 +00:00
|
|
|
MEM_freeN(viewport);
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:50:00 +00:00
|
|
|
/****************** debug ********************/
|
|
|
|
|
|
|
|
bool GPU_viewport_debug_depth_create(GPUViewport *viewport, int width, int height, int samples, char err_out[256])
|
|
|
|
{
|
|
|
|
viewport->debug_depth = GPU_texture_create_2D(width, height, NULL, GPU_HDR_HALF_FLOAT, err_out);
|
|
|
|
return (viewport->debug_depth != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_viewport_debug_depth_free(GPUViewport *viewport)
|
|
|
|
{
|
|
|
|
if (viewport->debug_depth != NULL) {
|
|
|
|
MEM_freeN(viewport->debug_depth);
|
|
|
|
viewport->debug_depth = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_viewport_debug_depth_store(GPUViewport *viewport, const int x, const int y)
|
|
|
|
{
|
|
|
|
const int w = GPU_texture_width(viewport->debug_depth);
|
|
|
|
const int h = GPU_texture_height(viewport->debug_depth);
|
|
|
|
|
|
|
|
GPU_texture_bind(viewport->debug_depth, 0);
|
|
|
|
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, x, y, w, h, 0);
|
|
|
|
GPU_texture_unbind(viewport->debug_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_viewport_debug_depth_draw(GPUViewport *viewport, const float znear, const float zfar)
|
|
|
|
{
|
|
|
|
const float w = (float)GPU_texture_width(viewport->debug_depth);
|
|
|
|
const float h = (float)GPU_texture_height(viewport->debug_depth);
|
|
|
|
|
|
|
|
const int activeTex = GL_TEXTURE0;
|
|
|
|
glActiveTexture(activeTex);
|
|
|
|
|
|
|
|
VertexFormat *format = immVertexFormat();
|
|
|
|
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
|
|
|
|
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
|
|
|
|
|
|
|
|
immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_DEPTH);
|
|
|
|
|
|
|
|
GPU_texture_bind(viewport->debug_depth, 0);
|
|
|
|
|
|
|
|
immUniform1f("znear", znear);
|
|
|
|
immUniform1f("zfar", zfar);
|
|
|
|
immUniform1i("image", activeTex);
|
|
|
|
|
|
|
|
immBegin(GL_QUADS, 4);
|
|
|
|
|
|
|
|
immAttrib2f(texcoord, 0.0f, 0.0f);
|
|
|
|
immVertex2f(pos, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
immAttrib2f(texcoord, 1.0f, 0.0f);
|
|
|
|
immVertex2f(pos, w, 0.0f);
|
|
|
|
|
|
|
|
immAttrib2f(texcoord, 1.0f, 1.0f);
|
|
|
|
immVertex2f(pos, w, h);
|
|
|
|
|
|
|
|
immAttrib2f(texcoord, 0.0f, 1.0f);
|
|
|
|
immVertex2f(pos, 0.0f, h);
|
|
|
|
|
|
|
|
immEnd();
|
|
|
|
|
|
|
|
GPU_texture_unbind(viewport->debug_depth);
|
|
|
|
|
|
|
|
immUnbindProgram();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_viewport_debug_depth_width(const GPUViewport *viewport)
|
|
|
|
{
|
|
|
|
return GPU_texture_width(viewport->debug_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_viewport_debug_depth_height(const GPUViewport *viewport)
|
|
|
|
{
|
|
|
|
return GPU_texture_height(viewport->debug_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPU_viewport_debug_depth_is_valid(GPUViewport *viewport)
|
|
|
|
{
|
|
|
|
return viewport->debug_depth != NULL;
|
|
|
|
}
|