Merge branch 'master' of github.com:bkaradzic/bgfx

This commit is contained in:
bkaradzic 2013-07-21 14:46:23 -07:00
commit 9852949a38
14 changed files with 502 additions and 76 deletions

View file

@ -0,0 +1,241 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include <bx/bx.h>
#if BX_PLATFORM_IOS
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/CAEAGLLayer.h>
#include <bgfxplatform.h>
#include <bx/uint32_t.h>
#include <bx/thread.h>
#include "entry_p.h"
#include "dbg.h"
extern int _main_(int _argc, char** _argv);
namespace bgfx
{
void renderFrame();
}
namespace entry
{
struct MainThreadEntry
{
int m_argc;
char** m_argv;
static int32_t threadFunc(void* _userData);
};
struct Context
{
Context()
{
const char* argv[1] = { "ios" };
m_mte.m_argc = 1;
m_mte.m_argv = const_cast<char**>(argv);
m_eventQueue.postSizeEvent(720, 1024);
m_thread.init(MainThreadEntry::threadFunc, &m_mte);
}
~Context()
{
m_thread.shutdown();
}
MainThreadEntry m_mte;
bx::Thread m_thread;
EventQueue m_eventQueue;
};
static Context* s_ctx;
int32_t MainThreadEntry::threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
int32_t result = _main_(self->m_argc, self->m_argv);
return result;
}
const Event* poll()
{
return s_ctx->m_eventQueue.poll();
}
void release(const Event* _event)
{
s_ctx->m_eventQueue.release(_event);
}
void setWindowSize(uint32_t _width, uint32_t _height)
{
}
void toggleWindowFrame()
{
}
void setMouseLock(bool _lock)
{
}
} // namespace entry
using namespace entry;
@interface EAGLView : UIView
{
EAGLContext* m_context;
CADisplayLink* m_displayLink;
}
@property (nonatomic, retain) EAGLContext* m_context;
@end
@implementation EAGLView
@synthesize m_context;
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
- (id)initWithFrame:(CGRect)rect
{
self = [super initWithFrame:rect];
if (nil == self)
{
return nil;
}
CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
layer.opaque = true;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys
: [NSNumber numberWithBool:false]
, kEAGLDrawablePropertyRetainedBacking
, kEAGLColorFormatRGBA8
, kEAGLDrawablePropertyColorFormat
, nil
];
m_context = [ [EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
BX_CHECK(NULL != m_context, "Failed to create kEAGLRenderingAPIOpenGLES2 context.");
[EAGLContext setCurrentContext:m_context];
bgfx::iosSetEaglContext(m_context, layer);
return self;
}
- (void)start
{
if (nil == m_displayLink)
{
m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
}
- (void)stop
{
if (nil != m_displayLink)
{
[m_displayLink invalidate];
m_displayLink = nil;
}
}
- (void)renderFrame
{
bgfx::renderFrame();
}
@end
@interface AppDelegate : UIResponder<UIApplicationDelegate>
{
UIWindow* m_window;
EAGLView* m_view;
}
@property (nonatomic, retain) UIWindow* m_window;
@property (nonatomic, retain) EAGLView* m_view;
@end
@implementation AppDelegate
@synthesize m_window;
@synthesize m_view;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect rect = [ [UIScreen mainScreen] bounds];
m_window = [ [UIWindow alloc] initWithFrame: rect];
m_view = [ [EAGLView alloc] initWithFrame: rect];
[m_window addSubview: m_view];
[m_window makeKeyAndVisible];
s_ctx = new Context;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[m_view stop];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[m_view start];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[m_view stop];
}
- (void)dealloc
{
[m_window release];
[m_view release];
[super dealloc];
}
@end
int main(int _argc, char* _argv[])
{
NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
[pool release];
return exitCode;
}
#endif // BX_PLATFORM_IOS

View file

@ -217,7 +217,6 @@ namespace entry
EventQueue m_eventQueue;
bool m_exit;
};
static Context s_ctx;

View file

@ -20,6 +20,12 @@ namespace bgfx
void androidSetWindow(::ANativeWindow* _window);
} // namespace bgfx
#elif BX_PLATFORM_IOS
namespace bgfx
{
void iosSetEaglContext(void* _context, void* _layer);
} // namespace bgfx
#elif BX_PLATFORM_LINUX
# include <X11/Xlib.h>

