UE4 Shader 著色器概述

2021-10-11 13:29:05 字數 3601 閱讀 4493

fshadertype用於序列化和反序列化,可以被例項化為具體的shaderclass

fshaderpipelinetype繫結著色器和乙個單個管線

fshaderresource編譯後的 shader 中間碼以及它執行時 rhi的資源

fshadertarget目標平台和頻段

fshader編譯後的乙個 fshadertype 的例項,根據乙個 fshaderresource 的資訊編譯獲得

包括了fshadetype

fshadertarget

fshaderparametermap

fshaderresource

fshaderpiplinetype

fvertexfactorytype成員變數

ue4中所有著色器的基類都是從fshader 繼承的。主要分為fglobalshader 和fmaterialshader

fglobalshader只可以有乙個例項

fmaterialshader材質繫結的著色器

都包含了shader 都有setparameters 函式,允許更改引數值,引數傳遞通過 fshaderparameter / fshaderresourceparameter

implement_shader_type 例項化shader巨集

#define implement_shader_type(templateprefix,shaderclass,sourcefilename,functionname,frequency) \

templateprefix \

shaderclass::shadermetatype shaderclass::statictype( \

text(#shaderclass), \

sourcefilename, \

functionname, \

frequency, \

shaderclass::constructserializedinstance, \

shaderclass::constructcompiledinstance, \

shaderclass::modifycompilationenvironment, \

shaderclass::shouldcache, \

shaderclass::getstreamoutelements \

);

繫結到 對應usf 檔案上,指定ps,vs入口

implement_shader_type(, flensdistortionuvgenerationvs, text("/plugin/lensdistortion/private/uvgeneration.usf"), text("mainvs"),sf_vertex)

implement_shader_type(, flensdistortionuvgenerationps, text("/plugin/lensdistortion/private/uvgeneration.usf"), text("mainps"),sf_pixel)

快取和編譯當你修改shader 的時候,ue4 會自動編譯 shader組合 ,這裡使用了shouldcache功能,modifycompilationenvironment可以編譯之前修改 hlsl**裡的巨集定義引數

封裝乙個頂點資料格式,並可以鏈結到乙個頂點著色器

flocalvertexfactory

提供了乙個簡單方法將頂點屬性從區域性空間變換到世界空間

fprimitivesceneproxy

是uprimitivecomponent渲染執行緒版本

通過createscenceproxy 決定建立的是哪個uprimitivecomponent版本

繫結到hlsl巨集

#define implement_vertex_factory_type(factoryclass,shaderfilename,busedwithmaterials,bsupportsstaticlighting,bsupportsdynamiclighting,bpreciseprevworldpos,bsupportspositiononly) \

fvertexfactoryshaderparameters* construct##factoryclass##shaderparameters(eshaderfrequency shaderfrequency) \

fvertexfactorytype factoryclass::statictype( \

text(#factoryclass), \

text(shaderfilename), \

busedwithmaterials, \

bsupportsstaticlighting, \

bsupportsdynamiclighting, \

bpreciseprevworldpos, \

bsupportspositiononly, \

construct##factoryclass##shaderparameters, \

factoryclass::shouldcache, \

factoryclass::modifycompilationenvironment, \

factoryclass::supportstessellationshaders \

);

implement_vertex_factory_type(flocalvertexfactory,"/engine/private/localvertexfactory.ush",true,true,true,true,true);
在usf 檔案裡main函式其實都包含fvertexfactoryinput 輸入結構,這個資料結構在這個資料結構在localvertexfactory.ush中定義。在gpuskinvertexfactory.ush也定義了這個結構

// mobilebasepassvertexshader.usf

/** entry point for the base pass vertex shader. */

void main(fvertexfactoryinput input)

// localvertexfactory.ush implements the fvertexfactoryinput struct

struct fvertexfactoryinput

// gpuskinvertexfactory.ush also implements the fvertexfactoryinput struct

struct fvertexfactoryinput

編輯於 2018-01-0

著色器(Shader)之畫素著色器

畫素著色器實際上就是對每乙個畫素進行光柵化的處理期間,在gpu上運算的一段程式。不同與頂點著色器,畫素著色器不會以軟體的形式來模擬畫素著色器。畫素著色器實質上是取代了固定功能流水線中多重紋理的環節,而且賦予了我們訪問單個畫素以及訪問每乙個畫素紋理座標的能力 多重紋理就是我們同時啟用多層紋理,然後規定...

著色器(Shader)之頂點著色器

頂點著色器其實就是我們自己編寫的一段在gpu中執行的程式,有了頂點著色器,我們就可以從固定的功能流水線中代替一些模組,從而獲得更多的頂點操作的靈活性。對於頂點位置進行操作的的能力具有廣泛的應該場合 織物模擬 粒子系統的點尺寸處理等。可程式設計流水線中的頂點結構比固定的流水線具有更加豐富的資料。首先我...

Android著色器Shader使用誤區

toc shader的基本使用不多說了,請參考blog.csdn.net iispring ar 這裡我們只講解shader使用過程中的小細節或誤區。bitmapshader載入的原圖如下 測試 如下 public class bitmapshaderview extends view public...