`

Molehill / ND2D – speeding up the engine

 
阅读更多

from: http://www.nulldesign.de/2011/04/07/molehill-nd2d-speeding-up-the-engine/

 

Molehill / ND2D – speeding up the engine

 

One of biggest challenges in modern computer graphics, still is the high cost of rendering thousands of different objects, no matter how simple they are. While developing ND2D, I’m experimenting and trying out different techniques to get a good performance.

 

To optimize the rendering you have to know it’s weaknesses. As a simple rule you can say: Every state change on the graphics context (Context3D) and especially the drawTriangles() call is using a lot of processing power. You’ll notice pretty fast, that if you try to render 2000 sprites (a sprite are just two textured triangles, so 4000 tri’s in total) and you’re doing a draw call for every single sprite, the overhead will be so high, that the output looks more like a slideshow than a smooth animation. The possible solution is simple: Just do as little state changes and draw calls as possible. The implementation is a bit more work…

 

So how do you save draw calls? The answer is geometry batching. Instead of drawing one sprite per draw call you just draw multiple sprites in a single call. To get it to work, you have to dig a bit deeper into pixel shader programming and the graphics hardware:

Single sprite per draw call:


A sprite consists of two triangles, a triangle of three vertices and each vertex has the following attributes: x,y,z, u,v, which will be the format for our vertex buffer. The shader input parameters (constants) will be the mvp matrix, a color (to tint a sprite and to enable transparency) and of course the texture image (image4). This way you’re able to draw one sprite per call, pretty easy and straight forward… but slow.

Improvement, batching calls:


You can only batch calls, if the sprites you want to draw have all the same texture (Setting a texture is also pretty expensive). The main idea is, that you pass multiple mvp matrices and multiple colors to the shader instead of just one. Within the shader, depending which sprite is drawn, a different mvp matrix is used. But how many values you can pass to the shader? Todays modern graphic hardware has at least 128 constant registers available in the GPU, so to be compatible with all the different graphics cards out there it’s limited to 128 in the Molehill API. In the following picture you can see the different inputs that are available for the vertex shader. We won’t bother with temp registers and input vectors now, because it’s just unlikely that we are running out of registers while drawing sprites. So just keep in mind, that the vertex shader has limited storage space. In our case we’re limited to 128 constants.

 



 (Image taken from the DX8 SDK documentation)

 

A single register can hold a float4. So, let’s do some simple math. The matrix uses 4 registers (4 x float4) and the color just one: 128 / 5 = 25. We should be able to batch 25 draw calls in a single call. But how does the shader know which matrix to use? To provide this information in the shader, we simply add a batch identifier to the vertex buffer: x,y,z, u,v, batchID. The vertex shader could look like this then:

 

...
parameter float4x4 clipSpaceMatrix[25];
 
void evaluateVertex()
{
    vertexClipPosition = vertexPosition * clipSpaceMatrix[batchID];
}

 

Yay! We just batched our draw calls and the engine will run a lot faster for sprites with the same texture.

But there is more… Right now, we can only batch sprites that share the same texture. Wouldn’t it be great if we could batch just everything? There is an idea called texture atlas. Basically it’s pretty simple as well: Instead of using different textures, you just “bake” every texture used in your game into a single big texture like this: Pocket God Texture Atlas. All you have to do then, is to adjust the UV coordinates of your sprites to match the original texture in the big one. Generating a texture atlas at runtime and adjusting the UV coords is in fact a bit more work…

Have fun exploring the GPU ;)

 

  • 大小: 15.5 KB
分享到:
评论

相关推荐

    nd2d:Flash Molehill(Stage3D)GPU加速2D游戏引擎

    ND2D-Flash Molehill(Stage3D)GPU加速的2D引擎。 ND2D是使用Flash的新GPU硬件功能的2D框架。 要运行此引擎,您必须下载并安装最新的Flash Player: 您必须使用FP11 playerglobal.swc和编译器选项-swf-version = ...

    [转]Flash ‘Molehill’ API上的GPU加速2D框架-M2D

    **标题解析:**“Flash ‘Molehill’ API上的GPU加速2D框架-M2D”指的是一项技术,它利用了Adobe Flash平台中的Molehill API来实现2D图形的GPU加速。M2D(可能是“Molehill 2D”的缩写)引擎是一个专门设计用于提高2...

    [转]M2D的介绍——针对Flash ‘Molehill’ API的GPU加速2D框架

    《M2D:基于Flash 'Molehill' API的GPU加速2D框架详解》 在数字媒体领域,尤其是在Web开发中,高效的图形处理是至关重要的。Flash作为一种广泛使用的平台,长期以来一直是动态内容和互动体验的核心。然而,随着硬件...

    源码Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide

    《源码Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide》是针对初学者的一本深入解析Adobe Flash 11中的Stage3D(Molehill)技术的游戏编程指南。这本书通过一系列章节的示例代码,旨在帮助...

    as3 molehill实例

    AS3 Molehill API的引入,使得Flash不仅限于2D领域,而是能够充分利用现代计算机的图形处理能力,为开发者提供了构建高性能3D应用的新途径。通过深入学习和实践这些实例,开发者可以掌握3D内容创作的技巧,为用户...

    flash3d的molehill参考资料

    Flash 3D技术是Adobe开发的一种在Web上实现三维图形渲染的技术,而Molehill则是Flash 3D技术的一个重要里程碑。Molehill是Adobe Flash Player的一个底层API,提供了对硬件加速3D图形的支持,使得开发者能够创建出...

    Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner 书源码

    Adobe Flash 11引入了一项革命性的新技术,称为Stage3D(代号Molehill),为Flash平台带来了强大的3D图形处理能力。这本书——"Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner"是针对初学者的一本...

    Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide

    Adobe Flash 11引入了一项革命性的技术,Stage3D(又称为Molehill),它为游戏开发者提供了在Flash平台上构建高性能、低延迟的3D图形应用的能力。这本书《Adobe Flash 11 Stage3D (Molehill) Game Programming ...

    FlashDevelop + FlashPlayer11(Molehill) + Away3D 4.0 Alpha配置

    ### FlashDevelop + FlashPlayer11(Molehill) + Away3D 4.0 Alpha 配置详解 #### 一、前言 随着3D技术在Web领域的应用日益广泛,Adobe Flash平台也推出了针对3D图形处理的支持。其中,FlashPlayer11通过引入代号为...

    Adobe Flash 11 Stage3D Molehill Game Programming Beginners Guide

    Stage3D (代号Molehill) 是Adobe Flash Player 11和AIR 3引入的一项新技术,它允许开发者通过低级的API访问GPU硬件加速,从而实现高性能的2D和3D图形渲染。这一技术大大提升了Flash平台上的图形处理能力,使得开发者...

    在Flash Builder中搭建Molehill的开发测试环境

    在Flash Builder中搭建Molehill的开发测试环境;在Flash Builder中搭建Molehill的开发测试环境

    Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide.pdf

    ### Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide #### 标题解析 **Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide** 这个标题包含了几个重要的关键词:Adobe ...

    Molehill API分析

    ### Molehill API 分析与 Flash 3D 应用 #### 一、Molehill API 概述 Molehill API 是 Adobe Flash Player 11 和 Adobe AIR 3 引入的一组新功能,主要提供了对 GPU 加速 3D 渲染的支持。这一 API 的出现极大地提升...

    Adobe Molehill Stage3d 编译器AGALMiniAssembler

    AGAL(Adobe Graphics Assembly Language)是Adobe开发的图形汇编语言,汇编语言是仅高于计算机二进制机器码的低级语言,可以精确地操控机器硬件比如可编程显卡,PC的Dirext9、MAC的OpenGL以及移动设备中的OpenGL ES 2...

    Packt.Adobe.Flash.11.Stage3D.(Molehill).Game.Programming.Beginner's.Guide

    《Adobe Flash 11 Stage3D (Molehill) 游戏编程初学者指南》这本书是为想要利用Adobe Flash平台进行3D游戏开发的初学者准备的。Flash Stage3D,又名Molehill,是Adobe引入的一个重要的图形加速技术,它显著提升了...

Global site tag (gtag.js) - Google Analytics