View file

@ -13,7 +13,8 @@ all:
premake4 --file=premake/premake4.lua --gcc=mingw gmake
premake4 --file=premake/premake4.lua --gcc=linux gmake
premake4 --file=premake/premake4.lua --gcc=osx gmake
premake4 --file=premake/premake4.lua --gcc=ios gmake
premake4 --file=premake/premake4.lua --gcc=ios-arm gmake
premake4 --file=premake/premake4.lua --gcc=ios-simulator gmake
premake4 --file=premake/premake4.lua --gcc=qnx-arm gmake
premake4 --file=premake/premake4.lua xcode4
make -s --no-print-directory -C src
@ -86,11 +87,17 @@ osx-release64:
make -C .build/projects/gmake-osx config=release64
osx: osx-debug32 osx-release32 osx-debug64 osx-release64
ios-debug:
make -R -C .build/projects/gmake-ios config=debug
ios-release:
make -R -C .build/projects/gmake-ios config=release
ios: ios-debug ios-release
ios-arm-debug:
make -R -C .build/projects/gmake-ios-arm config=debug
ios-arm-release:
make -R -C .build/projects/gmake-ios-arm config=release
ios-arm: ios-arm-debug ios-arm-release
ios-simulator-debug:
make -R -C .build/projects/gmake-ios-simulator config=debug
ios-simulator-release:
make -R -C .build/projects/gmake-ios-simulator config=release
ios-simulator: ios-simulator-debug ios-simulator-release
qnx-arm-debug:
make -R -C .build/projects/gmake-qnx-arm config=debug

View file

@ -30,12 +30,12 @@ project "bgfx"
"$(DXSDK_DIR)/include",
}
configuration { "osx or ios" }
configuration { "osx or ios*" }
files {
BGFX_DIR .. "src/**.mm",
}
configuration { "vs* or linux or mingw or osx or ios" }
configuration { "vs* or linux or mingw or osx or ios*" }
includedirs {
--nacl has GLES2 headers modified...
BGFX_DIR .. "3rdparty/glext",

View file

@ -102,7 +102,8 @@ function exampleProject(_name, _uuid)
"OpenGL.framework",
}
configuration { "ios" }
configuration { "ios*" }
kind "ConsoleApp"
files {
BGFX_DIR .. "examples/common/**.mm",
}
@ -110,6 +111,8 @@ function exampleProject(_name, _uuid)
"-framework CoreFoundation",
"-framework Foundation",
"-framework OpenGLES",
"-framework UIKit",
"-framework QuartzCore",
}
configuration { "qnx*" }

View file

@ -35,10 +35,19 @@ namespace bgfx
#if BX_PLATFORM_ANDROID
::ANativeWindow* g_bgfxAndroidWindow = NULL;
void androidSetWindow(ANativeWindow* _window)
void androidSetWindow(::ANativeWindow* _window)
{
g_bgfxAndroidWindow = _window;
}
#elif BX_PLATFORM_IOS
void* g_bgfxEaglContext = NULL;
void* g_bgfxEaglLayer = NULL;
void iosSetEaglContext(void* _context, void* _layer)
{
g_bgfxEaglContext = _context;
g_bgfxEaglLayer = _layer;
}
#elif BX_PLATFORM_OSX
void* g_bgfxNSWindow = NULL;
@ -638,7 +647,7 @@ namespace bgfx
s_threadIndex = BGFX_MAIN_THREAD_MAGIC;
// On NaCl renderer is on the main thread.
s_ctx.init(!BX_PLATFORM_NACL);
s_ctx.init(!BX_PLATFORM_NACL && !BX_PLATFORM_IOS);
}
void shutdown()

View file

