2013-01-13 21:39:25 -05:00
|
|
|
/*
|
2014-02-11 01:07:04 -05:00
|
|
|
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
|
2013-01-13 21:39:25 -05:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bgfx_p.h"
|
|
|
|
|
2014-03-30 02:12:15 -04:00
|
|
|
#if BX_PLATFORM_NACL & (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)
|
2013-09-30 23:09:09 -04:00
|
|
|
# include <bgfxplatform.h>
|
2013-01-13 21:39:25 -05:00
|
|
|
# include "renderer_gl.h"
|
|
|
|
|
|
|
|
namespace bgfx
|
|
|
|
{
|
2013-12-08 01:01:32 -05:00
|
|
|
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
|
2014-02-19 02:36:13 -05:00
|
|
|
# include "glimports.h"
|
2013-01-13 21:39:25 -05:00
|
|
|
|
2013-09-30 23:09:09 -04:00
|
|
|
void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/);
|
2013-01-13 21:39:25 -05:00
|
|
|
|
|
|
|
PP_CompletionCallback naclSwapComplete =
|
|
|
|
{
|
|
|
|
naclSwapCompleteCb,
|
|
|
|
NULL,
|
|
|
|
PP_COMPLETIONCALLBACK_FLAG_NONE
|
|
|
|
};
|
|
|
|
|
2013-09-30 23:09:09 -04:00
|
|
|
struct Ppapi
|
|
|
|
{
|
|
|
|
Ppapi()
|
|
|
|
: m_context(0)
|
|
|
|
, m_instance(0)
|
|
|
|
, m_instInterface(NULL)
|
|
|
|
, m_graphicsInterface(NULL)
|
|
|
|
, m_instancedArrays(NULL)
|
|
|
|
, m_postSwapBuffers(NULL)
|
|
|
|
, m_forceSwap(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-20 01:34:58 -05:00
|
|
|
bool setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers);
|
2013-09-30 23:09:09 -04:00
|
|
|
|
|
|
|
void resize(uint32_t _width, uint32_t _height, bool /*_vsync*/)
|
|
|
|
{
|
|
|
|
m_graphicsInterface->ResizeBuffers(m_context, _width, _height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap()
|
|
|
|
{
|
|
|
|
glSetCurrentContextPPAPI(m_context);
|
|
|
|
m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid() const
|
|
|
|
{
|
|
|
|
return 0 != m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP_Resource m_context;
|
|
|
|
PP_Instance m_instance;
|
|
|
|
const PPB_Instance* m_instInterface;
|
|
|
|
const PPB_Graphics3D* m_graphicsInterface;
|
|
|
|
const PPB_OpenGLES2InstancedArrays* m_instancedArrays;
|
|
|
|
PostSwapBuffersFn m_postSwapBuffers;
|
|
|
|
bool m_forceSwap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Ppapi s_ppapi;
|
|
|
|
|
|
|
|
void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
|
|
|
|
{
|
|
|
|
// For NaCl bgfx doesn't create render thread, but rendering is always
|
|
|
|
// multithreaded. Frame rendering is done on main thread, and context
|
|
|
|
// is initialized when PPAPI interfaces are set. Force swap is there to
|
|
|
|
// keep calling swap complete callback, so that initialization doesn't
|
|
|
|
// deadlock on semaphores.
|
|
|
|
if (s_ppapi.m_forceSwap)
|
|
|
|
{
|
|
|
|
s_ppapi.swap();
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:43:17 -05:00
|
|
|
renderFrame();
|
2013-09-30 23:09:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
|
|
|
|
{
|
|
|
|
s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
|
|
|
|
{
|
|
|
|
s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
|
2013-01-13 21:39:25 -05:00
|
|
|
{
|
2013-09-30 23:09:09 -04:00
|
|
|
s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount);
|
|
|
|
}
|
|
|
|
|
2014-01-20 01:34:58 -05:00
|
|
|
bool naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
|
2013-09-30 23:09:09 -04:00
|
|
|
{
|
2014-01-20 01:34:58 -05:00
|
|
|
return s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
|
2013-09-30 23:09:09 -04:00
|
|
|
}
|
|
|
|
|
2014-01-20 01:34:58 -05:00
|
|
|
bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
|
2013-09-30 23:09:09 -04:00
|
|
|
{
|
|
|
|
BX_TRACE("PPAPI Interfaces");
|
2014-01-19 17:58:05 -05:00
|
|
|
|
2013-09-30 23:09:09 -04:00
|
|
|
m_instance = _instance;
|
|
|
|
m_instInterface = _instInterface;
|
|
|
|
m_graphicsInterface = _graphicsInterface;
|
|
|
|
m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
|
|
|
|
m_postSwapBuffers = _postSwapBuffers;
|
|
|
|
|
2013-01-13 21:39:25 -05:00
|
|
|
int32_t attribs[] =
|
|
|
|
{
|
|
|
|
PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
|
|
|
|
PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
|
|
|
|
PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
|
|
|
|
PP_GRAPHICS3DATTRIB_SAMPLES, 0,
|
|
|
|
PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
|
2013-09-30 23:09:09 -04:00
|
|
|
PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
|
|
|
|
PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
|
2013-01-13 21:39:25 -05:00
|
|
|
PP_GRAPHICS3DATTRIB_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
|
2014-01-20 01:34:58 -05:00
|
|
|
if (0 == m_context)
|
|
|
|
{
|
|
|
|
BX_TRACE("Failed to create context!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-13 21:39:25 -05:00
|
|
|
m_instInterface->BindGraphics(m_instance, m_context);
|
|
|
|
glSetCurrentContextPPAPI(m_context);
|
|
|
|
m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
|
2013-09-30 23:09:09 -04:00
|
|
|
|
|
|
|
glVertexAttribDivisor = naclVertexAttribDivisor;
|
|
|
|
glDrawArraysInstanced = naclDrawArraysInstanced;
|
|
|
|
glDrawElementsInstanced = naclDrawElementsInstanced;
|
2014-01-20 01:34:58 -05:00
|
|
|
|
|
|
|
// Prevent render thread creation.
|
|
|
|
RenderFrame::Enum result = renderFrame();
|
|
|
|
return RenderFrame::NoContext == result;
|
2013-09-30 23:09:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::create(uint32_t _width, uint32_t _height)
|
|
|
|
{
|
2013-11-30 01:23:04 -05:00
|
|
|
BX_UNUSED(_width, _height);
|
2013-09-30 23:09:09 -04:00
|
|
|
BX_TRACE("GlContext::create");
|
2013-01-13 21:39:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::destroy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-30 23:09:09 -04:00
|
|
|
void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
|
2013-01-13 21:39:25 -05:00
|
|
|
{
|
2013-09-30 23:09:09 -04:00
|
|
|
s_ppapi.m_forceSwap = false;
|
|
|
|
s_ppapi.resize(_width, _height, _vsync);
|
2013-01-13 21:39:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::swap()
|
|
|
|
{
|
2013-09-30 23:09:09 -04:00
|
|
|
s_ppapi.swap();
|
2013-01-13 21:39:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlContext::import()
|
|
|
|
{
|
|
|
|
}
|
2013-09-30 23:09:09 -04:00
|
|
|
|
|
|
|
bool GlContext::isValid() const
|
|
|
|
{
|
|
|
|
return s_ppapi.isValid();
|
|
|
|
}
|
|
|
|
|
2013-01-13 21:39:25 -05:00
|
|
|
} // namespace bgfx
|
|
|
|
|
2014-03-30 02:12:15 -04:00
|
|
|
#endif // BX_PLATFORM_NACL & (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)
|