当前位置:网站首页>DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别
DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别
2022-06-24 19:35:00 【Jave.Lin】
目的
最近在做一些 DXBC to HLSL 的工作,其中遇到一些 DX HLSL 中的 mul(pos, matrix) 和 GL GLSL 中的 mul(matrix, pos) 的差异
为了更加方便做 shader 逆向,所以多记录一下 DX, GL 中的一些区别
问题

上面 红色框中 和 绿色框 中的代码,我大概看得出来就是 一些矩阵变换的运算
但是还原到 对应HLSL 时,或是看到一些 HLSL 示例时,发现 DX, GL 的 mul 函数用法 有丢丢 不同
这些 DX 与 GL 差异真的很烦,容易忘记,而且 Unity 使用的是 column major 的方式 来构建结构体
但是运算又是 GL 的方式来构建矩阵,差点把我懵逼了。。。
再记一次吧,不然容易忘记、搞混(毕竟没有经常去用 微软的 HLSL、FXC 编译器)
上图中的 一段代码其实可以改写为对应 HLSL 中 mul(floatN, matrixNxN)
// 2: mul r0.yzw, v0.yyyy, cb2[r0.x + 1].xxyz
r0.yzw = IN.positionOS.yyyy * cb2[r0.x + 1].xxyz;
// 3: mad r0.yzw, cb2[r0.x + 0].xxyz, v0.xxxx, r0.yyzw
r0.yzw = cb2[r0.x + 0].xxyz * IN.positionOS.xxxx + r0.yyzw;
// 4: mad r0.yzw, cb2[r0.x + 2].xxyz, v0.zzzz, r0.yyzw
r0.yzw = cb2[r0.x + 2].xxyz * IN.positionOS.zzzz + r0.yyzw;
// 5: add r0.yzw, r0.yyzw, cb2[r0.x + 3].xxyz
r0.yzw = r0.yyzw + cb2[r0.x + 3].xxyz;
// jave.lin : 上面 2~5 lines的 DXBC 代码分析,可以等价于
float4x4 matrixL2W = {
// jave.lin : local to world matrix
cb2[r0.x + 0].xxyz, // jave.lin : 新坐标基:x轴
cb2[r0.x + 1].xxyz, // jave.lin : 新坐标基:Y轴
cb2[r0.x + 2].xxyz, // jave.lin : 新坐标基:Z轴
cb2[r0.x + 3].xxyz // jave.lin : 放射变换的 xyz 平移量
};
r0.yzw = mul(float4(IN.positionOS, 1.0), matrixL2W);
从这 DXBC 看着还好,因为我之前习惯了 GL 中的 矩阵左乘写法,但是写成 HLSL 的话,就需要注意 DX 和 GL 的区别
我自己在 excel 中画了一些图来总结
OpenGL 中的 matrix[0] 取的是 列
DirectX 中的 matrix[0] 取的是 行
下面列出一些示例代码,和 官方 一两段 描述 和 代码
show me the code, talk is cheap.
示例
OpenGL:
vec3 pos = ...;
mat3x3 model_mat = mat3x3(
vec3(0,0,0), // column 1
vec3(1,1,1), // column 2
vec3(2,2,2), // column 3
);
vec3 posWS = mul(model_mat, pos); // model_mat 行 乘 pos 列
DirectX:
float3 pos = ...;
float3x3 model_mat = {
0, 0, 0, // row 1
1, 1, 1, // row 2
2, 2, 2 // row 3
};
float3 posWS = mul(pos, model_mat); // pos 行 乘 model_mat 列
文档
下面参考:DirectX 中的 matrix type 的 分量操作:Per-Component Math Operations - matrix type
Overloaded versions of the multiply intrinsic function handle cases where one operand is a vector and the other operand is a matrix. Such as: vector * vector, vector * matrix, matrix * vector, and matrix * matrix. For instance:
float4x3 World;
float4 main(float4 pos : SV_POSITION) : SV_POSITION
{
float4 val;
val.xyz = mul(pos,World);
val.w = 0;
return val;
}
produces the same result as:
float4x3 World;
float4 main(float4 pos : SV_POSITION) : SV_POSITION
{
float4 val;
val.xyz = (float3) mul((float1x4)pos,World);
val.w = 0;
return val;
}
This example casts the pos vector to a column vector using the (float1x4) cast. Changing a vector by casting, or swapping the order of the arguments supplied to multiply is equivalent to transposing the matrix.
Automatic cast conversion causes the multiply and dot intrinsic functions to return the same results as used here:
{
float4 val;
return mul(val,val);
}
This result of the multiply is a 1x4 * 4x1 = 1x1 vector. This is equivalent to a dot product:
{
float4 val;
return dot(val,val);
}
References
- DirectX 中的 matrix type 的 分量操作:Per-Component Math Operations - matrix type
边栏推荐
- socket done
- “阿里健康”们的逻辑早就变了
- Summary of papers on traveling salesman problem (TSP)
- 故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- 嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码
- Embedded development: tips and tricks -- clean jump from boot loader to application code
- YGG 近期游戏合作伙伴一览
- Filtered data analysis
- Main steps of system test
猜你喜欢

Notes on writing questions (18) -- binary tree: common ancestor problem

60 divine vs Code plug-ins!!

如何抓手机的包进行分析,Fiddler神器或许能帮到您!

树莓派初步使用

985测试工程师被吊打,学历和经验到底谁更重要?

The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the

AQS source code analysis

【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割

华大04a工作模式/低功耗模式

性能测试工具wrk安装使用详解
随机推荐
Want to be a test leader, do you know these 6 skills?
Seven principles of software design
关于自动控制原理资料更新
Interrupt, interrupted, isinterrupted differences
Flutter 如何使用在线转码工具将 JSON 转为 Model
Use of selector for NiO multiplexing
学习笔记23--多传感器信息融合基础理论(上)
是真干不过00后,给我卷的崩溃,想离职了...
Information update on automatic control principle
Raspberry pie preliminary use
St Table + two points
Flutter 库冲突问题解决
Find the maximum value in each tree row [extension of one of the hierarchical traversals]
Docker 安装 Redis-5.0.12,详细步骤
Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
socket done
Docker 安装 MySQL 8.0,详细步骤
“阿里健康”们的逻辑早就变了
Rotate the square array of two-dimensional array clockwise by 90 °
Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?