@ -156,6 +156,9 @@ namespace bgfx
{
#if BX_PLATFORM_ANDROID
extern ::ANativeWindow* g_bgfxAndroidWindow;
#elif BX_PLATFORM_IOS
extern void* g_bgfxEaglContext;
extern void* g_bgfxEaglLayer;
#elif BX_PLATFORM_OSX
extern void* g_bgfxNSWindow;
#elif BX_PLATFORM_WINDOWS

42
src/glcontext_eagl.h Executable file
View file

@ -0,0 +1,42 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __GLCONTEXT_EAGL_H__
#define __GLCONTEXT_EAGL_H__
#if BX_PLATFORM_IOS
namespace bgfx
{
struct GlContext
{
GlContext()
: m_context(0)
{
}
void create(uint32_t _width, uint32_t _height);
void destroy();
void resize(uint32_t _width, uint32_t _height, bool _vsync);
void swap();
void import();
bool isValid() const
{
return 0 != m_context;
}
void* m_view;
void* m_context;
GLuint m_fbo;
GLuint m_colorRbo;
GLuint m_depthRbo;
};
} // namespace bgfx
#endif // BX_PLATFORM_IOS
#endif // __GLCONTEXT_EAGL_H__

89
src/glcontext_eagl.mm Normal file
View file

@ -0,0 +1,89 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "bgfx_p.h"
#if BX_PLATFORM_IOS && (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)
# include <UIKit/UIKit.h>
# include <QuartzCore/CAEAGLLayer.h>
# include "renderer_gl.h"
namespace bgfx
{
void GlContext::create(uint32_t _width, uint32_t _height)
{
EAGLContext* context = (EAGLContext*)g_bgfxEaglContext;
CAEAGLLayer* layer = (CAEAGLLayer*)g_bgfxEaglLayer;
[EAGLContext setCurrentContext:context];
m_context = g_bgfxEaglContext;
GL_CHECK(glGenFramebuffers(1, &m_fbo) );
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );
GL_CHECK(glGenRenderbuffers(1, &m_colorRbo) );
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRbo) );
GLint width;
GLint height;
GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width) );
GL_CHECK(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height) );
BX_TRACE("Screen size: %d x %d", width, height);
GL_CHECK(glGenRenderbuffers(1, &m_depthRbo) );
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_depthRbo) );
GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height) );
GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthRbo) );
BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
, "glCheckFramebufferStatus failed 0x%08x"
, glCheckFramebufferStatus(GL_FRAMEBUFFER)
);
}
void GlContext::destroy()
{
if (0 != m_fbo)
{
GL_CHECK(glDeleteFramebuffers(1, &m_fbo) );
m_fbo = 0;
}
if (0 != m_colorRbo)
{
GL_CHECK(glDeleteRenderbuffers(1, &m_colorRbo) );
m_colorRbo = 0;
}
if (0 != m_depthRbo)
{
GL_CHECK(glDeleteRenderbuffers(1, &m_depthRbo) );
m_depthRbo = 0;
}
}
void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
{
BX_TRACE("resize context");
}
void GlContext::swap()
{
EAGLContext* context = (EAGLContext*)m_context;
[EAGLContext setCurrentContext:context];
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_colorRbo) );
[context presentRenderbuffer:GL_RENDERBUFFER];
}
void GlContext::import()
{
}
} // namespace bgfx
#endif // BX_PLATFORM_IOS && (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)

View file

@ -282,6 +282,8 @@ namespace bgfx
}
#endif // BGFX_CONFIG_RENDERER_OPENGL
extern GLuint m_backBufferFbo;
struct RendererContext
{
RendererContext()
@ -428,6 +430,10 @@ namespace bgfx
if (!m_glctx.isValid() )
{
m_glctx.create(_width, _height);
#if BX_PLATFORM_IOS
// BK - Temp, need to figure out how to deal with FBO created by context.
m_backBufferFbo = m_glctx.m_fbo;
#endif // BX_PLATFORM_IOS
}
else
{
@ -537,7 +543,7 @@ namespace bgfx
void init()
{
m_glctx.create(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
m_vendor = getGLString(GL_VENDOR);
m_renderer = getGLString(GL_RENDERER);
@ -2427,6 +2433,7 @@ namespace bgfx
s_drawArraysInstanced = stubDrawArraysInstanced;
s_drawElementsInstanced = stubDrawElementsInstanced;
# if !BX_PLATFORM_IOS
if (s_extension[Extension::ARB_instanced_arrays].m_supported
|| s_extension[Extension::ANGLE_instanced_arrays].m_supported)
{
@ -2439,6 +2446,7 @@ namespace bgfx
s_drawElementsInstanced = glDrawElementsInstanced;
}
}
# endif // !BX_PLATFORM_IOS
#endif // !BGFX_CONFIG_RENDERER_OPENGLES3
if (s_renderCtx.m_vaoSupport)
@ -2639,7 +2647,7 @@ namespace bgfx
GL_CHECK(glBindVertexArray(defaultVao) );
}
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderCtx.m_backBufferFbo) );
s_renderCtx.updateResolution(m_render->m_resolution);

