內建函式
Godot 支援大量內建函式,大致遵循 GLSL ES 3.0 規範。
備註
下列型別別名僅用於文件中,減少重複的函式宣告。每個別名可對應多種實際型別。
別名 |
實際類型 |
glsl 文件別名 |
|---|---|---|
vec_type |
float、vec2、vec3 或 vec4 |
genType |
vec_int_type |
int、ivec2、ivec3 或 ivec4 |
genIType |
vec_uint_type |
uint、uvec2、uvec3 或 uvec4 |
genUType |
vec_bool_type |
bool、bvec2、bvec3 或 bvec4 |
genBType |
mat_type |
mat2、mat3 或 mat4 |
mat |
gvec4_type |
vec4、ivec4 或 uvec4 |
gvec4 |
gsampler2D |
sampler2D、isampler2D 或 uSampler2D |
gsampler2D |
gsampler2DArray |
sampler2DArray、isampler2DArray 或 uSampler2DArray |
gsampler2DArray |
gsampler3D |
sampler3D、isampler3D 或 uSampler3D |
gsampler3D |
若多個參數指定這些型別時,除非特別說明,必須全部為相同型別。
備註
許多接受一個或多個向量或矩陣的函式,會對每個分量進行該操作。例如:
運算 |
等價標量運算 |
|---|---|
|
|
|
|
|
|
|
|
|
|
依據 GLSL 語言規範 第 5.10 節(向量與矩陣運算)說明:
除少數例外,運算皆以分量為單位執行。通常,當運算子用於向量或矩陣時,會對每個分量分別運算。[...] 例外情況為:矩陣乘以向量、向量乘以矩陣、及矩陣乘以矩陣。這些情況不按分量運算,而是進行正確的線性代數乘法。
本頁函式說明改編自 Khronos Group 以 Open Publication License 發布之 OpenGL 官方文件,每個函式均連結至其官方說明。此頁的修改紀錄可於 GitHub 查閱。
三角函式
回傳型別 |
函式 |
描述/回傳值 |
|---|---|---|
vec_type |
radians(vec_type degrees) |
將角度轉換為弧度。 |
vec_type |
degrees(vec_type radians) |
將弧度轉換為角度。 |
vec_type |
sin(vec_type x) |
正弦。 |
vec_type |
cos(vec_type x) |
餘弦。 |
vec_type |
tan(vec_type x) |
正切。 |
vec_type |
asin(vec_type x) |
反正弦。 |
vec_type |
acos(vec_type x) |
反餘弦。 |
vec_type
vec_type
|
反正切。 |
|
vec_type |
sinh(vec_type x) |
雙曲正弦。 |
vec_type |
cosh(vec_type x) |
雙曲餘弦。 |
vec_type |
tanh(vec_type x) |
雙曲正切。 |
vec_type |
asinh(vec_type x) |
反雙曲正弦。 |
vec_type |
acosh(vec_type x) |
反雙曲餘弦。 |
vec_type |
atanh(vec_type x) |
反雙曲正切。 |
三角函式說明
vec_type radians(vec_type degrees) 🔗
分量逐一運算
將以角度表示的數值轉換為弧度,公式為
degrees * (PI / 180)。
- 參數 degrees:
要轉換為弧度的角度數值。
- return:
輸入的
degrees轉換為弧度。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/radians.xhtml
vec_type degrees(vec_type radians) 🔗
分量逐一運算
將以弧度表示的數值轉換為角度,公式為
radians * (180 / PI)
- 參數 radians:
要轉換為角度的弧度數值。
- return:
輸入的
radians轉換為角度。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/degrees.xhtml
vec_type sin(vec_type angle) 🔗
分量逐一運算
回傳
angle的三角正弦值。
- 參數 angle:
以弧度表示,要計算正弦值的數值。
- return:
angle的正弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/sin.xhtml
vec_type cos(vec_type angle) 🔗
分量逐一運算
回傳
angle的三角餘弦值。
- 參數 angle:
以弧度表示,要計算餘弦值的數值。
- return:
angle的餘弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/cos.xhtml
vec_type tan(vec_type angle) 🔗
分量逐一運算
回傳
angle的三角正切值。
- 參數 angle:
以弧度表示,要計算正切值的數值。
- return:
angle的正切值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/tan.xhtml
vec_type asin(vec_type x) 🔗
分量逐一運算
反正弦(arcsin)。計算正弦值為
x的角度,結果範圍為[-PI/2, PI/2]。若x < -1或x > 1,結果未定義。
- 參數 x:
要計算反正弦的值。
- return:
其三角正弦為
x的角度。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/asin.xhtml
vec_type acos(vec_type x) 🔗
分量逐一運算
反餘弦(arccos)。計算餘弦值為
x的角度,結果範圍為[0, PI]。若
x < -1或x > 1,則結果未定義。
- 參數 x:
要計算反餘弦的值。
- return:
其三角餘弦為
x的角度。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/acos.xhtml
vec_type atan(vec_type y_over_x) 🔗
分量逐一運算
根據
y/x的值計算反正切(arctan)。備註
由於正切值有正負號的二義性,僅靠正切值無法判斷角度所屬象限。若需知道象限,請使用 atan(vec_type y, vec_type x)。
- 參數 y_over_x:
要計算反正切的分數值。
- return:
y_over_x的三角反正切值,範圍為[-PI/2, PI/2]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/atan.xhtml
vec_type atan(vec_type y, vec_type x) 🔗
分量逐一運算
給定分子與分母計算反正切值。會根據
y與x的正負來判斷角度所在象限。若x == 0,結果未定義。等同於 GDScript 中的 atan2()。
- 參數 y:
分數的分子(將計算反正切的數值)。
- 參數 x:
分數的分母(將計算反正切的數值)。
- return:
y/x的反正切值,範圍為[-PI, PI]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/atan.xhtml
vec_type sinh(vec_type x) 🔗
分量逐一運算
使用
(e^x - e^-x)/2計算雙曲正弦值(sinh)。
- 參數 x:
要計算雙曲正弦的值。
- return:
x的雙曲正弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/sinh.xhtml
vec_type cosh(vec_type x) 🔗
分量逐一運算
使用
(e^x + e^-x)/2計算雙曲餘弦值(cosh)。
- 參數 x:
要計算雙曲餘弦的值。
- return:
x的雙曲餘弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/cosh.xhtml
vec_type tanh(vec_type x) 🔗
分量逐一運算
使用
sinh(x)/cosh(x)計算雙曲正切值(tanh)。
- 參數 x:
要計算雙曲正切的值。
- return:
x的雙曲正切值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/tanh.xhtml
vec_type asinh(vec_type x) 🔗
分量逐一運算
計算
x的反雙曲正弦值(asinh),即sinh的反函數。
- 參數 x:
要計算反雙曲正弦的值。
- return:
x的反雙曲正弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/asinh.xhtml
vec_type acosh(vec_type x) 🔗
分量逐一運算
計算
x的反雙曲餘弦值(acosh),即cosh的非負反函數。若x < 1,結果未定義。
- 參數 x:
要計算反雙曲餘弦的值。
- return:
x的反雙曲餘弦值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/acosh.xhtml
vec_type atanh(vec_type x) 🔗
分量逐一運算
計算
x的反雙曲正切值(atanh),即tanh的反函數。若abs(x) > 1,結果未定義。
- 參數 x:
要計算反雙曲正切的值。
- return:
x的反雙曲正切值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/atanh.xhtml
指數與數學函式
回傳型別 |
函式 |
描述/回傳值 |
|---|---|---|
vec_type |
pow(vec_type x, vec_type y) |
次方運算(若 |
vec_type |
exp(vec_type x) |
以 e 為底的指數。 |
vec_type |
exp2(vec_type x) |
以 2 為底的指數。 |
vec_type |
log(vec_type x) |
自然對數(以 e 為底)。 |
vec_type |
log2(vec_type x) |
以 2 為底的對數。 |
vec_type |
sqrt(vec_type x) |
平方根。 |
vec_type |
inversesqrt(vec_type x) |
反平方根。 |
vec_type
vec_int_type
|
絕對值(若為負則傳回正值)。 |
|
vec_type |
sign(vec_type x) |
若為正回傳 |
vec_int_type |
sign(vec_int_type x) |
若為正回傳 |
vec_type |
floor(vec_type x) |
無條件捨去至較小整數。 |
vec_type |
round(vec_type x) |
四捨五入至最接近整數。 |
vec_type |
roundEven(vec_type x) |
四捨五入至最接近的偶數整數。 |
vec_type |
trunc(vec_type x) |
截斷。 |
vec_type |
ceil(vec_type x) |
無條件進位至較大整數。 |
vec_type |
fract(vec_type x) |
小數(返回“x - Floor(x)”)。 |
vec_type
vec_type
|
取餘數(模運算)。 |
|
vec_type |
modf(vec_type x, out vec_type i) |
|
vec_type
vec_type
vec_int_type
vec_int_type
vec_uint_type
vec_uint_type
|
|
|
vec_type
vec_type
vec_uint_type
vec_uint_type
vec_int_type
vec_int_type
|
|
|
vec_type
vec_type
vec_uint_type
vec_uint_type
vec_int_type
vec_int_type
|
將 |
|
vec_type
vec_type
vec_type
|
根據 |
|
vec_type |
fma(vec_type a, vec_type b, vec_type c) |
融合乘加運算: |
vec_type
vec_type
|
|
|
vec_type
vec_type
|
smoothstep(vec_type a, vec_type b, vec_type c)
smoothstep(float a, float b, vec_type c)
|
以 Hermite 方式根據 |
vec_bool_type |
isnan(vec_type x) |
若標量或向量分量為 |
vec_bool_type |
isinf(vec_type x) |
若標量或向量分量為 |
vec_int_type |
floatBitsToInt(vec_type x) |
|
vec_uint_type |
floatBitsToUint(vec_type x) |
|
vec_type |
intBitsToFloat(vec_int_type x) |
|
vec_type |
uintBitsToFloat(vec_uint_type x) |
|
指數與數學函式說明
vec_type pow(vec_type x, vec_type y) 🔗
分量逐一運算
將
x提升到y次方。若
x < 0,或x == 0且y <= 0,則結果未定義。
- 參數 x:
要被提升次方的數值
x。- 參數 y:
x要被提升的次方y。- return:
x的y次方。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/pow.xhtml
vec_type exp(vec_type x) 🔗
分量逐一運算
將數學常數 e 提升到
x次方,即自然指數。等同於
pow(e, x)。
- 參數 x:
要計算指數的數值。
- return:
x的自然指數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/exp.xhtml
vec_type exp2(vec_type x) 🔗
分量逐一運算
將 2 提升到
x次方。等同於
pow(2.0, x)。
- 參數 x:
2要被提升的次方值。- return:
2 的
x次方。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/exp2.xhtml
vec_type log(vec_type x) 🔗
分量逐一運算
回傳
x的自然對數(滿足x == pow(e, y)的y)。若x <= 0結果未定義。
- 參數 x:
要取自然對數的數值。
- return:
x的自然對數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/log.xhtml
vec_type log2(vec_type x) 🔗
分量逐一運算
回傳
x的以 2 為底的對數(滿足x == pow(2, y)的y)。若x <= 0結果未定義。
- 參數 x:
要取以 2 為底對數的數值。
- return:
x的以 2 為底對數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/log2.xhtml
vec_type sqrt(vec_type x) 🔗
分量逐一運算
回傳
x的平方根。若x < 0則結果未定義。
- 參數 x:
要取平方根的數值。
- return:
x的平方根。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/sqrt.xhtml
vec_type inversesqrt(vec_type x) 🔗
分量逐一運算
回傳
x的反平方根(即1.0 / sqrt(x))。若x <= 0則結果未定義。
- 參數 x:
要取反平方根的數值。
- return:
x的反平方根。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/inversesqrt.xhtml
vec_type abs(vec_type x) 🔗
vec_int_type abs(vec_int_type x) 🔗
分量逐一運算
回傳
x的絕對值。如果x為正則回傳x,否則回傳-1 * x。
- 參數 x:
要取絕對值的數值。
- return:
x的絕對值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/abs.xhtml
vec_type sign(vec_type x) 🔗
vec_int_type sign(vec_int_type x) 🔗
分量逐一運算
若
x < 0回傳-1,若x == 0回傳0,若x > 0回傳1。
- 參數 x:
要判斷正負號的數值。
- return:
x的正負號。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/sign.xhtml
vec_type floor(vec_type x) 🔗
分量逐一運算
回傳小於或等於
x的最大整數。
- 參數 x:
要向下取整的數值。
- return:
小於或等於
x的最近整數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/floor.xhtml
vec_type round(vec_type x) 🔗
分量逐一運算
將
x四捨五入到最接近的整數。備註
對於小數部分為
0.5的數值,其四捨五入方式取決於實作。這也代表round(x)在所有x的情況下,可能會與roundEven(x)回傳相同的結果。
- 參數 x:
要四捨五入的數值。
- return:
四捨五入的結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/round.xhtml
vec_type roundEven(vec_type x) 🔗
分量逐一運算
將
x四捨五入到最接近的整數。若小數部分為0.5,則總是四捨五入至最近的偶數,例如3.5和4.5都會變成4.0。
- 參數 x:
要四捨五入的數值。
- return:
四捨五入的結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/roundEven.xhtml
vec_type trunc(vec_type x) 🔗
分量逐一運算
截斷
x,回傳離x最近且絕對值不大於x的整數。
- 參數 x:
要評估的值。
- return:
截斷後的數值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/trunc.xhtml
vec_type ceil(vec_type x) 🔗
分量逐一運算
回傳大於或等於
x的最小整數。
- 參數 x:
要評估的值。
- return:
向上取整的結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/ceil.xhtml
vec_type fract(vec_type x) 🔗
分量逐一運算
回傳
x的小數部分。計算方式為
x - floor(x)。
- 參數 x:
要評估的值。
- return:
x的小數部分。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/fract.xhtml
vec_type mod(vec_type x, vec_type y) 🔗
vec_type mod(vec_type x, float y) 🔗
分量逐一運算
回傳
x對y的餘數(模),亦即x modulo y。計算方式為
x - y * floor(x/y)。
- 參數 x:
要評估的值。
- return:
x對y取模的結果。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mod.xhtml
vec_type modf(vec_type x, out vec_type i) 🔗
分量逐一運算
將浮點數
x拆分為整數部分與小數部分。函式回傳數值的小數部分,整數部分(以浮點型別表示)則回傳於輸出參數
i。
- 參數 x:
要拆分的數值。
- 輸出參數 i:
用來接收
x的整數部分的變數。- return:
數值的小數部分。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/modf.xhtml
vec_type min(vec_type a, vec_type b) 🔗
vec_type min(vec_type a, float b) 🔗
vec_int_type min(vec_int_type a, vec_int_type b) 🔗
vec_int_type min(vec_int_type a, int b) 🔗
vec_uint_type min(vec_uint_type a, vec_uint_type b) 🔗
vec_uint_type min(vec_uint_type a, uint b) 🔗
分量逐一運算
回傳
a與b兩值中的最小值。若
b < a則回傳b,否則回傳a。
- 參數 a:
第一個要比較的值。
- 參數 b:
第二個要比較的值。
- return:
最小值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/min.xhtml
vec_type max(vec_type a, vec_type b) 🔗
vec_type max(vec_type a, float b) 🔗
vec_uint_type max(vec_uint_type a, vec_uint_type b) 🔗
vec_uint_type max(vec_uint_type a, uint b) 🔗
vec_int_type max(vec_int_type a, vec_int_type b) 🔗
vec_int_type max(vec_int_type a, int b) 🔗
分量逐一運算
回傳
a與b兩值中的最大值。若
b > a則回傳b,否則回傳a。
- 參數 a:
第一個要比較的值。
- 參數 b:
第二個要比較的值。
- return:
最大值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/max.xhtml
vec_type clamp(vec_type x, vec_type minVal, vec_type maxVal) 🔗
vec_type clamp(vec_type x, float minVal, float maxVal) 🔗
vec_type clamp(vec_type x, float minVal, float maxVal) 🔗
vec_uint_type clamp(vec_int_type x, float minVal, float maxVal) 🔗
vec_int_type clamp(vec_type x, vec_type minVal, vec_type maxVal) 🔗
vec_int_type clamp(vec_type x, float minVal, float maxVal) 🔗
分量逐一運算
將
x夾制在minVal和maxVal之間(包含兩端點)。回傳值計算方式為
min(max(x, minVal), maxVal)。
- 參數 x:
要限制範圍的值。
- 參數 minVal:
用於限制
x的下界。- 參數 maxVal:
用於限制
x的上界。- return:
夾制後的值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/clamp.xhtml
vec_type mix(vec_type a, vec_type b, vec_type c) 🔗
vec_type mix(vec_type a, vec_type b, float c) 🔗
分量逐一運算
根據
c權重在線性插值a與b之間。依公式
a * (1 - c) + b * c計算。等同於 GDScript 中的 lerp()。
- 參數 a:
內插範圍的起點。
- 參數 b:
內插範圍的終點。
- 參數 c:
作為
a與b之間插值的權重。- return:
插值結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mix.xhtml
vec_type mix(vec_type a, vec_type b, vec_bool_type c) 🔗
根據
c的值選擇a或b。若c的某分量為 false,則回傳a對應分量,為 true 則回傳b對應分量。未被選取的a與b分量可為無效浮點數,對結果無影響。若
a、b、c皆為向量型別,則會 逐分量運算。例如mix(vec2(42, 314), vec2(9.8, 6e23), bvec2(true, false))會回傳vec2(9.8, 314)。
- 參數 a:
當
c為 false 時回傳的值。- 參數 b:
當
c為 true 時回傳的值。- 參數 c:
用於選擇
a或b的值。- return:
插值結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mix.xhtml
vec_type fma(vec_type a, vec_type b, vec_type c) 🔗
分量逐一運算
在可能的情況下,執行融合乘加運算(fused multiply-add),回傳
a * b + c。若回傳值最終被賦值給宣告為 precise 的變數:
fma()被視為單一運算,而a * b + c若被 precise 變數消耗則視為兩次運算。
fma()的精度可能與a * b + c表達式不同。
fma()在被 precise 變數消耗時,會以一致的精度進行運算,對於相同的 a、b、c 輸入值會得到不變的結果。否則,若未被 precise 變數消耗,則對於
fma()與a * b + c之間的運算次數或精度沒有特殊限制。
- 參數 a:
第一個要相乘的值。
- 參數 b:
第二個要相乘的值。
- 參數 c:
要加到結果上的值。
- return:
a * b + c的結果。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/fma.xhtml
vec_type step(vec_type a, vec_type b) 🔗
vec_type step(float a, vec_type b) 🔗
分量逐一運算
比較 b 與 a,產生階梯函式。
等同於
if (b < a) { return 0.0; } else { return 1.0; }。對於回傳值的第 i 個元素,若 b[i] < a[i] 則回傳 0.0 ,否則回傳 1.0 。
- 參數 a:
階梯函式的邊緣位置。
- 參數 b:
用於產生階梯函式的值。
- return:
0.0或1.0。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/step.xhtml
vec_type smoothstep(vec_type a, vec_type b, vec_type c) 🔗
vec_type smoothstep(float a, float b, vec_type c) 🔗
分量逐一運算
當 a < c < b 時,在
0與1之間執行平滑 Hermite 插值。這在需平滑過渡閾值函式時非常有用。Smoothstep 等同於:
vec_type t; t = clamp((c - a) / (b - a), 0.0, 1.0); return t * t * (3.0 - 2.0 * t);若
a >= b,結果未定義。
- 參數 a:
Hermite 函式的下界值。
- 參數 b:
Hermite 函式的上界值。
- 參數 c:
內插的來源值。
- return:
插值結果。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/smoothstep.xhtml
vec_bool_type isnan(vec_type x) 🔗
分量逐一運算
對於結果的每個分量 i,若 x[i] 為正或負的浮點 NaN(非數值)則回傳
true,否則回傳false。
- 參數 x:
要測試是否為 NaN 的值。
- return:
true或false。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/isnan.xhtml
vec_bool_type isinf(vec_type x) 🔗
分量逐一運算
對於結果的每個分量 i,若 x[i] 為正或負的浮點無窮大則回傳
true,否則回傳false。
- 參數 x:
要測試是否為無限大的值。
- return:
true或false。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/isinf.xhtml
vec_int_type floatBitsToInt(vec_type x) 🔗
分量逐一運算
將浮點參數的編碼以
int型態回傳。會保留浮點數的位元層級表示方式。
- 參數 x:
欲獲取其浮點編碼的數值。
- return:
x的浮點編碼值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/floatBitsToInt.xhtml
vec_uint_type floatBitsToUint(vec_type x) 🔗
分量逐一運算
將浮點參數的編碼以
uint型態回傳。會保留浮點數的位元層級表示方式。
- 參數 x:
欲獲取其浮點編碼的數值。
- return:
x的浮點編碼值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/floatBitsToInt.xhtml
vec_type intBitsToFloat(vec_int_type x) 🔗
分量逐一運算
將位元編碼轉換為浮點數值。與 floatBitsToInt<shader_func_floatBitsToInt> 相反
若
x為 NaN 的位元編碼,則不會產生訊號且結果未定義。若
x為無窮大的位元編碼,則結果為對應的(正或負)浮點無窮大。
- 參數 x:
要作為浮點值返回的位元編碼。
- return:
浮點值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/intBitsToFloat.xhtml
vec_type uintBitsToFloat(vec_uint_type x) 🔗
分量逐一運算
將位元編碼轉換為浮點數值。功能與 floatBitsToUint<shader_func_floatBitsToUint> 相反
若
x為 NaN 的位元編碼,則不會產生訊號且結果未定義。若
x為無窮大的位元編碼,則結果為對應的(正或負)浮點無窮大。
- 參數 x:
要作為浮點值返回的位元編碼。
- return:
浮點值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/intBitsToFloat.xhtml
幾何函式
浮點數 |
length(vec_type x) |
向量長度。 |
浮點數 |
distance(vec_type a, vec_type b) |
向量間距(即 |
浮點數 |
dot(vec_type a, vec_type b) |
點積。 |
vec3 |
cross(vec3 a, vec3 b) |
外積。 |
vec_type |
normalize(vec_type x) |
標準化為單位長度。 |
vec3 |
reflect(vec3 I, vec3 N) |
反射。 |
vec3 |
refract(vec3 I, vec3 N, float eta) |
折射。 |
vec_type |
faceforward(vec_type N, vec_type I, vec_type Nref) |
若 |
mat_type |
matrixCompMult(mat_type x, mat_type y) |
矩陣分量乘法。 |
mat_type |
outerProduct(vec_type column, vec_type row) |
矩陣外積。 |
mat_type |
transpose(mat_type m) |
矩陣轉置。 |
浮點數 |
determinant(mat_type m) |
矩陣行列式。 |
mat_type |
inverse(mat_type m) |
逆矩陣。 |
幾何函式說明
float length(vec_type x) 🔗
回傳向量的長度,例如
sqrt(x[0] * x[0] + x[1] * x[1] + ... + x[n] * x[n])
- 參數 x:
向量
- return:
向量的長度。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/length.xhtml
float distance(vec_type a, vec_type b) 🔗
回傳兩點 a 與 b 之間的距離。
即
length(b - a);
- 參數 a:
第一個點。
- 參數 b:
第二個點。
- return:
這兩點之間的純量距離
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/distance.xhtml
float dot(vec_type a, vec_type b) 🔗
回傳兩向量
a與b的點積。例如:a.x * b.x + a.y * b.y + ...
- 參數 a:
第一個向量。
- 參數 b:
第二個向量。
- return:
點積。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/dot.xhtml
vec3 cross(vec3 a, vec3 b) 🔗
回傳兩向量的外積(叉積)。例如:
vec2( a.y * b.z - b.y * a.z, a.z * b.x - b.z * a.x, a.x * b.z - b.x * a.y)
- 參數 a:
第一個向量。
- 參數 b:
第二個向量。
- return:
a和b的外積。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/cross.xhtml
vec_type normalize(vec_type x) 🔗
回傳與
x相同方向但長度為 1.0 的單位向量。
- 參數 x:
要標準化的向量。
- return:
已標準化的向量。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/normalize.xhtml
vec3 reflect(vec3 I, vec3 N) 🔗
計算入射向量的反射方向。
對於給定入射向量
I和表面法線N,reflect 會回傳以I - 2.0 * dot(N, I) * N計算的反射方向。備註
為取得正確結果,
N應先正規化。
- 參數 I:
入射向量。
- 參數 N:
法向量。
- return:
反射向量。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/reflect.xhtml
vec3 refract(vec3 I, vec3 N, float eta) 🔗
計算入射向量的折射方向。
對於給定的入射向量
I、表面法線N以及折射率比值eta,refract 會回傳折射向量R。
R計算方式如下:k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I)); if (k < 0.0) R = genType(0.0); // or genDType(0.0) else R = eta * I - (eta * dot(N, I) + sqrt(k)) * N;備註
輸入參數 I 與 N 應先標準化,才能獲得正確的結果。
- 參數 I:
入射向量。
- 參數 N:
法向量。
- 參數 eta:
折射率比值。
- return:
折射向量。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/refract.xhtml
vec_type faceforward(vec_type N, vec_type I, vec_type Nref) 🔗
回傳一個與另一向量方向相同的向量。
根據法線,將向量調整為遠離表面的方向。若
dot(Nref, I) < 0,faceforward 回傳N,否則回傳-N。
- 參數 N:
要進行方向調整的向量。
- 參數 I:
入射向量。
- 參數 Nref:
參考向量。
- return:
已調整方向的向量。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/faceforward.xhtml
mat_type matrixCompMult(mat_type x, mat_type y) 🔗
對兩個矩陣執行 元件逐一 相乘運算。
對兩個矩陣進行元件逐一相乘,產生一個結果矩陣,其每個元件
result[i][j]皆為x[i][j]與y[i][j]的純量乘積。
- 參數 x:
第一個矩陣乘數。
- 參數 y:
第二個矩陣乘數。
- return:
運算後的矩陣。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/matrixCompMult.xhtml
mat_type outerProduct(vec_type column, vec_type row) 🔗
計算一對向量的外積。
執行線性代數中的矩陣乘法
column * row,產生一個矩陣,其列數等於column的分量數,行數等於row的分量數。
- 參數 column:
作為乘法的欄向量。
- 參數 row:
作為乘法的列向量。
- return:
外積結果矩陣。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/outerProduct.xhtml
mat_type transpose(mat_type m) 🔗
計算矩陣的轉置。
- 參數 m:
要轉置的矩陣。
- return:
輸入矩陣
m的轉置結果。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/transpose.xhtml
float determinant(mat_type m) 🔗
計算矩陣的行列式。
- 參數 m:
矩陣。
- return:
輸入矩陣
m的行列式。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/determinant.xhtml
mat_type inverse(mat_type m) 🔗
計算矩陣的反矩陣。
若
m為奇異矩陣或條件數過大(接近奇異),回傳矩陣的值未定義。
- 參數 m:
要取反矩陣的矩陣。
- return:
輸入矩陣
m的反矩陣。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/inverse.xhtml
比較函式
vec_bool_type |
lessThan(vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 < 比較。 |
vec_bool_type |
greaterThan(vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 > 比較。 |
vec_bool_type |
lessThanEqual(vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 <= 比較。 |
vec_bool_type |
greaterThanEqual( vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 >= 比較。 |
vec_bool_type |
equal(vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 == 比較。 |
vec_bool_type |
notEqual(vec_type x, vec_type y) |
Bool 向量與 int/uint/float 向量進行 != 比較。 |
bool |
any(vec_bool_type x) |
只要任一分量為 |
bool |
all(vec_bool_type x) |
僅當所有分量皆為 |
vec_bool_type |
not(vec_bool_type x) |
反轉布林向量。 |
比較函式說明
vec_bool_type lessThan(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 小於比較。
- 參數 x:
要比較的第一個向量。
- 參數 y:
要比較的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] < y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/lessThan.xhtml
vec_bool_type greaterThan(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 大於比較。
- 參數 x:
要比較的第一個向量。
- 參數 y:
要比較的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] > y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/greaterThan.xhtml
vec_bool_type lessThanEqual(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 小於等於比較。
- 參數 x:
要比較的第一個向量。
- 參數 y:
要比較的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] <= y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/lessThanEqual.xhtml
vec_bool_type greaterThanEqual(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 大於等於比較。
- 參數 x:
要比較的第一個向量。
- 參數 y:
要比較的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] >= y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/greaterThanEqual.xhtml
vec_bool_type equal(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 等於比較。
- 參數 x:
要比較的第一個向量。
- 參數 y:
要比較的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] == y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/equal.xhtml
vec_bool_type notEqual(vec_type x, vec_type y) 🔗
對兩個向量執行 元件逐一 不等於比較。
- 參數 x:
比較用的第一個向量。
- 參數 y:
比較用的第二個向量。
- return:
結果為布林向量,每個分量 i 的值為
x[i] != y[i]。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/notEqual.xhtml
bool any(vec_bool_type x) 🔗
若布林向量任一元素為
true則回傳true,否則回傳false。功能上等同於:
bool any(bvec x) { // bvec can be bvec2, bvec3 or bvec4 bool result = false; int i; for (i = 0; i < x.length(); ++i) { result |= x[i]; } return result; }
- 參數 x:
要測試是否有 true 的布林向量。
- return:
若
x任一分量為 true 則回傳 true,否則為 false。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/any.xhtml
bool all(vec_bool_type x) 🔗
若布林向量所有元素皆為
true則回傳true,否則回傳false。功能上等同於:
bool all(bvec x) // bvec can be bvec2, bvec3 or bvec4 { bool result = true; int i; for (i = 0; i < x.length(); ++i) { result &= x[i]; } return result; }
- 參數 x:
要測試是否有 true 的布林向量。
- return:
僅當
x所有分量皆為 true 時回傳 true,否則為 false。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/all.xhtml
vec_bool_type not(vec_bool_type x) 🔗
邏輯反轉布林向量。
- 參數 x:
要反轉的布林向量。
- return:
回傳每個分量為 !x[i] 的新布林向量。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/not.xhtml
紋理函式
ivec2
ivec2
ivec2
ivec3
ivec3
|
textureSize(gsampler2D s, int lod)
textureSize(samplerCube s, int lod)
textureSize(samplerCubeArray s, int lod)
textureSize(gsampler2DArray s, int lod)
textureSize(gsampler3D s, int lod)
|
取得紋理大小。 |
vec2
vec3
vec2
vec2
|
textureQueryLod(gsampler2D s, vec2 p)
textureQueryLod(gsampler2DArray s, vec2 p)
textureQueryLod(gsampler3D s, vec3 p)
textureQueryLod(samplerCube s, vec3 p)
|
計算取樣紋理時會用到的細節層級(LOD)。 |
int
int
int
int
|
textureQueryLevels(gsampler2D s)
textureQueryLevels(gsampler2DArray s)
textureQueryLevels(gsampler3D s)
textureQueryLevels(samplerCube s)
|
取得紋理可存取的 mipmap 層數。 |
gvec4_type
gvec4_type
gvec4_type
vec4
vec4
vec4
|
執行紋理讀取。 |
|
gvec4_type
gvec4_type
gvec4_type
|
textureProj(gsampler2D s, vec3 p [, float bias] )
textureProj(gsampler2D s, vec4 p [, float bias] )
textureProj(gsampler3D s, vec4 p [, float bias] )
|
執行帶投影的紋理讀取。 |
gvec4_type
gvec4_type
gvec4_type
vec4
vec4
|
textureLod(gsampler2D s, vec2 p, float lod)
textureLod(gsampler2DArray s, vec3 p, float lod)
textureLod(gsampler3D s, vec3 p, float lod)
textureLod(samplerCube s, vec3 p, float lod)
textureLod(samplerCubeArray s, vec4 p, float lod)
|
在指定 mipmap 層進行紋理取樣。 |
gvec4_type
gvec4_type
gvec4_type
|
textureProjLod(gsampler2D s, vec3 p, float lod)
textureProjLod(gsampler2D s, vec4 p, float lod)
textureProjLod(gsampler3D s, vec4 p, float lod)
|
執行帶投影/LOD 的紋理取樣。 |
gvec4_type
gvec4_type
gvec4_type
vec4
vec4
|
textureGrad(gsampler2D s, vec2 p, vec2 dPdx, vec2 dPdy)
textureGrad(gsampler2DArray s, vec3 p, vec2 dPdx, vec2 dPdy)
textureGrad(gsampler3D s, vec3 p, vec2 dPdx, vec2 dPdy)
textureGrad(samplerCube s, vec3 p, vec3 dPdx, vec3 dPdy)
textureGrad(samplerCubeArray s, vec3 p, vec3 dPdx, vec3 dPdy)
|
執行具有明確梯度的紋理取樣。 |
gvec4_type
gvec4_type
gvec4_type
|
textureProjGrad(gsampler2D s, vec3 p, vec2 dPdx, vec2 dPdy)
textureProjGrad(gsampler2D s, vec4 p, vec2 dPdx, vec2 dPdy)
textureProjGrad(gsampler3D s, vec4 p, vec3 dPdx, vec3 dPdy)
|
執行帶有投影/細節層級(LOD)和明確梯度的材質讀取 |
gvec4_type
gvec4_type
gvec4_type
|
texelFetch(gsampler2D s, ivec2 p, int lod)
texelFetch(gsampler2DArray s, ivec3 p, int lod)
texelFetch(gsampler3D s, ivec3 p, int lod)
|
用整數座標讀取單一紋素(texel)。 |
gvec4_type
gvec4_type
vec4
|
textureGather(gsampler2D s, vec2 p [, int comps] )
textureGather(gsampler2DArray s, vec3 p [, int comps] )
textureGather(samplerCube s, vec3 p [, int comps] )
|
從紋理中收集四個紋素。 |
vec_type |
dFdx(vec_type p) |
對 |
vec_type |
dFdxCoarse(vec_type p) |
對 於使用相容性繪圖器時無法使用。 |
vec_type |
dFdxFine(vec_type p) |
對 於使用相容性繪圖器時無法使用。 |
vec_type |
dFdy(vec_type p) |
對 |
vec_type |
dFdyCoarse(vec_type p) |
對 於使用相容性繪圖器時無法使用。 |
vec_type |
dFdyFine(vec_type p) |
對 於使用相容性繪圖器時無法使用。 |
vec_type |
fwidth(vec_type p) |
|
vec_type |
fwidthCoarse(vec_type p) |
於使用相容性繪圖器時無法使用。 |
vec_type |
fwidthFine(vec_type p) |
於使用相容性繪圖器時無法使用。 |
紋理函式說明
ivec2 textureSize(gsampler2D s, int lod) 🔗
ivec2 textureSize(samplerCube s, int lod) 🔗
ivec2 textureSize(samplerCubeArray s, int lod) 🔗
ivec3 textureSize(gsampler2DArray s, int lod) 🔗
ivec3 textureSize(gsampler3D s, int lod) 🔗
取得材質指定層級的尺寸。
回傳綁定於取樣器的紋理,在指定
lod層級(若有)下的尺寸。回傳值的各分量依序填入紋理的寬度、高度與深度。若為陣列形式,最後一個分量則為紋理陣列的圖層數。
- 參數 s:
用於綁定目標紋理的取樣器。
- 參數 lod:
欲查詢尺寸的紋理層級。
- return:
綁定於取樣器的紋理,第
lod層的尺寸(若存在)。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureSize.xhtml
vec2 textureQueryLod(gsampler2D s, vec2 p) 🔗
vec2 textureQueryLod(gsampler2DArray s, vec2 p) 🔗
vec2 textureQueryLod(gsampler3D s, vec3 p) 🔗
vec2 textureQueryLod(samplerCube s, vec3 p) 🔗
備註
僅於片段著色器中可用。
計算取樣紋理時會用到的細節層級(LOD)。
會被存取的 mipmap 陣列會回傳於 x 分量。以基礎層級為基準計算的細節層級會回傳於 y 分量。
若針對不完整的紋理呼叫,操作結果未定義。
- 參數 s:
要查詢細節層級(LOD)的紋理所綁定的取樣器。
- 參數 p:
查詢細節層級時使用的紋理座標。
- return:
詳見說明。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureQueryLod.xhtml
int textureQueryLevels(gsampler2D s) 🔗
int textureQueryLevels(gsampler2DArray s) 🔗
int textureQueryLevels(gsampler3D s) 🔗
int textureQueryLevels(samplerCube s) 🔗
計算紋理可存取的 mipmap 層數。
若對不完整的紋理或未關聯紋理的取樣器呼叫,則回傳
0。
- 參數 s:
要查詢 mipmap 層數的紋理所綁定的取樣器。
- return:
紋理可存取的 mipmap 層數,或
0。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureQueryLevels.xhtml
gvec4_type texture(gsampler2D s, vec2 p [, float bias] ) 🔗
gvec4_type texture(gsampler2DArray s, vec3 p [, float bias] ) 🔗
gvec4_type texture(gsampler3D s, vec3 p [, float bias] ) 🔗
vec4 texture(samplerCube s, vec3 p [, float bias] ) 🔗
vec4 texture(samplerCubeArray s, vec4 p [, float bias] ) 🔗
vec4 texture(samplerExternalOES s, vec2 p [, float bias] ) 🔗
從材質中取得像素(texel)。
於座標
p上,從綁定於s的紋理取樣紋素。細節層級計算時可選用bias作為偏移,選擇要取樣的 mipmap 層。對於陰影型態,
p的最後一個分量作為 Dsub,用於比較,倒數第二個分量指定陣列層(1D 陰影查詢時第二個分量不使用)非陰影版本時,陣列層來自 P 的最後一個分量。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 bias:
細節層級(LOD)計算時可選用的偏移值。
- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/texture.xhtml
gvec4_type textureProj(gsampler2D s, vec3 p [, float bias] ) 🔗
gvec4_type textureProj(gsampler2D s, vec4 p [, float bias] ) 🔗
gvec4_type textureProj(gsampler3D s, vec4 p [, float bias] ) 🔗
執行帶投影的紋理查詢。
從
p取得的紋理座標(不含最後一個分量)會除以p的最後一個分量。若為陰影(shadow)形式,計算後的第三分量會作為 Dref。計算完成後,即依 texture 進行查詢。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 bias:
細節層級(LOD)計算時可選用的偏移量。
- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureProj.xhtml
gvec4_type textureLod(gsampler2D s, vec2 p, float lod) 🔗
gvec4_type textureLod(gsampler2DArray s, vec3 p, float lod) 🔗
gvec4_type textureLod(gsampler3D s, vec3 p, float lod) 🔗
vec4 textureLod(samplerCube s, vec3 p, float lod) 🔗
vec4 textureLod(samplerCubeArray s, vec4 p, float lod) 🔗
於由 sampler 綁定的紋理中,以
p為座標,並使用lod指定的細節層級進行查詢。lod指定 λbase,並將偏導數設為:δu/δx=0, δv/δx=0, δw/δx=0 δu/δy=0, δv/δy=0, δw/δy=0
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 lod:
指定的細節層級(LOD)。
- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureLod.xhtml
gvec4_type textureProjLod(gsampler2D s, vec3 p, float lod) 🔗
gvec4_type textureProjLod(gsampler2D s, vec4 p, float lod) 🔗
gvec4_type textureProjLod(gsampler3D s, vec4 p, float lod) 🔗
執行從指定細節層級(LOD)開始的帶投影材質查找。
從 P 取用的紋理座標(不含最後一個分量)會除以
p的最後分量。若為 shadow 形式,最後的第三分量作為 Dref。計算後,紋理查詢會如 textureLod<shader_func_textureLod> 一般,並由lod指定取樣細節層級。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 lod:
要從哪個明確細節層級(LOD)取得像素。
- return:
一個像素(texel)
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureProjLod.xhtml
gvec4_type textureGrad(gsampler2D s, vec2 p, vec2 dPdx, vec2 dPdy) 🔗
gvec4_type textureGrad(gsampler2DArray s, vec3 p, vec2 dPdx, vec2 dPdy) 🔗
gvec4_type textureGrad(gsampler3D s, vec3 p, vec2 dPdx, vec2 dPdy) 🔗
vec4 textureGrad(samplerCube s, vec3 p, vec3 dPdx, vec3 dPdy) 🔗
vec4 textureGrad(samplerCubeArray s, vec3 p, vec3 dPdx, vec3 dPdy) 🔗
- 於由取樣器綁定的紋理中,以
p為座標,並以dPdx和dPdy指定的紋理座標梯度進行查詢。設定如下:
對 1D 紋理為
δs/δx=δp/δx,否則為δp.s/δx對 1D 紋理為
δs/δy=δp/δy,否則為δp.s/δy對 1D 紋理為
δt/δx=0.0,否則為δp.t/δx對 1D 紋理為
δt/δy=0.0,否則為δp.t/δy對 1D 或 2D 紋理為
δr/δx=0.0,否則為δp.p/δx對 1D 或 2D 紋理為
δr/δy=0.0,否則為δp.p/δy對於立方體版本,
p的偏導數假設是投影到對應立方體面之前的座標系統。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 dPdx:
P 關於視窗 x 的偏導數。
- 參數 dPdy:
P 關於視窗 y 的偏導數。
- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureGrad.xhtml
gvec4_type textureProjGrad(gsampler2D s, vec3 p, vec2 dPdx, vec2 dPdy) 🔗
gvec4_type textureProjGrad(gsampler2D s, vec4 p, vec2 dPdx, vec2 dPdy) 🔗
gvec4_type textureProjGrad(gsampler3D s, vec4 p, vec3 dPdx, vec3 dPdy) 🔗
執行帶有投影與明確梯度的材質查找。
從
p取用的紋理座標(不含最後一個分量)會除以最後分量。計算完成後,紋理查詢會如 textureGrad<shader_func_textureGrad>,並以dPdx與dPdy作為梯度傳入。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 dPdx:
p對視窗 x 座標的偏導數。- 參數 dPdy:
p對視窗 y 座標的偏導數。- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureProjGrad.xhtml
gvec4_type texelFetch(gsampler2D s, ivec2 p, int lod) 🔗
gvec4_type texelFetch(gsampler2DArray s, ivec3 p, int lod) 🔗
gvec4_type texelFetch(gsampler3D s, ivec3 p, int lod) 🔗
從由取樣器綁定的紋理,以座標
p查詢單一 texel。
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 lod:
指定從哪個細節層級(level-of-detail)取出 texel。
- return:
一個紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/texelFetch.xhtml
gvec4_type textureGather(gsampler2D s, vec2 p [, int comps] ) 🔗
gvec4_type textureGather(gsampler2DArray s, vec3 p [, int comps] ) 🔗
vec4 textureGather(samplerCube s, vec3 p [, int comps] ) 🔗
從紋理中收集四個紋素。
回傳值:
vec4(Sample_i0_j1(p, base).comps, Sample_i1_j1(p, base).comps, Sample_i1_j0(p, base).comps, Sample_i0_j0(p, base).comps);
- 參數 s:
從中取樣紋素的紋理所綁定的取樣器。
- 參數 p:
要取樣的紋理座標。
- 參數 comps:
選填 來源紋理的分量(0 → x, 1 → y, 2 → z, 3 → w),用於產生結果向量。未指定時預設為 0。
- return:
收集到的紋素。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/textureGather.xhtml
vec_type dFdx(vec_type p) 🔗
備註
僅於片段著色器中可用。
以局部差分方式,回傳
p相對於視窗 x 座標的偏導數。回傳 dFdxCoarse 或 dFdxFine。實作端可根據效能或
GL_FRAGMENT_SHADER_DERIVATIVE_HINTAPI 參數等因素選擇計算方式。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type dFdxCoarse(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳
p相對於視窗 x 座標的偏導數。根據目前片段鄰近片段的
p值,使用區域差分計算導數,可能但不一定包含目前片段本身的值。也就是說,在同一範圍內,實作可在比 dFdxFine 更少的位置計算導數。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://registry.khronos.org/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type dFdxFine(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳
p相對於視窗 x 座標的偏導數。根據目前片段與其相鄰片段的
p值,使用區域差分計算導數。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://registry.khronos.org/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type dFdy(vec_type p) 🔗
備註
僅於片段著色器中可用。
以局部差分方式回傳
p相對於視窗 y 座標的偏導數。回傳 dFdyCoarse 或 dFdyFine。實作端可根據效能或
GL_FRAGMENT_SHADER_DERIVATIVE_HINTAPI 參數等因素選擇計算方式。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://registry.khronos.org/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type dFdyCoarse(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳
p相對於視窗 y 座標的偏導數。使用當前片段鄰近區塊的
p值進行局部差分計算導數,且可能(但不一定)包含當前片段的值。也就是說,在給定區域內,實作端可在比 dFdyFine 等函式更少的位置計算導數。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://registry.khronos.org/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type dFdyFine(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳
p相對於視窗 y 座標的偏導數。根據目前片段與其相鄰片段的
p值,使用區域差分計算導數。警告
出現高階導數(如
dFdx(dFdx(n)))或混合導數(如dFdx(dFdy(n)))的表達式,結果未定義。
- 參數 p:
欲計算偏導數的運算式。
備註
假設運算式
p是連續的,因此經由非均勻控制流程計算的表達式可能結果未定義。- return:
p的偏導數。https://registry.khronos.org/OpenGL-Refpages/gl4/html/dFdx.xhtml
vec_type fwidth(vec_type p) 🔗
回傳 x 與 y 方向導數的絕對值總和。
對輸入參數
p使用局部差分。等同於
abs(dFdx(p)) + abs(dFdy(p))。
- 參數 p:
欲計算偏導數的運算式。
- return:
偏導數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/fwidth.xhtml
vec_type fwidthCoarse(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳 x 與 y 方向導數的絕對值總和。
對輸入參數 p 使用區域差分運算。
等同於
abs(dFdxCoarse(p)) + abs(dFdyCoarse(p))。
- 參數 p:
欲計算偏導數的運算式。
- return:
偏導數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/fwidth.xhtml
vec_type fwidthFine(vec_type p) 🔗
備註
僅於片段著色器可用。使用相容性繪圖器時無法使用。
回傳 x 與 y 方向導數的絕對值總和。
對輸入參數 p 使用區域差分運算。
等同於
abs(dFdxFine(p)) + abs(dFdyFine(p))。
- 參數 p:
欲計算偏導數的運算式。
- return:
偏導數。
https://registry.khronos.org/OpenGL-Refpages/gl4/html/fwidth.xhtml
打包與解包函式
這些函式可將浮點數轉換為不同大小的整數,並將其打包為單一 32 位元無符號整數。'unpack' 函式則反向操作,還原為原本的浮點數。
uint
vec2
|
packHalf2x16(vec2 v)
unpackHalf2x16(uint v)
|
將兩個 32 位元 float 轉換並打包為 16 位元 float。 |
uint
vec2
|
packUnorm2x16(vec2 v)
unpackUnorm2x16(uint v)
|
將兩個正規化(範圍 0..1)的 32 位元 float 轉換並打包為 16 位元 float。 |
uint
vec2
|
packSnorm2x16(vec2 v)
unpackSnorm2x16(uint v)
|
將兩個有號正規化(範圍 -1..1)的 32 位元 float 轉換並打包為 16 位元 float。 |
uint
vec4
|
packUnorm4x8(vec4 v)
unpackUnorm4x8(uint v)
|
將四個正規化(範圍 0..1)的 32 位元 float 轉換並打包為 8 位元 float。 |
uint
vec4
|
packSnorm4x8(vec4 v)
unpackSnorm4x8(uint v)
|
將四個有號正規化(範圍 -1..1)的 32 位元 float 轉換並打包為 8 位元 float。 |
打包與解包函式說明
uint packHalf2x16(vec2 v) 🔗
將兩個 32 位元浮點數轉換為 16 位元浮點數,並封裝成一個 32 位元整數。
將兩個分量的 32 位元浮點數轉為 OpenGL 規範中的 16 位元浮點格式,並將這兩個 16 位元值打包為一個 32 位元無符號整數。第一個分量成為低 16 位元,第二個分量成為高 16 位元。
- 參數 v:
要轉換為 16 位元並打包的兩分量 32 位元浮點向量。
- return:
打包後的值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/packHalf2x16.xhtml
vec2 unpackHalf2x16(uint v) 🔗
與 packHalf2x16 相反。
將一個 32 位元整數解包為兩個 16 位元浮點值,再轉換為 32 位元浮點數後輸出為向量。第一分量取自
v的低 16 位元,第二分量取自高 16 位元。
- 參數 v:
包含兩個已打包 16 位元浮點數的 32 位元無符號整數。
- return:
解包後的兩個浮點數值。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/unpackHalf2x16.xhtml
uint packUnorm2x16(vec2 v) 🔗
將浮點數值打包成無符號整數。
將每個分量的歸一化浮點數值 v 轉為 16 位元整數,然後打包為一個 32 位元無符號整數。
將
v的分量 c 轉為定點數的運算如下:round(clamp(c, 0.0, 1.0) * 65535.0)向量的第一個分量寫入輸出值的最低有效位,最後一個分量寫入最高有效位。
- 參數 v:
要打包成無符號整數的向量值。
- return:
已打包向量的 32 位元無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/packUnorm.xhtml
vec2 unpackUnorm2x16(uint v) 🔗
從無符號整數解包浮點數值。
將單一 32 位元無符號整數解包為兩個 16 位元無符號整數,然後將每個分量轉換為正規化浮點值,最後回傳兩分量向量。
將解包後的定點值 f 轉回浮點數的運算如下:
f / 65535.0
回傳向量的第一個分量取自輸入值的最低有效位,最後一個分量取自最高有效位。
- 參數 v:
包含已打包浮點數值的無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/unpackUnorm.xhtml
uint packSnorm2x16(vec2 v) 🔗
將浮點數值打包成無符號整數。
將每個分量的歸一化浮點數值
v轉為 16 位元整數,然後打包為一個 32 位元無符號整數。將
v的分量 c 轉為定點數的運算如下:round(clamp(c, -1.0, 1.0) * 32767.0)向量的第一個分量寫入輸出值的最低有效位,最後一個分量寫入最高有效位。
- 參數 v:
要打包成無符號整數的向量值。
- return:
已打包向量的 32 位元無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/packUnorm.xhtml
vec2 unpackSnorm2x16(uint v) 🔗
從無符號整數解包浮點數值。
將單一 32 位元無符號整數解包為兩個 16 位元有符號整數,然後轉換為正規化浮點數後組成兩分量向量回傳。
將解包後的定點值 f 轉回浮點數的運算如下:
clamp(f / 32727.0, -1.0, 1.0)
回傳向量的第一個分量取自輸入值的最低有效位,最後一個分量取自最高有效位。
- 參數 v:
包含已打包浮點數值的無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/unpackUnorm.xhtml
uint packUnorm4x8(vec4 v) 🔗
將浮點數值打包成無符號整數。
將正規化浮點值
v的每個分量轉換為 8 位元整數,並將結果組合打包為一個 32 位元無符號整數。將
v的分量 c 轉為定點數的運算如下:round(clamp(c, 0.0, 1.0) * 255.0)向量的第一個分量寫入輸出值的最低有效位,最後一個分量寫入最高有效位。
- 參數 v:
要打包成無符號整數的向量值。
- return:
已打包向量的 32 位元無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/packUnorm.xhtml
vec4 unpackUnorm4x8(uint v) 🔗
從無符號整數解包浮點數值。
將單一 32 位元無符號整數解包為四個 8 位元無符號整數,然後每個分量轉換為正規化浮點數,最後組成四分量向量回傳。
將解包後的定點值 f 轉回浮點數的運算如下:
f / 255.0
回傳向量的第一個分量取自輸入值的最低有效位,最後一個分量取自最高有效位。
- 參數 v:
包含已打包浮點數值的無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/unpackUnorm.xhtml
uint packSnorm4x8(vec4 v) 🔗
將浮點數值打包成無符號整數。
將每個分量的歸一化浮點數值
v轉為 16 位元整數,然後打包為一個 32 位元無符號整數。將
v的分量 c 轉為定點數的運算如下:round(clamp(c, -1.0, 1.0) * 127.0)向量的第一個分量寫入輸出值的最低有效位,最後一個分量寫入最高有效位。
- 參數 v:
要打包成無符號整數的向量值。
- return:
已打包向量的 32 位元無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/packUnorm.xhtml
vec4 unpackSnorm4x8(uint v) 🔗
從無符號整數解包浮點數值。
將單一 32 位元無符號整數解包為四個 8 位元有符號整數,再分別轉換為正規化浮點數,最後組成四分量向量回傳。
將解包後的定點值 f 轉回浮點數的運算如下:
clamp(f / 127.0, -1.0, 1.0)
回傳向量的第一個分量取自輸入值的最低有效位,最後一個分量取自最高有效位。
- 參數 v:
包含已打包浮點數值的無符號整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/unpackUnorm.xhtml
位元運算函式
vec_int_type
vec_uint_type
|
bitfieldExtract(vec_int_type value, int offset, int bits)
bitfieldExtract(vec_uint_type value, int offset, int bits)
|
從整數中擷取一段位元。 |
vec_int_type
vec_uint_type
|
bitfieldInsert(vec_int_type base, vec_int_type insert, int offset, int bits)
bitfieldInsert(vec_uint_type base, vec_uint_type insert, int offset, int bits)
|
將一段位元插入整數。 |
vec_int_type
vec_uint_type
|
bitfieldReverse(vec_int_type value)
bitfieldReverse(vec_uint_type value)
|
反轉整數的位元順序。 |
vec_int_type
vec_uint_type
|
計算整數中 1 的數量。 |
|
vec_int_type
vec_uint_type
|
尋找整數中最低位且為 1 的位元索引。 |
|
vec_int_type
vec_uint_type
|
尋找整數中最高位且為 1 的位元索引。 |
|
void
void
|
imulExtended(vec_int_type x, vec_int_type y, out vec_int_type msb, out vec_int_type lsb)
umulExtended(vec_uint_type x, vec_uint_type y, out vec_uint_type msb, out vec_uint_type lsb)
|
將兩個 32 位元數相乘,產生 64 位元結果。 |
vec_uint_type |
uaddCarry(vec_uint_type x, vec_uint_type y, out vec_uint_type carry) |
兩個無號整數相加並產生進位。 |
vec_uint_type |
usubBorrow(vec_uint_type x, vec_uint_type y, out vec_uint_type borrow) |
兩個無符號整數相減並產生借位。 |
vec_type |
ldexp(vec_type x, out vec_int_type exp) |
由數值與指數組合成一個浮點數。 |
vec_type |
frexp(vec_type x, out vec_int_type exp) |
將浮點數 ( |
位元運算函式說明
vec_int_type bitfieldExtract(vec_int_type value, int offset, int bits) 🔗
從
value中擷取指定範圍的位元,並將其放於結果的最低有效位元。擷取範圍為[offset, offset + bits - 1]。結果的最高有效位元會被設為零。
備註
若 bits 為零,則結果也會為零。
警告
若發生以下狀況,結果未定義:
offset 或 bits 為負值。
如果 offset 與 bits 的總和大於運算元位元數。
- 參數 value:
要擷取位元的整數。
- 參數 offset:
要擷取的第一個位元索引。
- 參數 bits:
要擷取的位元數。
- return:
包含擷取後位元的整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/bitfieldExtract.xhtml
vec_uint_type bitfieldExtract(vec_uint_type value, int offset, int bits) 🔗
分量逐一運算
從
value中擷取指定範圍的位元,並將其放於結果的最低有效位元。擷取範圍為[offset, offset + bits - 1]。最高有效位元將設為
offset + base - 1的值(即以符號擴展至回傳型別寬度)。備註
若 bits 為零,則結果也會為零。
警告
若發生以下狀況,結果未定義:
offset 或 bits 為負值。
如果 offset 與 bits 的總和大於運算元位元數。
- 參數 value:
要擷取位元的整數。
- 參數 offset:
要擷取的第一個位元索引。
- 參數 bits:
要擷取的位元數。
- return:
包含擷取後位元的整數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/bitfieldExtract.xhtml
vec_uint_type bitfieldExtract(vec_uint_type value, int offset, int bits) 🔗
vec_uint_type bitfieldInsert(vec_uint_type base, vec_uint_type insert, int offset, int bits) 🔗
分量逐一運算
將
insert的最低bits位元插入base,位置自offset起。回傳值的 [offset, offset + bits + 1] 位元來自
insert的 [0, bits - 1],其餘位元則直接取自 base 對應位元。備註
若 bits 為零,則結果為 base 原始值。
警告
若發生以下狀況,結果未定義:
offset 或 bits 為負值。
如果 offset 與 bits 的總和大於運算元位元數。
- 參數 base:
要插入
insert位元的整數。- 參數 insert:
要插入的位元值。
- 參數 offset:
要插入的第一個位元索引。
- 參數 bits:
要插入的位元數。
- return:
已插入指定位元的
base。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/bitfieldInsert.xhtml
vec_int_type bitfieldReverse(vec_int_type value) 🔗
vec_uint_type bitfieldReverse(vec_uint_type value) 🔗
分量逐一運算
反轉整數的位元順序。
第
n號位元會取自value的第(bits - 1) - n位,其中 bits 為value的總位元數。
- 參數 value:
要反轉其位元的值。
- return:
反轉位元後的
value。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/bitfieldReverse.xhtml
vec_int_type bitCount(vec_int_type value) 🔗
vec_uint_type bitCount(vec_uint_type value) 🔗
分量逐一運算
計算整數中 1 的數量。
- 參數 value:
要計算其位元數的值。
- return:
value二進位表示中為 1 的位元數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/bitCount.xhtml
vec_int_type findLSB(vec_int_type value) 🔗
vec_uint_type findLSB(vec_uint_type value) 🔗
分量逐一運算
尋找最低有效位元且為
1的索引。備註
若
value為零,則回傳-1。
- 參數 value:
要掃描其位元的值。
- return:
該二進位值中,最低位且為 1 的位元索引。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/findLSB.xhtml
vec_int_type findMSB(vec_int_type value) 🔗
vec_uint_type findMSB(vec_uint_type value) 🔗
分量逐一運算
尋找最高有效位元且為 1 的索引。
備註
- 對有符號整數型別,會先檢查符號位元,然後:
若為正整數,結果為最左側為 1 的位元編號。
若為負整數,則結果為最高有效位為 0 的位元索引。
備註
若值為 0 或 -1,則回傳 -1。
- 參數 value:
要掃描其位元的值。
- return:
該二進位值中,最高位且為 1 的位元索引。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/findMSB.xhtml
void imulExtended(vec_int_type x, vec_int_type y, out vec_int_type msb, out vec_int_type lsb) 🔗
分量逐一運算
進行 32 位元帶號整數乘法,產生 64 位元結果。
乘積的低 32 位元回傳於
lsb,高 32 位元回傳於msb。
- 參數 x:
第一個乘數。
- 參數 y:
第二個乘數。
- 參數 msb:
用於存放乘積高位元的變數。
- 參數 lsb:
用於存放乘積低位元的變數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/umulExtended.xhtml
void umulExtended(vec_uint_type x, vec_uint_type y, out vec_uint_type msb, out vec_uint_type lsb) 🔗
分量逐一運算
進行 32 位元無號整數乘法,產生 64 位元結果。
乘積的低 32 位元回傳於
lsb,高 32 位元回傳於msb。
- 參數 x:
第一個乘數。
- 參數 y:
第二個乘數。
- 參數 msb:
用於存放乘積高位元的變數。
- 參數 lsb:
用於存放乘積低位元的變數。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/umulExtended.xhtml
vec_uint_type uaddCarry(vec_uint_type x, vec_uint_type y, out vec_uint_type carry) 🔗
分量逐一運算
將無符號整數相加並產生進位。
將兩個 32 位元無號整數(純量或向量)相加,產生一個 32 位元無號整數結果並帶有進位輸出。carry 為進位值。
- 參數 x:
第一個運算元。
- 參數 y:
第二個運算元。
- 參數 carry:
若總和小於 232 則為 0,否則為 1。
- return:
(x + y) % 2^32。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/uaddCarry.xhtml
vec_uint_type usubBorrow(vec_uint_type x, vec_uint_type y, out vec_uint_type borrow) 🔗
分量逐一運算
兩個無號整數相減並產生借位。
- 參數 x:
第一個運算元。
- 參數 y:
第二個運算元。
- 參數 borrow:
若
x >= y則為0,否則為1。- return:
如果
x減y為非負則為差值,否則為 232 加上差值。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/usubBorrow.xhtml
vec_type ldexp(vec_type x, out vec_int_type exp) 🔗
分量逐一運算
由數值與指數組合成浮點數。
警告
若該乘積過大無法以浮點型別表示,則結果未定義。
- 參數 x:
作為有效數來源的值。
- 參數 exp:
作為指數來源的值。
- return:
x * 2^exphttps://www.khronos.org/registry/OpenGL-Refpages/gl4/html/ldexp.xhtml
vec_type frexp(vec_type x, out vec_int_type exp) 🔗
分量逐一運算
將
x拆分為 [0.5, 1.0) 範圍內的有效數與二的整數次方,使其滿足:x = significand * 2 ^ exponent若為零,則有效數與指數皆為零。
警告
若為無窮大或 NaN,則結果未定義。
- 參數 x:
要拆分有效數與指數的值。
- 參數 exp:
用於存放
x指數的變數。- return:
x的有效數。https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/frexp.xhtml