- 浏览: 314897 次
- 性别:
- 来自: 益阳
文章分类
最新评论
-
duckbit:
楼主是否能把共享layout的例子发给我,有点没明白 谢谢额! ...
Android换肤apk -
天涯海角262253:
...
Androidpn里的Xmpp的理解 -
lbstudy:
Activity之间的切换动画 -
dumbnesslys:
楼主可不可以给个源码 ,就build.xml的 772774 ...
Ant自动打包 -
finaljava:
build.xml 这么复杂,看看这个吧http://angr ...
Ant自动打包
The Replica Island renderer is based heavily on the GLSurfaceView class that ships with the Android SDK. I've made a couple of modifications but the code is pretty similar to the regular version: a derivation of GLSurfaceView.Renderer that draws the frame gets called every frame, followed by a call to eglSwapBuffers() to actually display the rendered frame.
GLSurfaceView provides a way to run user code in the same thread as the renderer. This makes writing games pretty easy; you can just implement a Runnable, implement a Renderer, stick them both into a GLSurfaceView and get stuff moving around on the screen. Indeed, it's more than sufficient for many applications; my SpriteMethodTest demo works this way just fine.
But for Replica Island I took a different approach. The problem with the single GLSurfaceView thread is that eglSwapBuffers() must block on the hardware until the previous frame finishes drawing. That means that even if you have nothing to draw, a call to eglSwapBuffers() takes 16.67ms to complete. (And of course, if you have a lot to draw, it could take a lot longer).
Now, just in case you are not used to thinking in terms of milliseconds, here's a quick primer. To achieve the magical "60 frames per second" that many games strive for, you need to have a new frame displayed to the user every 16.67 ms. If you go for 30 fps, you have ~32 ms to complete a frame. All your game code, plus all your OpenGL code, plus the actual time it takes to draw the frame must fit within 16.67 ms to achieve 60fps.
In Replica Island, the game code is fairly heavy-weight. I have all that collision to run, plus updates of all the active entities on the screen, plus sound playback and all that jazz. Turns out that it's usually more work to calculate a single simulation step than it is to actually draw the frame. Since this code takes time to execute, the 16 ms block that eglSwapBuffers() incurs makes it really hard to hit 60 fps. What I really want to be able to do is run game code while eglSwapBuffers() is blocking; that way I can pipeline the game updates while the hardware is busy drawing the frame.
So I split the game code off into a separate thread. This makes three threads, by the way: the main UI thread that all Activities have by default, the GLSurfaceView render thread, and this new game thread (actually, there are a few more that are generated by the system for things like orientation sensor updates, but they don't affect the equation much). Now my game code and my renderer can run asynchronously, and I win back some of that time spent in eglSwapBuffers().
Now comes the tricky part. I have two threads running in parallel that need to sync up once a frame so that the game thread can tell the render thread what to do. There's a lot of ways to go about synchronizing these two threads, but I went with a double buffer solution. The game thread fills up a buffer of commands to draw the next frame, and when it is ready it waits for the render thread to begin the next frame. At that point, the buffer is passed to to the render, which can then go off and draw the next frame asynchronously. The buffer that was used to draw the last frame is passed back to the game thread, which fills it up again the next frame. So drawing is the process of swapping these two buffers back and forth during a (hopefully short) choke point at which both threads stop and communicate.
This solution was attractive to me because it was simple, and so far it seems to be plenty fast. However, another solution might be to have a queue that is shared by both threads, with the game thread pushing commands in one end and the renderer executing commands out of the other. In theory such a solution wouldn't need both threads to ever perfectly align--blocking would only occur when one thread or the other was starved. But I haven't done this yet because it is going to be significantly more complex than the double buffer.
My render commands are objects that are allocated out of pools that the game thread owns, and must be returned to those pools when they have been drawn. In the double buffer system, the queue that is returned from the render thread contains commands that can be safely returned to their pools, but in the shared queue system there's no obvious way for the game thread to know how much has been drawn. I suppose there could be two shared queues, one in each direction, but that would still be a lot more complicated than what I have now. Right now almost no code outside of the buffer swap system knows about other threads; the pool objects and the objects they contain are not thread safe and, as it stands, don't need to be.
Is my solution the best for Android apps? I don't know. It seems to work pretty well and it is uncomplicated, which are two points in its favor. Still, I'd like to give this shared queue idea a shot at some point; my gut tells me that it will be slightly faster than the double buffer (less blocking in the average case) but a lot more complex, which might make it not worth the effort. Programmer guts are, however, extremely unreliable, so I will probably give this method a shot after Replica Island ships.
发表评论
-
Want to Change the Game Industry? Support the Xperia PLAY.
2011-12-07 21:05 0The problems with the console g ... -
Replica Island Updated
2011-12-07 21:04 0Yesterday I uploaded the first ... -
Hot Failure
2011-12-07 21:04 0An article I originally wrote f ... -
Building a Reflective Object System in C++
2011-12-07 21:03 0Every game engine I've worked w ... -
Leveraging Java and C++ for Hybrid Games
2011-12-07 21:02 0I've been thinking a lot lately ... -
Game Object Construction Rabbit Hole
2011-12-07 21:01 0Today I want to write a little ... -
Long Time, No See
2011-12-07 21:00 0I haven't written anything here ... -
Replica Island Passes a Million Installs
2011-12-07 20:59 0Holy crap!Replica Island passed ... -
Control Configuration and Abstraction
2011-12-07 20:58 0The #1 thing that I've learn ... -
Design Post-Mortem: Three Mistakes
2011-12-07 20:58 0While I'm pretty happy with Rep ... -
Replica Island: One Month On
2011-12-07 20:57 0As of today, Replica Island ... -
My New Favorite User Feedback
2011-12-07 20:56 0You can't write this stuff ... -
Replica Island User Comments Are Hilarious
2011-12-07 20:55 0Update: Blogger apparently isn' ... -
Design Post-Mortem: The Possession Orb
2011-12-07 20:55 0Rather than write a big lo ... -
Replica Island Released!
2011-12-07 20:54 0I released Replica Island almos ... -
Fragmentation? More like Fragmentawesome.
2011-12-07 20:53 0I'm lucky enough to have occasi ... -
The Elusive Perfect Platformer Camera
2011-12-07 20:52 0I've come to believe that platf ... -
Game Play Video
2011-12-07 20:51 0Here's some footage of an early ... -
Tuning With Metrics Redux
2011-12-07 20:50 0A while back I posted about the ... -
Some Screenshots
2011-12-07 20:49 0I've posted some screenshots of ...
相关推荐
《Real-Time_3D_Rendering_with_DirectX_and_HLSL》是一本专注于实时三维渲染技术的专著,尤其强调了DirectX和High-Level Shading Language(HLSL)的应用。这本书是为那些想要深入理解现代游戏开发、虚拟现实、可视...
- **非摄影真实渲染 (Non-photorealistic rendering)**:一种计算机图形学技术,用于模拟非现实世界中的渲染效果,如手绘风格、卡通风格等。 - **水彩 (Watercolor)**:一种绘画技法,使用水溶性颜料在纸张或其他...
《实时三维渲染:DirectX与HLSL实战》是一本深度探讨实时图形编程技术的专著,主要聚焦于DirectX和High-Level Shader Language (HLSL)的应用。这本书旨在帮助读者掌握利用这两种强大的工具进行实时3D场景渲染的技能...
ShaderX3 Advanced Rendering with DirectX and OpenGL
Written by active members of the Direct3D community, Practical Rendering and Computation with Direct3D 11 provides a deep understanding of both the high and low level concepts related to using Direct...
Real-Time 3D Rendering with DirectX and HLSL 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
在文件“Rendering with DirectX & Direct3D 11.3 Functional Specification.pdf”中,提到了三种不同的渲染算法:前向渲染(Forward Rendering)、延迟渲染(Deferred Shading)和前向加(Forward+ Rendering)。...
This all-new collection is packed with insightful new techniques, innovative approaches to common problems, and practical tools and tricks that will help you in all areas of shader programming....
《Practical Rendering and Computation with Direct3D11》是一本深入探讨Direct3D 11编程技术的专业书籍。Direct3D是微软开发的一个图形应用程序接口(API),主要用于Windows平台上的游戏开发、科学可视化和专业...
"Programming with OpenGL Advanced Rendering" 主要关注的是如何利用OpenGL的高级特性来实现复杂的图形渲染效果。这包括但不限于阴影、光照、纹理映射、着色器编程、几何变换、抗锯齿、景深模糊、后处理效果等。 1...
《Practical Rendering and Computation with Direct3D 11》是一本深入探讨Direct3D 11渲染与计算技术的专业书籍。源码是该书理论知识的实际应用展示,同时也是实现一个完整DirectX 11游戏引擎的基础。在这个压缩包...