View file

@ -66,9 +66,28 @@
#elif BGFX_CONFIG_RENDERER_OPENGLES2 || BGFX_CONFIG_RENDERER_OPENGLES3
# if BGFX_CONFIG_RENDERER_OPENGLES2
# if BX_PLATFORM_IOS
# include <OpenGLES/ES2/gl.h>
# include <OpenGLES/ES2/glext.h>
typedef void (GL_APIENTRYP PFNGLBINDVERTEXARRAYOESPROC) (GLuint array);
typedef void (GL_APIENTRYP PFNGLDELETEVERTEXARRAYSOESPROC) (GLsizei n, const GLuint *arrays);
typedef void (GL_APIENTRYP PFNGLGENVERTEXARRAYSOESPROC) (GLsizei n, GLuint *arrays);
typedef GLboolean (GL_APIENTRYP PFNGLISVERTEXARRAYOESPROC) (GLuint array);
typedef void (GL_APIENTRYP PFNGLGETPROGRAMBINARYOESPROC) (GLuint program, GLsizei bufSize, GLsizei *length, GLenum *binaryFormat, GLvoid *binary);
typedef void (GL_APIENTRYP PFNGLPROGRAMBINARYOESPROC) (GLuint program, GLenum binaryFormat, const GLvoid *binary, GLint length);
typedef void (GL_APIENTRYP PFLGLDRAWARRAYSINSTANCEDANGLEPROC) (GLenum mode, GLint first, GLsizei count, GLsizei primcount);
typedef void (GL_APIENTRYP PFLGLDRAWELEMENTSINSTANCEDANGLEPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount);
typedef void (GL_APIENTRYP PFLGLVERTEXATTRIBDIVISORANGLEPROC) (GLuint index, GLuint divisor);
//#define GL_UNSIGNED_INT_10_10_10_2_OES 0x8DF6
#define GL_UNSIGNED_INT_2_10_10_10_REV_EXT 0x8368
#define GL_SAMPLER_3D_OES 0x8B5F
#define GL_PROGRAM_BINARY_LENGTH_OES 0x8741
# else
# include <GLES2/gl2platform.h>
# include <GLES2/gl2.h>
# include <GLES2/gl2ext.h>
# endif // BX_PLATFORM_
# define glProgramBinary glProgramBinaryOES
# define glGetProgramBinary glGetProgramBinaryOES
# define glBindVertexArray glBindVertexArrayOES
@ -214,7 +233,7 @@ typedef void (*PFNGLGETTRANSLATEDSHADERSOURCEANGLEPROC)(GLuint shader, GLsizei b
#elif BX_PLATFORM_OSX
# include "glcontext_nsgl.h"
#elif BX_PLATFORM_IOS
# include "glcontext_ios.h"
# include "glcontext_eagl.h"
#endif // BX_PLATFORM_
#if BGFX_CONFIG_DEBUG_GREMEDY && (BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX)

View file

@ -345,6 +345,14 @@ inline uint32_t rgbaToAbgr(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a)
;
}
struct GroupSortByMaterial
{
bool operator()(const Group& _lhs, const Group& _rhs)
{
return _lhs.m_material < _rhs.m_material;
}
};
int main(int _argc, const char* _argv[])
{
bx::CommandLine cmdLine(_argc, _argv);
@ -635,14 +643,6 @@ int main(int _argc, const char* _argv[])
parseElapsed += now;
int64_t convertElapsed = -now;
struct GroupSortByMaterial
{
bool operator()(const Group& _lhs, const Group& _rhs)
{
return _lhs.m_material < _rhs.m_material;
}
};
std::sort(groups.begin(), groups.end(), GroupSortByMaterial() );
bool hasColor = false;