| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296 |
- /*
- * Copyright 2013 Dario Manesku. All rights reserved.
- * License: http://www.opensource.org/licenses/BSD-2-Clause
- */
- #include <string>
- #include <vector>
- #include <algorithm>
- #include "common.h"
- #include <bgfx.h>
- #include <bx/timer.h>
- #include <bx/readerwriter.h>
- #include "entry/entry.h"
- #include "entry/camera.h"
- #include "fpumath.h"
- #include "imgui/imgui.h"
- #define RENDER_PASS_0 1
- #define RENDER_PASS_1 2
- #define RENDER_PASS_2 3
- #define RENDER_PASS_3 4
- #define RENDER_PASS_4 5
- #define RENDER_PASS_5 6
- #define RENDER_PASS_6 7
- #define RENDER_PASS_7 8
- #define RENDER_PASS_8 9
- #define RENDER_PASS_9 10
- #define RENDER_PASS_10 11
- #define RENDER_PASS_11 12
- #define RENDER_PASS_12 13
- #define RENDER_PASS_13 14
- #define RENDER_PASS_14 15
- #define RENDER_PASS_15 16
- #define RENDER_PASS_16 17
- #define RENDER_PASS_17 18
- #define RENDER_PASS_18 19
- uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
- {
- union
- {
- uint32_t ui32;
- uint8_t arr[4];
- } un;
- un.arr[0] = _x;
- un.arr[1] = _y;
- un.arr[2] = _z;
- un.arr[3] = _w;
- return un.ui32;
- }
- uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
- {
- const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
- const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
- const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
- const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
- return packUint32(xx, yy, zz, ww);
- }
- struct LightType
- {
- enum Enum
- {
- SpotLight = 0,
- PointLight = 1,
- DirectionalLight = 2,
- Count
- };
- };
- struct DepthImpl
- {
- enum Enum
- {
- InvZ = 0,
- Linear = 1,
- Count
- };
- };
- struct PackDepth
- {
- enum Enum
- {
- RGBA = 0,
- VSM = 1,
- Count
- };
- };
- struct SmImpl
- {
- enum Enum
- {
- Hard = 0,
- PCF = 1,
- VSM = 2,
- ESM = 3,
- Count
- };
- };
- struct SmType
- {
- enum Enum
- {
- Single = 0,
- Omni = 1,
- Cascade = 2,
- Count
- };
- };
- struct TetrahedronFaces
- {
- enum Enum
- {
- Green = 0,
- Yellow = 1,
- Blue = 2,
- Red = 3,
- Count = 4
- };
- };
- struct ProjType
- {
- enum Enum
- {
- Horizontal = 0,
- Vertical = 1,
- Count
- };
- };
- struct ShadowMapRenderTargets
- {
- enum Enum
- {
- First = 0,
- Second = 1,
- Third = 2,
- Fourth = 3,
- Count
- };
- };
- void imguiEnum(SmImpl::Enum& _enum)
- {
- if (imguiCheck("Hard", SmImpl::Hard == _enum) )
- {
- _enum = SmImpl::Hard;
- }
- if (imguiCheck("PCF", SmImpl::PCF == _enum) )
- {
- _enum = SmImpl::PCF;
- }
- if (imguiCheck("VSM", SmImpl::VSM == _enum) )
- {
- _enum = SmImpl::VSM;
- }
- if (imguiCheck("ESM", SmImpl::ESM == _enum) )
- {
- _enum = SmImpl::ESM;
- }
- }
- void imguiEnum(DepthImpl::Enum& _enum)
- {
- if (imguiCheck("InvZ", DepthImpl::InvZ == _enum) )
- {
- _enum = DepthImpl::InvZ;
- }
- if (imguiCheck("Linear", DepthImpl::Linear == _enum) )
- {
- _enum = DepthImpl::Linear;
- }
- }
- void imguiEnum(LightType::Enum& _enum)
- {
- if (imguiCheck("Spot light", LightType::SpotLight == _enum) )
- {
- _enum = LightType::SpotLight;
- }
- if (imguiCheck("Point light", LightType::PointLight == _enum) )
- {
- _enum = LightType::PointLight;
- }
- if (imguiCheck("Directional light", LightType::DirectionalLight == _enum) )
- {
- _enum = LightType::DirectionalLight;
- }
- }
- void imguiBool(const char* _str, bool& _flag, bool _enabled = true)
- {
- if (imguiCheck(_str, _flag, _enabled) )
- {
- _flag = !_flag;
- }
- }
- struct PosNormalTexcoordVertex
- {
- float m_x;
- float m_y;
- float m_z;
- uint32_t m_normal;
- float m_u;
- float m_v;
- };
- static const float s_texcoord = 5.0f;
- static const uint32_t s_numHPlaneVertices = 4;
- static PosNormalTexcoordVertex s_hplaneVertices[s_numHPlaneVertices] =
- {
- { -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), s_texcoord, s_texcoord },
- { 1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), s_texcoord, 0.0f },
- { -1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), 0.0f, s_texcoord },
- { 1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
- };
- static const uint32_t s_numVPlaneVertices = 4;
- static PosNormalTexcoordVertex s_vplaneVertices[s_numVPlaneVertices] =
- {
- { -1.0f, 1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
- { 1.0f, 1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
- { -1.0f, -1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
- { 1.0f, -1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
- };
- static const uint32_t s_numPlaneIndices = 6;
- static const uint16_t s_planeIndices[s_numPlaneIndices] =
- {
- 0, 1, 2,
- 1, 3, 2,
- };
- static const char* s_shaderPath = NULL;
- static bool s_flipV = false;
- static float s_texelHalf = 0.0f;
- static bgfx::UniformHandle u_texColor;
- static bgfx::UniformHandle u_shadowMap[ShadowMapRenderTargets::Count];
- static bgfx::RenderTargetHandle s_rtShadowMap[ShadowMapRenderTargets::Count];
- static bgfx::RenderTargetHandle s_rtBlur;
- static void shaderFilePath(char* _out, const char* _name)
- {
- strcpy(_out, s_shaderPath);
- strcat(_out, _name);
- strcat(_out, ".bin");
- }
- long int fsize(FILE* _file)
- {
- long int pos = ftell(_file);
- fseek(_file, 0L, SEEK_END);
- long int size = ftell(_file);
- fseek(_file, pos, SEEK_SET);
- return size;
- }
- static const bgfx::Memory* load(const char* _filePath)
- {
- FILE* file = fopen(_filePath, "rb");
- if (NULL != file)
- {
- uint32_t size = (uint32_t)fsize(file);
- const bgfx::Memory* mem = bgfx::alloc(size+1);
- size_t ignore = fread(mem->data, 1, size, file);
- BX_UNUSED(ignore);
- fclose(file);
- mem->data[mem->size-1] = '\0';
- return mem;
- }
- return NULL;
- }
- static const bgfx::Memory* loadShader(const char* _name)
- {
- char filePath[512];
- shaderFilePath(filePath, _name);
- return load(filePath);
- }
- static const bgfx::Memory* loadTexture(const char* _name)
- {
- char filePath[512];
- strcpy(filePath, "textures/");
- strcat(filePath, _name);
- return load(filePath);
- }
- static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
- {
- const bgfx::Memory* mem;
- // Load vertex shader.
- mem = loadShader(_vsName);
- bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
- // Load fragment shader.
- mem = loadShader(_fsName);
- bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
- // Create program from shaders.
- bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
- // We can destroy vertex and fragment shader here since
- // their reference is kept inside bgfx after calling createProgram.
- // Vertex and fragment shader will be destroyed once program is
- // destroyed.
- bgfx::destroyVertexShader(vsh);
- bgfx::destroyFragmentShader(fsh);
- return program;
- }
- void mtxScaleRotateTranslate(float* _result
- , const float _scaleX
- , const float _scaleY
- , const float _scaleZ
- , const float _rotX
- , const float _rotY
- , const float _rotZ
- , const float _translateX
- , const float _translateY
- , const float _translateZ
- )
- {
- float mtxRotateTranslate[16];
- float mtxScale[16];
- mtxRotateXYZ(mtxRotateTranslate, _rotX, _rotY, _rotZ);
- mtxRotateTranslate[12] = _translateX;
- mtxRotateTranslate[13] = _translateY;
- mtxRotateTranslate[14] = _translateZ;
- memset(mtxScale, 0, sizeof(float)*16);
- mtxScale[0] = _scaleX;
- mtxScale[5] = _scaleY;
- mtxScale[10] = _scaleZ;
- mtxScale[15] = 1.0f;
- mtxMul(_result, mtxScale, mtxRotateTranslate);
- }
- void mtxBillboard(float* __restrict _result
- , const float* __restrict _view
- , const float* __restrict _pos
- , const float* __restrict _scale)
- {
- _result[ 0] = _view[0] * _scale[0];
- _result[ 1] = _view[4] * _scale[0];
- _result[ 2] = _view[8] * _scale[0];
- _result[ 3] = 0.0f;
- _result[ 4] = _view[1] * _scale[1];
- _result[ 5] = _view[5] * _scale[1];
- _result[ 6] = _view[9] * _scale[1];
- _result[ 7] = 0.0f;
- _result[ 8] = _view[2] * _scale[2];
- _result[ 9] = _view[6] * _scale[2];
- _result[10] = _view[10] * _scale[2];
- _result[11] = 0.0f;
- _result[12] = _pos[0];
- _result[13] = _pos[1];
- _result[14] = _pos[2];
- _result[15] = 1.0f;
- }
- void mtxYawPitchRoll(float* __restrict _result
- , float _yaw
- , float _pitch
- , float _roll
- )
- {
- float sroll = sinf(_roll);
- float croll = cosf(_roll);
- float spitch = sinf(_pitch);
- float cpitch = cosf(_pitch);
- float syaw = sinf(_yaw);
- float cyaw = cosf(_yaw);
- _result[ 0] = sroll * spitch * syaw + croll * cyaw;
- _result[ 1] = sroll * cpitch;
- _result[ 2] = sroll * spitch * cyaw - croll * syaw;
- _result[ 3] = 0.0f;
- _result[ 4] = croll * spitch * syaw - sroll * cyaw;
- _result[ 5] = croll * cpitch;
- _result[ 6] = croll * spitch * cyaw + sroll * syaw;
- _result[ 7] = 0.0f;
- _result[ 8] = cpitch * syaw;
- _result[ 9] = -spitch;
- _result[10] = cpitch * cyaw;
- _result[11] = 0.0f;
- _result[12] = 0.0f;
- _result[13] = 0.0f;
- _result[14] = 0.0f;
- _result[15] = 1.0f;
- }
- struct Material
- {
- union Ambient
- {
- struct
- {
- float m_r;
- float m_g;
- float m_b;
- float m_unused;
- };
- float m_v[4];
- };
- union Diffuse
- {
- struct
- {
- float m_r;
- float m_g;
- float m_b;
- float m_unused;
- };
- float m_v[4];
- };
- union Specular
- {
- struct
- {
- float m_r;
- float m_g;
- float m_b;
- float m_ns;
- };
- float m_v[4];
- };
- Ambient m_ka;
- Diffuse m_kd;
- Specular m_ks;
- };
- struct Light
- {
- union Position
- {
- struct
- {
- float m_x;
- float m_y;
- float m_z;
- float m_w;
- };
- float m_v[4];
- };
- union LightRgbPower
- {
- struct
- {
- float m_r;
- float m_g;
- float m_b;
- float m_power;
- };
- float m_v[4];
- };
- union SpotDirectionInner
- {
- struct
- {
- float m_x;
- float m_y;
- float m_z;
- float m_inner;
- };
- float m_v[4];
- };
- union AttenuationSpotOuter
- {
- struct
- {
- float m_attnConst;
- float m_attnLinear;
- float m_attnQuadrantic;
- float m_outer;
- };
- float m_v[4];
- };
- void computeViewSpaceComponents(float* _viewMtx)
- {
- vec4MulMtx(m_position_viewSpace, m_position.m_v, _viewMtx);
- float tmp[] =
- {
- m_spotDirectionInner.m_x
- , m_spotDirectionInner.m_y
- , m_spotDirectionInner.m_z
- , 0.0f
- };
- vec4MulMtx(m_spotDirectionInner_viewSpace, tmp, _viewMtx);
- m_spotDirectionInner_viewSpace[3] = m_spotDirectionInner.m_v[3];
- }
- Position m_position;
- float m_position_viewSpace[4];
- LightRgbPower m_ambientPower;
- LightRgbPower m_diffusePower;
- LightRgbPower m_specularPower;
- SpotDirectionInner m_spotDirectionInner;
- float m_spotDirectionInner_viewSpace[4];
- AttenuationSpotOuter m_attenuationSpotOuter;
- };
- struct Uniforms
- {
- void init()
- {
- m_ambientPass = 1.0f;
- m_lightningPass = 1.0f;
- m_shadowMapBias = 0.003f;
- m_shadowMapOffset = 0.0f;
- m_shadowMapParam0 = 0.5;
- m_shadowMapParam1 = 1.0;
- m_depthValuePow = 1.0f;
- m_showSmCoverage = 1.0f;
- m_shadowMapTexelSize = 1.0f/512.0f;
- m_csmFarDistances[0] = 30.0f;
- m_csmFarDistances[1] = 90.0f;
- m_csmFarDistances[2] = 180.0f;
- m_csmFarDistances[3] = 1000.0f;
- m_tetraNormalGreen[0] = 0.0f;
- m_tetraNormalGreen[1] = -0.57735026f;
- m_tetraNormalGreen[2] = 0.81649661f;
- m_tetraNormalYellow[0] = 0.0f;
- m_tetraNormalYellow[1] = -0.57735026f;
- m_tetraNormalYellow[2] = -0.81649661f;
- m_tetraNormalBlue[0] = -0.81649661f;
- m_tetraNormalBlue[1] = 0.57735026f;
- m_tetraNormalBlue[2] = 0.0f;
- m_tetraNormalRed[0] = 0.81649661f;
- m_tetraNormalRed[1] = 0.57735026f;
- m_tetraNormalRed[2] = 0.0f;
- m_XNum = 2.0f;
- m_YNum = 2.0f;
- m_XOffset = 10.0f/512.0f;
- m_YOffset = 10.0f/512.0f;
- u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Uniform4fv);
- u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Uniform4fv);
- u_params2 = bgfx::createUniform("u_params2", bgfx::UniformType::Uniform4fv);
- u_color = bgfx::createUniform("u_color", bgfx::UniformType::Uniform4fv);
- u_smSamplingParams = bgfx::createUniform("u_smSamplingParams", bgfx::UniformType::Uniform4fv);
- u_csmFarDistances = bgfx::createUniform("u_csmFarDistances", bgfx::UniformType::Uniform4fv);
- u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Uniform4x4fv);
- u_tetraNormalGreen = bgfx::createUniform("u_tetraNormalGreen", bgfx::UniformType::Uniform3fv);
- u_tetraNormalYellow = bgfx::createUniform("u_tetraNormalYellow", bgfx::UniformType::Uniform3fv);
- u_tetraNormalBlue = bgfx::createUniform("u_tetraNormalBlue", bgfx::UniformType::Uniform3fv);
- u_tetraNormalRed = bgfx::createUniform("u_tetraNormalRed", bgfx::UniformType::Uniform3fv);
- u_shadowMapMtx0 = bgfx::createUniform("u_shadowMapMtx0", bgfx::UniformType::Uniform4x4fv);
- u_shadowMapMtx1 = bgfx::createUniform("u_shadowMapMtx1", bgfx::UniformType::Uniform4x4fv);
- u_shadowMapMtx2 = bgfx::createUniform("u_shadowMapMtx2", bgfx::UniformType::Uniform4x4fv);
- u_shadowMapMtx3 = bgfx::createUniform("u_shadowMapMtx3", bgfx::UniformType::Uniform4x4fv);
- u_lightPosition = bgfx::createUniform("u_lightPosition", bgfx::UniformType::Uniform4fv);
- u_lightAmbientPower = bgfx::createUniform("u_lightAmbientPower", bgfx::UniformType::Uniform4fv);
- u_lightDiffusePower = bgfx::createUniform("u_lightDiffusePower", bgfx::UniformType::Uniform4fv);
- u_lightSpecularPower = bgfx::createUniform("u_lightSpecularPower", bgfx::UniformType::Uniform4fv);
- u_lightSpotDirectionInner = bgfx::createUniform("u_lightSpotDirectionInner", bgfx::UniformType::Uniform4fv);
- u_lightAttenuationSpotOuter = bgfx::createUniform("u_lightAttenuationSpotOuter", bgfx::UniformType::Uniform4fv);
- u_materialKa = bgfx::createUniform("u_materialKa", bgfx::UniformType::Uniform4fv);
- u_materialKd = bgfx::createUniform("u_materialKd", bgfx::UniformType::Uniform4fv);
- u_materialKs = bgfx::createUniform("u_materialKs", bgfx::UniformType::Uniform4fv);
- }
- void setPtrs(Material* _materialPtr, Light* _lightPtr, float* _colorPtr, float* _lightMtxPtr, float* _shadowMapMtx0, float* _shadowMapMtx1, float* _shadowMapMtx2, float* _shadowMapMtx3)
- {
- m_lightMtxPtr = _lightMtxPtr;
- m_colorPtr = _colorPtr;
- m_materialPtr = _materialPtr;
- m_lightPtr = _lightPtr;
- m_shadowMapMtx0 = _shadowMapMtx0;
- m_shadowMapMtx1 = _shadowMapMtx1;
- m_shadowMapMtx2 = _shadowMapMtx2;
- m_shadowMapMtx3 = _shadowMapMtx3;
- }
- // Call this once at initialization.
- void submitConstUniforms()
- {
- bgfx::setUniform(u_tetraNormalGreen, m_tetraNormalGreen);
- bgfx::setUniform(u_tetraNormalYellow, m_tetraNormalYellow);
- bgfx::setUniform(u_tetraNormalBlue, m_tetraNormalBlue);
- bgfx::setUniform(u_tetraNormalRed, m_tetraNormalRed);
- }
- // Call this once per frame.
- void submitPerFrameUniforms()
- {
- bgfx::setUniform(u_params1, m_params1);
- bgfx::setUniform(u_params2, m_params2);
- bgfx::setUniform(u_smSamplingParams, m_paramsBlur);
- bgfx::setUniform(u_csmFarDistances, m_csmFarDistances);
- bgfx::setUniform(u_materialKa, &m_materialPtr->m_ka);
- bgfx::setUniform(u_materialKd, &m_materialPtr->m_kd);
- bgfx::setUniform(u_materialKs, &m_materialPtr->m_ks);
- bgfx::setUniform(u_lightPosition, &m_lightPtr->m_position_viewSpace);
- bgfx::setUniform(u_lightAmbientPower, &m_lightPtr->m_ambientPower);
- bgfx::setUniform(u_lightDiffusePower, &m_lightPtr->m_diffusePower);
- bgfx::setUniform(u_lightSpecularPower, &m_lightPtr->m_specularPower);
- bgfx::setUniform(u_lightSpotDirectionInner, &m_lightPtr->m_spotDirectionInner_viewSpace);
- bgfx::setUniform(u_lightAttenuationSpotOuter, &m_lightPtr->m_attenuationSpotOuter);
- }
- // Call this before each draw call.
- void submitPerDrawUniforms()
- {
- bgfx::setUniform(u_shadowMapMtx0, m_shadowMapMtx0);
- bgfx::setUniform(u_shadowMapMtx1, m_shadowMapMtx1);
- bgfx::setUniform(u_shadowMapMtx2, m_shadowMapMtx2);
- bgfx::setUniform(u_shadowMapMtx3, m_shadowMapMtx3);
- bgfx::setUniform(u_params0, m_params0);
- bgfx::setUniform(u_lightMtx, m_lightMtxPtr);
- bgfx::setUniform(u_color, m_colorPtr);
- }
- void destroy()
- {
- bgfx::destroyUniform(u_params0);
- bgfx::destroyUniform(u_params1);
- bgfx::destroyUniform(u_params2);
- bgfx::destroyUniform(u_color);
- bgfx::destroyUniform(u_smSamplingParams);
- bgfx::destroyUniform(u_csmFarDistances);
- bgfx::destroyUniform(u_materialKa);
- bgfx::destroyUniform(u_materialKd);
- bgfx::destroyUniform(u_materialKs);
- bgfx::destroyUniform(u_tetraNormalGreen);
- bgfx::destroyUniform(u_tetraNormalYellow);
- bgfx::destroyUniform(u_tetraNormalBlue);
- bgfx::destroyUniform(u_tetraNormalRed);
- bgfx::destroyUniform(u_shadowMapMtx0);
- bgfx::destroyUniform(u_shadowMapMtx1);
- bgfx::destroyUniform(u_shadowMapMtx2);
- bgfx::destroyUniform(u_shadowMapMtx3);
- bgfx::destroyUniform(u_lightMtx);
- bgfx::destroyUniform(u_lightPosition);
- bgfx::destroyUniform(u_lightAmbientPower);
- bgfx::destroyUniform(u_lightDiffusePower);
- bgfx::destroyUniform(u_lightSpecularPower);
- bgfx::destroyUniform(u_lightSpotDirectionInner);
- bgfx::destroyUniform(u_lightAttenuationSpotOuter);
- }
- union
- {
- struct
- {
- float m_ambientPass;
- float m_lightningPass;
- float m_unused00;
- float m_unused01;
- };
- float m_params0[4];
- };
- union
- {
- struct
- {
- float m_shadowMapBias;
- float m_shadowMapOffset;
- float m_shadowMapParam0;
- float m_shadowMapParam1;
- };
- float m_params1[4];
- };
- union
- {
- struct
- {
- float m_depthValuePow;
- float m_showSmCoverage;
- float m_shadowMapTexelSize;
- float m_unused23;
- };
- float m_params2[4];
- };
- union
- {
- struct
- {
- float m_XNum;
- float m_YNum;
- float m_XOffset;
- float m_YOffset;
- };
- float m_paramsBlur[4];
- };
- float m_tetraNormalGreen[3];
- float m_tetraNormalYellow[3];
- float m_tetraNormalBlue[3];
- float m_tetraNormalRed[3];
- float m_csmFarDistances[4];
- float* m_lightMtxPtr;
- float* m_colorPtr;
- Light* m_lightPtr;
- float* m_shadowMapMtx0;
- float* m_shadowMapMtx1;
- float* m_shadowMapMtx2;
- float* m_shadowMapMtx3;
- Material* m_materialPtr;
- private:
- bgfx::UniformHandle u_params0;
- bgfx::UniformHandle u_params1;
- bgfx::UniformHandle u_params2;
- bgfx::UniformHandle u_color;
- bgfx::UniformHandle u_smSamplingParams;
- bgfx::UniformHandle u_csmFarDistances;
- bgfx::UniformHandle u_materialKa;
- bgfx::UniformHandle u_materialKd;
- bgfx::UniformHandle u_materialKs;
- bgfx::UniformHandle u_tetraNormalGreen;
- bgfx::UniformHandle u_tetraNormalYellow;
- bgfx::UniformHandle u_tetraNormalBlue;
- bgfx::UniformHandle u_tetraNormalRed;
- bgfx::UniformHandle u_shadowMapMtx0;
- bgfx::UniformHandle u_shadowMapMtx1;
- bgfx::UniformHandle u_shadowMapMtx2;
- bgfx::UniformHandle u_shadowMapMtx3;
- bgfx::UniformHandle u_lightMtx;
- bgfx::UniformHandle u_lightPosition;
- bgfx::UniformHandle u_lightAmbientPower;
- bgfx::UniformHandle u_lightDiffusePower;
- bgfx::UniformHandle u_lightSpecularPower;
- bgfx::UniformHandle u_lightSpotDirectionInner;
- bgfx::UniformHandle u_lightAttenuationSpotOuter;
- };
- static Uniforms s_uniforms;
- struct RenderState
- {
- enum Enum
- {
- Default = 0,
- ShadowMap_PackDepth,
- ShadowMap_PackDepthHoriz,
- ShadowMap_PackDepthVert,
- Custom_BlendLightTexture,
- Custom_DrawPlaneBottom,
- Count
- };
- uint64_t m_state;
- uint32_t m_blendFactorRgba;
- uint32_t m_fstencil;
- uint32_t m_bstencil;
- };
- static RenderState s_renderStates[RenderState::Count] =
- {
- { // Default
- 0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_DEPTH_WRITE
- | BGFX_STATE_CULL_CCW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_NONE
- , BGFX_STENCIL_NONE
- },
- { // ShadowMap_PackDepth
- 0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- | BGFX_STATE_DEPTH_WRITE
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_CULL_CCW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_NONE
- , BGFX_STENCIL_NONE
- },
- { // ShadowMap_PackDepthHoriz
- 0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- | BGFX_STATE_DEPTH_WRITE
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_CULL_CCW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_TEST_EQUAL
- | BGFX_STENCIL_FUNC_REF(1)
- | BGFX_STENCIL_FUNC_RMASK(0xff)
- | BGFX_STENCIL_OP_FAIL_S_KEEP
- | BGFX_STENCIL_OP_FAIL_Z_KEEP
- | BGFX_STENCIL_OP_PASS_Z_KEEP
- , BGFX_STENCIL_NONE
- },
- { // ShadowMap_PackDepthVert
- 0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- | BGFX_STATE_DEPTH_WRITE
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_CULL_CCW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_TEST_EQUAL
- | BGFX_STENCIL_FUNC_REF(0)
- | BGFX_STENCIL_FUNC_RMASK(0xff)
- | BGFX_STENCIL_OP_FAIL_S_KEEP
- | BGFX_STENCIL_OP_FAIL_Z_KEEP
- | BGFX_STENCIL_OP_PASS_Z_KEEP
- , BGFX_STENCIL_NONE
- },
- { // Custom_BlendLightTexture
- BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- | BGFX_STATE_DEPTH_WRITE
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
- | BGFX_STATE_CULL_CCW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_NONE
- , BGFX_STENCIL_NONE
- },
- { // Custom_DrawPlaneBottom
- BGFX_STATE_RGB_WRITE
- | BGFX_STATE_CULL_CW
- | BGFX_STATE_MSAA
- , UINT32_MAX
- , BGFX_STENCIL_NONE
- , BGFX_STENCIL_NONE
- },
- };
- struct ViewState
- {
- ViewState(uint32_t _width = 1280, uint32_t _height = 720)
- : m_width(_width)
- , m_height(_height)
- {
- }
- uint32_t m_width;
- uint32_t m_height;
- float m_view[16];
- float m_proj[16];
- };
- struct ClearValues
- {
- ClearValues(uint32_t _clearRgba = 0x30303000
- , float _clearDepth = 1.0f
- , uint8_t _clearStencil = 0
- )
- : m_clearRgba(_clearRgba)
- , m_clearDepth(_clearDepth)
- , m_clearStencil(_clearStencil)
- {
- }
- uint32_t m_clearRgba;
- float m_clearDepth;
- uint8_t m_clearStencil;
- };
- struct Aabb
- {
- float m_min[3];
- float m_max[3];
- };
- struct Obb
- {
- float m_mtx[16];
- };
- struct Sphere
- {
- float m_center[3];
- float m_radius;
- };
- struct Primitive
- {
- uint32_t m_startIndex;
- uint32_t m_numIndices;
- uint32_t m_startVertex;
- uint32_t m_numVertices;
- Sphere m_sphere;
- Aabb m_aabb;
- Obb m_obb;
- };
- typedef std::vector<Primitive> PrimitiveArray;
- struct Group
- {
- Group()
- {
- reset();
- }
- void reset()
- {
- m_vbh.idx = bgfx::invalidHandle;
- m_ibh.idx = bgfx::invalidHandle;
- m_prims.clear();
- }
- bgfx::VertexBufferHandle m_vbh;
- bgfx::IndexBufferHandle m_ibh;
- Sphere m_sphere;
- Aabb m_aabb;
- Obb m_obb;
- PrimitiveArray m_prims;
- };
- struct Mesh
- {
- void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
- {
- Group group;
- const bgfx::Memory* mem;
- uint32_t size;
- size = _numVertices*_decl.getStride();
- mem = bgfx::makeRef(_vertices, size);
- group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
- size = _numIndices*2;
- mem = bgfx::makeRef(_indices, size);
- group.m_ibh = bgfx::createIndexBuffer(mem);
- //TODO:
- // group.m_sphere = ...
- // group.m_aabb = ...
- // group.m_obb = ...
- // group.m_prims = ...
- m_groups.push_back(group);
- }
- void load(const char* _filePath)
- {
- #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
- #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
- #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
- bx::CrtFileReader reader;
- reader.open(_filePath);
- Group group;
- uint32_t chunk;
- while (4 == bx::read(&reader, chunk) )
- {
- switch (chunk)
- {
- case BGFX_CHUNK_MAGIC_VB:
- {
- bx::read(&reader, group.m_sphere);
- bx::read(&reader, group.m_aabb);
- bx::read(&reader, group.m_obb);
- bx::read(&reader, m_decl);
- uint16_t stride = m_decl.getStride();
- uint16_t numVertices;
- bx::read(&reader, numVertices);
- const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
- bx::read(&reader, mem->data, mem->size);
- group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
- }
- break;
- case BGFX_CHUNK_MAGIC_IB:
- {
- uint32_t numIndices;
- bx::read(&reader, numIndices);
- const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
- bx::read(&reader, mem->data, mem->size);
- group.m_ibh = bgfx::createIndexBuffer(mem);
- }
- break;
- case BGFX_CHUNK_MAGIC_PRI:
- {
- uint16_t len;
- bx::read(&reader, len);
- std::string material;
- material.resize(len);
- bx::read(&reader, const_cast<char*>(material.c_str() ), len);
- uint16_t num;
- bx::read(&reader, num);
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- bx::read(&reader, len);
- std::string name;
- name.resize(len);
- bx::read(&reader, const_cast<char*>(name.c_str() ), len);
- Primitive prim;
- bx::read(&reader, prim.m_startIndex);
- bx::read(&reader, prim.m_numIndices);
- bx::read(&reader, prim.m_startVertex);
- bx::read(&reader, prim.m_numVertices);
- bx::read(&reader, prim.m_sphere);
- bx::read(&reader, prim.m_aabb);
- bx::read(&reader, prim.m_obb);
- group.m_prims.push_back(prim);
- }
- m_groups.push_back(group);
- group.reset();
- }
- break;
- default:
- DBG("%08x at %d", chunk, reader.seek() );
- break;
- }
- }
- reader.close();
- }
- void unload()
- {
- for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
- {
- const Group& group = *it;
- bgfx::destroyVertexBuffer(group.m_vbh);
- if (bgfx::invalidHandle != group.m_ibh.idx)
- {
- bgfx::destroyIndexBuffer(group.m_ibh);
- }
- }
- m_groups.clear();
- }
- void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState)
- {
- bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
- submit(_viewId, _mtx, _program, _renderState, texture);
- }
- void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bgfx::TextureHandle _texture)
- {
- for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
- {
- const Group& group = *it;
- // Set uniforms.
- s_uniforms.submitPerDrawUniforms();
- // Set model matrix for rendering.
- bgfx::setTransform(_mtx);
- bgfx::setProgram(_program);
- bgfx::setIndexBuffer(group.m_ibh);
- bgfx::setVertexBuffer(group.m_vbh);
- // Set textures.
- if (bgfx::invalidHandle != _texture.idx)
- {
- bgfx::setTexture(0, u_texColor, _texture);
- }
- for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
- {
- bgfx::setTexture(4 + ii, u_shadowMap[ii], s_rtShadowMap[ii]);
- }
- // Apply render state.
- bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
- bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
- // Submit.
- bgfx::submit(_viewId);
- }
- }
- bgfx::VertexDecl m_decl;
- typedef std::vector<Group> GroupArray;
- GroupArray m_groups;
- };
- struct PosColorTexCoord0Vertex
- {
- float m_x;
- float m_y;
- float m_z;
- uint32_t m_rgba;
- float m_u;
- float m_v;
- static void init()
- {
- ms_decl.begin();
- ms_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
- ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
- ms_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
- ms_decl.end();
- }
- static bgfx::VertexDecl ms_decl;
- };
- bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
- void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = true, float _width = 1.0f, float _height = 1.0f)
- {
- if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
- {
- bgfx::TransientVertexBuffer vb;
- bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
- PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
- const float zz = 0.0f;
- const float minx = -_width;
- const float maxx = _width;
- const float miny = 0.0f;
- const float maxy = _height*2.0f;
- const float texelHalfW = s_texelHalf/_textureWidth;
- const float texelHalfH = s_texelHalf/_textureHeight;
- const float minu = -1.0f + texelHalfW;
- const float maxu = 1.0f + texelHalfW;
- float minv = texelHalfH;
- float maxv = 2.0f + texelHalfH;
- if (_originBottomLeft)
- {
- std::swap(minv, maxv);
- minv -= 1.0f;
- maxv -= 1.0f;
- }
- vertex[0].m_x = minx;
- vertex[0].m_y = miny;
- vertex[0].m_z = zz;
- vertex[0].m_rgba = 0xffffffff;
- vertex[0].m_u = minu;
- vertex[0].m_v = minv;
- vertex[1].m_x = maxx;
- vertex[1].m_y = miny;
- vertex[1].m_z = zz;
- vertex[1].m_rgba = 0xffffffff;
- vertex[1].m_u = maxu;
- vertex[1].m_v = minv;
- vertex[2].m_x = maxx;
- vertex[2].m_y = maxy;
- vertex[2].m_z = zz;
- vertex[2].m_rgba = 0xffffffff;
- vertex[2].m_u = maxu;
- vertex[2].m_v = maxv;
- bgfx::setVertexBuffer(&vb);
- }
- }
- void worldSpaceFrustumCorners(float* _corners24f
- , float _near
- , float _far
- , float _projWidth
- , float _projHeight
- , const float* __restrict _invViewMtx
- )
- {
- // Define frustum corners in view space.
- const float nw = _near * _projWidth;
- const float nh = _near * _projHeight;
- const float fw = _far * _projWidth;
- const float fh = _far * _projHeight;
- const uint8_t numCorners = 8;
- const float corners[numCorners][3] =
- {
- { -nw, nh, _near },
- { nw, nh, _near },
- { nw, -nh, _near },
- { -nw, -nh, _near },
- { -fw, fh, _far },
- { fw, fh, _far },
- { fw, -fh, _far },
- { -fw, -fh, _far },
- };
- // Convert them to world space.
- float (*out)[3] = (float(*)[3])_corners24f;
- for (uint8_t ii = 0; ii < numCorners; ++ii)
- {
- vec3MulMtx( (float*)&out[ii], (float*)&corners[ii], _invViewMtx);
- }
- }
- /**
- * _splits = { near0, far0, near1, far1... nearN, farN }
- * N = _numSplits
- */
- void splitFrustum(float* _splits, uint8_t _numSplits, float _near, float _far, float _splitWeight = 0.75f)
- {
- const float l = _splitWeight;
- const float ratio = _far/_near;
- const int8_t numSlices = _numSplits*2;
- const float numSlicesf = float(numSlices);
- // First slice.
- _splits[0] = _near;
- for (uint8_t nn = 2, ff = 1; nn < numSlices; nn+=2, ff+=2)
- {
- float si = float(int8_t(ff) ) / numSlicesf;
- const float nearp = l*(_near*powf(ratio, si) ) + (1 - l)*(_near + (_far - _near)*si);
- _splits[nn] = nearp; //near
- _splits[ff] = nearp * 1.005f; //far from previous split
- }
- // Last slice.
- _splits[numSlices-1] = _far;
- }
- struct Programs
- {
- void init()
- {
- // Misc.
- m_black = loadProgram("vs_shadowmaps_color", "fs_shadowmaps_color_black");
- m_texture = loadProgram("vs_shadowmaps_texture", "fs_shadowmaps_texture");
- m_colorTexture = loadProgram("vs_shadowmaps_color_texture", "fs_shadowmaps_color_texture");
- // Blur.
- m_vBlur[PackDepth::RGBA] = loadProgram("vs_shadowmaps_vblur", "fs_shadowmaps_vblur");
- m_hBlur[PackDepth::RGBA] = loadProgram("vs_shadowmaps_hblur", "fs_shadowmaps_hblur");
- m_vBlur[PackDepth::VSM] = loadProgram("vs_shadowmaps_vblur", "fs_shadowmaps_vblur_vsm");
- m_hBlur[PackDepth::VSM] = loadProgram("vs_shadowmaps_hblur", "fs_shadowmaps_hblur_vsm");
- // Draw depth.
- m_drawDepth[PackDepth::RGBA] = loadProgram("vs_shadowmaps_unpackdepth", "fs_shadowmaps_unpackdepth");
- m_drawDepth[PackDepth::VSM] = loadProgram("vs_shadowmaps_unpackdepth", "fs_shadowmaps_unpackdepth_vsm");
- // Pack depth.
- m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] = loadProgram("vs_shadowmaps_packdepth", "fs_shadowmaps_packdepth");
- m_packDepth[DepthImpl::InvZ][PackDepth::VSM] = loadProgram("vs_shadowmaps_packdepth", "fs_shadowmaps_packdepth_vsm");
- m_packDepth[DepthImpl::Linear][PackDepth::RGBA] = loadProgram("vs_shadowmaps_packdepth_linear", "fs_shadowmaps_packdepth_linear");
- m_packDepth[DepthImpl::Linear][PackDepth::VSM] = loadProgram("vs_shadowmaps_packdepth_linear", "fs_shadowmaps_packdepth_vsm_linear");
- // Color lightning.
- m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning", "fs_shadowmaps_color_lightning_hard");
- m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning", "fs_shadowmaps_color_lightning_pcf");
- m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning", "fs_shadowmaps_color_lightning_vsm");
- m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning", "fs_shadowmaps_color_lightning_esm");
- m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning_linear", "fs_shadowmaps_color_lightning_hard_linear");
- m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning_linear", "fs_shadowmaps_color_lightning_pcf_linear");
- m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning_linear", "fs_shadowmaps_color_lightning_vsm_linear");
- m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning_linear", "fs_shadowmaps_color_lightning_esm_linear");
- m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning_omni", "fs_shadowmaps_color_lightning_hard_omni");
- m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning_omni", "fs_shadowmaps_color_lightning_pcf_omni");
- m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning_omni", "fs_shadowmaps_color_lightning_vsm_omni");
- m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning_omni", "fs_shadowmaps_color_lightning_esm_omni");
- m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning_linear_omni", "fs_shadowmaps_color_lightning_hard_linear_omni");
- m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning_linear_omni", "fs_shadowmaps_color_lightning_pcf_linear_omni");
- m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning_linear_omni", "fs_shadowmaps_color_lightning_vsm_linear_omni");
- m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning_linear_omni", "fs_shadowmaps_color_lightning_esm_linear_omni");
- m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning_csm", "fs_shadowmaps_color_lightning_hard_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning_csm", "fs_shadowmaps_color_lightning_pcf_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning_csm", "fs_shadowmaps_color_lightning_vsm_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning_csm", "fs_shadowmaps_color_lightning_esm_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lightning_linear_csm", "fs_shadowmaps_color_lightning_hard_linear_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lightning_linear_csm", "fs_shadowmaps_color_lightning_pcf_linear_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lightning_linear_csm", "fs_shadowmaps_color_lightning_vsm_linear_csm");
- m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lightning_linear_csm", "fs_shadowmaps_color_lightning_esm_linear_csm");
- }
- void destroy()
- {
- // Color lightning.
- for (uint8_t ii = 0; ii < SmType::Count; ++ii)
- {
- for (uint8_t jj = 0; jj < DepthImpl::Count; ++jj)
- {
- for (uint8_t kk = 0; kk < SmImpl::Count; ++kk)
- {
- bgfx::destroyProgram(m_colorLightning[ii][jj][kk]);
- }
- }
- }
- // Pack depth.
- for (uint8_t ii = 0; ii < DepthImpl::Count; ++ii)
- {
- for (uint8_t jj = 0; jj < PackDepth::Count; ++jj)
- {
- bgfx::destroyProgram(m_packDepth[ii][jj]);
- }
- }
- // Draw depth.
- for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
- {
- bgfx::destroyProgram(m_drawDepth[ii]);
- }
- // Hblur.
- for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
- {
- bgfx::destroyProgram(m_hBlur[ii]);
- }
- // Vblur.
- for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
- {
- bgfx::destroyProgram(m_vBlur[ii]);
- }
- // Misc.
- bgfx::destroyProgram(m_colorTexture);
- bgfx::destroyProgram(m_texture);
- bgfx::destroyProgram(m_black);
- }
- bgfx::ProgramHandle m_black;
- bgfx::ProgramHandle m_texture;
- bgfx::ProgramHandle m_colorTexture;
- bgfx::ProgramHandle m_vBlur[PackDepth::Count];
- bgfx::ProgramHandle m_hBlur[PackDepth::Count];
- bgfx::ProgramHandle m_drawDepth[PackDepth::Count];
- bgfx::ProgramHandle m_packDepth[DepthImpl::Count][PackDepth::Count];
- bgfx::ProgramHandle m_colorLightning[SmType::Count][DepthImpl::Count][SmImpl::Count];
- };
- static Programs s_programs;
- struct ShadowMapSettings
- {
- #define IMGUI_FLOAT_PARAM(_name) float _name, _name##Min, _name##Max, _name##Step
- IMGUI_FLOAT_PARAM(m_sizePwrTwo);
- IMGUI_FLOAT_PARAM(m_depthValuePow);
- IMGUI_FLOAT_PARAM(m_near);
- IMGUI_FLOAT_PARAM(m_far);
- IMGUI_FLOAT_PARAM(m_bias);
- IMGUI_FLOAT_PARAM(m_normalOffset);
- IMGUI_FLOAT_PARAM(m_customParam0);
- IMGUI_FLOAT_PARAM(m_customParam1);
- IMGUI_FLOAT_PARAM(m_xNum);
- IMGUI_FLOAT_PARAM(m_yNum);
- IMGUI_FLOAT_PARAM(m_xOffset);
- IMGUI_FLOAT_PARAM(m_yOffset);
- bool m_doBlur;
- bgfx::ProgramHandle* m_progPack;
- bgfx::ProgramHandle* m_progDraw;
- #undef IMGUI_FLOAT_PARAM
- };
- int _main_(int /*_argc*/, char** /*_argv*/)
- {
- uint32_t debug = BGFX_DEBUG_TEXT;
- uint32_t reset = BGFX_RESET_VSYNC;
- ViewState viewState(1280, 720);
- ClearValues clearValues(0x00000000, 1.0f, 0);
- bgfx::init();
- bgfx::reset(viewState.m_width, viewState.m_height, reset);
- // Enable debug text.
- bgfx::setDebug(debug);
- // Setup root path for binary shaders. Shader binaries are different
- // for each renderer.
- switch (bgfx::getRendererType() )
- {
- default:
- case bgfx::RendererType::Direct3D9:
- s_shaderPath = "shaders/dx9/";
- s_texelHalf = 0.5f;
- break;
- case bgfx::RendererType::Direct3D11:
- s_shaderPath = "shaders/dx11/";
- break;
- case bgfx::RendererType::OpenGL:
- s_shaderPath = "shaders/glsl/";
- s_flipV = true;
- break;
- case bgfx::RendererType::OpenGLES2:
- case bgfx::RendererType::OpenGLES3:
- s_shaderPath = "shaders/gles/";
- s_flipV = true;
- break;
- }
- // Imgui.
- FILE* file = fopen("font/droidsans.ttf", "rb");
- uint32_t size = (uint32_t)fsize(file);
- void* data = malloc(size);
- size_t ignore = fread(data, 1, size, file);
- BX_UNUSED(ignore);
- fclose(file);
- imguiCreate(data, size);
- // Uniforms.
- s_uniforms.init();
- u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
- u_shadowMap[0] = bgfx::createUniform("u_shadowMap0", bgfx::UniformType::Uniform1iv);
- u_shadowMap[1] = bgfx::createUniform("u_shadowMap1", bgfx::UniformType::Uniform1iv);
- u_shadowMap[2] = bgfx::createUniform("u_shadowMap2", bgfx::UniformType::Uniform1iv);
- u_shadowMap[3] = bgfx::createUniform("u_shadowMap3", bgfx::UniformType::Uniform1iv);
- // Programs.
- s_programs.init();
- // Vertex declarations.
- bgfx::VertexDecl PosNormalTexcoordDecl;
- PosNormalTexcoordDecl.begin();
- PosNormalTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
- PosNormalTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
- PosNormalTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
- PosNormalTexcoordDecl.end();
- bgfx::VertexDecl posDecl;
- posDecl.begin();
- posDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
- posDecl.end();
- PosColorTexCoord0Vertex::init();
- // Textures.
- const bgfx::Memory* mem;
- mem = loadTexture("figure-rgba.dds");
- bgfx::TextureHandle texFigure = bgfx::createTexture(mem);
- mem = loadTexture("flare.dds");
- bgfx::TextureHandle texFlare = bgfx::createTexture(mem);
- mem = loadTexture("fieldstone-rgba.dds");
- bgfx::TextureHandle texFieldstone = bgfx::createTexture(mem);
- // Meshes.
- Mesh bunnyMesh;
- Mesh treeMesh;
- Mesh cubeMesh;
- Mesh hollowcubeMesh;
- Mesh hplaneMesh;
- Mesh vplaneMesh;
- bunnyMesh.load("meshes/bunny.bin");
- treeMesh.load("meshes/tree.bin");
- cubeMesh.load("meshes/cube.bin");
- hollowcubeMesh.load("meshes/hollowcube.bin");
- hplaneMesh.load(s_hplaneVertices, s_numHPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
- vplaneMesh.load(s_vplaneVertices, s_numVPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
- // Materials.
- Material defaultMaterial =
- {
- {{ 1.0f, 1.0f, 1.0f, 0.0f }} //ambient
- ,{{ 1.0f, 1.0f, 1.0f, 0.0f }} //diffuse
- ,{{ 1.0f, 1.0f, 1.0f, 0.0f }} //specular, exponent
- };
- // Lights.
- Light pointLight =
- {
- {{ 0.0f, 0.0f, 0.0f, 1.0f }}, //position
- { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
- {{ 1.0f, 1.0f, 1.0f, 0.0f }}, //ambient
- {{ 1.0f, 1.0f, 1.0f, 850.0f }}, //diffuse
- {{ 1.0f, 1.0f, 1.0f, 0.0f }}, //specular
- {{ 0.0f,-0.4f,-0.6f, 0.0f }}, //spotdirection, spotexponent
- { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
- {{ 1.0f, 0.0f, 1.0f, 91.0f }}, //attenuation, spotcutoff
- };
- Light directionalLight =
- {
- {{ 0.5f,-1.0f, 0.1f, 0.0f }}, //position
- { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
- {{ 1.0f, 1.0f, 1.0f, 0.02f }}, //ambient
- {{ 1.0f, 1.0f, 1.0f, 0.4f }}, //diffuse
- {{ 1.0f, 1.0f, 1.0f, 0.0f }}, //specular
- {{ 0.0f, 0.0f, 0.0f, 1.0f }}, //spotdirection, spotexponent
- { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
- {{ 0.0f, 0.0f, 0.0f, 1.0f }}, //attenuation, spotcutoff
- };
- // Setup uniforms.
- float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
- float lightMtx[16];
- float shadowMapMtx[ShadowMapRenderTargets::Count][16];
- s_uniforms.setPtrs(&defaultMaterial
- , &pointLight
- , color
- , lightMtx
- , &shadowMapMtx[ShadowMapRenderTargets::First][0]
- , &shadowMapMtx[ShadowMapRenderTargets::Second][0]
- , &shadowMapMtx[ShadowMapRenderTargets::Third][0]
- , &shadowMapMtx[ShadowMapRenderTargets::Fourth][0]
- );
- s_uniforms.submitConstUniforms();
- // Settings.
- ShadowMapSettings smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count] =
- {
- { //LightType::Spot
- { //DepthImpl::InvZ
- { //SmImpl::Hard
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 8.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.045f, 0.0f, 0.1f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
- , 450.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 3.0f, 1.0f, 10.0f, 0.01f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.02f, 0.0f, 0.3f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
- }
- },
- { //DepthImpl::Linear
- { //SmImpl::Hard
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 2000.0f, 1.0f, 2000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
- , 300.0f, 1.0f, 1500.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 0.01f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0055f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Single][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
- }
- }
- },
- { //LightType::Point
- { //DepthImpl::InvZ
- { //SmImpl::Hard
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 8.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.055f, 0.0f, 0.1f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
- , 450.0f, 1.0f, 900.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 3.0f, 1.0f, 10.0f, 0.01f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.035f, 0.0f, 0.1f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
- }
- },
- { //DepthImpl::Linear
- { //SmImpl::Hard
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.003f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.006f, 0.0f, 0.1f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
- , 400.0f, 1.0f, 900.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 0.01f // m_near
- , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 8000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
- , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
- }
- }
- },
- { //LightType::Directional
- { //DepthImpl::InvZ
- { //SmImpl::Hard
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
- , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 0.01f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
- }
- },
- { //DepthImpl::Linear
- { //SmImpl::Hard
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
- },
- { //SmImpl::PCF
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 99.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
- , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
- , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
- },
- { //SmImpl::VSM
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 1.0f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
- , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
- },
- { //SmImpl::ESM
- 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
- , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
- , 1.0f, 1.0f, 10.0f, 0.01f // m_near
- , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
- , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
- , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
- , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
- , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
- , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
- , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
- , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
- , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
- , true // m_doBlur
- , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
- , &s_programs.m_colorLightning[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
- }
- }
- }
- };
- struct SceneSettings
- {
- LightType::Enum m_lightType;
- DepthImpl::Enum m_depthImpl;
- SmImpl::Enum m_smImpl;
- float m_spotOuterAngle;
- float m_spotInnerAngle;
- float m_fovXAdjust;
- float m_fovYAdjust;
- float m_coverageSpotL;
- float m_numSplitsf;
- float m_splitDistribution;
- uint8_t m_numSplits;
- bool m_updateLights;
- bool m_updateScene;
- bool m_drawDepthBuffer;
- bool m_showSmCoverage;
- bool m_stencilPack;
- bool m_stabilize;
- };
- SceneSettings settings;
- settings.m_lightType = LightType::SpotLight;
- settings.m_depthImpl = DepthImpl::InvZ;
- settings.m_smImpl = SmImpl::Hard;
- settings.m_spotOuterAngle = 45.0f;
- settings.m_spotInnerAngle = 30.0f;
- settings.m_fovXAdjust = 0.0f;
- settings.m_fovYAdjust = 0.0f;
- settings.m_coverageSpotL = 90.0f;
- settings.m_numSplitsf = 4.0f;
- settings.m_splitDistribution = 0.6f;
- settings.m_numSplits = uint8_t(settings.m_numSplitsf);
- settings.m_updateLights = true;
- settings.m_updateScene = true;
- settings.m_drawDepthBuffer = false;
- settings.m_showSmCoverage = false;
- settings.m_stencilPack = true;
- settings.m_stabilize = true;
- ShadowMapSettings* currentSmSettings = &smSettings[settings.m_lightType][settings.m_depthImpl][settings.m_smImpl];
- // Render targets.
- uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
- uint16_t currentShadowMapSize = shadowMapSize;
- float currentShadowMapSizef = float(int16_t(currentShadowMapSize) );
- s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
- for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
- {
- s_rtShadowMap[ii] = bgfx::createRenderTarget(currentShadowMapSize, currentShadowMapSize, BGFX_RENDER_TARGET_COLOR_RGBA8 | BGFX_RENDER_TARGET_DEPTH_D24S8);
- }
- s_rtBlur = bgfx::createRenderTarget(currentShadowMapSize, currentShadowMapSize, BGFX_RENDER_TARGET_COLOR_RGBA8);
- // Setup camera.
- float initialPos[3] = { 0.0f, 60.0f, -105.0f };
- cameraSetPosition(initialPos);
- cameraSetVerticalAngle(-0.45f);
- // Set view and projection matrices.
- const float camFovy = 60.0f;
- const float camAspect = float(int32_t(viewState.m_width) ) / float(int32_t(viewState.m_height) );
- const float camNear = 0.1f;
- const float camFar = 2000.0f;
- const float projHeight = 1.0f/tanf(camFovy*( (float)M_PI/180.0f)*0.5f);
- const float projWidth = projHeight * 1.0f/camAspect;
- mtxProj(viewState.m_proj, camFovy, camAspect, camNear, camFar);
- cameraGetViewMtx(viewState.m_view);
- float timeAccumulatorLight = 0.0f;
- float timeAccumulatorScene = 0.0f;
- entry::MouseState mouseState;
- while (!entry::processEvents(viewState.m_width, viewState.m_height, debug, reset, &mouseState) )
- {
- // Imgui.
- imguiBeginFrame(mouseState.m_mx
- , mouseState.m_my
- , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
- | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
- , 0
- , viewState.m_width
- , viewState.m_height
- );
- static int32_t rightScrollArea = 0;
- imguiBeginScrollArea("Settings", viewState.m_width - 256 - 10, 10, 256, 660, &rightScrollArea);
- #define IMGUI_FLOAT_SLIDER(_name, _val) \
- imguiSlider(_name \
- , &_val \
- , *(((float*)&_val)+1) \
- , *(((float*)&_val)+2) \
- , *(((float*)&_val)+3) \
- )
- imguiBool("Update lights", settings.m_updateLights);
- imguiBool("Update scene", settings.m_updateScene);
- imguiSeparatorLine();
- imguiLabel("Shadow map depth:");
- imguiEnum(settings.m_depthImpl);
- currentSmSettings = &smSettings[settings.m_lightType][settings.m_depthImpl][settings.m_smImpl];
- imguiSeparator();
- imguiBool("Draw depth buffer.", settings.m_drawDepthBuffer);
- if (settings.m_drawDepthBuffer)
- {
- IMGUI_FLOAT_SLIDER("Depth value pow:", currentSmSettings->m_depthValuePow);
- }
- imguiSeparatorLine();
- imguiLabel("Shadow Map implementation:");
- imguiEnum(settings.m_smImpl);
- currentSmSettings = &smSettings[settings.m_lightType][settings.m_depthImpl][settings.m_smImpl];
- imguiSeparator();
- IMGUI_FLOAT_SLIDER("Bias:", currentSmSettings->m_bias);
- IMGUI_FLOAT_SLIDER("Normal offset:", currentSmSettings->m_normalOffset);
- imguiSeparator();
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- IMGUI_FLOAT_SLIDER("Near plane:", currentSmSettings->m_near);
- }
- IMGUI_FLOAT_SLIDER("Far plane:", currentSmSettings->m_far);
- imguiSeparator();
- switch(settings.m_smImpl)
- {
- case SmImpl::Hard:
- //imguiLabel("Hard");
- break;
- case SmImpl::PCF:
- imguiLabel("PCF");
- IMGUI_FLOAT_SLIDER("X Offset:", currentSmSettings->m_xOffset);
- IMGUI_FLOAT_SLIDER("Y Offset:", currentSmSettings->m_yOffset);
- break;
- case SmImpl::VSM:
- imguiLabel("VSM");
- IMGUI_FLOAT_SLIDER("Min variance", currentSmSettings->m_customParam0);
- IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
- imguiBool("Blur shadow map", currentSmSettings->m_doBlur);
- if (currentSmSettings->m_doBlur)
- {
- IMGUI_FLOAT_SLIDER("Blur X Offset:", currentSmSettings->m_xOffset);
- IMGUI_FLOAT_SLIDER("Blur Y Offset:", currentSmSettings->m_yOffset);
- }
- break;
- case SmImpl::ESM:
- imguiLabel("ESM");
- IMGUI_FLOAT_SLIDER("ESM Hardness", currentSmSettings->m_customParam0);
- IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
- imguiBool("Blur shadow map", currentSmSettings->m_doBlur);
- if (currentSmSettings->m_doBlur)
- {
- IMGUI_FLOAT_SLIDER("Blur X Offset:", currentSmSettings->m_xOffset);
- IMGUI_FLOAT_SLIDER("Blur Y Offset:", currentSmSettings->m_yOffset);
- }
- break;
- case SmImpl::Count:
- default:
- break;
- };
- imguiEndScrollArea();
- static int32_t leftScrollArea = 0;
- imguiBeginScrollArea("Light", 10, 70, 256, 334, &leftScrollArea);
- const LightType::Enum ltBefore = settings.m_lightType;
- imguiEnum(settings.m_lightType);
- const LightType::Enum ltAfter = settings.m_lightType;
- const bool bLtChanged = (ltAfter != ltBefore);
- imguiSeparator();
- imguiBool("Show shadow map coverage.", settings.m_showSmCoverage);
- imguiSeparator();
- imguiLabel("Shadow map resolution: %ux%u", currentShadowMapSize, currentShadowMapSize);
- IMGUI_FLOAT_SLIDER(" ", currentSmSettings->m_sizePwrTwo);
- imguiSeparatorLine();
- if (LightType::SpotLight == settings.m_lightType)
- {
- imguiLabel("Spot light");
- imguiSlider("Shadow map area:", &settings.m_coverageSpotL, 45.0f, 120.0f, 1.0f);
- imguiSeparator();
- imguiSlider("Spot outer cone:", &settings.m_spotOuterAngle, 0.0f, 91.0f, 0.1f);
- imguiSlider("Spot inner cone:", &settings.m_spotInnerAngle, 0.0f, 90.0f, 0.1f);
- }
- else if (LightType::PointLight == settings.m_lightType)
- {
- imguiLabel("Point light");
- imguiBool("Stencil pack", settings.m_stencilPack);
- imguiSlider("Fov X adjust:", &settings.m_fovXAdjust, -20.0f, 20.0f, 0.0001f);
- imguiSlider("Fov Y adjust:", &settings.m_fovYAdjust, -20.0f, 20.0f, 0.0001f);
- }
- else if (LightType::DirectionalLight == settings.m_lightType)
- {
- imguiLabel("Directional light");
- imguiBool("Stabilize cascades", settings.m_stabilize);
- imguiSlider("Cascade splits:", &settings.m_numSplitsf, 1.0f, 4.0f, 1.0f);
- imguiSlider("Cascade distribution:", &settings.m_splitDistribution, 0.0f, 1.0f, 0.001f);
- settings.m_numSplits = uint8_t(settings.m_numSplitsf);
- }
- #undef IMGUI_FLOAT_SLIDER
- imguiEndScrollArea();
- imguiEndFrame();
- // Update uniforms.
- s_uniforms.m_shadowMapBias = currentSmSettings->m_bias;
- s_uniforms.m_shadowMapOffset = currentSmSettings->m_normalOffset;
- s_uniforms.m_shadowMapParam0 = currentSmSettings->m_customParam0;
- s_uniforms.m_shadowMapParam1 = currentSmSettings->m_customParam1;
- s_uniforms.m_depthValuePow = currentSmSettings->m_depthValuePow;
- s_uniforms.m_XNum = currentSmSettings->m_xNum;
- s_uniforms.m_YNum = currentSmSettings->m_yNum;
- s_uniforms.m_XOffset = currentSmSettings->m_xOffset;
- s_uniforms.m_YOffset = currentSmSettings->m_yOffset;
- s_uniforms.m_showSmCoverage = float(settings.m_showSmCoverage);
- s_uniforms.m_lightPtr = (LightType::DirectionalLight == settings.m_lightType) ? &directionalLight : &pointLight;
- if (LightType::SpotLight == settings.m_lightType)
- {
- pointLight.m_attenuationSpotOuter.m_outer = settings.m_spotOuterAngle;
- pointLight.m_spotDirectionInner.m_inner = settings.m_spotInnerAngle;
- }
- else
- {
- pointLight.m_attenuationSpotOuter.m_outer = 91.0f; //above 90.0f means point light
- }
- s_uniforms.submitPerFrameUniforms();
- // Time.
- int64_t now = bx::getHPCounter();
- static int64_t last = now;
- const int64_t frameTime = now - last;
- last = now;
- const double freq = double(bx::getHPFrequency() );
- const double toMs = 1000.0/freq;
- const float deltaTime = float(frameTime/freq);
- // Use debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/16-shadowmaps");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
- // Update camera.
- cameraUpdate(deltaTime);
- // Update view mtx.
- cameraGetViewMtx(viewState.m_view);
- // Update lights.
- pointLight.computeViewSpaceComponents(viewState.m_view);
- directionalLight.computeViewSpaceComponents(viewState.m_view);
- // Update time accumulators.
- if (settings.m_updateLights) { timeAccumulatorLight += deltaTime; }
- if (settings.m_updateScene) { timeAccumulatorScene += deltaTime; }
- // Setup lights.
- pointLight.m_position.m_x = cos(timeAccumulatorLight) * 20.0f;
- pointLight.m_position.m_y = 26.0f;
- pointLight.m_position.m_z = sin(timeAccumulatorLight) * 20.0f;
- pointLight.m_spotDirectionInner.m_x = -pointLight.m_position.m_x;
- pointLight.m_spotDirectionInner.m_y = -pointLight.m_position.m_y;
- pointLight.m_spotDirectionInner.m_z = -pointLight.m_position.m_z;
- directionalLight.m_position.m_x = -cos(timeAccumulatorLight);
- directionalLight.m_position.m_y = -1.0f;
- directionalLight.m_position.m_z = -sin(timeAccumulatorLight);
- // Setup instance matrices.
- float mtxFloor[16];
- const float floorScale = 550.0f;
- mtxScaleRotateTranslate(mtxFloor
- , floorScale //scaleX
- , floorScale //scaleY
- , floorScale //scaleZ
- , 0.0f //rotX
- , 0.0f //rotY
- , 0.0f //rotZ
- , 0.0f //translateX
- , 0.0f //translateY
- , 0.0f //translateZ
- );
- float mtxBunny[16];
- mtxScaleRotateTranslate(mtxBunny
- , 5.0f
- , 5.0f
- , 5.0f
- , 0.0f
- , 1.56f - timeAccumulatorScene
- , 0.0f
- , 15.0f
- , 5.0f
- , 0.0f
- );
- float mtxHollowcube[16];
- mtxScaleRotateTranslate(mtxHollowcube
- , 2.5f
- , 2.5f
- , 2.5f
- , 0.0f
- , 1.56f - timeAccumulatorScene
- , 0.0f
- , 0.0f
- , 10.0f
- , 0.0f
- );
- float mtxCube[16];
- mtxScaleRotateTranslate(mtxCube
- , 2.5f
- , 2.5f
- , 2.5f
- , 0.0f
- , 1.56f - timeAccumulatorScene
- , 0.0f
- , -15.0f
- , 5.0f
- , 0.0f
- );
- const uint8_t numTrees = 10;
- float mtxTrees[numTrees][16];
- for (uint8_t ii = 0; ii < numTrees; ++ii)
- {
- mtxScaleRotateTranslate(mtxTrees[ii]
- , 2.0f
- , 2.0f
- , 2.0f
- , 0.0f
- , float(ii)
- , 0.0f
- , sin(float(ii)*2.0f*float(M_PI)/float(numTrees) ) * 60.0f
- , 0.0f
- , cos(float(ii)*2.0f*float(M_PI)/float(numTrees) ) * 60.0f
- );
- }
- // Compute transform matrices.
- const uint8_t shadowMapPasses = ShadowMapRenderTargets::Count;
- float lightView[shadowMapPasses][16];
- float lightProj[shadowMapPasses][16];
- float mtxYpr[TetrahedronFaces::Count][16];
- float screenProj[16];
- float screenView[16];
- mtxIdentity(screenView);
- mtxOrtho(screenProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
- if (LightType::SpotLight == settings.m_lightType)
- {
- const float fovy = settings.m_coverageSpotL;
- const float aspect = 1.0f;
- mtxProj(lightProj[ProjType::Horizontal], fovy, aspect, currentSmSettings->m_near, currentSmSettings->m_far);
- //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
- if (DepthImpl::Linear == settings.m_depthImpl)
- {
- lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
- lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
- }
- float at[3];
- vec3Add(at, pointLight.m_position.m_v, pointLight.m_spotDirectionInner.m_v);
- mtxLookAt(lightView[TetrahedronFaces::Green], pointLight.m_position.m_v, at);
- }
- else if (LightType::PointLight == settings.m_lightType)
- {
- float ypr[TetrahedronFaces::Count][3] =
- {
- { toRad( 0.0f), toRad( 27.36780516f), toRad(0.0f) }
- ,{ toRad(180.0f), toRad( 27.36780516f), toRad(0.0f) }
- ,{ toRad(-90.0f), toRad(-27.36780516f), toRad(0.0f) }
- ,{ toRad( 90.0f), toRad(-27.36780516f), toRad(0.0f) }
- };
- if (settings.m_stencilPack)
- {
- const float fovx = 143.98570868f + 3.51f + settings.m_fovXAdjust;
- const float fovy = 125.26438968f + 9.85f + settings.m_fovYAdjust;
- const float aspect = tanf(toRad(fovx*0.5f) )/tanf(toRad(fovy*0.5f) );
- mtxProj(lightProj[ProjType::Vertical]
- , fovx
- , aspect
- , currentSmSettings->m_near
- , currentSmSettings->m_far
- );
- //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
- if (DepthImpl::Linear == settings.m_depthImpl)
- {
- lightProj[ProjType::Vertical][10] /= currentSmSettings->m_far;
- lightProj[ProjType::Vertical][14] /= currentSmSettings->m_far;
- }
- ypr[TetrahedronFaces::Green][2] = toRad(180.0f);
- ypr[TetrahedronFaces::Yellow][2] = toRad( 0.0f);
- ypr[TetrahedronFaces::Blue][2] = toRad( 90.0f);
- ypr[TetrahedronFaces::Red][2] = toRad(-90.0f);
- }
- const float fovx = 143.98570868f + 7.8f + settings.m_fovXAdjust;
- const float fovy = 125.26438968f + 3.0f + settings.m_fovYAdjust;
- const float aspect = tanf(toRad(fovx*0.5f) )/tanf(toRad(fovy*0.5f) );
- mtxProj(lightProj[ProjType::Horizontal], fovy, aspect, currentSmSettings->m_near, currentSmSettings->m_far);
- //For linear depth, prevent depth division by variable w component in shaders and divide here by far plane
- if (DepthImpl::Linear == settings.m_depthImpl)
- {
- lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
- lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
- }
- for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
- {
- float mtxTmp[16];
- mtxYawPitchRoll(mtxTmp, ypr[ii][0], ypr[ii][1], ypr[ii][2]);
- float tmp[3] =
- {
- -vec3Dot(pointLight.m_position.m_v, &mtxTmp[0])
- , -vec3Dot(pointLight.m_position.m_v, &mtxTmp[4])
- , -vec3Dot(pointLight.m_position.m_v, &mtxTmp[8])
- };
- mtxTranspose(mtxYpr[ii], mtxTmp);
- memcpy(lightView[ii], mtxYpr[ii], 12*sizeof(float) );
- lightView[ii][12] = tmp[0];
- lightView[ii][13] = tmp[1];
- lightView[ii][14] = tmp[2];
- lightView[ii][15] = 1.0f;
- }
- }
- else // LightType::DirectionalLight == settings.m_lightType
- {
- // Setup light view mtx.
- float eye[3] =
- {
- -directionalLight.m_position.m_x
- , -directionalLight.m_position.m_y
- , -directionalLight.m_position.m_z
- };
- float at[3] = { 0.0f, 0.0f, 0.0f };
- mtxLookAt(lightView[0], eye, at);
- // Compute camera inverse view mtx.
- float mtxViewInv[16];
- mtxInverse(mtxViewInv, viewState.m_view);
- // Compute split distances.
- const uint8_t maxNumSplits = 4;
- BX_CHECK(maxNumSplits >= settings.m_numSplits, "Error! Max num splits.");
- float splitSlices[maxNumSplits*2];
- splitFrustum(splitSlices, settings.m_numSplits, currentSmSettings->m_near, currentSmSettings->m_far, settings.m_splitDistribution);
- // Update uniforms.
- for (uint8_t ii = 0, ff = 1; ii < settings.m_numSplits; ++ii, ff+=2)
- {
- // This lags for 1 frame, but it's not a problem.
- s_uniforms.m_csmFarDistances[ii] = splitSlices[ff];
- }
- float mtxProj[16];
- mtxOrtho(mtxProj, 1.0f, -1.0f, 1.0f, -1.0f, -currentSmSettings->m_far, currentSmSettings->m_far);
- const uint8_t numCorners = 8;
- float frustumCorners[maxNumSplits][numCorners][3];
- for (uint8_t ii = 0, nn = 0, ff = 1; ii < settings.m_numSplits; ++ii, nn+=2, ff+=2)
- {
- // Compute frustum corners for one split in world space.
- worldSpaceFrustumCorners( (float*)frustumCorners[ii], splitSlices[nn], splitSlices[ff], projWidth, projHeight, mtxViewInv);
- float min[3] = { 9000.0f, 9000.0f, 9000.0f };
- float max[3] = { -9000.0f, -9000.0f, -9000.0f };
- for (uint8_t jj = 0; jj < numCorners; ++jj)
- {
- // Transform to light space.
- float lightSpaceFrustumCorner[3];
- vec3MulMtx(lightSpaceFrustumCorner, frustumCorners[ii][jj], lightView[0]);
- // Update bounding box.
- min[0] = fminf(min[0], lightSpaceFrustumCorner[0]);
- max[0] = fmaxf(max[0], lightSpaceFrustumCorner[0]);
- min[1] = fminf(min[1], lightSpaceFrustumCorner[1]);
- max[1] = fmaxf(max[1], lightSpaceFrustumCorner[1]);
- min[2] = fminf(min[2], lightSpaceFrustumCorner[2]);
- max[2] = fmaxf(max[2], lightSpaceFrustumCorner[2]);
- }
- float minproj[3];
- float maxproj[3];
- vec3MulMtxH(minproj, min, mtxProj);
- vec3MulMtxH(maxproj, max, mtxProj);
- float offsetx, offsety;
- float scalex, scaley;
- scalex = 2.0f / (maxproj[0] - minproj[0]);
- scaley = 2.0f / (maxproj[1] - minproj[1]);
- if (settings.m_stabilize)
- {
- const float quantizer = 64.0f;
- scalex = quantizer / ceilf(quantizer / scalex);
- scaley = quantizer / ceilf(quantizer / scaley);
- }
- offsetx = 0.5f * (maxproj[0] + minproj[0]) * scalex;
- offsety = 0.5f * (maxproj[1] + minproj[1]) * scaley;
- if (settings.m_stabilize)
- {
- const float halfSize = currentShadowMapSizef * 0.5f;
- offsetx = ceilf(offsetx * halfSize) / halfSize;
- offsety = ceilf(offsety * halfSize) / halfSize;
- }
- float mtxCrop[16];
- mtxIdentity(mtxCrop);
- mtxCrop[ 0] = scalex;
- mtxCrop[ 5] = scaley;
- mtxCrop[12] = offsetx;
- mtxCrop[13] = offsety;
- mtxMul(lightProj[ii], mtxCrop, mtxProj);
- }
- }
- // Reset render targets.
- const uint32_t viewMask = (uint32_t(1) << (RENDER_PASS_18+1) ) - 1;
- const bgfx::RenderTargetHandle invalidRt = BGFX_INVALID_HANDLE;
- bgfx::setViewRenderTargetMask(viewMask, invalidRt);
- // Determine on-screen rectangle size where depth buffer will be drawn.
- const uint16_t depthRectHeight = uint16_t(float(viewState.m_height) / 2.5f);
- const uint16_t depthRectWidth = depthRectHeight;
- const uint16_t depthRectX = 0;
- const uint16_t depthRectY = viewState.m_height - depthRectHeight;
- // Setup views and render targets.
- bgfx::setViewRect(0, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewTransform(0, viewState.m_view, viewState.m_proj);
- if (LightType::SpotLight == settings.m_lightType)
- {
- /**
- * RENDER_PASS_0 - Clear shadow map. (used as convenience, otherwise render_pass_1 could be cleared)
- * RENDER_PASS_1 - Craft shadow map.
- * RENDER_PASS_5 - Vertical blur.
- * RENDER_PASS_6 - Horizontal blur.
- * RENDER_PASS_13 - Draw scene.
- * RENDER_PASS_14 - Draw floor bottom.
- * RENDER_PASS_15 - Draw depth buffer.
- */
- bgfx::setViewRect(RENDER_PASS_0, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_1, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_5, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_6, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_13, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_14, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_15, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewTransform(RENDER_PASS_0, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_1, lightView[0], lightProj[ProjType::Horizontal]);
- bgfx::setViewTransform(RENDER_PASS_5, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_6, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_13, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_14, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_15, screenView, screenProj);
- bgfx::setViewRenderTarget(RENDER_PASS_0, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_1, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_5, s_rtBlur);
- bgfx::setViewRenderTarget(RENDER_PASS_6, s_rtShadowMap[0]);
- }
- else if (LightType::PointLight == settings.m_lightType)
- {
- /**
- * RENDER_PASS_0 - Clear entire shadow map.
- * RENDER_PASS_1 - Craft green tetrahedron shadow face.
- * RENDER_PASS_2 - Craft yellow tetrahedron shadow face.
- * RENDER_PASS_3 - Craft blue tetrahedron shadow face.
- * RENDER_PASS_4 - Craft red tetrahedron shadow face.
- * RENDER_PASS_5 - Vertical blur.
- * RENDER_PASS_6 - Horizontal blur.
- * RENDER_PASS_13 - Draw scene.
- * RENDER_PASS_14 - Draw floor bottom.
- * RENDER_PASS_15 - Draw depth buffer.
- */
- bgfx::setViewRect(RENDER_PASS_0, 0, 0, currentShadowMapSize, currentShadowMapSize);
- if (settings.m_stencilPack)
- {
- const uint16_t f = currentShadowMapSize; //full size
- const uint16_t h = currentShadowMapSize/2; //half size
- bgfx::setViewRect(RENDER_PASS_1, 0, 0, f, h);
- bgfx::setViewRect(RENDER_PASS_2, 0, h, f, h);
- bgfx::setViewRect(RENDER_PASS_3, 0, 0, h, f);
- bgfx::setViewRect(RENDER_PASS_4, h, 0, h, f);
- }
- else
- {
- const uint16_t h = currentShadowMapSize/2; //half size
- bgfx::setViewRect(RENDER_PASS_1, 0, 0, h, h);
- bgfx::setViewRect(RENDER_PASS_2, h, 0, h, h);
- bgfx::setViewRect(RENDER_PASS_3, 0, h, h, h);
- bgfx::setViewRect(RENDER_PASS_4, h, h, h, h);
- }
- bgfx::setViewRect(RENDER_PASS_5, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_6, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_13, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_14, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_15, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewTransform(RENDER_PASS_0, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_1, lightView[TetrahedronFaces::Green], lightProj[ProjType::Horizontal]);
- bgfx::setViewTransform(RENDER_PASS_2, lightView[TetrahedronFaces::Yellow], lightProj[ProjType::Horizontal]);
- if(settings.m_stencilPack)
- {
- bgfx::setViewTransform(RENDER_PASS_3, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Vertical]);
- bgfx::setViewTransform(RENDER_PASS_4, lightView[TetrahedronFaces::Red], lightProj[ProjType::Vertical]);
- }
- else
- {
- bgfx::setViewTransform(RENDER_PASS_3, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Horizontal]);
- bgfx::setViewTransform(RENDER_PASS_4, lightView[TetrahedronFaces::Red], lightProj[ProjType::Horizontal]);
- }
- bgfx::setViewTransform(RENDER_PASS_5, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_6, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_13, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_14, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_15, screenView, screenProj);
- bgfx::setViewRenderTarget(RENDER_PASS_0, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_1, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_2, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_3, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_4, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_5, s_rtBlur);
- bgfx::setViewRenderTarget(RENDER_PASS_6, s_rtShadowMap[0]);
- }
- else // LightType::DirectionalLight == settings.m_lightType
- {
- /**
- * RENDER_PASS_1 - Craft shadow map for first split.
- * RENDER_PASS_2 - Craft shadow map for second split.
- * RENDER_PASS_3 - Craft shadow map for third split.
- * RENDER_PASS_4 - Craft shadow map for fourth split.
- * RENDER_PASS_5 - Vertical blur for first split.
- * RENDER_PASS_6 - Horizontal blur for first split.
- * RENDER_PASS_7 - Vertical blur for second split.
- * RENDER_PASS_8 - Horizontal blur for second split.
- * RENDER_PASS_9 - Vertical blur for third split.
- * RENDER_PASS_10 - Horizontal blur for third split.
- * RENDER_PASS_11 - Vertical blur for fourth split.
- * RENDER_PASS_12 - Horizontal blur for fourth split.
- * RENDER_PASS_13 - Draw scene.
- * RENDER_PASS_14 - Draw floor bottom.
- * RENDER_PASS_15 - Draw depth buffer for first split.
- * RENDER_PASS_16 - Draw depth buffer for second split.
- * RENDER_PASS_17 - Draw depth buffer for third split.
- * RENDER_PASS_18 - Draw depth buffer for fourth split.
- */
- const uint16_t depthRectHeight = viewState.m_height / 3;
- const uint16_t depthRectWidth = depthRectHeight;
- const uint16_t depthRectX = 0;
- const uint16_t depthRectY = viewState.m_height - depthRectHeight;
- bgfx::setViewRect(RENDER_PASS_1, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_2, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_3, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_4, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_5, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_6, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_7, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_8, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_9, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_10, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_11, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_12, 0, 0, currentShadowMapSize, currentShadowMapSize);
- bgfx::setViewRect(RENDER_PASS_13, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_14, 0, 0, viewState.m_width, viewState.m_height);
- bgfx::setViewRect(RENDER_PASS_15, depthRectX+(0*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewRect(RENDER_PASS_16, depthRectX+(1*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewRect(RENDER_PASS_17, depthRectX+(2*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewRect(RENDER_PASS_18, depthRectX+(3*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
- bgfx::setViewTransform(RENDER_PASS_1, lightView[0], lightProj[0]);
- bgfx::setViewTransform(RENDER_PASS_2, lightView[0], lightProj[1]);
- bgfx::setViewTransform(RENDER_PASS_3, lightView[0], lightProj[2]);
- bgfx::setViewTransform(RENDER_PASS_4, lightView[0], lightProj[3]);
- bgfx::setViewTransform(RENDER_PASS_5, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_6, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_7, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_8, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_9, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_10, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_11, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_12, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_13, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_14, viewState.m_view, viewState.m_proj);
- bgfx::setViewTransform(RENDER_PASS_15, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_16, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_17, screenView, screenProj);
- bgfx::setViewTransform(RENDER_PASS_18, screenView, screenProj);
- bgfx::setViewRenderTarget(RENDER_PASS_1, s_rtShadowMap[0]);
- bgfx::setViewRenderTarget(RENDER_PASS_2, s_rtShadowMap[1]);
- bgfx::setViewRenderTarget(RENDER_PASS_3, s_rtShadowMap[2]);
- bgfx::setViewRenderTarget(RENDER_PASS_4, s_rtShadowMap[3]);
- bgfx::setViewRenderTarget(RENDER_PASS_5, s_rtBlur); //vblur
- bgfx::setViewRenderTarget(RENDER_PASS_6, s_rtShadowMap[0]); //hblur
- bgfx::setViewRenderTarget(RENDER_PASS_7, s_rtBlur); //vblur
- bgfx::setViewRenderTarget(RENDER_PASS_8, s_rtShadowMap[1]); //hblur
- bgfx::setViewRenderTarget(RENDER_PASS_9, s_rtBlur); //vblur
- bgfx::setViewRenderTarget(RENDER_PASS_10, s_rtShadowMap[2]); //hblur
- bgfx::setViewRenderTarget(RENDER_PASS_11, s_rtBlur); //vblur
- bgfx::setViewRenderTarget(RENDER_PASS_12, s_rtShadowMap[3]); //hblur
- }
- // Clear backbuffer at beginning.
- bgfx::setViewClear(0
- , BGFX_CLEAR_COLOR_BIT
- | BGFX_CLEAR_DEPTH_BIT
- , clearValues.m_clearRgba
- , clearValues.m_clearDepth
- , clearValues.m_clearStencil
- );
- bgfx::submit(0);
- // Clear shadowmap rendertarget at beginning.
- const uint8_t flags0 = (LightType::DirectionalLight == settings.m_lightType)
- ? 0
- : BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT | BGFX_CLEAR_STENCIL_BIT
- ;
- bgfx::setViewClear(RENDER_PASS_0
- , flags0
- , 0xfefefefe //blur fails on completely white regions
- , clearValues.m_clearDepth
- , clearValues.m_clearStencil
- );
- bgfx::submit(RENDER_PASS_0);
- const uint8_t flags1 = (LightType::DirectionalLight == settings.m_lightType)
- ? BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
- : 0
- ;
- for (uint8_t ii = 0; ii < 4; ++ii)
- {
- bgfx::setViewClear(RENDER_PASS_1+ii
- , flags1
- , 0xfefefefe //blur fails on completely white regions
- , clearValues.m_clearDepth
- , clearValues.m_clearStencil
- );
- bgfx::submit(RENDER_PASS_1+ii);
- }
- // Render.
- // Craft shadow map.
- {
- // Craft stencil mask for point light shadow map packing.
- if(LightType::PointLight == settings.m_lightType && settings.m_stencilPack)
- {
- if (bgfx::checkAvailTransientVertexBuffer(6, posDecl) )
- {
- struct Pos
- {
- float m_x, m_y, m_z;
- };
- bgfx::TransientVertexBuffer vb;
- bgfx::allocTransientVertexBuffer(&vb, 6, posDecl);
- Pos* vertex = (Pos*)vb.data;
- const float min = 0.0f;
- const float max = 1.0f;
- const float center = 0.5f;
- const float zz = 0.0f;
- vertex[0].m_x = min;
- vertex[0].m_y = min;
- vertex[0].m_z = zz;
- vertex[1].m_x = max;
- vertex[1].m_y = min;
- vertex[1].m_z = zz;
- vertex[2].m_x = center;
- vertex[2].m_y = center;
- vertex[2].m_z = zz;
- vertex[3].m_x = center;
- vertex[3].m_y = center;
- vertex[3].m_z = zz;
- vertex[4].m_x = max;
- vertex[4].m_y = max;
- vertex[4].m_z = zz;
- vertex[5].m_x = min;
- vertex[5].m_y = max;
- vertex[5].m_z = zz;
- bgfx::setProgram(s_programs.m_black);
- bgfx::setState(0);
- bgfx::setStencil(BGFX_STENCIL_TEST_ALWAYS
- | BGFX_STENCIL_FUNC_REF(1)
- | BGFX_STENCIL_FUNC_RMASK(0xff)
- | BGFX_STENCIL_OP_FAIL_S_REPLACE
- | BGFX_STENCIL_OP_FAIL_Z_REPLACE
- | BGFX_STENCIL_OP_PASS_Z_REPLACE
- );
- bgfx::setVertexBuffer(&vb);
- bgfx::submit(RENDER_PASS_0);
- }
- }
- // Draw scene into shadowmap.
- uint8_t drawNum;
- if (LightType::SpotLight == settings.m_lightType)
- {
- drawNum = 1;
- }
- else if (LightType::PointLight == settings.m_lightType)
- {
- drawNum = 4;
- }
- else //LightType::DirectionalLight == settings.m_lightType)
- {
- drawNum = settings.m_numSplits;
- }
- for (uint8_t ii = 0; ii < drawNum; ++ii)
- {
- const uint8_t viewId = RENDER_PASS_1 + ii;
- uint8_t renderStateIndex = RenderState::ShadowMap_PackDepth;
- if(LightType::PointLight == settings.m_lightType && settings.m_stencilPack)
- {
- renderStateIndex = (ii < 2) ? RenderState::ShadowMap_PackDepthHoriz : RenderState::ShadowMap_PackDepthVert;
- }
- // Floor.
- hplaneMesh.submit(viewId
- , mtxFloor
- , *currentSmSettings->m_progPack
- , s_renderStates[renderStateIndex]
- );
- // Bunny.
- bunnyMesh.submit(viewId
- , mtxBunny
- , *currentSmSettings->m_progPack
- , s_renderStates[renderStateIndex]
- );
- // Hollow cube.
- hollowcubeMesh.submit(viewId
- , mtxHollowcube
- , *currentSmSettings->m_progPack
- , s_renderStates[renderStateIndex]
- );
- // Cube.
- cubeMesh.submit(viewId
- , mtxCube
- , *currentSmSettings->m_progPack
- , s_renderStates[renderStateIndex]
- );
- // Trees.
- for (uint8_t ii = 0; ii < numTrees; ++ii)
- {
- treeMesh.submit(viewId
- , mtxTrees[ii]
- , *currentSmSettings->m_progPack
- , s_renderStates[renderStateIndex]
- );
- }
- }
- }
- PackDepth::Enum depthType = (SmImpl::VSM == settings.m_smImpl) ? PackDepth::VSM : PackDepth::RGBA;
- bool bVsmOrEsm = (SmImpl::VSM == settings.m_smImpl) || (SmImpl::ESM == settings.m_smImpl);
- // Blur shadow map.
- if (bVsmOrEsm && currentSmSettings->m_doBlur)
- {
- bgfx::setTexture(4, u_shadowMap[0], s_rtShadowMap[0]);
- bgfx::setProgram(s_programs.m_vBlur[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(RENDER_PASS_5);
- bgfx::setTexture(4, u_shadowMap[0], s_rtBlur);
- bgfx::setProgram(s_programs.m_hBlur[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(RENDER_PASS_6);
- if (LightType::DirectionalLight == settings.m_lightType)
- {
- for (uint8_t ii = 1, jj = 2; ii < settings.m_numSplits; ++ii, jj+=2)
- {
- const uint8_t viewId = RENDER_PASS_5 + jj;
- bgfx::setTexture(4, u_shadowMap[0], s_rtShadowMap[ii]);
- bgfx::setProgram(s_programs.m_vBlur[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(viewId);
- bgfx::setTexture(4, u_shadowMap[0], s_rtBlur);
- bgfx::setProgram(s_programs.m_hBlur[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(viewId+1);
- }
- }
- }
- // Draw scene.
- {
- // Setup shadow mtx.
- float mtxShadow[16];
- const float ymul = (s_flipV) ? 0.5f : -0.5f;
- const float zadd = (DepthImpl::Linear == settings.m_depthImpl) ? 0.0f : 0.5f;
- const float mtxBias[16] =
- {
- 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, ymul, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.5f, 0.5f, zadd, 1.0f,
- };
- if (LightType::SpotLight == settings.m_lightType)
- {
- float mtxTmp[16];
- mtxMul(mtxTmp, lightProj[ProjType::Horizontal], mtxBias);
- mtxMul(mtxShadow, lightView[0], mtxTmp); //lightViewProjBias
- }
- else if (LightType::PointLight == settings.m_lightType)
- {
- const float s = (s_flipV) ? 1.0f : -1.0f; //sign
- const float zadd = (DepthImpl::Linear == settings.m_depthImpl) ? 0.0f : 0.5f;
- const float mtxCropBias[2][TetrahedronFaces::Count][16] =
- {
- { // settings.m_stencilPack == false
- { // D3D: Green, OGL: Blue
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.25f, 0.25f, zadd, 1.0f,
- },
- { // D3D: Yellow, OGL: Red
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.75f, 0.25f, zadd, 1.0f,
- },
- { // D3D: Blue, OGL: Green
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.25f, 0.75f, zadd, 1.0f,
- },
- { // D3D: Red, OGL: Yellow
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.75f, 0.75f, zadd, 1.0f,
- },
- },
- { // settings.m_stencilPack == true
- { // D3D: Red, OGL: Blue
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.25f, 0.5f, zadd, 1.0f,
- },
- { // D3D: Blue, OGL: Red
- 0.25f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.75f, 0.5f, zadd, 1.0f,
- },
- { // D3D: Green, OGL: Green
- 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.5f, 0.75f, zadd, 1.0f,
- },
- { // D3D: Yellow, OGL: Yellow
- 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, s*0.25f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.5f, 0.25f, zadd, 1.0f,
- },
- }
- };
- //Use as: [stencilPack][flipV][tetrahedronFace]
- static const uint8_t cropBiasIndices[2][2][4] =
- {
- { // settings.m_stencilPack == false
- { 0, 1, 2, 3 }, //flipV == false
- { 2, 3, 0, 1 }, //flipV == true
- },
- { // settings.m_stencilPack == true
- { 3, 2, 0, 1 }, //flipV == false
- { 2, 3, 0, 1 }, //flipV == true
- },
- };
- for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
- {
- ProjType::Enum projType = (settings.m_stencilPack) ? ProjType::Enum(ii>1) : ProjType::Horizontal;
- uint8_t biasIndex = cropBiasIndices[settings.m_stencilPack][uint8_t(s_flipV)][ii];
- float mtxTmp[16];
- mtxMul(mtxTmp, mtxYpr[ii], lightProj[projType]);
- mtxMul(shadowMapMtx[ii], mtxTmp, mtxCropBias[settings.m_stencilPack][biasIndex]); //mtxYprProjBias
- }
- mtxTranslate(mtxShadow //lightInvTranslate
- , -pointLight.m_position.m_v[0]
- , -pointLight.m_position.m_v[1]
- , -pointLight.m_position.m_v[2]
- );
- }
- else //LightType::DirectionalLight == settings.m_lightType
- {
- for (uint8_t ii = 0; ii < settings.m_numSplits; ++ii)
- {
- float mtxTmp[16];
- mtxMul(mtxTmp, lightProj[ii], mtxBias);
- mtxMul(shadowMapMtx[ii], lightView[0], mtxTmp); //lViewProjCropBias
- }
- }
- // Floor.
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- mtxMul(lightMtx, mtxFloor, mtxShadow); //not needed for directional light
- }
- hplaneMesh.submit(RENDER_PASS_13
- , mtxFloor
- , *currentSmSettings->m_progDraw
- , s_renderStates[RenderState::Default]
- );
- // Bunny.
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- mtxMul(lightMtx, mtxBunny, mtxShadow);
- }
- bunnyMesh.submit(RENDER_PASS_13
- , mtxBunny
- , *currentSmSettings->m_progDraw
- , s_renderStates[RenderState::Default]
- );
- // Hollow cube.
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- mtxMul(lightMtx, mtxHollowcube, mtxShadow);
- }
- hollowcubeMesh.submit(RENDER_PASS_13
- , mtxHollowcube
- , *currentSmSettings->m_progDraw
- , s_renderStates[RenderState::Default]
- );
- // Cube.
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- mtxMul(lightMtx, mtxCube, mtxShadow);
- }
- cubeMesh.submit(RENDER_PASS_13
- , mtxCube
- , *currentSmSettings->m_progDraw
- , s_renderStates[RenderState::Default]
- );
- // Trees.
- for (uint8_t ii = 0; ii < numTrees; ++ii)
- {
- if (LightType::DirectionalLight != settings.m_lightType)
- {
- mtxMul(lightMtx, mtxTrees[ii], mtxShadow);
- }
- treeMesh.submit(RENDER_PASS_13
- , mtxTrees[ii]
- , *currentSmSettings->m_progDraw
- , s_renderStates[RenderState::Default]
- );
- }
- // Lights.
- if (LightType::SpotLight == settings.m_lightType || LightType::PointLight == settings.m_lightType)
- {
- const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
- float mtx[16];
- mtxBillboard(mtx, viewState.m_view, pointLight.m_position.m_v, lightScale);
- vplaneMesh.submit(RENDER_PASS_13
- , mtx
- , s_programs.m_colorTexture
- , s_renderStates[RenderState::Custom_BlendLightTexture]
- , texFlare
- );
- }
- // Draw floor bottom.
- float floorBottomMtx[16];
- mtxScaleRotateTranslate(floorBottomMtx
- , floorScale //scaleX
- , floorScale //scaleY
- , floorScale //scaleZ
- , 0.0f //rotX
- , 0.0f //rotY
- , 0.0f //rotZ
- , 0.0f //translateX
- , -0.1f //translateY
- , 0.0f //translateZ
- );
- hplaneMesh.submit(RENDER_PASS_14
- , floorBottomMtx
- , s_programs.m_texture
- , s_renderStates[RenderState::Custom_DrawPlaneBottom]
- , texFigure
- );
- }
- // Draw depth rect.
- if (settings.m_drawDepthBuffer)
- {
- bgfx::setTexture(4, u_shadowMap[0], s_rtShadowMap[0]);
- bgfx::setProgram(s_programs.m_drawDepth[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(RENDER_PASS_15);
- if (LightType::DirectionalLight == settings.m_lightType)
- {
- for (uint8_t ii = 1; ii < settings.m_numSplits; ++ii)
- {
- bgfx::setTexture(4, u_shadowMap[0], s_rtShadowMap[ii]);
- bgfx::setProgram(s_programs.m_drawDepth[depthType]);
- bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
- screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
- bgfx::submit(RENDER_PASS_15+ii);
- }
- }
- }
- // Update render target size.
- shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
- if (bLtChanged || currentShadowMapSize != shadowMapSize)
- {
- currentShadowMapSize = shadowMapSize;
- currentShadowMapSizef = float(int16_t(currentShadowMapSize) );
- s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
- bgfx::destroyRenderTarget(s_rtShadowMap[0]);
- s_rtShadowMap[0] = bgfx::createRenderTarget(currentShadowMapSize, currentShadowMapSize, BGFX_RENDER_TARGET_COLOR_RGBA8 | BGFX_RENDER_TARGET_DEPTH_D24S8);
- if (LightType::DirectionalLight == settings.m_lightType)
- {
- for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii)
- {
- bgfx::destroyRenderTarget(s_rtShadowMap[ii]);
- s_rtShadowMap[ii] = bgfx::createRenderTarget(currentShadowMapSize, currentShadowMapSize, BGFX_RENDER_TARGET_COLOR_RGBA8 | BGFX_RENDER_TARGET_DEPTH_D24S8);
- }
- }
- bgfx::destroyRenderTarget(s_rtBlur);
- s_rtBlur = bgfx::createRenderTarget(currentShadowMapSize, currentShadowMapSize, BGFX_RENDER_TARGET_COLOR_RGBA8);
- }
- // Advance to next frame. Rendering thread will be kicked to
- // process submitted rendering primitives.
- bgfx::frame();
- }
- bunnyMesh.unload();
- treeMesh.unload();
- cubeMesh.unload();
- hollowcubeMesh.unload();
- hplaneMesh.unload();
- vplaneMesh.unload();
- bgfx::destroyTexture(texFigure);
- bgfx::destroyTexture(texFieldstone);
- bgfx::destroyTexture(texFlare);
- for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
- {
- bgfx::destroyRenderTarget(s_rtShadowMap[ii]);
- }
- bgfx::destroyRenderTarget(s_rtBlur);
- s_programs.destroy();
- bgfx::destroyUniform(u_texColor);
- bgfx::destroyUniform(u_shadowMap[3]);
- bgfx::destroyUniform(u_shadowMap[2]);
- bgfx::destroyUniform(u_shadowMap[1]);
- bgfx::destroyUniform(u_shadowMap[0]);
- s_uniforms.destroy();
- imguiDestroy();
- // Shutdown bgfx.
- bgfx::shutdown();
- return 0;
- }
|