1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-06 10:45:33 +00:00

Render MyGUI in background thread, i.e. refactor to get rid of DYNAMIC DataVariance

This commit is contained in:
scrawl 2015-05-05 02:38:59 +02:00
parent ee60df1e27
commit 100d6e5fff
3 changed files with 26 additions and 16 deletions

View file

@ -144,8 +144,6 @@ namespace MWRender
zNear = 5.f; zNear = 5.f;
zFar = mViewDistance; zFar = mViewDistance;
mViewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar); mViewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
mViewer.getCamera()->setCullMask(mViewer.getCamera()->getCullMask() & (~Mask_GUI));
} }
RenderingManager::~RenderingManager() RenderingManager::~RenderingManager()

View file

@ -7,10 +7,7 @@
#include <osg/Drawable> #include <osg/Drawable>
#include <osg/Geode> #include <osg/Geode>
#include <osg/PolygonMode>
#include <osg/BlendFunc> #include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/TexEnv>
#include <osg/Texture2D> #include <osg/Texture2D>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
@ -141,7 +138,9 @@ class Drawable : public osg::Drawable {
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);
for (std::vector<Batch>::const_iterator it = mBatchVector.begin(); it != mBatchVector.end(); ++it) mReadFrom = (mReadFrom+1)%2;
const std::vector<Batch>& vec = mBatchVector[mReadFrom];
for (std::vector<Batch>::const_iterator it = vec.begin(); it != vec.end(); ++it)
{ {
const Batch& batch = *it; const Batch& batch = *it;
osg::VertexBufferObject *vbo = batch.mVertexBuffer; osg::VertexBufferObject *vbo = batch.mVertexBuffer;
@ -174,7 +173,10 @@ class Drawable : public osg::Drawable {
} }
public: public:
Drawable(osgMyGUI::RenderManager *parent = nullptr) : mParent(parent) Drawable(osgMyGUI::RenderManager *parent = nullptr)
: mParent(parent)
, mWriteTo(0)
, mReadFrom(0)
{ {
setSupportsDisplayList(false); setSupportsDisplayList(false);
@ -182,7 +184,7 @@ public:
collectDrawCalls->setRenderManager(mParent); collectDrawCalls->setRenderManager(mParent);
setCullCallback(collectDrawCalls); setCullCallback(collectDrawCalls);
osg::ref_ptr<FrameUpdate> frameUpdate = new Drawable::FrameUpdate; osg::ref_ptr<FrameUpdate> frameUpdate = new FrameUpdate;
frameUpdate->setRenderManager(mParent); frameUpdate->setRenderManager(mParent);
setUpdateCallback(frameUpdate); setUpdateCallback(frameUpdate);
} }
@ -199,30 +201,35 @@ public:
osg::ref_ptr<osg::Texture2D> mTexture; osg::ref_ptr<osg::Texture2D> mTexture;
osg::ref_ptr<osg::VertexBufferObject> mVertexBuffer; osg::ref_ptr<osg::VertexBufferObject> mVertexBuffer;
// need to hold on to this too as the mVertexBuffer does not hold a ref to its own array
osg::ref_ptr<osg::UByteArray> mArray;
size_t mVertexCount; size_t mVertexCount;
}; };
void addBatch(const Batch& batch) void addBatch(const Batch& batch)
{ {
mBatchVector.push_back(batch); mBatchVector[mWriteTo].push_back(batch);
} }
void clear() void clear()
{ {
mBatchVector.clear(); mWriteTo = (mWriteTo+1)%2;
mBatchVector[mWriteTo].clear();
} }
META_Object(osgMyGUI, Drawable) META_Object(osgMyGUI, Drawable)
private: private:
std::vector<Batch> mBatchVector; std::vector<Batch> mBatchVector[2];
int mWriteTo;
mutable int mReadFrom;
}; };
class OSGVertexBuffer : public MyGUI::IVertexBuffer class OSGVertexBuffer : public MyGUI::IVertexBuffer
{ {
osg::ref_ptr<osg::VertexBufferObject> mBuffer; osg::ref_ptr<osg::VertexBufferObject> mBuffer;
osg::ref_ptr<osg::UByteArray> mVertexArray; osg::ref_ptr<osg::UByteArray> mVertexArray;
std::vector<MyGUI::Vertex> mLockedData;
size_t mNeedVertexCount; size_t mNeedVertexCount;
@ -241,6 +248,7 @@ public:
void create(); void create();
osg::VertexBufferObject *getBuffer() const { return mBuffer.get(); } osg::VertexBufferObject *getBuffer() const { return mBuffer.get(); }
osg::UByteArray *getArray() const { return mVertexArray.get(); }
}; };
OSGVertexBuffer::OSGVertexBuffer() OSGVertexBuffer::OSGVertexBuffer()
@ -270,6 +278,12 @@ size_t OSGVertexBuffer::getVertexCount()
MyGUI::Vertex *OSGVertexBuffer::lock() MyGUI::Vertex *OSGVertexBuffer::lock()
{ {
// Force recreating the buffer, to make sure we are not modifying a buffer currently
// queued for rendering in the last frame's draw thread.
// a more efficient solution might be double buffering
destroy();
create();
MYGUI_PLATFORM_ASSERT(mBuffer.valid(), "Vertex buffer is not created"); MYGUI_PLATFORM_ASSERT(mBuffer.valid(), "Vertex buffer is not created");
mVertexArray->resize(mNeedVertexCount * sizeof(MyGUI::Vertex)); mVertexArray->resize(mNeedVertexCount * sizeof(MyGUI::Vertex));
@ -297,6 +311,7 @@ void OSGVertexBuffer::create()
mBuffer = new osg::VertexBufferObject; mBuffer = new osg::VertexBufferObject;
mBuffer->setDataVariance(osg::Object::DYNAMIC); mBuffer->setDataVariance(osg::Object::DYNAMIC);
mBuffer->setUsage(GL_DYNAMIC_DRAW); mBuffer->setUsage(GL_DYNAMIC_DRAW);
// NB mBuffer does not own the array
mBuffer->setArray(0, mVertexArray.get()); mBuffer->setArray(0, mVertexArray.get());
} }
@ -338,7 +353,6 @@ void RenderManager::initialise()
mUpdate = false; mUpdate = false;
mDrawable = new Drawable(this); mDrawable = new Drawable(this);
mDrawable->setDataVariance(osg::Object::DYNAMIC);
osg::ref_ptr<osg::Geode> geode = new osg::Geode; osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(mDrawable.get()); geode->addDrawable(mDrawable.get());
@ -394,6 +408,7 @@ void RenderManager::doRender(MyGUI::IVertexBuffer *buffer, MyGUI::ITexture *text
Drawable::Batch batch; Drawable::Batch batch;
batch.mVertexCount = count; batch.mVertexCount = count;
batch.mVertexBuffer = static_cast<OSGVertexBuffer*>(buffer)->getBuffer(); batch.mVertexBuffer = static_cast<OSGVertexBuffer*>(buffer)->getBuffer();
batch.mArray = static_cast<OSGVertexBuffer*>(buffer)->getArray();
if (texture) if (texture)
batch.mTexture = static_cast<OSGTexture*>(texture)->getTexture(); batch.mTexture = static_cast<OSGTexture*>(texture)->getTexture();

View file

@ -46,9 +46,6 @@ class RenderManager : public MyGUI::RenderManager, public MyGUI::IRenderTarget
osg::ref_ptr<osg::Camera> mGuiRoot; osg::ref_ptr<osg::Camera> mGuiRoot;
// Only valid during drawFrame()!
osg::RenderInfo *mRenderInfo;
void destroyAllResources(); void destroyAllResources();
public: public: