summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
b23a9af)
The global context state is now held in Device.
The changes mask in PipelineState was also moved to the backend class,
because pipeline state is applied very differently in Vulkan.
#include <msp/gl/extensions/oes_mapbuffer.h>
#include "buffer.h"
#include "buffer_backend.h"
#include <msp/gl/extensions/oes_mapbuffer.h>
#include "buffer.h"
#include "buffer_backend.h"
using namespace std;
namespace Msp {
namespace GL {
using namespace std;
namespace Msp {
namespace GL {
-OpenGLBuffer *OpenGLBuffer::scratch_binding = 0;
-
OpenGLBuffer::OpenGLBuffer()
{
static Require _req(ARB_vertex_buffer_object);
OpenGLBuffer::OpenGLBuffer()
{
static Require _req(ARB_vertex_buffer_object);
OpenGLBuffer::~OpenGLBuffer()
{
OpenGLBuffer::~OpenGLBuffer()
{
- if(this==scratch_binding)
+ if(this==Device::get_current().get_state().scratch_buffer)
unbind_scratch();
if(id)
glDeleteBuffers(1, &id);
unbind_scratch();
if(id)
glDeleteBuffers(1, &id);
void OpenGLBuffer::bind_scratch()
{
void OpenGLBuffer::bind_scratch()
{
+ const OpenGLBuffer *&scratch_binding = Device::get_current().get_state().scratch_buffer;
if(scratch_binding!=this)
{
glBindBuffer(GL_ARRAY_BUFFER, id);
if(scratch_binding!=this)
{
glBindBuffer(GL_ARRAY_BUFFER, id);
void OpenGLBuffer::unbind_scratch()
{
void OpenGLBuffer::unbind_scratch()
{
+ const OpenGLBuffer *&scratch_binding = Device::get_current().get_state().scratch_buffer;
if(scratch_binding)
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
if(scratch_binding)
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
protected:
unsigned id = 0;
protected:
unsigned id = 0;
- static OpenGLBuffer *scratch_binding;
-
OpenGLBuffer();
OpenGLBuffer(OpenGLBuffer &&);
~OpenGLBuffer();
OpenGLBuffer();
OpenGLBuffer(OpenGLBuffer &&);
~OpenGLBuffer();
feat.ext_texture_array = EXT_texture_array;
feat.uniform_binding_range = lim.max_uniform_bindings;
feat.texture_binding_range = lim.max_texture_bindings;
feat.ext_texture_array = EXT_texture_array;
feat.uniform_binding_range = lim.max_uniform_bindings;
feat.texture_binding_range = lim.max_texture_bindings;
+
+ state.bound_tex_targets.resize(lim.max_texture_bindings);
+ state.bound_uniform_blocks.resize(lim.max_uniform_bindings);
#ifndef MSP_GL_DEVICE_BACKEND_H_
#define MSP_GL_DEVICE_BACKEND_H_
#ifndef MSP_GL_DEVICE_BACKEND_H_
#define MSP_GL_DEVICE_BACKEND_H_
#include <msp/core/noncopyable.h>
#include <msp/graphics/glcontext.h>
namespace Msp {
namespace GL {
#include <msp/core/noncopyable.h>
#include <msp/graphics/glcontext.h>
namespace Msp {
namespace GL {
+class OpenGLBuffer;
+class OpenGLPipelineState;
+class OpenGLTexture;
+
+struct OpenGLDeviceState
+{
+ const OpenGLPipelineState *last_pipeline = 0;
+ std::vector<int> bound_tex_targets;
+ std::vector<char> bound_uniform_blocks;
+ unsigned restart_index = 0;
+ unsigned n_clip_distances = 0;
+ const OpenGLBuffer *scratch_buffer = 0;
+ const OpenGLTexture *scratch_texture = 0;
+};
+
class OpenGLDevice: public NonCopyable
{
protected:
Graphics::GLContext context;
class OpenGLDevice: public NonCopyable
{
protected:
Graphics::GLContext context;
+ OpenGLDeviceState state;
OpenGLDevice(Graphics::Window &, const Graphics::GLOptions &);
OpenGLDevice(Graphics::Window &, const Graphics::GLOptions &);
void fill_info();
Graphics::GLContext &get_context() { return context; }
void fill_info();
Graphics::GLContext &get_context() { return context; }
+
+public:
+ OpenGLDeviceState &get_state() { return state; }
};
using DeviceBackend = OpenGLDevice;
};
using DeviceBackend = OpenGLDevice;
namespace Msp {
namespace GL {
namespace Msp {
namespace GL {
-const OpenGLPipelineState *OpenGLPipelineState::last_applied = 0;
-vector<int> OpenGLPipelineState::bound_tex_targets;
-vector<char> OpenGLPipelineState::bound_uniform_blocks;
-unsigned OpenGLPipelineState::restart_index = 0;
-unsigned OpenGLPipelineState::n_clip_distances = 0;
-
-OpenGLPipelineState::OpenGLPipelineState()
-{
- if(bound_tex_targets.empty())
- bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings);
- if(bound_uniform_blocks.empty())
- bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings);
-}
-
OpenGLPipelineState::~OpenGLPipelineState()
{
OpenGLPipelineState::~OpenGLPipelineState()
{
- if(this==last_applied)
- last_applied = 0;
+ if(applied_to)
+ applied_to->get_state().last_pipeline = 0;
}
void OpenGLPipelineState::apply() const
{
}
void OpenGLPipelineState::apply() const
{
- if(!last_applied)
- OpenGLTexture::unbind_scratch();
+ const PipelineState &self = *static_cast<const PipelineState *>(this);
+ Device &device = Device::get_current();
+ unsigned mask = changes;
- apply(this==last_applied ? static_cast<const PipelineState *>(this)->changes : ~0U);
-}
+ if(applied_to && applied_to!=&device)
+ {
+ applied_to->get_state().last_pipeline = 0;
+ mask = ~0U;
+ }
-void OpenGLPipelineState::apply(unsigned mask) const
-{
- const PipelineState &self = *static_cast<const PipelineState *>(this);
+ OpenGLDeviceState &dev_state = device.get_state();
+ if(!dev_state.last_pipeline)
+ OpenGLTexture::unbind_scratch();
+
+ if(this!=dev_state.last_pipeline)
+ {
+ if(dev_state.last_pipeline)
+ dev_state.last_pipeline->applied_to = 0;
+ mask = ~0U;
+ }
if(mask&PipelineState::FRAMEBUFFER)
{
if(mask&PipelineState::FRAMEBUFFER)
{
glUseProgram(self.shprog ? self.shprog->id : 0);
unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
glUseProgram(self.shprog ? self.shprog->id : 0);
unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
- if(ncd!=n_clip_distances)
+ if(ncd!=dev_state.n_clip_distances)
- for(unsigned i=0; (i<ncd || i<n_clip_distances); ++i)
+ for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
{
if(i<ncd)
glEnable(GL_CLIP_PLANE0+i);
else
glDisable(GL_CLIP_PLANE0+i);
}
{
if(i<ncd)
glEnable(GL_CLIP_PLANE0+i);
else
glDisable(GL_CLIP_PLANE0+i);
}
- n_clip_distances = ncd;
+ dev_state.n_clip_distances = ncd;
if(u.binding>=0)
{
glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
if(u.binding>=0)
{
glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
- bound_uniform_blocks[u.binding] = 1;
+ dev_state.bound_uniform_blocks[u.binding] = 1;
}
else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
{
}
else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
{
else
{
glActiveTexture(GL_TEXTURE0+t.binding);
else
{
glActiveTexture(GL_TEXTURE0+t.binding);
- if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
- glBindTexture(bound_tex_targets[t.binding], 0);
+ if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
+ glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
glBindTexture(t.texture->target, t.texture->id);
}
glBindTexture(t.texture->target, t.texture->id);
}
- bound_tex_targets[t.binding] = t.texture->target;
+ dev_state.bound_tex_targets[t.binding] = t.texture->target;
glBindSampler(t.binding, t.sampler->id);
t.sampler->refresh();
glBindSampler(t.binding, t.sampler->id);
t.sampler->refresh();
vertex_setup->refresh();
unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
vertex_setup->refresh();
unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
+ if(ri!=dev_state.restart_index)
+ if(!dev_state.restart_index)
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(ri);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(ri);
+ dev_state.restart_index = ri;
- last_applied = this;
- self.changes &= ~mask;
+ applied_to = &device;
+ dev_state.last_pipeline = this;
+ changes = 0;
}
void OpenGLPipelineState::clear()
{
}
void OpenGLPipelineState::clear()
{
+ OpenGLDeviceState &dev_state = Device::get_current().get_state();
+ if(dev_state.last_pipeline)
{
glUseProgram(0);
glBindVertexArray(0);
{
glUseProgram(0);
glBindVertexArray(0);
- for(unsigned i=0; i<n_clip_distances; ++i)
+ for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
glDisable(GL_CLIP_PLANE0+i);
glDisable(GL_CLIP_PLANE0+i);
+ dev_state.n_clip_distances = 0;
- for(unsigned i=0; i<bound_tex_targets.size(); ++i)
- if(bound_tex_targets[i])
+ for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
+ if(dev_state.bound_tex_targets[i])
{
if(ARB_direct_state_access)
glBindTextureUnit(i, 0);
else
{
glActiveTexture(GL_TEXTURE0+i);
{
if(ARB_direct_state_access)
glBindTextureUnit(i, 0);
else
{
glActiveTexture(GL_TEXTURE0+i);
- glBindTexture(bound_tex_targets[i], 0);
+ glBindTexture(dev_state.bound_tex_targets[i], 0);
- bound_tex_targets[i] = 0;
+ dev_state.bound_tex_targets[i] = 0;
- for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
- if(bound_uniform_blocks[i])
+ for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
+ if(dev_state.bound_uniform_blocks[i])
{
glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
{
glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
- bound_uniform_blocks[i] = 0;
+ dev_state.bound_uniform_blocks[i] = 0;
}
glDisable(GL_DEPTH_TEST);
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
+ dev_state.last_pipeline->applied_to = 0;
+ dev_state.last_pipeline = 0;
namespace Msp {
namespace GL {
namespace Msp {
namespace GL {
class OpenGLPipelineState: public NonCopyable
{
friend class OpenGLCommands;
protected:
class OpenGLPipelineState: public NonCopyable
{
friend class OpenGLCommands;
protected:
- static const OpenGLPipelineState *last_applied;
- static std::vector<int> bound_tex_targets;
- static std::vector<char> bound_uniform_blocks;
- static unsigned restart_index;
- static unsigned n_clip_distances;
+ mutable Device *applied_to = 0;
+ mutable unsigned changes = 0;
+ OpenGLPipelineState() = default;
OpenGLPipelineState(OpenGLPipelineState &&) { }
~OpenGLPipelineState();
void apply() const;
OpenGLPipelineState(OpenGLPipelineState &&) { }
~OpenGLPipelineState();
void apply() const;
-private:
- void apply(unsigned) const;
-protected:
#include <msp/gl/extensions/arb_texture_swizzle.h>
#include <msp/gl/extensions/ext_framebuffer_object.h>
#include <msp/gl/extensions/khr_debug.h>
#include <msp/gl/extensions/arb_texture_swizzle.h>
#include <msp/gl/extensions/ext_framebuffer_object.h>
#include <msp/gl/extensions/khr_debug.h>
#include "gl.h"
#include "error.h"
#include "texture.h"
#include "gl.h"
#include "error.h"
#include "texture.h"
namespace Msp {
namespace GL {
namespace Msp {
namespace GL {
-OpenGLTexture *OpenGLTexture::scratch_binding = 0;
-
OpenGLTexture::OpenGLTexture(unsigned t):
target(t)
{
OpenGLTexture::OpenGLTexture(unsigned t):
target(t)
{
OpenGLTexture::~OpenGLTexture()
{
OpenGLTexture::~OpenGLTexture()
{
- if(this==scratch_binding)
+ if(this==Device::get_current().get_state().scratch_texture)
unbind_scratch();
if(id)
glDeleteTextures(1, &id);
unbind_scratch();
if(id)
glDeleteTextures(1, &id);
void OpenGLTexture::bind_scratch()
{
void OpenGLTexture::bind_scratch()
{
+ const OpenGLTexture *&scratch_binding = Device::get_current().get_state().scratch_texture;
if(!scratch_binding)
glActiveTexture(GL_TEXTURE0);
if(scratch_binding!=this)
if(!scratch_binding)
glActiveTexture(GL_TEXTURE0);
if(scratch_binding!=this)
void OpenGLTexture::unbind_scratch()
{
void OpenGLTexture::unbind_scratch()
{
+ const OpenGLTexture *&scratch_binding = Device::get_current().get_state().scratch_texture;
if(scratch_binding)
{
glBindTexture(scratch_binding->target, 0);
if(scratch_binding)
{
glBindTexture(scratch_binding->target, 0);
unsigned target;
std::string debug_name;
unsigned target;
std::string debug_name;
- static OpenGLTexture *scratch_binding;
-
OpenGLTexture(unsigned);
OpenGLTexture(OpenGLTexture &&);
~OpenGLTexture();
OpenGLTexture(unsigned);
OpenGLTexture(OpenGLTexture &&);
~OpenGLTexture();
const DepthTest *depth_test = 0;
const StencilTest *stencil_test = 0;
const Blend *blend = 0;
const DepthTest *depth_test = 0;
const StencilTest *stencil_test = 0;
const Blend *blend = 0;
- mutable unsigned changes = 0;
template<typename T>
void set(T &, T, unsigned);
template<typename T>
void set(T &, T, unsigned);