当前位置:网站首页>Row and column differences in matrix construction of DX HLSL and GL glsl
Row and column differences in matrix construction of DX HLSL and GL glsl
2022-06-24 22:33:00 【Jave. Lin】
List of articles
Purpose
I've been doing some work recently DXBC to HLSL The job of , Some of them DX HLSL Medium mul(pos, matrix) and GL GLSL Medium mul(matrix, pos) The difference of
To make it easier to do shader reverse , So keep a record DX, GL Some of the differences in
problem

above In the red box and Green box The code in , I can probably see that Some matrix transformation operations
But revert to Corresponding HLSL when , Or see some HLSL Example time , Find out DX, GL Of mul The usage function Have lost lose Different
these DX And GL The difference is really annoying , Easy to forget , and Unity It uses column major The way To build the structure
But the operation is GL To build the matrix , I almost lost my mind ...
Write it down again , Otherwise, it's easy to forget 、 Mix up ( After all, I don't use it often Microsoft HLSL、FXC compiler )
In the picture above A piece of code can actually be rewritten to correspond to HLSL in 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 : above 2~5 lines Of DXBC The code analysis , Can be equivalent to
float4x4 matrixL2W = {
// jave.lin : local to world matrix
cb2[r0.x + 0].xxyz, // jave.lin : New coordinate base :x Axis
cb2[r0.x + 1].xxyz, // jave.lin : New coordinate base :Y Axis
cb2[r0.x + 2].xxyz, // jave.lin : New coordinate base :Z Axis
cb2[r0.x + 3].xxyz // jave.lin : Radially transformed xyz Translation
};
r0.yzw = mul(float4(IN.positionOS, 1.0), matrixL2W);
From here DXBC It looks good , Because I used to GL Medium Matrix left multiplication , But it's written HLSL Words , We need to pay attention to DX and GL The difference between
I'm on my own excel Some pictures are drawn to summarize 
OpenGL Medium matrix[0] What is taken is Column
DirectX Medium matrix[0] What is taken is That's ok
Here are some sample code , and official One or two paragraphs describe and Code
show me the code, talk is cheap.
Example
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 That's ok ride pos Column
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 That's ok ride model_mat Column
file
Let's refer to :DirectX Medium matrix type Of Component operation :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 Medium matrix type Of Component operation :Per-Component Math Operations - matrix type
边栏推荐
- NIO、BIO、AIO
- CSRF and SSRF for web attacks
- Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
- 嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码
- leetcode:55. Jumping game [classic greed]
- Embedded development: tips and tricks -- clean jump from boot loader to application code
- How to automatically remove all . orig files in Mercurial working tree?
- 直播软件app开发,左右自动滑动的轮播图广告
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- 关于自动控制原理资料更新
猜你喜欢

Data center basic network platform

A pit in try with resources

DAO 中常见的投票治理方式

Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022

Raspberry pie preliminary use

try-with-resources 中的一个坑,注意避让

嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码

Use of selector for NiO multiplexing

PostMan工具介绍及安装使用
![Find the maximum value in each tree row [extension of one of the hierarchical traversals]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
Find the maximum value in each tree row [extension of one of the hierarchical traversals]
随机推荐
Genesis公链与美国一众加密投资者齐聚Consensus 2022
堆內存分配的並發問題
leetcode:45. 跳跃游戏 II【经典贪心】
Future development of education industry of e-commerce Express
ansible基本配置
How to automatically remove all . orig files in Mercurial working tree?
Use of selector for NiO multiplexing
The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
What aspects should we start with in the feasibility analysis of dry goods?
字符串习题总结2
Resolving the conflict problem of the flutter Library
How to solve the problem that the computer suddenly can't connect to WiFi
[Software Engineering] key points at the end of the period
Raspberry pie preliminary use
Docker installs MySQL 8.0. Detailed steps
Huada 04A operating mode / low power consumption mode
Zero code can apply data visualization to enterprise management
堆内存分配的并发问题
NIO多路复用之Selector的使用