高级着色语言HLSL入门(2)

16.2 编译HLSL 着色器

16.2.1 常量表

每个着色器有一个常量表,用来保存它的变量。D3DX库通过ID3DXConstantTable接口,提供给应用程序访问着色器的常量表。通过这个接口我们能够在应用程序中设置着色器源代码中的变量。

我们现在描述ID3DXConstantTable接口的方法列表的实现,全部的列表请查阅Direct3D文档。

16.2.1.1 取得常量句柄

为了在应用程序中设置着色器中的一个特定变量,需要有一种方法去引用它,我们能够在应用程序中用D3DXHANDLE引用一个在着色器中的变量,下面的方法返回一个着色器中的变量的D3DXHANDLE,使用时,需要传递一个变量的名字作为参数:

D3DXHANDLE ID3DXConstantTable::GetConstantByName(

D3DXHANDLE hConstant, // scope of constant

LPCSTR pName          // name of constant

);

Hconstant——我们要取得的父结构中变量句柄的D3DXHANDLE标识。例如,如果我们想获得一个特定数据结构中单一数据成员的句柄,我们可以传递结构实例的句柄。如果我们获得一个顶级变量的句柄,给这个参数设为NULL。

PName——我们想获得的句柄的着色器代码中的变量的名字。

Gets a constant by looking up its name.

D3DXHANDLE GetConstantByName(  D3DXHANDLE hConstant,  LPCSTR pName);

Parameters

hConstant
[in] Unique identifier to the parent data structure. If the constant is a top-level parameter (there is no parent data structure), use NULL.
pName
[in] Name of the constant.

Return Values

Returns a unique identifier to the constant.

例如,如果在着色器中变量的名字为ViewProjMatrix,并且这是顶级变量,我们这么写:

// 取得着色器中ViewProjMatrix变量的句柄

D3DXHANDLE h0;

h0 = ConstTable->GetConstantByName(0, "ViewProjMatrix");

16.2.1.2 设置常量

一旦应用程序有了一个D3DXHANDLE,要引用着色器代码中的具体变量,我们可以在应用程序中使用ID3DXConstantTable::SetXXX方法设置变量。如果我们想设置一个向量数组类型的变量,方法名是SetVectorArray。

ID3DXConstantTable::SetXXX的一般语法是:

HRESULT ID3DXConstantTable::SetXXX(

LPDIRECT3DDEVICE9 pDevice,

D3DXHANDLE hConstant,

XXX value

);

PDevice:常量表所关联的设备的指针。

HConstant:我们正在设置的变量句柄的引用。

Value:我们要把变量设置成的值,XXX是我们设置的要替换的变量类型名,对于有些类型(bool, int, float),传递变量值的COPY,另外一些类型(vectors, matrices, structures),传递值的指针。

下面的列表描述了我们能用ID3DXConstantTable接口设置的类型列表。这里假定我们有一个有效的设备,和一个有效句柄。

SetBool—Used to set a Boolean value. Sample call:

bool b = true;

ConstTable->SetBool(Device, handle, b);

Sets a Boolean value.

HRESULT SetBool(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  BOOL b);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the constant. See D3DXHANDLE.
b
[in] Boolean value.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetBoolArray—Used to set a Boolean array. Sample call:

bool b[3] = {true, false, true};

ConstTable->SetBoolArray(Device, handle, b, 3);

Sets an array of Boolean values.

HRESULT SetBoolArray(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  CONST BOOL* pB,  UINT Count);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the array of constants. See D3DXHANDLE.
pB
[in] Array of Boolean values.
Count
[in] Number of Boolean values in the array.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetFloat—Used to set a float. Sample call:

float f = 3.14f;

ConstTable->SetFloat(Device, handle, f);

Sets a floating-point number.

HRESULT SetFloat(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  FLOAT f);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the constant. See D3DXHANDLE.
f
[in] Floating-point number.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetFloatArray—Used to set a float array. Sample call:

float f[2] = {1.0f, 2.0f};

ConstTable->SetFloatArray(Device, handle, f, 2);

Sets an array of floating-point numbers.

HRESULT SetFloatArray(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  CONST FLOAT* pf,  UINT Count);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the array of constants. See D3DXHANDLE.
pf
[in] Array of floating-point numbers.
Count
[in] Number of floating-point values in the array.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetInt—Used to set an integer. Sample call:

int x = 4;

ConstTable->SetInt(Device, handle, x);

Sets an integer value.

HRESULT SetInt(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  INT n);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the constant. See D3DXHANDLE.
n
[in] Integer.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetMatrix—Used to set a 4 × 4 matrix. Sample call:

D3DXMATRIX M(…);

ConstTable->SetMatrix(Device, handle, &M);

Sets a nontransposed matrix.

HRESULT SetMatrix(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  CONST D3DXMATRIX* pMatrix);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the matrix of constants. See D3DXHANDLE.
pMatrix
[in] Pointer to a nontransposed matrix. See D3DXMATRIX.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetMatrixArray—Used to set a 4 × 4 matrix array. Sample call:

D3DXMATRIX M[4];

// ...Initialize matrices

ConstTable->SetMatrixArray(Device, handle, M, 4);

Sets an array of nontransposed matrices.

HRESULT SetMatrixArray(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  CONST D3DXMATRIX* pMatrix,  UINT Count);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to the array of constant matrices.
pMatrix
[in] Array of nontransposed matrices.
Count
[in] Number of matrices in the array.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

SetMatrixPointerArray—Used to set an array of 4 × 4 matrix pointers. Sample call:

D3DXMATRIX* M[4];

// ...Allocate and initialize matrix pointers

ConstTable->SetMatrixPointerArray(Device, handle, M, 4);

Sets an array of pointers to nontransposed matrices.

HRESULT SetMatrixPointerArray(  LPDIRECT3DDEVICE9 pDevice,  D3DXHANDLE hConstant,  CONST D3DXMATRIX ** ppMatrix,  UINT Count);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table.
hConstant
[in] Unique identifier to an array of constant matrices.
ppMatrix
[in] Array of pointers to nontransposed matrices.
Count
[in] Number of matrices in the array.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

Remarks

A nontransposed matrix contains row-major data; that is, each vector is contained in a row.

高级着色语言HLSL入门(2)相关推荐

  1. 《高级着色语言HLSL入门》系列文章

    此系列文章由 博客园 天行健 归纳整理,此处仅作收藏与共享之用! 如欲转载该系列文章,需按如下方式于文章起始位置显式标明文章作者以及原文出处,以示尊重!! 文章作者:天行健 君子当自强而不息 原文出处 ...

  2. 高级着色语言HLSL入门(7)

    高级着色语言HLSL入门(7) max (Direct3D 9 HLSL) Selects the greater of x and y. Syntax ret max(x, y) Where: Na ...

  3. 高级着色语言HLSL入门(6)

    高级着色语言HLSL入门(6) 16.7内建函数 HLSL有一个丰富的内建函数的集合,它们对3D图形来说非常有用.下表是一个删减了的列表,注意:要得到更多的参考,可以参看DirectX文档中内建HLS ...

  4. 高级着色语言HLSL入门(5)

    高级着色语言HLSL入门(5) 16.4关键字.语句和强制转换 16.4.1 关键字 为便于参考,这里给出一个HLSL定义的关键字列表: asm        bool        compile  ...

  5. 高级着色语言HLSL入门(4)

    高级着色语言HLSL入门(4) 16.3 变量类型 注意:除了下列各小节中描述的类型外,HLSL还有一些内建的对象类型(如:纹理对象). 16.3.1 数值类型 HLSL支持下列数值类型(scalar ...

  6. 高级着色语言HLSL入门(3)

    高级着色语言HLSL入门(3) SetMatrixTranspose-Used to set a transposed 4 × 4 matrix. Sample call: D3DXMATRIX M( ...

  7. 高级着色语言HLSL入门(1)

    高级着色语言HLSL入门(1) 在我们写的程序里顶点和像素是很小的对象,它们由GPU来执行,是固定功能管线的一部分.用我们自己写的着色器程序替换一部分固定功能管线,在绘制效果上我们获得很大的灵活性.我 ...

  8. 高级着色语言HLSL入门

    http://www.bullock.cn/blogs/geeker/archives/68700.aspx 高级着色语言HLSL入门(1) 在我们写的程序里顶点和像素是很小的对象,它们由GPU来执行 ...

  9. DirectX 高级着色语言HLSL入门

    为了编写着色器程序,我们需要一种高级着色器语言(High-Level Shading Language ,简称HLSL) . 在DirectX 8中,着色器是用低级着色器汇编语言编写的. Direct ...

最新文章

  1. 非常有创意的音乐网站
  2. 2019年Java架构师必读书籍
  3. websphere一直安装部署_WebSphere集群安装配置及部署应用说明
  4. pytorch之BatchNorm
  5. python中定义的类的方法调用老出现missing 1 postional argument 或者self的解决办法
  6. 工具的使用——windows操作系统的使用(创建宽带连接)
  7. 用 Graphviz+pvtrace 可视化函数调用
  8. Linux QQ 2.0 Beta版初体验
  9. 哈利波特英文单词统计频率
  10. 2022年11月网络工程师考试知识点分布
  11. 少年派的奇幻漂流 Life of Pi (2012)
  12. 1103 Integer Factorization (30分)
  13. 算法的时间与空间复杂度(一看就懂)
  14. 塑胶卡扣弹性计算公式_弹性的计算方法
  15. 51系列密码锁:Proteus仿真+Keil程序(免费提供)
  16. Maven安装与配置详解、多镜像节点的配置
  17. JavaScript中的name和classname有什么区别
  18. Unity Awake OnEnable Start 和Update 先后顺序探究
  19. Unity接入QuickSdk实战
  20. 输入压缩空间量是分区量吗_win7系统压缩分区的操作教程

热门文章

  1. JQ实现单击按钮 倒计时获取验证码
  2. 洛谷——P1320 压缩技术(续集版)
  3. DoWhile(Java)
  4. vue递归组件eventBus策略解决父子组件通讯问题
  5. [sitemap 索引情况提示] 根据 sitemap 的规则[0],当前页面 [pages/loading/loading] 将被索引
  6. Open3d之python版本快速安装和使用
  7. 什么叫侧面指纹识别_哪种指纹识别方式好?侧边指纹识别可能会成为主流
  8. mybatis 依赖于jdbc_第1章 MyBatis快速入门
  9. python语言与c语言相比在分支结构上有什么不同_C语言顺序结构和分支结构总结...
  10. C# 创建、部署和调用WebService的简单示例