From c1939de49bb7b90303695fbfc8e77f280c48b66f Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 10 Oct 2013 12:37:03 -0700 Subject: [PATCH] added z-stencil buffers to GL and made a GS_MAX_TEXTURES macro --- libobs-d3d11/d3d11-subsystem.cpp | 10 +-- libobs-d3d11/d3d11-subsystem.hpp | 8 +-- libobs-opengl/gl-helpers.h | 6 ++ libobs-opengl/gl-shader.c | 6 +- libobs-opengl/gl-subsystem.c | 9 --- libobs-opengl/gl-subsystem.h | 20 ++++-- libobs-opengl/gl-zstencil.c | 65 +++++++++++++++++++ libobs/graphics/graphics.h | 2 + vs/2010/libobs-opengl/libobs-opengl.vcxproj | 1 + .../libobs-opengl.vcxproj.filters | 3 + 10 files changed, 104 insertions(+), 26 deletions(-) create mode 100644 libobs-opengl/gl-zstencil.c diff --git a/libobs-d3d11/d3d11-subsystem.cpp b/libobs-d3d11/d3d11-subsystem.cpp index 0a955c3f9..aa555ac69 100644 --- a/libobs-d3d11/d3d11-subsystem.cpp +++ b/libobs-d3d11/d3d11-subsystem.cpp @@ -397,7 +397,7 @@ gs_device::gs_device(gs_init_data *data) memset(&viewport, 0, sizeof(viewport)); - for (size_t i = 0; i < MAX_TEXTURES; i++) { + for (size_t i = 0; i < GS_MAX_TEXTURES; i++) { curTextures[i] = NULL; curSamplers[i] = NULL; } @@ -789,17 +789,17 @@ void device_load_vertexshader(device_t device, shader_t vertshader) static inline void clear_textures(device_t device) { - ID3D11ShaderResourceView *views[MAX_TEXTURES]; + ID3D11ShaderResourceView *views[GS_MAX_TEXTURES]; memset(views, 0, sizeof(views)); memset(device->curTextures, 0, sizeof(device->curTextures)); - device->context->PSSetShaderResources(0, MAX_TEXTURES, views); + device->context->PSSetShaderResources(0, GS_MAX_TEXTURES, views); } void device_load_pixelshader(device_t device, shader_t pixelshader) { ID3D11PixelShader *shader = NULL; ID3D11Buffer *constants = NULL; - ID3D11SamplerState *states[MAX_TEXTURES]; + ID3D11SamplerState *states[GS_MAX_TEXTURES]; if (device->curPixelShader == pixelshader) return; @@ -825,7 +825,7 @@ void device_load_pixelshader(device_t device, shader_t pixelshader) device->curPixelShader = ps; device->context->PSSetShader(shader, NULL, 0); device->context->PSSetConstantBuffers(0, 1, &constants); - device->context->PSSetSamplers(0, MAX_TEXTURES, states); + device->context->PSSetSamplers(0, GS_MAX_TEXTURES, states); } void device_load_defaultsamplerstate(device_t device, bool b_3d, diff --git a/libobs-d3d11/d3d11-subsystem.hpp b/libobs-d3d11/d3d11-subsystem.hpp index 6b436fd0d..f3baec812 100644 --- a/libobs-d3d11/d3d11-subsystem.hpp +++ b/libobs-d3d11/d3d11-subsystem.hpp @@ -47,8 +47,6 @@ using namespace std; * "public" and "private" does not matter at all for this subproject. */ -#define MAX_TEXTURES 8 - static inline uint32_t GetWinVer() { OSVERSIONINFO ovi; @@ -420,7 +418,7 @@ struct gs_pixel_shader : gs_shader { size_t i; for (i = 0; i < samplers.size(); i++) states[i] = samplers[i].sampler.state; - for (; i < MAX_TEXTURES; i++) + for (; i < GS_MAX_TEXTURES; i++) states[i] = NULL; } @@ -574,8 +572,8 @@ struct gs_device { gs_texture_2d *curRenderTarget; gs_zstencil_buffer *curZStencilBuffer; int curRenderSide; - gs_texture *curTextures[MAX_TEXTURES]; - gs_sampler_state *curSamplers[MAX_TEXTURES]; + gs_texture *curTextures[GS_MAX_TEXTURES]; + gs_sampler_state *curSamplers[GS_MAX_TEXTURES]; gs_vertex_buffer *curVertexBuffer; gs_vertex_shader *curVertexShader; gs_index_buffer *curIndexBuffer; diff --git a/libobs-opengl/gl-helpers.h b/libobs-opengl/gl-helpers.h index efbf05884..9c9404891 100644 --- a/libobs-opengl/gl-helpers.h +++ b/libobs-opengl/gl-helpers.h @@ -60,6 +60,12 @@ static inline bool gl_bind_buffer(GLenum target, GLuint buffer) return gl_success("glBindBuffer"); } +static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer) +{ + glBindRenderbuffer(target, buffer); + return gl_success("glBindRendebuffer"); +} + extern bool upload_face(GLenum type, uint32_t num_levels, GLenum format, GLint internal_format, bool compressed, uint32_t width, uint32_t height, uint32_t size, void ***p_data); diff --git a/libobs-opengl/gl-shader.c b/libobs-opengl/gl-shader.c index 2dd575de8..b8e84e62d 100644 --- a/libobs-opengl/gl-shader.c +++ b/libobs-opengl/gl-shader.c @@ -29,7 +29,6 @@ static inline void shader_param_init(struct shader_param *param) memset(param, 0, sizeof(struct shader_param)); } - static inline void shader_param_free(struct shader_param *param) { bfree(param->name); @@ -50,6 +49,7 @@ static void gl_get_program_info(GLuint program, char **error_string) errors = bmalloc(info_len+1); memset(errors, 0, info_len+1); glGetProgramInfoLog(program, info_len, &chars_written, errors); + gl_success("glGetProgramInfoLog"); *error_string = errors; } @@ -154,7 +154,8 @@ static struct gs_shader *shader_create(device_t device, enum shader_type type, bool success = true; memset(shader, 0, sizeof(struct gs_shader)); - shader->type = type; + shader->device = device; + shader->type = type; gl_shader_parser_init(&glsp); if (!gl_shader_parse(&glsp, shader_str, file)) @@ -306,6 +307,7 @@ void shader_setvec4(shader_t shader, sparam_t param, void shader_settexture(shader_t shader, sparam_t param, texture_t val) { + param->texture = val; } static void shader_setval_data(shader_t shader, sparam_t param, diff --git a/libobs-opengl/gl-subsystem.c b/libobs-opengl/gl-subsystem.c index c5194fd7e..e2f49363a 100644 --- a/libobs-opengl/gl-subsystem.c +++ b/libobs-opengl/gl-subsystem.c @@ -100,11 +100,6 @@ texture_t device_create_volumetexture(device_t device, uint32_t width, return NULL; } -zstencil_t device_create_zstencil(device_t device, uint32_t width, - uint32_t height, enum gs_zstencil_format format) -{ -} - stagesurf_t device_create_stagesurface(device_t device, uint32_t width, uint32_t height, enum gs_color_format color_format) { @@ -393,10 +388,6 @@ void stagesurface_unmap(stagesurf_t stagesurf) { } -void zstencil_destroy(zstencil_t zstencil) -{ -} - void samplerstate_destroy(samplerstate_t samplerstate) { } diff --git a/libobs-opengl/gl-subsystem.h b/libobs-opengl/gl-subsystem.h index a73586e69..6885966db 100644 --- a/libobs-opengl/gl-subsystem.h +++ b/libobs-opengl/gl-subsystem.h @@ -75,7 +75,7 @@ static inline GLint convert_gs_internal_format(enum gs_color_format format) } } -static inline GLint convert_zstencil_format(enum gs_zstencil_format format) +static inline GLenum convert_zstencil_format(enum gs_zstencil_format format) { switch (format) { case GS_Z16: return GL_DEPTH_COMPONENT16; @@ -171,6 +171,8 @@ struct shader_param { size_t sampler_id; int array_count; + struct gs_texture *texture; + DARRAY(uint8_t) cur_value; DARRAY(uint8_t) def_value; bool changed; @@ -216,18 +218,26 @@ struct gs_texture_cube { uint32_t size; }; +struct gs_zstencil_buffer { + device_t device; + GLuint buffer; + GLenum format; +}; + struct gs_swap_chain { - device_t device; + device_t device; struct gl_windowinfo *wi; struct gs_init_data info; }; struct gs_device { - struct gl_platform *plat; + struct gl_platform *plat; - struct gs_swap_chain *cur_swap; - int cur_render_side; struct gs_texture *cur_render_texture; + int cur_render_side; + struct gs_texture *cur_textures[GS_MAX_TEXTURES]; + struct gs_sampler *cur_samplers[GS_MAX_TEXTURES]; + struct gs_swap_chain *cur_swap; }; extern struct gl_platform *gl_platform_create(device_t device, diff --git a/libobs-opengl/gl-zstencil.c b/libobs-opengl/gl-zstencil.c new file mode 100644 index 000000000..23b1bcd9e --- /dev/null +++ b/libobs-opengl/gl-zstencil.c @@ -0,0 +1,65 @@ +/****************************************************************************** + Copyright (C) 2013 by Hugh Bailey + + 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 3 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, see . +******************************************************************************/ + +#include "gl-subsystem.h" + +static bool gl_init_zsbuffer(struct gs_zstencil_buffer *zs, uint32_t width, + uint32_t height) +{ + glGenRenderbuffers(1, &zs->buffer); + if (!gl_success("glGenRenderbuffers")) + return false; + + if (!gl_bind_renderbuffer(GL_RENDERBUFFER, zs->buffer)) + return false; + + glRenderbufferStorage(GL_RENDERBUFFER, zs->format, width, height); + if (!gl_success("glRenderbufferStorage")) + return false; + + gl_bind_renderbuffer(GL_RENDERBUFFER, 0); + return true; +} + +zstencil_t device_create_zstencil(device_t device, uint32_t width, + uint32_t height, enum gs_zstencil_format format) +{ + struct gs_zstencil_buffer *zs; + + zs = bmalloc(sizeof(struct gs_zstencil_buffer)); + memset(zs, 0, sizeof(struct gs_zstencil_buffer)); + zs->format = convert_zstencil_format(format); + + if (!gl_init_zsbuffer(zs, width, height)) { + zstencil_destroy(zs); + return NULL; + } + + return zs; +} + +void zstencil_destroy(zstencil_t zs) +{ + if (zs) { + if (zs->buffer) { + glDeleteRenderbuffers(1, &zs->buffer); + gl_success("glDeleteRenderbuffers"); + } + + bfree(zs); + } +} diff --git a/libobs/graphics/graphics.h b/libobs/graphics/graphics.h index ef0900db7..a1e004149 100644 --- a/libobs/graphics/graphics.h +++ b/libobs/graphics/graphics.h @@ -32,6 +32,8 @@ extern "C" { #endif +#define GS_MAX_TEXTURES 8 + struct vec2; struct vec3; struct vec4; diff --git a/vs/2010/libobs-opengl/libobs-opengl.vcxproj b/vs/2010/libobs-opengl/libobs-opengl.vcxproj index b2f48c881..dff697c92 100644 --- a/vs/2010/libobs-opengl/libobs-opengl.vcxproj +++ b/vs/2010/libobs-opengl/libobs-opengl.vcxproj @@ -168,6 +168,7 @@ + diff --git a/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters b/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters index 666c8c137..6ab05779e 100644 --- a/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters +++ b/vs/2010/libobs-opengl/libobs-opengl.vcxproj.filters @@ -50,5 +50,8 @@ Source Files + + Source Files + \ No newline at end of file