From 3d4af0869ac3e153fa27e27cdc0a71c6c795c663 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 28 Apr 2011 19:01:37 +0000 Subject: [PATCH] ps3: start a minimal PS3 port using PSGL, but not CG yet. --- src/debugfps.cpp | 2 + src/debugquad.cpp | 4 ++ src/debugrecord.cpp | 2 + src/debugsphere.cpp | 2 + src/debugstats.cpp | 2 + src/dict.cpp | 2 + src/font.cpp | 6 ++- src/image.cpp | 2 + src/layer.cpp | 2 + src/lolgl.h | 3 ++ src/map.cpp | 2 + src/matrix.cpp | 2 + src/matrix.h | 2 +- src/numeric.h | 2 +- src/ps3app.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++ src/ps3app.h | 41 ++++++++++++++ src/sample.cpp | 2 + src/scene.cpp | 6 +++ src/shader.cpp | 18 +++++++ src/text.cpp | 2 + src/tileset.cpp | 2 + src/timer.cpp | 19 +++++++ src/video.cpp | 13 +++++ 23 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 src/ps3app.cpp create mode 100644 src/ps3app.h diff --git a/src/debugfps.cpp b/src/debugfps.cpp index 396f8a21..87185446 100644 --- a/src/debugfps.cpp +++ b/src/debugfps.cpp @@ -17,6 +17,8 @@ #include "core.h" #include "debugfps.h" +using namespace std; + namespace lol { diff --git a/src/debugquad.cpp b/src/debugquad.cpp index 7bbe02ea..080729b3 100644 --- a/src/debugquad.cpp +++ b/src/debugquad.cpp @@ -18,6 +18,8 @@ #include "lolgl.h" #include "loldebug.h" +using namespace std; + namespace lol { @@ -159,6 +161,7 @@ void DebugQuad::TickDraw(float deltams) attr_col = data->shader->GetAttribLocation("in_Color"); attr_tex = data->shader->GetAttribLocation("in_TexCoord"); +#if !defined __CELLOS_LV2__ glEnableVertexAttribArray(attr_pos); glEnableVertexAttribArray(attr_col); glEnableVertexAttribArray(attr_tex); @@ -184,6 +187,7 @@ void DebugQuad::TickDraw(float deltams) glDisableVertexAttribArray(attr_pos); glDisableVertexAttribArray(attr_col); glDisableVertexAttribArray(attr_tex); +#endif } DebugQuad::~DebugQuad() diff --git a/src/debugrecord.cpp b/src/debugrecord.cpp index cdc6e5bd..fcc412c8 100644 --- a/src/debugrecord.cpp +++ b/src/debugrecord.cpp @@ -21,6 +21,8 @@ #include "core.h" #include "debugrecord.h" +using namespace std; + namespace lol { diff --git a/src/debugsphere.cpp b/src/debugsphere.cpp index 609ddbe6..462e51ed 100644 --- a/src/debugsphere.cpp +++ b/src/debugsphere.cpp @@ -24,6 +24,8 @@ #include "lolgl.h" #include "debugsphere.h" +using namespace std; + namespace lol { diff --git a/src/debugstats.cpp b/src/debugstats.cpp index 78287e51..45630b07 100644 --- a/src/debugstats.cpp +++ b/src/debugstats.cpp @@ -17,6 +17,8 @@ #include "core.h" #include "debugstats.h" +using namespace std; + namespace lol { diff --git a/src/dict.cpp b/src/dict.cpp index 5b2e5325..ce75aeb1 100644 --- a/src/dict.cpp +++ b/src/dict.cpp @@ -21,6 +21,8 @@ # define strcasecmp _stricmp #endif +using namespace std; + namespace lol { diff --git a/src/font.cpp b/src/font.cpp index ee2bd67b..677e215d 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -12,11 +12,13 @@ # include "config.h" #endif -#include -#include +#include +#include #include "core.h" +using namespace std; + namespace lol { diff --git a/src/image.cpp b/src/image.cpp index ef04a49d..e46b4838 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -26,6 +26,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/layer.cpp b/src/layer.cpp index 13230bf0..30d141f2 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -16,6 +16,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/lolgl.h b/src/lolgl.h index c3d8616f..31000bbb 100644 --- a/src/lolgl.h +++ b/src/lolgl.h @@ -35,6 +35,9 @@ # if defined __APPLE__ && defined __MACH__ # include # include +# elif defined __CELLOS_LV2__ +# include +# include # else # include # include diff --git a/src/map.cpp b/src/map.cpp index d89c463b..52880e8a 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -20,6 +20,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/matrix.cpp b/src/matrix.cpp index 16969a57..8f5044b9 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -17,6 +17,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/matrix.h b/src/matrix.h index e104a0f0..7485a598 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -113,7 +113,7 @@ namespace lol \ inline float len() const \ { \ - return sqrtf((float)sqlen()); \ + return std::sqrtf((float)sqlen()); \ } template struct Vec2; diff --git a/src/numeric.h b/src/numeric.h index 870480ab..8f72aa9f 100644 --- a/src/numeric.h +++ b/src/numeric.h @@ -26,7 +26,7 @@ namespace lol /* Random float value */ static inline float RandF() { - return (float)rand() / RAND_MAX; + return (float)std::rand() / RAND_MAX; } static inline float RandF(float val) diff --git a/src/ps3app.cpp b/src/ps3app.cpp new file mode 100644 index 00000000..56b61433 --- /dev/null +++ b/src/ps3app.cpp @@ -0,0 +1,128 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#if defined __CELLOS_LV2__ +# include +# include /* SYS_HOST_ROOT */ +# include +# include +#endif + +#include "core.h" +#include "lolgl.h" +#include "ps3app.h" + +namespace lol +{ + +/* + * PS3 App implementation class + */ + +class Ps3AppData +{ + friend class Ps3App; + +private: +#if defined __CELLOS_LV2__ + static void SysCallBack(uint64_t status, uint64_t param, void *data) + { + if (status == CELL_SYSUTIL_REQUEST_EXITGAME) + Ticker::Shutdown(); + } +#endif +}; + +/* + * Public Ps3App class + */ + +Ps3App::Ps3App(char const *title, vec2i res, float fps) : + data(new Ps3AppData()) +{ +#if defined __CELLOS_LV2__ + sys_spu_initialize(6, 1); + + /* FIXME: we should check for CELL_SYSMODULE_ERROR_UNKNOWN and others, + * but what could we do anyway? */ + cellSysmoduleLoadModule(CELL_SYSMODULE_GCM_SYS); + cellSysmoduleLoadModule(CELL_SYSMODULE_FS); + cellSysmoduleLoadModule(CELL_SYSMODULE_USBD); + cellSysmoduleLoadModule(CELL_SYSMODULE_IO); + + cellSysutilRegisterCallback(0, Ps3AppData::SysCallBack, NULL); + + PSGLinitOptions psglio = + { + enable: PSGL_INIT_MAX_SPUS | PSGL_INIT_INITIALIZE_SPUS + | PSGL_INIT_HOST_MEMORY_SIZE, + maxSPUs: 1, + initializeSPUs: false, + persistentMemorySize: 0, + transientMemorySize: 0, + errorConsole: 0, + fifoSize: 0, + hostMemorySize: 128 * 1024 * 1024, + }; + + psglInit(&psglio); + + PSGLdevice* psgl = psglCreateDeviceAuto(GL_ARGB_SCE, GL_DEPTH_COMPONENT24, + GL_MULTISAMPLING_4X_SQUARE_ROTATED_SCE); + GLuint w, h; + psglGetDeviceDimensions(psgl, &w, &h); + + PSGLcontext *ctx = psglCreateContext(); + psglMakeCurrent(ctx, psgl); + psglLoadShaderLibrary(REMOTE_PATH "/shaders.bin"); + psglResetCurrentContext(); + + /* Initialise everything */ + Ticker::Setup(fps); + Video::Setup(res); + Audio::Setup(2); +#endif +} + +void Ps3App::Run() +{ + while (!Ticker::Finished()) + { + /* Tick the game */ + Ticker::TickGame(); + + /* Tick the renderer, show the frame and clamp to desired framerate. */ + Ticker::TickDraw(); + +#if defined __CELLOS_LV2__ + psglSwap(); + + /* Check if exit callback was called */ + cellSysutilCheckCallback(); +#endif + + Ticker::ClampFps(); + } +} + +Ps3App::~Ps3App() +{ +#if defined __CELLOS_LV2__ + glFinish(); +#endif + delete data; +} + +} /* namespace lol */ + diff --git a/src/ps3app.h b/src/ps3app.h new file mode 100644 index 00000000..22ad8a9d --- /dev/null +++ b/src/ps3app.h @@ -0,0 +1,41 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The Ps3App class +// ---------------- +// + +#if !defined __LOL_PS3APP_H__ +#define __LOL_PS3APP_H__ + +#include "matrix.h" + +namespace lol +{ + +class Ps3AppData; + +class Ps3App +{ +public: + Ps3App(char const *title, vec2i res, float fps); + virtual ~Ps3App(); + + void Run(); + +private: + Ps3AppData *data; +}; + +} /* namespace lol */ + +#endif // __LOL_PS3APP_H__ + diff --git a/src/sample.cpp b/src/sample.cpp index 22c6eff0..9a80c080 100644 --- a/src/sample.cpp +++ b/src/sample.cpp @@ -24,6 +24,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/scene.cpp b/src/scene.cpp index d9401da4..8935ce6e 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -157,11 +157,13 @@ void Scene::Render() // XXX: rename to Blit() attr_pos = stdshader->GetAttribLocation("in_Position"); attr_tex = stdshader->GetAttribLocation("in_TexCoord"); +#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc. stdshader->Bind(); uni_mat = stdshader->GetUniformLocation("model_matrix"); glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &model_matrix[0][0]); uni_tex = stdshader->GetUniformLocation("in_Texture"); glUniform1i(uni_tex, 0); +#endif glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); @@ -208,6 +210,7 @@ void Scene::Render() // XXX: rename to Blit() #if defined HAVE_GL_2X glBindVertexArray(data->vao); #endif +#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc. glEnableVertexAttribArray(attr_pos); glEnableVertexAttribArray(attr_tex); @@ -220,6 +223,7 @@ void Scene::Render() // XXX: rename to Blit() glBufferData(GL_ARRAY_BUFFER, 12 * (n - i) * sizeof(GLfloat), texture, GL_STATIC_DRAW); glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0); +#endif /* Draw arrays */ glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6); @@ -227,8 +231,10 @@ void Scene::Render() // XXX: rename to Blit() #if defined HAVE_GL_2X glBindVertexArray(0); #endif +#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc. glDisableVertexAttribArray(attr_pos); glDisableVertexAttribArray(attr_tex); +#endif free(vertex); free(texture); diff --git a/src/shader.cpp b/src/shader.cpp index 39c7b623..bcf14c0e 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -79,6 +79,7 @@ Shader::Shader(char const *vert, char const *frag) char buf[4096]; GLsizei len; +#if !defined __CELLOS_LV2__ data->vert_crc = Hash::Crc32(vert); data->vert_id = glCreateShader(GL_VERTEX_SHADER); glShaderSource(data->vert_id, 1, &vert, NULL); @@ -103,31 +104,48 @@ Shader::Shader(char const *vert, char const *frag) glLinkProgram(data->prog_id); glValidateProgram(data->prog_id); +#endif } int Shader::GetAttribLocation(char const *attr) const { +#if defined __CELLOS_LV2__ + return 0; +#else return glGetAttribLocation(data->prog_id, attr); +#endif } int Shader::GetUniformLocation(char const *uni) const { +#if defined __CELLOS_LV2__ + return 0; +#else return glGetUniformLocation(data->prog_id, uni); +#endif } void Shader::Bind() const { +#if defined __CELLOS_LV2__ + ; +#else glUseProgram(data->prog_id); +#endif } Shader::~Shader() { +#if defined __CELLOS_LV2__ + ; +#else glDetachShader(data->prog_id, data->vert_id); glDetachShader(data->prog_id, data->frag_id); glDeleteShader(data->vert_id); glDeleteShader(data->frag_id); glDeleteProgram(data->prog_id); delete data; +#endif } } /* namespace lol */ diff --git a/src/text.cpp b/src/text.cpp index fd60e17c..e99b270c 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -18,6 +18,8 @@ #include "core.h" +using namespace std; + namespace lol { diff --git a/src/tileset.cpp b/src/tileset.cpp index 5d30a5a3..39103300 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -25,6 +25,8 @@ #include "core.h" #include "lolgl.h" +using namespace std; + namespace lol { diff --git a/src/timer.cpp b/src/timer.cpp index d4258822..25d62ebe 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -21,6 +21,10 @@ #elif defined _WIN32 # define WIN32_LEAN_AND_MEAN # include +#elif defined __CELLOS_LV2__ +# include +# include +# include #else # include #endif @@ -48,6 +52,9 @@ private: QueryPerformanceFrequency(&tmp); ms_per_cycle = 1e3f / tmp.QuadPart; QueryPerformanceCounter(&cycles0); +#elif defined __CELLOS_LV2__ + ms_per_cycle = 1e3f / sys_time_get_timebase_frequency(); + SYS_TIMEBASE_GET(cycles0); #else SDL_Init(SDL_INIT_TIMER); ticks0 = SDL_GetTicks(); @@ -76,6 +83,15 @@ private: towait = deltams - ret; if (towait > 5e-4f) Sleep((int)(towait + 0.5f)); +#elif defined __CELLOS_LV2__ + uint64_t cycles; + SYS_TIMEBASE_GET(cycles); + ret = ms_per_cycle * cycles; + if (update) + cycles0 = cycles; + towait = deltams - ret; + if (towait > 0.0f) + sys_timer_usleep((int)(towait * 1e3f)); #else /* The crappy SDL fallback */ Uint32 ticks = SDL_GetTicks(); @@ -94,6 +110,9 @@ private: #elif defined _WIN32 float ms_per_cycle; LARGE_INTEGER cycles0; +#elif defined __CELLOS_LV2__ + float ms_per_cycle; + uint64_t cycles0; #else Uint32 ticks0; #endif diff --git a/src/video.cpp b/src/video.cpp index 51dd69d3..9ab5b2e0 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -22,6 +22,8 @@ #include "core.h" #include "lolgl.h" +using namespace std; + namespace lol { @@ -188,11 +190,13 @@ void Video::SetFov(float theta) view_matrix = mat4(1.0f); stdshader->Bind(); /* Required on GLES 2.x? */ +#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc. GLuint uni; uni = stdshader->GetUniformLocation("proj_matrix"); glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]); uni = stdshader->GetUniformLocation("view_matrix"); glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]); +#endif } void Video::SetDepth(bool set) @@ -220,7 +224,13 @@ void Video::Destroy() void Video::Capture(uint32_t *buffer) { GLint v[4]; +#if defined __CELLOS_LV2__ + // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions + v[2] = 1920; + v[3] = 1080; +#else glGetIntegerv(GL_VIEWPORT, v); +#endif int width = v[2], height = v[3]; #if defined HAVE_GL_2X @@ -247,6 +257,9 @@ vec2i Video::GetSize() { #if defined ANDROID_NDK return saved_viewport; +#elif defined __CELLOS_LV2__ + // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions + return vec2i(1920, 1080); #else GLint v[4]; glGetIntegerv(GL_VIEWPORT, v);