当前位置:网站首页>Understand encodefloatrgba and decodefloatrgba
Understand encodefloatrgba and decodefloatrgba
2022-07-05 04:47:00 【Haro3378】
Recently, I'm looking at various methods of rendering shadows , Found generating ShadowMap There is a very common compression method , But you can only find the code on the Internet , I can't see a more detailed explanation , So here is an explanation that I think is relatively easy to understand .
inline float4 EncodeFloatRGBA( float v ) {
float4 enc = float4(1.0, 255.0, 65025.0, 16581375.0) * v;
enc = frac(enc);
enc -= enc.yzww * float4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
return enc;
}
inline float DecodeFloatRGBA( float4 rgba ) {
return dot( rgba, float4(1.0, 1/255.0, 1/65025.0, 1/16581375.0) );
}
First, let's introduce the usage scenarios
ShadowMap Saved when viewing the scene from the light angle , The depth value corresponding to each pixel , The scope is [0,1], This value is later Pass It is used to judge whether it is in the shadow , So we need to save this value , Because calculation ShadowMap Is in a separate Pass in , So you can render it to the map .
We also need to understand the accuracy , float The accuracy of the type is 4btye, Maps have various formats , But in order to 4btye The data is completely saved , We choose R8G8B8A8 Format map as the rendering target .
Now let's calculate step by step
First of all, will float As four parts 8bit constitute :
Because we chose RT yes R8G8B8A8, So it happens that each channel stores 8bit data . The four channels are independent , So the natural way is right float Four parts of data are shifted , Then store them separately , These descriptions may still be unclear , You can see the diagram below .
1. First, the data of four parts are “ alignment ”.
about b、c、d, Separate shift 8、16、24, such abcd It appears in a dimension ,
This corresponds to the code
float4 enc = float4(1.0, 255.0, 65025.0, 16581375.0) * v;
The result is like this :
The first line is multiplicative 1, So it can be regarded as raw data . In the end, we just need to save the data in the Yellow grid .
2. Delete extra data
First of all, remember , Because it's handled ShadowMap Depth in ,float The scope of 0~1, So for beyond 32 Bit data can be regarded as greater than 1 Part of , Delete these data first , This corresponds to the method Frac
The result is shown in Fig. :
Then we need to deal with 0-24 A data , It can be found by observation that , There are two parts of the same data !
So we can continue the displacement , Let the two parts be subtracted . Before will float ride 2^n It's moving left , Then the division operation is shifted to the right , Extract YZW Divide the vector by the corresponding number of digits ( Because the previous data is XYZW, So in order to match , We use YZWW To calculate , But the last one W By 0).
This step corresponds to the code
enc -= enc.yzww * float4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
After this step, only the yellow box is left , At this time, the data is directly stored in R8G8B8A8 You won't lose accuracy .Decode and Encode Is a similar idea , It's just the opposite .
边栏推荐
- [groovy] closure (closure call | closure default parameter it | code example)
- Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
- Label exchange experiment
- Uncover the seven quirky brain circuits necessary for technology leaders
- Function (basic: parameter, return value)
- jmeter -- 分布式压测
- Scope of package class package
- 官宣!第三届云原生编程挑战赛正式启动!
- Introduce Hamming distance and calculation examples
- Mxnet imports various libcudarts * so、 libcuda*. So not found
猜你喜欢
介绍汉明距离及计算示例
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
程序员应该怎么学数学
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
XSS injection
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Label exchange experiment
2022-2028 global and Chinese virtual data storage Market Research Report
随机推荐
TPG x AIDU | AI leading talent recruitment plan in progress!
How to carry out "small step reconstruction"?
level18
Invalid bound statement (not found) in idea -- problem solving
Function template
线上故障突突突?如何紧急诊断、排查与恢复
775 Div.1 B. integral array mathematics
Mode in BST (binary tree & Notes on question brushing)
Here comes the Lantern Festival red envelope!
Components in protective circuit
程序员应该怎么学数学
Download the details and sequence of the original data access from the ENA database in EBI
[groovy] closure (closure as function parameter | code example)
Raki's notes on reading paper: code and named entity recognition in stackoverflow
假设检验——《概率论与数理统计》第八章学习笔记
Practice | mobile end practice
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
49 pictures and 26 questions explain in detail what is WiFi?
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem