当前位置:网站首页>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 .
边栏推荐
- Discussion on the dimension of confrontation subspace
- Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
- Components in protective circuit
- The difference between bundle, chunk and module
- Invalid bound statement (not found) in idea -- problem solving
- Emlog博客主题模板源码简约好看响应式
- A survey of automatic speech recognition (ASR) research
- Stage experience
- 775 Div.1 B. integral array mathematics
- 直播预告 | 容器服务 ACK 弹性预测最佳实践
猜你喜欢
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Wenet: E2E speech recognition tool for industrial implementation
官宣!第三届云原生编程挑战赛正式启动!
The remainder operation is a hash function
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
Live broadcast preview | container service ack elasticity prediction best practice
【acwing】528. cheese
Matplotlib draws three-dimensional scatter and surface graphs
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
随机推荐
Uncover the seven quirky brain circuits necessary for technology leaders
Setting up redis cluster cluster under Windows
Construction d'un Cluster redis sous Windows
2021 huashubei mathematical modeling idea + reference + paper
How to force activerecord to reload a class- How do I force ActiveRecord to reload a class?
解密函数计算异步任务能力之「任务的状态及生命周期管理」
函数(易错)
2022-2028 global and Chinese virtual data storage Market Research Report
#775 Div.1 C. Tyler and Strings 组合数学
Managed service network: application architecture evolution in the cloud native Era
Number theoretic function and its summation to be updated
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
Practice | mobile end practice
Reading and visualization of DICOM, MHD and raw files in medical imaging
【acwing】528. cheese
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
Solution of circular dependency
CSDN正文自动生成目录
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Stage experience