2009年2月28日星期六
2009年2月26日星期四
Generating Shaders From HLSL Fragments
Shaders are cool. You can do all sorts of interesting things with them: this and previous ShaderX books are full of examples. Alongside their power, however, programmable shaders can lead to an explosion of permutations: my last Xbox game contained 89 different pixel shaders, and my current project already has far more. Many of these shaders are variations on a few basic themes, for instance level-of-detail approximations of a material, or the same lighting model both with and without animation skinning. The total number of combinations is huge and is increasing all the time. Typing everything out by hand would be time consuming, error prone, and a maintenance nightmare.
This article will describe how to automatically generate large numbers of shader permutations from a smaller set of handwritten input fragments.
2009年2月23日星期一
Nebula3的点和向量
点:
点+向量=点
点-向量=点
点-点=向量
向量:
向量+向量=向量
向量-向量=向量
向量*常量=向量
Nebula3使用的一些关键字
在VC++中可使用另一关键字_forceinline 代替inline 关键字.这个关键字将命令编译器跳过一般的ROI 分析(Return On Investment --一种编程缩略语),将所对应的代码强行内联.在有写时候,编译器会拒绝将一个函数内联,使用这个关键字,用户只得到一个编译警告,就可强行内联.
在使用内联函数时,是由编译器决定它们是按普通函数处理还是将调用函数部分用实际的函数体代码替换。不允许将递归函数进行内联(VC++可进行编译器选项设置,允许内联扩展到一定深度)
下面情况不宜使用内联:
1.如果函数体内的代码比较长,使用内联将导致内存消耗代价较高。
2.如果函数体内出现循环,那么执行函数体内代码的时间要比函数调用的开销大。
volatile:
如果将将变量加上volatile修饰,则编译器保证对此变量的读写操作都不会被优化。
一般说来,volatile用在如下的几个地方:
1、中断服务程序中修改的供其它程序检测的变量需要加volatile。
2、多任务环境下各任务间共享的标志应该加volatile。
3、存储器映射的硬件寄存器通常也要加volatile说明,因为每次对它的读写都可能有不同意义。
__cdecl:
C,C++的默认调用规范,参数从右到左传替,由调用函数管理堆栈。
2009年2月19日星期四
Nebula3单例
可以通过静态的Instance()方法获得单例对象,该方法返回单例类的单一实例。保证返回的指针是有效的。在Instance()方法被调用时候如果单例对象不存在,那么将抛出一个断言。
// obtain a pointer to the Core::Server singleton
Ptr
你也可以检查给定的单例是否存在:
// does the Core::Server object exist?
if (Core::Server::HasInstance())
{
// yep, the core server exists
}
Nebula3提供了一些宏帮助实现单例类:
// declare a singleton class
class MySingletonClass : public Core::RefCounted
{
DeclareClass(MySingletonClass);
DeclareSingleton(MySingletonClass);
public:
/// constructor
MySingletonClass();
/// destructor
virtual ~MySingletonClass();
...
};
// implement the singleton class
ImplementClass(MyNamespace::MySingletonClass, 'MYSC', Core::RefCounted);
ImplementSingleton(MyNamespace::MySingletonClass);
//------------------------------------------------------------------------------
/**
Implements the Singleton constructor.
*/
MySingletonClass::MySingletonClass()
{
ConstructSingleton;
}
//------------------------------------------------------------------------------
/**
Implements the Singleton destructor.
*/
MySingletonClass:~MySingletonClass()
{
DestructSingleton;
}
DeclareSingleton()和ImplementSingleton()宏跟DeclareClass()和ImplementClass()宏相似。它们往类里增加了一些静态方法(Instance()和HasInstance()方法)。类的构造函数和析构函数必须包含ConstructSingleton和DestructSingleton宏。ConstructSingleton宏初始化一个私有的静态单例指针并确定这个类没有其它的实例存在(否则,将抛出一个断言)。DestructSingleton宏让静态单例指针无效。
默认是从本地线程获取一个单例。这意味着单例是创建在Nebula3应用程序的一个线程中,并且其它线程不能访问该单例。这种方式遵循着“并行Nebula”的设计,让多线程编程变得更加简单。隐藏在“并行Nebula3”背后的构想是,一个典型的Nebula3应用程序包含一些运行在分开的CPU核上的“胖线程”("Fat Threads")。胖线程实现例如异步IO,渲染,物理等等。每一个胖线程初始化它自己的Nebula3运行库,这个库包含胖线程执行特定任务所需的最小Nebula3环境。这基本上消除了大部分Nebula3代码所需的细粒度同步,并且把“线程相关”的代码集中在几个定义明确的范围内用于胖线程之间的通讯。“并行Nebulas”设计的另外一个有用效果是,让程序员不必太关注代码运行在一个多线程环境。典型的Nebula3代码就像普通的单线程代码,然而它可以运行在它自己的胖线程内。
原文:
[声明]:限于译者水平,文中难免错漏之处,欢迎各位网友批评指正;
Nebula3运行时类型信息系统
例子:
using namespace Util;
using namespace Core;
// check whether an object is instance of a specific class
if (myObj->IsInstanceOf(MyClass::RTTI))
{
// it's a MyClass object
}
// check whether an object is instance of a derived class
if (myObj->IsA(RefCounted::RTTI))
{
// it's a RefCounted instance or some RefCounted-derived instance
}
// get the class name of my object, this yields "MyNamespace::MyClass"
const String& className = myObj->GetClassName();
// get the fourcc class identifier of my object, this yields 'MYCL'
const FourCC& fourcc = myObj->GetClassFourCC();
你也可以通过中心工厂对象查询给定的类是否注册了:
using namespace Core;
// check if a class has been registered by class name
if (Factory::Instance()->ClassExists("MyNamespace::MyClass"))
{
// yep, the class exists
}
// check if a class has been registered by class fourcc code
if (Factory::Instance()->ClassExists(FourCC('MYCL')))
{
// yep, the class exists
}
原文:
[声明]:限于译者水平,文中难免错漏之处,欢迎各位网友批评指正;