12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018 |
- package h3d.impl;
- #if (hldx && dx12)
- import h3d.impl.Driver;
- import dx.Dx12;
- import haxe.Int64;
- import h3d.mat.Pass;
- import h3d.mat.Stencil;
- private typedef Driver = Dx12;
- class TempBuffer {
- public var next : TempBuffer;
- public var buffer : GpuResource;
- public var size : Int;
- public var lastUse : Int;
- public function new() {
- }
- public inline function count() {
- var b = this;
- var k = 0;
- while( b != null ) {
- k++;
- b = b.next;
- }
- return k;
- }
- }
- class ManagedHeapArray {
- var heaps : Array<ManagedHeap>;
- var type : DescriptorHeapType;
- var size : Int;
- var cursor : Int;
- public function new(type,size) {
- this.type = type;
- this.size = size;
- heaps = [];
- }
- public function reset() {
- cursor = 0;
- }
- public function next() {
- var h = heaps[cursor++];
- if( h == null ) {
- h = new ManagedHeap(type, size);
- heaps.push(h);
- } else
- h.clear();
- return h;
- }
- }
- class DxFrame {
- public var backBuffer : ResourceData;
- public var backBufferView : Address;
- public var depthBuffer : GpuResource;
- public var allocator : CommandAllocator;
- public var commandList : CommandList;
- public var fenceValue : Int64;
- public var toRelease : Array<Resource> = [];
- public var tmpBufToNullify : Array<Texture> = [];
- public var tmpBufToRelease : Array<dx.Dx12.GpuResource> = [];
- public var shaderResourceViews : ManagedHeap;
- public var samplerViews : ManagedHeap;
- public var shaderResourceCache : ManagedHeapArray;
- public var samplerCache : ManagedHeapArray;
- public var availableBuffers : TempBuffer;
- public var usedBuffers : TempBuffer;
- public var queryHeaps : Array<QueryHeap> = [];
- public var queriesPending : Array<Query> = [];
- public var queryCurrentHeap : Int;
- public var queryHeapOffset : Int;
- public var queryBuffer : GpuResource;
- public function new() {
- }
- }
- class CachedPipeline {
- public var bytes : hl.Bytes;
- public var size : Int;
- public var pipeline : GraphicsPipelineState;
- public function new() {
- }
- }
- class ShaderRegisters {
- public var globals : Int;
- public var params : Int;
- public var buffers : Int;
- public var textures : Int;
- public var samplers : Int;
- public var texturesCount : Int;
- public var textures2DCount : Int;
- public function new() {
- }
- }
- class CompiledShader {
- public var vertexRegisters : ShaderRegisters;
- public var fragmentRegisters : ShaderRegisters;
- public var format : hxd.BufferFormat;
- public var pipeline : GraphicsPipelineStateDesc;
- public var pipelines : Map<Int,hl.NativeArray<CachedPipeline>> = new Map();
- public var rootSignature : RootSignature;
- public var inputLayout : hl.CArray<InputElementDesc>;
- public var inputCount : Int;
- public var shader : hxsl.RuntimeShader;
- public function new() {
- }
- }
- @:struct class TempObjects {
- public var renderTargets : hl.BytesAccess<Address>;
- public var depthStencils : hl.BytesAccess<Address>;
- public var vertexViews : hl.CArray<VertexBufferView>;
- public var descriptors2 : hl.NativeArray<DescriptorHeap>;
- @:packed public var heap(default,null) : HeapProperties;
- @:packed public var barrier(default,null) : ResourceBarrier;
- @:packed public var clearColor(default,null) : ClearColor;
- @:packed public var clearValue(default,null) : ClearValue;
- @:packed public var viewport(default,null) : Viewport;
- @:packed public var rect(default,null) : Rect;
- @:packed public var tex2DSRV(default,null) : Tex2DSRV;
- @:packed public var texCubeSRV(default,null) : TexCubeSRV;
- @:packed public var tex2DArraySRV(default,null) : Tex2DArraySRV;
- @:packed public var bufferSRV(default,null) : BufferSRV;
- @:packed public var samplerDesc(default,null) : SamplerDesc;
- @:packed public var cbvDesc(default,null) : ConstantBufferViewDesc;
- @:packed public var rtvDesc(default,null) : RenderTargetViewDesc;
- public var pass : h3d.mat.Pass;
- public function new() {
- renderTargets = new hl.Bytes(8 * 8);
- depthStencils = new hl.Bytes(8);
- vertexViews = hl.CArray.alloc(VertexBufferView, 16);
- pass = new h3d.mat.Pass("default");
- pass.stencil = new h3d.mat.Stencil();
- tex2DSRV.dimension = TEXTURE2D;
- texCubeSRV.dimension = TEXTURECUBE;
- tex2DArraySRV.dimension = TEXTURE2DARRAY;
- tex2DSRV.mipLevels = texCubeSRV.mipLevels = tex2DArraySRV.mipLevels = -1;
- tex2DSRV.shader4ComponentMapping = ShaderComponentMapping.DEFAULT;
- texCubeSRV.shader4ComponentMapping = ShaderComponentMapping.DEFAULT;
- tex2DArraySRV.shader4ComponentMapping = ShaderComponentMapping.DEFAULT;
- bufferSRV.dimension = BUFFER;
- bufferSRV.flags = RAW;
- bufferSRV.shader4ComponentMapping = ShaderComponentMapping.DEFAULT;
- samplerDesc.comparisonFunc = NEVER;
- samplerDesc.maxLod = 1e30;
- descriptors2 = new hl.NativeArray(2);
- barrier.subResource = -1; // all
- }
- }
- class ManagedHeap {
- public var stride(default,null) : Int;
- var size : Int;
- var start : Int;
- var cursor : Int;
- var limit : Int;
- var type : DescriptorHeapType;
- var heap : DescriptorHeap;
- var address : Address;
- var cpuToGpu : Int64;
- public var available(get,never) : Int;
- public function new(type,size=8) {
- this.type = type;
- this.stride = Driver.getDescriptorHandleIncrementSize(type);
- allocHeap(size);
- }
- function allocHeap( size : Int ) {
- var desc = new DescriptorHeapDesc();
- desc.type = type;
- desc.numDescriptors = size;
- if( type == CBV_SRV_UAV || type == SAMPLER )
- desc.flags = SHADER_VISIBLE;
- heap = new DescriptorHeap(desc);
- limit = cursor = start = 0;
- this.size = size;
- address = heap.getHandle(false);
- cpuToGpu = desc.flags == SHADER_VISIBLE ? ( heap.getHandle(true).value - address.value ) : 0;
- }
- public dynamic function onFree( prev : DescriptorHeap ) {
- throw "Too many buffers";
- }
- public function alloc( count : Int ) {
- if( cursor >= limit && cursor + count > size ) {
- cursor = 0;
- if( limit == 0 ) {
- var prev = heap;
- allocHeap((size * 3) >> 1);
- onFree(prev);
- }
- }
- if( cursor < limit && cursor + count >= limit ) {
- var prev = heap;
- allocHeap((size * 3) >> 1);
- onFree(prev);
- }
- var pos = cursor;
- cursor += count;
- return address.offset(pos * stride);
- }
- inline function get_available() {
- var d = limit - cursor;
- return d <= 0 ? size + d : d;
- }
- public inline function grow( onFree ) {
- var prev = heap;
- allocHeap((size*3)>>1);
- onFree(prev);
- return heap;
- }
- public function clear() {
- limit = cursor = start = 0;
- }
- public function next() {
- limit = start;
- start = cursor;
- }
- public inline function toGPU( address : Address ) : Address {
- return new Address(address.value + cpuToGpu);
- }
- }
- class ResourceData {
- public var res : GpuResource;
- public var state : ResourceState;
- public function new() {
- }
- }
- class BufferData extends ResourceData {
- public var uploaded : Bool;
- }
- class VertexBufferData extends BufferData {
- public var view : dx.Dx12.VertexBufferView;
- public var iview : dx.Dx12.IndexBufferView;
- public var size : Int;
- }
- class TextureData extends ResourceData {
- public var format : DxgiFormat;
- public var color : h3d.Vector4;
- public var tmpBuf : dx.Dx12.GpuResource;
- var clearColorChanges : Int;
- public function setClearColor( c : h3d.Vector4 ) {
- var color = color;
- if( clearColorChanges > 10 || (color.r == c.r && color.g == c.g && color.b == c.b && color.a == c.a) )
- return false;
- clearColorChanges++;
- color.load(c);
- return true;
- }
- }
- class QueryData {
- public var heap : Int;
- public var offset : Int;
- public var result : Float;
- public function new() {
- }
- }
- class DX12Driver extends h3d.impl.Driver {
- static inline var PSIGN_MATID = 0;
- static inline var PSIGN_COLOR_MASK = PSIGN_MATID + 4;
- static inline var PSIGN_UNUSED = PSIGN_COLOR_MASK + 1;
- static inline var PSIGN_STENCIL_MASK = PSIGN_UNUSED + 1;
- static inline var PSIGN_STENCIL_OPS = PSIGN_STENCIL_MASK + 2;
- static inline var PSIGN_RENDER_TARGETS = PSIGN_STENCIL_OPS + 4;
- static inline var PSIGN_LAYOUT = PSIGN_RENDER_TARGETS + 8;
- var pipelineSignature = new hl.Bytes(64);
- var adlerOut = new hl.Bytes(4);
- var driver : DriverInstance;
- var hasDeviceError = false;
- var window : dx.Window;
- var onContextLost : Void -> Void;
- var frames : Array<DxFrame>;
- var frame : DxFrame;
- var fence : Fence;
- var fenceEvent : WaitEvent;
- var renderTargetViews : ManagedHeap;
- var depthStenciViews : ManagedHeap;
- var indirectCommand : CommandSignature;
- var currentFrame : Int;
- var fenceValue : Int64 = 0;
- var needPipelineFlush = false;
- var currentPass : h3d.mat.Pass;
- var currentWidth : Int;
- var currentHeight : Int;
- var currentShader : CompiledShader;
- var compiledShaders : Map<Int,CompiledShader> = new Map();
- var compiler : ShaderCompiler;
- var currentIndex : Buffer;
- var tmp : TempObjects;
- var currentRenderTargets : Array<h3d.mat.Texture> = [];
- var defaultDepth : h3d.mat.Texture;
- var depthEnabled = true;
- var curStencilRef : Int = -1;
- var rtWidth : Int;
- var rtHeight : Int;
- var frameCount : Int;
- var tsFreq : haxe.Int64;
- public static var INITIAL_RT_COUNT = 1024;
- public static var BUFFER_COUNT = 2;
- public static var DEVICE_NAME = null;
- public static var DEBUG = false;
- public function new() {
- window = @:privateAccess dx.Window.windows[0];
- reset();
- }
- override function hasFeature(f:Feature) {
- return switch(f) {
- case Queries, BottomLeftCoords:
- false;
- default:
- true;
- };
- }
- override function isSupportedFormat(fmt:h3d.mat.Data.TextureFormat):Bool {
- return true;
- }
- function reset() {
- var flags = new DriverInitFlags();
- if( DEBUG ) flags.set(DriverInitFlag.DEBUG);
- driver = Driver.create(window, flags, DEVICE_NAME);
- frames = [];
- for(i in 0...BUFFER_COUNT) {
- var f = new DxFrame();
- f.backBuffer = new ResourceData();
- f.allocator = new CommandAllocator(DIRECT);
- f.commandList = new CommandList(DIRECT, f.allocator, null);
- f.commandList.close();
- f.shaderResourceCache = new ManagedHeapArray(CBV_SRV_UAV, 1024);
- f.samplerCache = new ManagedHeapArray(SAMPLER, 1024);
- frames.push(f);
- }
- fence = new Fence(0, NONE);
- fenceEvent = new WaitEvent(false);
- tmp = new TempObjects();
- renderTargetViews = new ManagedHeap(RTV, INITIAL_RT_COUNT);
- depthStenciViews = new ManagedHeap(DSV, INITIAL_RT_COUNT);
- renderTargetViews.onFree = function(prev) frame.toRelease.push(prev);
- depthStenciViews.onFree = function(prev) frame.toRelease.push(prev);
- defaultDepth = new h3d.mat.Texture(0,0, Depth24Stencil8);
- defaultDepth.t = new TextureData();
- defaultDepth.t.state = DEPTH_WRITE;
- defaultDepth.name = "defaultDepth";
- var desc = new CommandSignatureDesc();
- var adesc = hl.CArray.alloc(IndirectArgumentDesc, 1);
- desc.byteStride = 5 * 4;
- desc.numArgumentDescs = 1;
- desc.argumentDescs = adesc;
- adesc[0].type = DRAW_INDEXED;
- indirectCommand = Driver.createCommandSignature(desc,null);
- tsFreq = Driver.getTimestampFrequency();
- compiler = new ShaderCompiler();
- resize(window.width, window.height);
- }
- function beginFrame() {
- frameCount = hxd.Timer.frameCount;
- currentFrame = Driver.getCurrentBackBufferIndex();
- var prevFrame = frame;
- frame = frames[currentFrame];
- defaultDepth.t.res = frame.depthBuffer;
- frame.allocator.reset();
- frame.commandList.reset(frame.allocator, null);
- while( frame.toRelease.length > 0 )
- frame.toRelease.pop().release();
- while( frame.tmpBufToRelease.length > 0 ) {
- var tmpBuf = frame.tmpBufToRelease.pop();
- if ( tmpBuf != null )
- tmpBuf.release();
- }
- if ( prevFrame != null ) {
- while ( prevFrame.tmpBufToNullify.length > 0 ) {
- var t = prevFrame.tmpBufToNullify.pop();
- frame.tmpBufToRelease.push(t.tmpBuf);
- t.tmpBuf = null;
- }
- }
- beginQueries();
- var used = frame.usedBuffers;
- var b = frame.availableBuffers;
- var prev = null;
- while( b != null ) {
- if( b.lastUse < frameCount - 120 ) {
- b.buffer.release();
- b = b.next;
- } else {
- var n = b.next;
- b.next = used;
- used = b;
- b = n;
- }
- }
- frame.availableBuffers = used;
- frame.usedBuffers = null;
- transition(frame.backBuffer, RENDER_TARGET);
- frame.commandList.iaSetPrimitiveTopology(TRIANGLELIST);
- renderTargetViews.next();
- depthStenciViews.next();
- curStencilRef = -1;
- currentIndex = null;
- frame.backBufferView = renderTargetViews.alloc(1);
- Driver.createRenderTargetView(frame.backBuffer.res, null, frame.backBufferView);
- setRenderTarget(null);
- frame.shaderResourceCache.reset();
- frame.samplerCache.reset();
- frame.shaderResourceViews = frame.shaderResourceCache.next();
- frame.samplerViews = frame.samplerCache.next();
- var arr = tmp.descriptors2;
- arr[0] = @:privateAccess frame.shaderResourceViews.heap;
- arr[1] = @:privateAccess frame.samplerViews.heap;
- frame.commandList.setDescriptorHeaps(arr);
- }
- override function clear(?color:Vector4, ?depth:Float, ?stencil:Int) {
- if( color != null ) {
- var clear = tmp.clearColor;
- clear.r = color.r;
- clear.g = color.g;
- clear.b = color.b;
- clear.a = color.a;
- var count = currentRenderTargets.length;
- for( i in 0...count ) {
- var tex = currentRenderTargets[i];
- if( tex != null && tex.t.setClearColor(color) ) {
- // update texture to use another clear value
- var prev = tex.t;
- tex.t = allocTexture(tex);
- @:privateAccess tex.t.clearColorChanges = prev.clearColorChanges;
- frame.toRelease.push(prev.res);
- Driver.createRenderTargetView(tex.t.res, null, tmp.renderTargets[i]);
- }
- frame.commandList.clearRenderTargetView(tmp.renderTargets[i], clear);
- }
- // clear backbuffer
- if( count == 0 )
- frame.commandList.clearRenderTargetView(frame.backBufferView, clear);
- }
- if( depth != null || stencil != null )
- frame.commandList.clearDepthStencilView(tmp.depthStencils[0], depth != null ? (stencil != null ? BOTH : DEPTH) : STENCIL, (depth:Float), stencil);
- }
- function waitGpu() {
- Driver.signal(fence, fenceValue);
- fence.setEvent(fenceValue, fenceEvent);
- fenceEvent.wait(-1);
- fenceValue++;
- }
- override function resize(width:Int, height:Int) {
- if( currentWidth == width && currentHeight == height )
- return;
- currentWidth = rtWidth = width;
- currentHeight = rtHeight = height;
- @:privateAccess defaultDepth.width = width;
- @:privateAccess defaultDepth.height = height;
- if( frame != null )
- flushFrame(true);
- waitGpu();
- for( f in frames ) {
- if( f.backBuffer.res != null )
- f.backBuffer.res.release();
- if( f.depthBuffer != null )
- f.depthBuffer.release();
- }
- Driver.resize(width, height, BUFFER_COUNT, R8G8B8A8_UNORM);
- renderTargetViews.clear();
- depthStenciViews.clear();
- for( i => f in frames ) {
- f.backBuffer.res = Driver.getBackBuffer(i);
- f.backBuffer.res.setName("Backbuffer#"+i);
- f.backBuffer.state = PRESENT;
- var desc = new ResourceDesc();
- var flags = new haxe.EnumFlags();
- desc.dimension = TEXTURE2D;
- desc.width = width;
- desc.height = height;
- desc.depthOrArraySize = 1;
- desc.mipLevels = 1;
- desc.sampleDesc.count = 1;
- desc.format = D24_UNORM_S8_UINT;
- desc.flags.set(ALLOW_DEPTH_STENCIL);
- tmp.heap.type = DEFAULT;
- tmp.clearValue.format = desc.format;
- tmp.clearValue.depth = 1;
- tmp.clearValue.stencil= 0;
- f.depthBuffer = Driver.createCommittedResource(tmp.heap, flags, desc, DEPTH_WRITE, tmp.clearValue);
- f.depthBuffer.setName("Depthbuffer#"+i);
- }
- beginFrame();
- }
- override function begin(frame:Int) {
- }
- override function isDisposed() {
- return hasDeviceError;
- }
- override function init( onCreate : Bool -> Void, forceSoftware = false ) {
- onContextLost = onCreate.bind(true);
- haxe.Timer.delay(onCreate.bind(false), 1);
- }
- override function getDriverName(details:Bool) {
- var desc = "DX12";
- if( details ) desc += " "+Driver.getDeviceName();
- return desc;
- }
- public function forceDeviceError() {
- hasDeviceError = true;
- }
- function transition( res : ResourceData, to : ResourceState ) {
- if( res.state == to )
- return;
- var b = tmp.barrier;
- b.resource = res.res;
- b.stateBefore = res.state;
- b.stateAfter = to;
- frame.commandList.resourceBarrier(b);
- res.state = to;
- }
- function getRTBits( tex : h3d.mat.Texture ) {
- inline function mk(channels,format) {
- return ((channels - 1) << 2) | (format + 1);
- }
- return switch( tex.format ) {
- case RGBA: mk(4,0);
- case R8: mk(1, 0);
- case RG8: mk(2, 0);
- case RGB8: mk(3, 0);
- case R16F: mk(1,1);
- case RG16F: mk(2,1);
- case RGB16F: mk(3,1);
- case RGBA16F: mk(4,1);
- case R32F: mk(1,2);
- case RG32F: mk(2,2);
- case RGB32F: mk(3,2);
- case RGBA32F: mk(4,2);
- default: throw "Unsupported RT format "+tex.format;
- }
- }
- function getDepthViewFromTexture( tex : h3d.mat.Texture, readOnly : Bool ) {
- if ( tex != null && tex.depthBuffer == null ) {
- depthEnabled = false;
- return null;
- }
- if ( tex != null ) {
- var w = tex.depthBuffer.width;
- var h = tex.depthBuffer.height;
- if( w != tex.width || h != tex.height )
- throw "Depth size mismatch";
- }
- return getDepthView(tex == null ? null : tex.depthBuffer, readOnly);
- }
- function getDepthView( depthBuffer : h3d.mat.Texture, readOnly : Bool ) {
- var res = depthBuffer == null ? frame.depthBuffer : depthBuffer.t.res;
- var depthView = depthStenciViews.alloc(1);
- var viewDesc = new DepthStencilViewDesc();
- viewDesc.arraySize = 1;
- viewDesc.mipSlice = 0;
- viewDesc.firstArraySlice = 0;
- viewDesc.format = D24_UNORM_S8_UINT;
- viewDesc.viewDimension = TEXTURE2D;
- if ( readOnly ) {
- viewDesc.flags.set(READ_ONLY_DEPTH);
- viewDesc.flags.set(READ_ONLY_STENCIL);
- }
- Driver.createDepthStencilView(res, viewDesc, depthView);
- var depths = tmp.depthStencils;
- depths[0] = depthView;
- depthEnabled = true;
- if ( depthBuffer != null )
- transition(depthBuffer.t, readOnly ? DEPTH_READ : DEPTH_WRITE);
- return depths;
- }
- override function getDefaultDepthBuffer():h3d.mat.Texture {
- return defaultDepth;
- }
- function initViewport(w,h) {
- rtWidth = w;
- rtHeight = h;
- tmp.viewport.width = w;
- tmp.viewport.height = h;
- tmp.viewport.maxDepth = 1;
- tmp.rect.top = 0;
- tmp.rect.left = 0;
- tmp.rect.right = w;
- tmp.rect.bottom = h;
- frame.commandList.rsSetScissorRects(1, tmp.rect);
- frame.commandList.rsSetViewports(1, tmp.viewport);
- }
- override function setRenderTarget(tex:Null<h3d.mat.Texture>, layer:Int = 0, mipLevel:Int = 0, depthBinding : h3d.Engine.DepthBinding = ReadWrite) {
- if( tex != null ) {
- if( tex.t == null ) tex.alloc();
- transition(tex.t, RENDER_TARGET);
- }
- depthEnabled = depthBinding != NotBound;
- var isArr = tex != null && (tex.flags.has(IsArray) || tex.flags.has(Cube));
- var desc = null;
- if( layer != 0 || mipLevel != 0 || isArr ) {
- desc = tmp.rtvDesc;
- desc.format = tex.t.format;
- if( isArr ) {
- desc.viewDimension = TEXTURE2DARRAY;
- desc.mipSlice = mipLevel;
- desc.firstArraySlice = layer;
- desc.arraySize = 1;
- desc.planeSlice = 0;
- } else {
- desc.viewDimension = TEXTURE2D;
- desc.mipSlice = mipLevel;
- desc.planeSlice = 0;
- }
- }
- if (tex != null) {
- var texView = renderTargetViews.alloc(1);
- Driver.createRenderTargetView(tex.t.res, desc, texView);
- tmp.renderTargets[0] = texView;
- }
- else {
- tmp.renderTargets[0] = frame.backBufferView;
- }
- if ( tex != null && !tex.flags.has(WasCleared) ) {
- tex.flags.set(WasCleared);
- var clear = tmp.clearColor;
- clear.r = 0;
- clear.g = 0;
- clear.b = 0;
- clear.a = 0;
- frame.commandList.clearRenderTargetView(tmp.renderTargets[0], clear);
- }
- frame.commandList.omSetRenderTargets(1, tmp.renderTargets, true, depthEnabled ? getDepthViewFromTexture(tex, depthBinding == ReadOnly ) : null);
- while( currentRenderTargets.length > 0 ) currentRenderTargets.pop();
- if( tex != null ) currentRenderTargets.push(tex);
- var w = tex == null ? currentWidth : tex.width >> mipLevel;
- var h = tex == null ? currentHeight : tex.height >> mipLevel;
- if( w == 0 ) w = 1;
- if( h == 0 ) h = 1;
- initViewport(w, h);
- pipelineSignature.setI32(PSIGN_RENDER_TARGETS, tex == null ? 0 : getRTBits(tex) | (depthEnabled ? 0x80000000 : 0));
- needPipelineFlush = true;
- }
- override function setRenderTargets(textures:Array<h3d.mat.Texture>, depthBinding : h3d.Engine.DepthBinding = ReadWrite) {
- while( currentRenderTargets.length > textures.length )
- currentRenderTargets.pop();
- depthEnabled = depthBinding != NotBound;
- var t0 = textures[0];
- var texViews = renderTargetViews.alloc(textures.length);
- var bits = 0;
- for( i => t in textures ) {
- if ( t.t == null ) {
- t.alloc();
- if ( hasDeviceError ) return;
- }
- var view = texViews.offset(renderTargetViews.stride * i);
- Driver.createRenderTargetView(t.t.res, null, view);
- tmp.renderTargets[i] = view;
- currentRenderTargets[i] = t;
- bits |= getRTBits(t) << (i << 2);
- if ( !t.flags.has(WasCleared) ) {
- t.flags.set(WasCleared);
- var clear = tmp.clearColor;
- clear.r = 0;
- clear.g = 0;
- clear.b = 0;
- clear.a = 0;
- frame.commandList.clearRenderTargetView(tmp.renderTargets[i], clear);
- }
- transition(t.t, RENDER_TARGET);
- }
- frame.commandList.omSetRenderTargets(textures.length, tmp.renderTargets, true, depthEnabled ? getDepthViewFromTexture(t0, depthBinding == ReadOnly) : null);
- initViewport(t0.width, t0.height);
- pipelineSignature.setI32(PSIGN_RENDER_TARGETS, bits | (depthEnabled ? 0x80000000 : 0));
- needPipelineFlush = true;
- }
- override function setDepth(depthBuffer : h3d.mat.Texture) {
- var view = getDepthView(depthBuffer, false);
- depthEnabled = true;
- frame.commandList.omSetRenderTargets(0, null, true, view);
- while( currentRenderTargets.length > 0 ) currentRenderTargets.pop();
- initViewport(depthBuffer.width, depthBuffer.height);
- pipelineSignature.setI32(PSIGN_RENDER_TARGETS, 0x80000000);
- needPipelineFlush = true;
- }
- override function setRenderZone(x:Int, y:Int, width:Int, height:Int) {
- if( width < 0 && height < 0 && x == 0 && y == 0 ) {
- tmp.rect.left = 0;
- tmp.rect.top = 0;
- tmp.rect.right = rtWidth;
- tmp.rect.bottom = rtHeight;
- frame.commandList.rsSetScissorRects(1, tmp.rect);
- } else {
- tmp.rect.left = x;
- tmp.rect.top = y;
- tmp.rect.right = x + width;
- tmp.rect.bottom = y + height;
- frame.commandList.rsSetScissorRects(1, tmp.rect);
- }
- }
- override function captureRenderBuffer( pixels : hxd.Pixels ) {
- var rt = currentRenderTargets[0];
- if( rt == null )
- throw "Can't capture main render buffer in DirectX";
- captureTexPixels(pixels, rt, 0, 0);
- }
- override function capturePixels(tex:h3d.mat.Texture, layer:Int, mipLevel:Int, ?region:h2d.col.IBounds):hxd.Pixels {
- var pixels : hxd.Pixels;
- if (region != null) {
- if (region.xMax > tex.width) region.xMax = tex.width;
- if (region.yMax > tex.height) region.yMax = tex.height;
- if (region.xMin < 0) region.xMin = 0;
- if (region.yMin < 0) region.yMin = 0;
- var w = region.width >> mipLevel;
- var h = region.height >> mipLevel;
- if( w == 0 ) w = 1;
- if( h == 0 ) h = 1;
- pixels = hxd.Pixels.alloc(w, h, tex.format);
- captureTexPixels(pixels, tex, layer, mipLevel, region.xMin, region.yMin);
- } else {
- var w = tex.width >> mipLevel;
- var h = tex.height >> mipLevel;
- if( w == 0 ) w = 1;
- if( h == 0 ) h = 1;
- pixels = hxd.Pixels.alloc(w, h, tex.format);
- captureTexPixels(pixels, tex, layer, mipLevel);
- }
- return pixels;
- }
- function captureTexPixels( pixels: hxd.Pixels, tex:h3d.mat.Texture, layer:Int, mipLevel:Int, x : Int = 0, y : Int = 0) {
- if( pixels.width == 0 || pixels.height == 0 )
- return;
- var totalSize : hl.BytesAccess<Int64> = new hl.Bytes(8);
- var src = new TextureCopyLocation();
- src.res = tex.t.res;
- src.subResourceIndex = mipLevel + layer * tex.mipLevels;
- var srcDesc = makeTextureDesc(tex);
- var dst = new TextureCopyLocation();
- dst.type = PLACED_FOOTPRINT;
- Driver.getCopyableFootprints(srcDesc, src.subResourceIndex, 1, 0, dst.placedFootprint, null, null, totalSize);
- var desc = new ResourceDesc();
- var flags = new haxe.EnumFlags();
- desc.dimension = BUFFER;
- desc.width = totalSize[0];
- desc.height = 1;
- desc.depthOrArraySize = 1;
- desc.mipLevels = 1;
- desc.sampleDesc.count = 1;
- desc.layout = ROW_MAJOR;
- tmp.heap.type = READBACK;
- var tmpBuf = Driver.createCommittedResource(tmp.heap, flags, desc, COPY_DEST, null);
- var box = new Box();
- box.left = x;
- box.right = pixels.width;
- box.top = y;
- box.bottom = pixels.height;
- box.back = 1;
- transition(tex.t, COPY_SOURCE);
- dst.res = tmpBuf;
- frame.commandList.copyTextureRegion(dst, 0, 0, 0, src, box);
- flushFrame();
- waitGpu();
- var output = tmpBuf.map(0, null);
- var stride = hxd.Pixels.calcStride(pixels.width, tex.format);
- var rowStride = dst.placedFootprint.footprint.rowPitch;
- if( rowStride == stride )
- (pixels.bytes:hl.Bytes).blit(pixels.offset, output, 0, stride * pixels.height);
- else {
- for( i in 0...pixels.height )
- (pixels.bytes:hl.Bytes).blit(pixels.offset + i * stride, output, i * rowStride, stride);
- }
- tmpBuf.unmap(0,null);
- tmpBuf.release();
- beginFrame();
- }
- // ---- SHADERS -----
- static var VERTEX_FORMATS = [null,null,R32G32_FLOAT,R32G32B32_FLOAT,R32G32B32A32_FLOAT];
- function getBinaryPayload( code : String ) {
- var bin = code.indexOf("//BIN=");
- if( bin >= 0 ) {
- var end = code.indexOf("#", bin);
- if( end >= 0 )
- return haxe.crypto.Base64.decode(code.substr(bin + 6, end - bin - 6));
- }
- if( shaderCache != null )
- return shaderCache.resolveShaderBinary(code);
- return null;
- }
- function compileSource( sh : hxsl.RuntimeShader.RuntimeShaderData, profile, baseRegister, rootStr = "" ) {
- var args = [];
- var out = new hxsl.HlslOut();
- out.baseRegister = baseRegister;
- if ( sh.code == null ) {
- sh.code = out.run(sh.data);
- sh.code = rootStr + sh.code;
- }
- var bytes = getBinaryPayload(sh.code);
- if ( bytes == null ) {
- return compiler.compile(sh.code, profile, args);
- }
- return bytes;
- }
- override function getNativeShaderCode( shader : hxsl.RuntimeShader ) {
- var out = new hxsl.HlslOut();
- var vsSource = out.run(shader.vertex.data);
- var out = new hxsl.HlslOut();
- var psSource = out.run(shader.fragment.data);
- return vsSource+"\n\n\n\n"+psSource;
- }
- function stringifyRootSignature( sign : RootSignatureDesc, name : String, params : hl.CArray<RootParameterConstants>, paramsCount : Int ) : String {
- var s = '#define ${name} "RootFlags(';
- if ( sign.flags.toInt() == 0 )
- s += '0'; // no flags
- else {
- // RootFlags
- for ( f in haxe.EnumTools.getConstructors(RootSignatureFlag) ) {
- if ( !sign.flags.has(haxe.EnumTools.createByName(RootSignatureFlag, f)) )
- continue;
- s += Std.string(f) + '|';
- }
- s = s.substr(0, s.length - 1);
- }
- s += ')",';
- for ( i in 0...paramsCount ) {
- var param = params[i];
- var vis = 'SHADER_VISIBILITY_${param.shaderVisibility == VERTEX ? "VERTEX" : "PIXEL"}';
- if ( param.parameterType == CONSTANTS ) {
- var shaderRegister = param.shaderRegister;
- s += 'RootConstants(num32BitConstants=${param.num32BitValues},b${shaderRegister}, visibility=${vis}),';
- } else {
- try {
- var p = unsafeCastTo(param, RootParameterDescriptorTable);
- if( p == null || p.descriptorRanges == null ) continue;
- var descRange = p.descriptorRanges[0];
- var baseShaderRegister = descRange.baseShaderRegister;
- switch ( descRange.rangeType) {
- case CBV:
- s += 'DescriptorTable(CBV(b${baseShaderRegister}), visibility = ${vis}),';
- case SRV:
- s += 'DescriptorTable(SRV(t${baseShaderRegister},numDescriptors = ${descRange.numDescriptors}), visibility = ${vis}),';
- case SAMPLER:
- var baseShaderRegister = descRange.baseShaderRegister;
- s += 'DescriptorTable(Sampler(s${baseShaderRegister}, space=${descRange.registerSpace}, numDescriptors = ${descRange.numDescriptors}), visibility = ${vis}),';
- case UAV:
- throw "Not supported";
- }
- } catch ( e : Dynamic ) {
- continue;
- }
- }
- }
- s += '\n';
- return s;
- }
- inline function unsafeCastTo<T,R>( v : T, c : Class<R> ) : R {
- var arr = new hl.NativeArray<T>(1);
- arr[0] = v;
- return (cast arr : hl.NativeArray<R>)[0];
- }
- function computeRootSignature( shader : hxsl.RuntimeShader ) {
- var allocatedParams = 16;
- var params = hl.CArray.alloc(RootParameterConstants,allocatedParams);
- var paramsCount = 0, regCount = 0;
- var texDescs = [];
- var vertexParamsCBV = false;
- var fragmentParamsCBV = false;
- function allocDescTable(vis) {
- var p = unsafeCastTo(params[paramsCount++], RootParameterDescriptorTable);
- p.parameterType = DESCRIPTOR_TABLE;
- p.numDescriptorRanges = 1;
- var rangeArr = hl.CArray.alloc(DescriptorRange,1);
- var range = rangeArr[0];
- texDescs.push(range);
- p.descriptorRanges = rangeArr;
- p.shaderVisibility = vis;
- return range;
- }
- function allocConsts(size,vis,useCBV) {
- var reg = regCount++;
- if( size == 0 ) return -1;
- if( useCBV ) {
- var pid = paramsCount;
- var r = allocDescTable(vis);
- r.rangeType = CBV;
- r.numDescriptors = 1;
- r.baseShaderRegister = reg;
- r.registerSpace = 0;
- return pid | 0x100;
- }
- var pid = paramsCount++;
- var p = params[pid];
- p.parameterType = CONSTANTS;
- p.shaderRegister = reg;
- p.shaderVisibility = vis;
- p.num32BitValues = size << 2;
- return pid;
- }
- function allocParams( sh : hxsl.RuntimeShader.RuntimeShaderData ) {
- var vis = sh.kind == Vertex ? VERTEX : PIXEL;
- var regs = new ShaderRegisters();
- regs.globals = allocConsts(sh.globalsSize, vis, false);
- regs.params = allocConsts(sh.paramsSize, vis, sh.kind == Vertex ? vertexParamsCBV : fragmentParamsCBV);
- if( sh.bufferCount > 0 ) {
- regs.buffers = paramsCount;
- for( i in 0...sh.bufferCount )
- allocConsts(1, vis, true);
- }
- if( sh.texturesCount > 0 ) {
- regs.texturesCount = sh.texturesCount;
- regs.textures = paramsCount;
- var p = sh.textures;
- while( p != null ) {
- switch( p.type ) {
- case TArray( TSampler2D , SConst(n) ): regs.textures2DCount = n;
- default:
- }
- p = p.next;
- }
- var r = allocDescTable(vis);
- r.rangeType = SRV;
- r.baseShaderRegister = 0;
- r.registerSpace = 0;
- r.numDescriptors = sh.texturesCount;
- regs.samplers = paramsCount;
- var r = allocDescTable(vis);
- r.rangeType = SAMPLER;
- r.baseShaderRegister = 0;
- r.registerSpace = 0;
- r.numDescriptors = sh.texturesCount;
- }
- return regs;
- }
- // Costs in units:
- // Descriptor Tables cost 1 each
- // Root CBVs cost 2 each
- // Root SRVs cost 2 each
- // Root UAVs cost 2 each
- // Root Constants cost 1 per 32-bit value
- function calcSize( sh : hxsl.RuntimeShader.RuntimeShaderData ) {
- var s = (sh.globalsSize + sh.paramsSize) << 2;
- s += sh.texturesCount;
- return s;
- }
- var totalVertex = calcSize(shader.vertex);
- var totalFragment = calcSize(shader.fragment);
- var total = totalVertex + totalFragment;
- if( total > 64 ) {
- var vertexParamSizeCost = (shader.vertex.paramsSize << 2);
- var fragmentParamSizeCost = (shader.fragment.paramsSize << 2);
- // Remove the size cost of the root constant and add one descriptor table.
- var withoutVP = total - vertexParamSizeCost + 1;
- var withoutFP = total - fragmentParamSizeCost + 1;
- if( withoutVP < 64 || withoutFP > 64 ) {
- vertexParamsCBV = true;
- total = withoutVP;
- }
- if( total > 64 ) {
- fragmentParamsCBV = true;
- total = total - fragmentParamSizeCost + 1;
- }
- if( total > 64 )
- throw "Too many globals";
- }
- var vertexRegisters = allocParams(shader.vertex);
- var fragmentRegStart = regCount;
- var fragmentRegisters = allocParams(shader.fragment);
- if( paramsCount > allocatedParams )
- throw "ASSERT : Too many parameters";
- var sign = new RootSignatureDesc();
- sign.flags.set(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
- sign.flags.set(DENY_HULL_SHADER_ROOT_ACCESS);
- sign.flags.set(DENY_DOMAIN_SHADER_ROOT_ACCESS);
- sign.flags.set(DENY_GEOMETRY_SHADER_ROOT_ACCESS);
- sign.numParameters = paramsCount;
- sign.parameters = cast params;
- return { sign : sign, fragmentRegStart : fragmentRegStart, vertexRegisters : vertexRegisters, fragmentRegisters : fragmentRegisters, params : params, paramsCount : paramsCount, texDescs : texDescs };
- }
- function compileShader( shader : hxsl.RuntimeShader ) : CompiledShader {
- var res = computeRootSignature(shader);
- var c = new CompiledShader();
- c.vertexRegisters = res.vertexRegisters;
- c.fragmentRegisters = res.fragmentRegisters;
- var rootStr = stringifyRootSignature(res.sign, "ROOT_SIGNATURE", res.params, res.paramsCount);
- var vs = compileSource(shader.vertex, "vs_6_0", 0, rootStr);
- var ps = compileSource(shader.fragment, "ps_6_0", res.fragmentRegStart, rootStr);
- var signSize = 0;
- var signBytes = Driver.serializeRootSignature(res.sign, 1, signSize);
- var sign = new RootSignature(signBytes,signSize);
- var inputs = [];
- for( v in shader.vertex.data.vars )
- switch( v.kind ) {
- case Input: inputs.push(v);
- default:
- }
- var inputLayout = hl.CArray.alloc(InputElementDesc, inputs.length);
- var format : Array<hxd.BufferFormat.BufferInput> = [];
- for( i => v in inputs ) {
- var d = inputLayout[i];
- var perInst = 0;
- if( v.qualifiers != null )
- for( q in v.qualifiers )
- switch( q ) {
- case PerInstance(k): perInst = k;
- default:
- }
- d.semanticName = @:privateAccess hxsl.HlslOut.semanticName(v.name).toUtf8();
- d.inputSlot = i;
- format.push({ name : v.name, type : hxd.BufferFormat.InputFormat.fromHXSL(v.type) });
- if( perInst > 0 ) {
- d.inputSlotClass = PER_INSTANCE_DATA;
- d.instanceDataStepRate = perInst;
- } else
- d.inputSlotClass = PER_VERTEX_DATA;
- }
- var p = new GraphicsPipelineStateDesc();
- p.rootSignature = sign;
- p.vs.bytecodeLength = vs.length;
- p.vs.shaderBytecode = vs;
- p.ps.bytecodeLength = ps.length;
- p.ps.shaderBytecode = ps;
- p.rasterizerState.fillMode = SOLID;
- p.rasterizerState.cullMode = NONE;
- p.primitiveTopologyType = TRIANGLE;
- p.numRenderTargets = 1;
- p.rtvFormats[0] = R8G8B8A8_UNORM;
- p.dsvFormat = UNKNOWN;
- p.sampleDesc.count = 1;
- p.sampleMask = -1;
- p.inputLayout.inputElementDescs = inputLayout;
- p.inputLayout.numElements = inputs.length;
- //Driver.createGraphicsPipelineState(p);
- c.format = hxd.BufferFormat.make(format);
- c.pipeline = p;
- c.rootSignature = sign;
- c.inputLayout = inputLayout;
- c.inputCount = inputs.length;
- c.shader = shader;
- for( i in 0...inputs.length )
- inputLayout[i].alignedByteOffset = 1; // will trigger error if not set in makePipeline()
- return c;
- }
- function disposeResource( r : ResourceData ) {
- frame.toRelease.push(r.res);
- r.res = null;
- r.state = PRESENT;
- }
- // ----- BUFFERS
- function allocGPU( size : Int, heapType, state ) {
- var desc = new ResourceDesc();
- var flags = new haxe.EnumFlags();
- desc.dimension = BUFFER;
- desc.width = size;
- desc.height = 1;
- desc.depthOrArraySize = 1;
- desc.mipLevels = 1;
- desc.sampleDesc.count = 1;
- desc.layout = ROW_MAJOR;
- tmp.heap.type = heapType;
- return Driver.createCommittedResource(tmp.heap, flags, desc, state, null);
- }
- override function allocBuffer( m : h3d.Buffer ) : GPUBuffer {
- var buf = new VertexBufferData();
- var size = m.getMemSize();
- var bufSize = m.flags.has(UniformBuffer) ? calcCBVSize(size) : size;
- buf.state = COPY_DEST;
- buf.res = allocGPU(bufSize, DEFAULT, COMMON);
- if( m.flags.has(UniformBuffer) ) {
- // no view
- } else if( m.flags.has(IndexBuffer) ) {
- var view = new IndexBufferView();
- view.bufferLocation = buf.res.getGpuVirtualAddress();
- view.format = m.format.strideBytes == 4 ? R32_UINT : R16_UINT;
- view.sizeInBytes = size;
- buf.iview = view;
- } else {
- var view = new VertexBufferView();
- view.bufferLocation = buf.res.getGpuVirtualAddress();
- view.sizeInBytes = size;
- view.strideInBytes = m.format.strideBytes;
- buf.view = view;
- }
- buf.size = bufSize;
- buf.uploaded = m.flags.has(Dynamic);
- return buf;
- }
- override function allocInstanceBuffer(b:InstanceBuffer, bytes:haxe.io.Bytes) {
- var dataSize = b.commandCount * 5 * 4;
- var buf = allocGPU(dataSize, DEFAULT, COMMON);
- var tmpBuf = allocDynamicBuffer(bytes, dataSize);
- frame.commandList.copyBufferRegion(buf, 0, tmpBuf, 0, dataSize);
- b.data = buf;
- var b = tmp.barrier;
- b.resource = buf;
- b.stateBefore = COPY_DEST;
- b.stateAfter = NON_PIXEL_SHADER_RESOURCE;
- frame.commandList.resourceBarrier(b);
- }
- override function disposeBuffer(v:Buffer) {
- disposeResource(v.vbuf);
- }
- override function disposeInstanceBuffer(b:InstanceBuffer) {
- frame.toRelease.push((b.data:GpuResource));
- // disposeResource(b.data);
- b.data = null;
- }
- function updateBuffer( b : BufferData, bytes : hl.Bytes, startByte : Int, bytesCount : Int ) {
- var tmpBuf;
- if( b.uploaded )
- tmpBuf = allocDynamicBuffer(bytes.offset(startByte), bytesCount);
- else {
- var size = calcCBVSize(bytesCount);
- tmpBuf = allocGPU(size, UPLOAD, GENERIC_READ);
- var ptr = tmpBuf.map(0, null);
- ptr.blit(0, bytes, 0, bytesCount);
- tmpBuf.unmap(0,null);
- }
- frame.commandList.copyBufferRegion(b.res, startByte, tmpBuf, 0, bytesCount);
- if( !b.uploaded ) {
- frame.toRelease.push(tmpBuf);
- b.uploaded = true;
- }
- }
- override function uploadIndexData(i:Buffer, startIndice:Int, indiceCount:Int, buf:hxd.IndexBuffer, bufPos:Int) {
- var bits = i.format.strideBytes >> 1;
- transition(i.vbuf, COPY_DEST);
- updateBuffer(i.vbuf, hl.Bytes.getArray(buf.getNative()).offset(bufPos << bits), startIndice << bits, indiceCount << bits);
- transition(i.vbuf, INDEX_BUFFER);
- }
- override function uploadBufferData(b:Buffer, startVertex:Int, vertexCount:Int, buf:hxd.FloatBuffer, bufPos:Int) {
- var data = hl.Bytes.getArray(buf.getNative()).offset(bufPos<<2);
- transition(b.vbuf, COPY_DEST);
- updateBuffer(b.vbuf, data, startVertex * b.format.strideBytes, vertexCount * b.format.strideBytes);
- transition(b.vbuf, b.flags.has(IndexBuffer) ? INDEX_BUFFER : VERTEX_AND_CONSTANT_BUFFER);
- }
- override function uploadBufferBytes(b:Buffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
- transition(b.vbuf, COPY_DEST);
- updateBuffer(b.vbuf, @:privateAccess buf.b.offset(bufPos), startVertex * b.format.strideBytes, vertexCount * b.format.strideBytes);
- transition(b.vbuf, b.flags.has(IndexBuffer) ? INDEX_BUFFER : VERTEX_AND_CONSTANT_BUFFER);
- }
- // ------------ TEXTURES -------
- function getTextureFormat( t : h3d.mat.Texture ) : DxgiFormat {
- return switch( t.format ) {
- case RGBA: R8G8B8A8_UNORM;
- case RGBA16F: R16G16B16A16_FLOAT;
- case RGBA32F: R32G32B32A32_FLOAT;
- case R32F: R32_FLOAT;
- case R16F: R16_FLOAT;
- case R8: R8_UNORM;
- case RG8: R8G8_UNORM;
- case RG16F: R16G16_FLOAT;
- case RG32F: R32G32_FLOAT;
- case RGB32F: R32G32B32_FLOAT;
- case RGB10A2: R10G10B10A2_UNORM;
- case RG11B10UF: R11G11B10_FLOAT;
- case SRGB_ALPHA: R8G8B8A8_UNORM_SRGB;
- case R16U: R16_UNORM;
- case RG16U: R16G16_UNORM;
- case RGBA16U: R16G16B16A16_UNORM;
- case S3TC(n):
- switch( n ) {
- case 1: BC1_UNORM;
- case 2: BC2_UNORM;
- case 3: BC3_UNORM;
- case 4: BC4_UNORM;
- case 5: BC5_UNORM;
- case 6: BC6H_UF16;
- case 7: BC7_UNORM;
- default: throw "assert";
- }
- default: throw "Unsupported texture format " + t.format;
- }
- }
- function makeTextureDesc(t:h3d.mat.Texture) {
- var desc = new ResourceDesc();
- desc.dimension = TEXTURE2D;
- desc.width = t.width;
- desc.height = t.height;
- desc.depthOrArraySize = t.layerCount;
- desc.mipLevels = t.mipLevels;
- desc.sampleDesc.count = 1;
- desc.format = getTextureFormat(t);
- return desc;
- }
- override function allocTexture(t:h3d.mat.Texture):Texture {
- if( t.format.match(S3TC(_)) && (t.width & 3 != 0 || t.height & 3 != 0) )
- throw t+" is compressed "+t.width+"x"+t.height+" but should be a 4x4 multiple";
- var isRT = t.flags.has(Target);
- var flags = new haxe.EnumFlags();
- var desc = makeTextureDesc(t);
- var td = new TextureData();
- td.format = desc.format;
- tmp.heap.type = DEFAULT;
- var clear = null;
- if( isRT ) {
- var color = t.t == null || t.t.color == null ? new h3d.Vector4(0,0,0,0) : t.t.color; // reuse prev color
- desc.flags.set(ALLOW_RENDER_TARGET);
- clear = tmp.clearValue;
- clear.format = desc.format;
- clear.color.r = color.r;
- clear.color.g = color.g;
- clear.color.b = color.b;
- clear.color.a = color.a;
- td.color = color;
- }
- td.state = isRT ? RENDER_TARGET : COPY_DEST;
- td.res = Driver.createCommittedResource(tmp.heap, flags, desc, isRT ? RENDER_TARGET : COMMON, clear);
- td.res.setName(t.name == null ? "Texture#"+t.id : t.name);
- t.lastFrame = frameCount;
- t.flags.unset(WasCleared);
- return td;
- }
- override function allocDepthBuffer(b:h3d.mat.Texture):Texture {
- var td = new TextureData();
- var desc = new ResourceDesc();
- var flags = new haxe.EnumFlags();
- desc.dimension = TEXTURE2D;
- desc.width = b.width;
- desc.height = b.height;
- desc.depthOrArraySize = 1;
- desc.mipLevels = 1;
- desc.sampleDesc.count = 1;
- desc.format = R24G8_TYPELESS;
- desc.flags.set(ALLOW_DEPTH_STENCIL);
- tmp.heap.type = DEFAULT;
- tmp.clearValue.format = D24_UNORM_S8_UINT;
- tmp.clearValue.depth = 1;
- tmp.clearValue.stencil= 0;
- td.state = DEPTH_WRITE;
- td.res = Driver.createCommittedResource(tmp.heap, flags, desc, DEPTH_WRITE, tmp.clearValue);
- return td;
- }
- override function disposeTexture(t:h3d.mat.Texture) {
- disposeResource(t.t);
- t.t = null;
- }
- override function disposeDepthBuffer(t:h3d.mat.Texture) {
- disposeResource(t.t);
- t.t = null;
- }
- override function uploadTextureBitmap(t:h3d.mat.Texture, bmp:hxd.BitmapData, mipLevel:Int, side:Int) {
- var pixels = bmp.getPixels();
- uploadTexturePixels(t, pixels, mipLevel, side);
- pixels.dispose();
- }
- override function uploadTexturePixels(t:h3d.mat.Texture, pixels:hxd.Pixels, mipLevel:Int, side:Int) {
- pixels.convert(t.format);
- if( mipLevel >= t.mipLevels ) throw "Mip level outside texture range : " + mipLevel + " (max = " + (t.mipLevels - 1) + ")";
- tmp.heap.type = UPLOAD;
- var subRes = mipLevel + side * t.mipLevels;
- var nbRes = t.mipLevels * t.layerCount;
- // Todo : optimize for video, currently allocating a new tmpBuf every frame.
- if ( t.t.tmpBuf == null ) {
- var tmpSize = t.t.res.getRequiredIntermediateSize(0, nbRes).low;
- t.t.tmpBuf = allocGPU(tmpSize, UPLOAD, GENERIC_READ);
- }
- var previousSize : hl.BytesAccess<Int64> = new hl.Bytes(8);
- Driver.getCopyableFootprints(makeTextureDesc(t), 0, subRes, 0, null, null, null, previousSize);
- var offsetAligned = ((previousSize[0] + 512 - 1) / 512) * 512;
- var upd = new SubResourceData();
- var stride = @:privateAccess pixels.stride;
- switch( t.format ) {
- case S3TC(n): stride = pixels.width * ((n == 1 || n == 4) ? 2 : 4); // "uncompressed" stride ?
- default:
- }
- upd.data = (pixels.bytes:hl.Bytes).offset(pixels.offset);
- upd.rowPitch = stride;
- upd.slicePitch = pixels.dataSize;
- transition(t.t, COPY_DEST);
- if( !Driver.updateSubResource(frame.commandList, t.t.res, t.t.tmpBuf, offsetAligned, subRes, 1, upd) )
- throw "Failed to update sub resource";
- transition(t.t, PIXEL_SHADER_RESOURCE);
- frame.tmpBufToNullify.push(t.t);
- t.flags.set(WasCleared);
- }
- override function copyTexture(from:h3d.mat.Texture, to:h3d.mat.Texture):Bool {
- if( from.t == null || from.format != to.format || from.width != to.width || from.height != to.height || from.layerCount != to.layerCount )
- return false;
- if( to.t == null ) {
- var prev = from.lastFrame;
- from.preventAutoDispose();
- to.alloc();
- from.lastFrame = prev;
- if( from.t == null ) throw "assert";
- if( to.t == null ) return false;
- }
- transition(from.t, COPY_SOURCE);
- transition(to.t, COPY_DEST);
- var dst = new TextureCopyLocation();
- var src = new TextureCopyLocation();
- dst.res = to.t.res;
- src.res = from.t.res;
- frame.commandList.copyTextureRegion(dst, 0, 0, 0, src, null);
- to.flags.set(WasCleared);
- for( t in currentRenderTargets )
- if( t == to || t == from ) {
- transition(t.t, RENDER_TARGET);
- break;
- }
- return true;
- }
- // ----- PIPELINE UPDATE
- override function uploadShaderBuffers(buffers:h3d.shader.Buffers, which:h3d.shader.Buffers.BufferKind) {
- uploadBuffers(buffers, buffers.vertex, which, currentShader.shader.vertex, currentShader.vertexRegisters);
- uploadBuffers(buffers, buffers.fragment, which, currentShader.shader.fragment, currentShader.fragmentRegisters);
- }
- function calcCBVSize( dataSize : Int ) {
- // the view must be a mult of 256
- var sz = dataSize & ~0xFF;
- if( sz != dataSize ) sz += 0x100;
- return sz;
- }
- function allocDynamicBuffer( data : hl.Bytes, dataSize : Int ) {
- var b = frame.availableBuffers, prev = null;
- var tmpBuf = null;
- var size = calcCBVSize(dataSize);
- if ( size == 0 ) size = 1;
- while( b != null ) {
- if( b.size >= size && b.size < size << 1 ) {
- tmpBuf = b.buffer;
- if( prev == null )
- frame.availableBuffers = b.next;
- else
- prev.next = b.next;
- b.lastUse = frameCount;
- b.next = frame.usedBuffers;
- frame.usedBuffers = b;
- break;
- }
- prev = b;
- b = b.next;
- }
- if( tmpBuf == null ) {
- tmpBuf = allocGPU(size, UPLOAD, GENERIC_READ);
- var b = new TempBuffer();
- b.buffer = tmpBuf;
- b.size = size;
- b.lastUse = frameCount;
- b.next = frame.usedBuffers;
- frame.usedBuffers = b;
- }
- var ptr = tmpBuf.map(0, null);
- ptr.blit(0, data, 0, dataSize);
- tmpBuf.unmap(0,null);
- return tmpBuf;
- }
- function uploadBuffers( buffers : h3d.shader.Buffers, buf : h3d.shader.Buffers.ShaderBuffers, which:h3d.shader.Buffers.BufferKind, shader : hxsl.RuntimeShader.RuntimeShaderData, regs : ShaderRegisters ) {
- switch( which ) {
- case Params:
- if( shader.paramsSize > 0 ) {
- var data = hl.Bytes.getArray(buf.params.toData());
- var dataSize = shader.paramsSize << 4;
- if( regs.params & 0x100 != 0 ) {
- // update CBV
- var srv = frame.shaderResourceViews.alloc(1);
- var cbv = allocDynamicBuffer(data,dataSize);
- var desc = tmp.cbvDesc;
- desc.bufferLocation = cbv.getGpuVirtualAddress();
- desc.sizeInBytes = calcCBVSize(dataSize);
- Driver.createConstantBufferView(desc, srv);
- frame.commandList.setGraphicsRootDescriptorTable(regs.params & 0xFF, frame.shaderResourceViews.toGPU(srv));
- } else
- frame.commandList.setGraphicsRoot32BitConstants(regs.params, dataSize >> 2, data, 0);
- }
- case Globals:
- if( shader.globalsSize > 0 )
- frame.commandList.setGraphicsRoot32BitConstants(regs.globals, shader.globalsSize << 2, hl.Bytes.getArray(buf.globals.toData()), 0);
- case Textures:
- if( regs.texturesCount > 0 ) {
- var srv = frame.shaderResourceViews.alloc(regs.texturesCount);
- var sampler = frame.samplerViews.alloc(regs.texturesCount);
- for( i in 0...regs.texturesCount ) {
- var t = buf.tex[i];
- if( t == null || t.isDisposed() ) {
- if( i < regs.textures2DCount ) {
- var color = h3d.mat.Defaults.loadingTextureColor;
- t = h3d.mat.Texture.fromColor(color, (color >>> 24) / 255);
- } else {
- t = h3d.mat.Texture.defaultCubeTexture();
- }
- }
- if( t != null && t.t == null && t.realloc != null ) {
- var s = currentShader;
- t.alloc();
- t.realloc();
- if( hasDeviceError ) return;
- if( s != currentShader ) {
- // realloc triggered a shader change !
- // we need to reset the original shader and reupload everything
- currentShader = null;
- selectShader(s.shader);
- uploadShaderBuffers(buffers,Globals);
- uploadShaderBuffers(buffers,Params);
- uploadShaderBuffers(buffers,Textures);
- return;
- }
- }
- var tdesc : ShaderResourceViewDesc;
- if( t.flags.has(Cube) ) {
- var desc = tmp.texCubeSRV;
- desc.format = t.t.format;
- desc.mostDetailedMip = t.startingMip;
- tdesc = desc;
- } else if( t.flags.has(IsArray) ) {
- var desc = tmp.tex2DArraySRV;
- desc.format = t.t.format;
- desc.arraySize = t.layerCount;
- desc.mostDetailedMip = t.startingMip;
- tdesc = desc;
- } else if ( t.isDepth() ) {
- var desc = tmp.tex2DSRV;
- desc.format = R24_UNORM_X8_TYPELESS;
- desc.mostDetailedMip = t.startingMip;
- tdesc = desc;
- } else {
- var desc = tmp.tex2DSRV;
- desc.format = t.t.format;
- desc.mostDetailedMip = t.startingMip;
- tdesc = desc;
- }
- t.lastFrame = frameCount;
- var state = if ( t.isDepth() )
- DEPTH_READ;
- else if ( shader.kind == Fragment )
- PIXEL_SHADER_RESOURCE
- else
- NON_PIXEL_SHADER_RESOURCE;
- transition(t.t, state);
- Driver.createShaderResourceView(t.t.res, tdesc, srv.offset(i * frame.shaderResourceViews.stride));
- var desc = tmp.samplerDesc;
- desc.filter = switch( [t.filter, t.mipMap] ) {
- case [Nearest, None|Nearest]: MIN_MAG_MIP_POINT;
- case [Nearest, Linear]: MIN_MAG_POINT_MIP_LINEAR;
- case [Linear, None|Nearest]: MIN_MAG_LINEAR_MIP_POINT;
- case [Linear, Linear]: MIN_MAG_MIP_LINEAR;
- }
- desc.addressU = desc.addressV = desc.addressW = switch( t.wrap ) {
- case Clamp: CLAMP;
- case Repeat: WRAP;
- }
- desc.mipLODBias = t.lodBias;
- Driver.createSampler(desc, sampler.offset(i * frame.samplerViews.stride));
- }
- frame.commandList.setGraphicsRootDescriptorTable(regs.textures, frame.shaderResourceViews.toGPU(srv));
- frame.commandList.setGraphicsRootDescriptorTable(regs.samplers, frame.samplerViews.toGPU(sampler));
- }
- case Buffers:
- if( shader.bufferCount > 0 ) {
- for( i in 0...shader.bufferCount ) {
- var srv = frame.shaderResourceViews.alloc(1);
- var b = buf.buffers[i];
- var cbv = b.vbuf;
- if( cbv.view != null )
- throw "Buffer was allocated without UniformBuffer flag";
- transition(cbv, VERTEX_AND_CONSTANT_BUFFER);
- var desc = tmp.cbvDesc;
- desc.bufferLocation = cbv.res.getGpuVirtualAddress();
- desc.sizeInBytes = cbv.size;
- Driver.createConstantBufferView(desc, srv);
- frame.commandList.setGraphicsRootDescriptorTable(regs.buffers + i, frame.shaderResourceViews.toGPU(srv));
- }
- }
- }
- }
- override function selectShader( shader : hxsl.RuntimeShader ) {
- var sh = compiledShaders.get(shader.id);
- if( sh == null ) {
- sh = compileShader(shader);
- compiledShaders.set(shader.id, sh);
- }
- if( currentShader == sh )
- return false;
- currentShader = sh;
- needPipelineFlush = true;
- frame.commandList.setGraphicsRootSignature(currentShader.rootSignature);
- return true;
- }
- override function selectMaterial( pass : h3d.mat.Pass ) @:privateAccess {
- needPipelineFlush = true;
- pipelineSignature.setI32(PSIGN_MATID, pass.bits);
- pipelineSignature.setUI8(PSIGN_COLOR_MASK, pass.colorMask);
- var st = pass.stencil;
- if( st != null ) {
- pipelineSignature.setUI16(PSIGN_STENCIL_MASK, st.maskBits & 0xFFFF);
- pipelineSignature.setI32(PSIGN_STENCIL_OPS, st.opBits);
- if( curStencilRef != st.reference ) {
- curStencilRef = st.reference;
- frame.commandList.omSetStencilRef(st.reference);
- }
- } else {
- pipelineSignature.setUI16(PSIGN_STENCIL_MASK, 0);
- pipelineSignature.setI32(PSIGN_STENCIL_OPS, 0);
- }
- }
- override function selectBuffer(buffer:Buffer) {
- var views = tmp.vertexViews;
- var bview = buffer.vbuf.view;
- var map = buffer.format.resolveMapping(currentShader.format);
- var vbuf = buffer.vbuf;
- for( i in 0...currentShader.inputCount ) {
- var v = views[i];
- var inf = map[i];
- v.bufferLocation = bview.bufferLocation;
- v.sizeInBytes = bview.sizeInBytes;
- v.strideInBytes = bview.strideInBytes;
- if( inf.offset >= 256 ) throw "assert";
- pipelineSignature.setUI8(PSIGN_LAYOUT + i, inf.offset | inf.precision.toInt());
- }
- needPipelineFlush = true;
- frame.commandList.iaSetVertexBuffers(0, currentShader.inputCount, views[0]);
- }
- override function selectMultiBuffers(formats:hxd.BufferFormat.MultiFormat,buffers:Array<h3d.Buffer>) {
- var views = tmp.vertexViews;
- var map = formats.resolveMapping(currentShader.format);
- for( i in 0...map.length ) {
- var v = views[i];
- var inf = map[i];
- var bview = @:privateAccess buffers[inf.bufferIndex].vbuf.view;
- v.bufferLocation = bview.bufferLocation;
- v.sizeInBytes = bview.sizeInBytes;
- v.strideInBytes = bview.strideInBytes;
- if( inf.offset >= 256 ) throw "assert";
- pipelineSignature.setUI8(PSIGN_LAYOUT + i, inf.offset | inf.precision.toInt());
- }
- needPipelineFlush = true;
- frame.commandList.iaSetVertexBuffers(0, map.length, views[0]);
- }
- static var CULL : Array<CullMode> = [NONE,BACK,FRONT,NONE];
- static var BLEND_OP : Array<BlendOp> = [ADD,SUBTRACT,REV_SUBTRACT,MIN,MAX];
- static var COMP : Array<ComparisonFunc> = [ALWAYS, NEVER, EQUAL, NOT_EQUAL, GREATER, GREATER_EQUAL, LESS, LESS_EQUAL];
- static var BLEND : Array<Blend> = [
- ONE,ZERO,SRC_ALPHA,SRC_COLOR,DEST_ALPHA,DEST_COLOR,INV_SRC_ALPHA,INV_SRC_COLOR,INV_DEST_ALPHA,INV_DEST_COLOR,
- SRC1_COLOR,SRC1_ALPHA,INV_SRC1_COLOR,INV_SRC1_ALPHA,SRC_ALPHA_SAT
- ];
- static var BLEND_ALPHA : Array<Blend> = [
- ONE,ZERO,SRC_ALPHA,SRC_ALPHA,DEST_ALPHA,DEST_ALPHA,INV_SRC_ALPHA,INV_SRC_ALPHA,INV_DEST_ALPHA,INV_DEST_ALPHA,
- SRC1_ALPHA,SRC1_ALPHA,INV_SRC1_ALPHA,INV_SRC1_ALPHA,SRC_ALPHA_SAT,
- ];
- static var STENCIL_OP : Array<StencilOp> = [KEEP, ZERO, REPLACE, INCR_SAT, INCR, DECR_SAT, DECR, INVERT];
- function makePipeline( shader : CompiledShader ) {
- var p = shader.pipeline;
- var passBits = pipelineSignature.getI32(PSIGN_MATID);
- var colorMask = pipelineSignature.getUI8(PSIGN_COLOR_MASK);
- var stencilMask = pipelineSignature.getUI16(PSIGN_STENCIL_MASK);
- var stencilOp = pipelineSignature.getI32(PSIGN_STENCIL_OPS);
- var csrc = Pass.getBlendSrc(passBits);
- var cdst = Pass.getBlendDst(passBits);
- var asrc = Pass.getBlendAlphaSrc(passBits);
- var adst = Pass.getBlendAlphaDst(passBits);
- var cop = Pass.getBlendOp(passBits);
- var aop = Pass.getBlendAlphaOp(passBits);
- var dw = Pass.getDepthWrite(passBits);
- var cmp = Pass.getDepthTest(passBits);
- var cull = Pass.getCulling(passBits);
- var wire = Pass.getWireframe(passBits);
- if( wire != 0 ) cull = 0;
- var rtCount = currentRenderTargets.length;
- if( rtCount == 0 ) rtCount = 1;
- p.numRenderTargets = rtCount;
- p.rasterizerState.cullMode = CULL[cull];
- p.rasterizerState.fillMode = wire == 0 ? SOLID : WIREFRAME;
- p.depthStencilDesc.depthEnable = cmp != 0;
- p.depthStencilDesc.depthWriteMask = dw == 0 || !depthEnabled ? ZERO : ALL;
- p.depthStencilDesc.depthFunc = COMP[cmp];
- var bl = p.blendState;
- for( i in 0...rtCount ) {
- var t = bl.renderTargets[i];
- t.blendEnable = csrc != 0 || cdst != 1;
- t.srcBlend = BLEND[csrc];
- t.dstBlend = BLEND[cdst];
- t.srcBlendAlpha = BLEND_ALPHA[asrc];
- t.dstBlendAlpha = BLEND_ALPHA[adst];
- t.blendOp = BLEND_OP[cop];
- t.blendOpAlpha = BLEND_OP[aop];
- t.renderTargetWriteMask = colorMask;
- var t = currentRenderTargets[i];
- p.rtvFormats[i] = t == null ? R8G8B8A8_UNORM : t.t.format;
- }
- p.dsvFormat = depthEnabled ? D24_UNORM_S8_UINT : UNKNOWN;
- for( i in 0...shader.inputCount ) {
- var d = shader.inputLayout[i];
- var offset = pipelineSignature.getUI8(PSIGN_LAYOUT + i);
- d.alignedByteOffset = offset & ~3;
- d.format = @:privateAccess switch( [shader.format.inputs[i].type, new hxd.BufferFormat.Precision(offset&3)] ) {
- case [DFloat, F32]: R32_FLOAT;
- case [DFloat, F16]: R16_FLOAT;
- case [DFloat, S8]: R8_SNORM;
- case [DFloat, U8]: R8_UNORM;
- case [DVec2, F32]: R32G32_FLOAT;
- case [DVec2, F16]: R16G16_FLOAT;
- case [DVec2, S8]: R8G8_SNORM;
- case [DVec2, U8]: R8G8_UNORM;
- case [DVec3, F32]: R32G32B32_FLOAT;
- case [DVec3, F16]: R16G16B16A16_FLOAT; // padding
- case [DVec3, S8]: R8G8B8A8_SNORM; // padding
- case [DVec3, U8]: R8G8B8A8_UNORM; // padding
- case [DVec4, F32]: R32G32B32A32_FLOAT;
- case [DVec4, F16]: R16G16B16A16_FLOAT;
- case [DVec4, S8]: R8G8B8A8_SNORM;
- case [DVec4, U8]: R8G8B8A8_UNORM;
- case [DBytes4, _]: R8G8B8A8_UINT;
- default: throw "assert";
- };
- }
- var stencil = stencilMask != 0 || stencilOp != 0;
- var st = p.depthStencilDesc;
- st.stencilEnable = stencil;
- if( stencil ) {
- var front = st.frontFace;
- var back = st.backFace;
- st.stencilReadMask = stencilMask & 0xFF;
- st.stencilWriteMask = stencilMask >> 8;
- front.stencilFunc = COMP[Stencil.getFrontTest(stencilOp)];
- front.stencilPassOp = STENCIL_OP[Stencil.getFrontPass(stencilOp)];
- front.stencilFailOp = STENCIL_OP[Stencil.getFrontSTfail(stencilOp)];
- front.stencilDepthFailOp = STENCIL_OP[Stencil.getFrontDPfail(stencilOp)];
- back.stencilFunc = COMP[Stencil.getBackTest(stencilOp)];
- back.stencilPassOp = STENCIL_OP[Stencil.getBackPass(stencilOp)];
- back.stencilFailOp = STENCIL_OP[Stencil.getBackSTfail(stencilOp)];
- back.stencilDepthFailOp = STENCIL_OP[Stencil.getBackDPfail(stencilOp)];
- }
- return Driver.createGraphicsPipelineState(p);
- }
- function flushPipeline() {
- if( !needPipelineFlush ) return;
- needPipelineFlush = false;
- var signature = pipelineSignature;
- var signatureSize = PSIGN_LAYOUT + currentShader.inputCount;
- adlerOut.setI32(0, 0);
- hl.Format.digest(adlerOut, signature, signatureSize, 3);
- var hash = adlerOut.getI32(0);
- var pipes = currentShader.pipelines.get(hash);
- if( pipes == null ) {
- pipes = new hl.NativeArray(1);
- currentShader.pipelines.set(hash, pipes);
- }
- var insert = -1;
- for( i in 0...pipes.length ) {
- var p = pipes[i];
- if( p == null ) {
- insert = i;
- break;
- }
- if( p.size == signatureSize && p.bytes.compare(0, signature, 0, signatureSize) == 0 ) {
- frame.commandList.setPipelineState(p.pipeline);
- return;
- }
- }
- var signatureBytes = @:privateAccess new haxe.io.Bytes(pipelineSignature, signatureSize);
- if( insert < 0 ) {
- var pipes2 = new hl.NativeArray(pipes.length + 1);
- pipes2.blit(0, pipes, 0, insert);
- currentShader.pipelines.set(hash, pipes2);
- pipes = pipes2;
- }
- var cp = new CachedPipeline();
- cp.bytes = signature.sub(0, signatureSize);
- cp.size = signatureSize;
- cp.pipeline = makePipeline(currentShader);
- pipes[insert] = cp;
- frame.commandList.setPipelineState(cp.pipeline);
- }
- // QUERIES
- static inline var QUERY_COUNT = 128;
- override function allocQuery( queryKind : QueryKind ) : Query {
- if( queryKind != TimeStamp )
- throw "Not implemented";
- return new Query();
- }
- override function deleteQuery( q : Query ) {
- // nothing to do
- }
- override function beginQuery( q : Query ) {
- // nothing
- }
- override function endQuery( q : Query ) {
- var heap = frame.queryHeaps[frame.queryCurrentHeap];
- if( heap == null ) {
- var desc = new QueryHeapDesc();
- desc.type = TIMESTAMP;
- desc.count = QUERY_COUNT;
- heap = Driver.createQueryHeap(desc);
- frame.queryHeaps[frame.queryCurrentHeap] = heap;
- if( frame.queryBuffer != null ) {
- frame.queryBuffer.release();
- frame.queryBuffer = null;
- }
- }
- q.offset = frame.queryHeapOffset++;
- q.heap = frame.queryCurrentHeap;
- frame.commandList.endQuery(heap, TIMESTAMP, q.offset);
- frame.queriesPending.push(q);
- if( frame.queryHeapOffset == QUERY_COUNT ) {
- frame.queryHeapOffset = 0;
- frame.queryCurrentHeap++;
- }
- }
- override function queryResultAvailable( q : Query ) {
- return q.heap < 0;
- }
- override function queryResult( q : Query ) {
- return q.result;
- }
- function beginQueries() {
- if( frame.queryBuffer == null || frame.queriesPending.length == 0 )
- return;
- var ptr : hl.BytesAccess<Int64> = frame.queryBuffer.map(0, null);
- while( true ) {
- var q = frame.queriesPending.pop();
- if( q == null ) break;
- if( q.heap >= 0 ) {
- var position = q.heap * QUERY_COUNT + q.offset;
- var v = ptr[position];
- q.result = ((v / tsFreq).low + (v % tsFreq).low / tsFreq.low) * 1e9;
- q.heap = -1;
- }
- }
- frame.queryBuffer.unmap(0, null);
- }
- function flushQueries() {
- if( frame.queryHeapOffset > 0 )
- frame.queryCurrentHeap++;
- if( frame.queryCurrentHeap == 0 )
- return;
- if( frame.queryBuffer == null )
- frame.queryBuffer = allocGPU(frame.queryHeaps.length * QUERY_COUNT * 8, READBACK, COPY_DEST);
- var position = 0;
- for( i in 0...frame.queryCurrentHeap ) {
- var count = i < frame.queryCurrentHeap - 1 ? QUERY_COUNT : frame.queryHeapOffset;
- frame.commandList.resolveQueryData(frame.queryHeaps[i], TIMESTAMP, 0, count, frame.queryBuffer, position);
- position += count * 8;
- }
- frame.queryCurrentHeap = 0;
- frame.queryHeapOffset = 0;
- }
- // --- DRAW etc.
- override function draw( ibuf : Buffer, startIndex : Int, ntriangles : Int ) {
- flushPipeline();
- if( currentIndex != ibuf ) {
- currentIndex = ibuf;
- frame.commandList.iaSetIndexBuffer(ibuf.vbuf.iview);
- }
- frame.commandList.drawIndexedInstanced(ntriangles * 3,1,startIndex,0,0);
- flushResources();
- }
- override function drawInstanced(ibuf:Buffer, commands:InstanceBuffer) {
- flushPipeline();
- if( currentIndex != ibuf ) {
- currentIndex = ibuf;
- frame.commandList.iaSetIndexBuffer(ibuf.vbuf.iview);
- }
- if( commands.data != null ) {
- frame.commandList.executeIndirect(indirectCommand, commands.commandCount, commands.data, 0, null, 0);
- } else {
- frame.commandList.drawIndexedInstanced(commands.indexCount, commands.commandCount, commands.startIndex, 0, 0);
- }
- flushResources();
- }
- function flushResources() {
- if( frame.shaderResourceViews.available < 128 || frame.samplerViews.available < 64 ) {
- frame.shaderResourceViews = frame.shaderResourceCache.next();
- frame.samplerViews = frame.samplerCache.next();
- var arr = tmp.descriptors2;
- arr[0] = @:privateAccess frame.shaderResourceViews.heap;
- arr[1] = @:privateAccess frame.samplerViews.heap;
- frame.commandList.setDescriptorHeaps(arr);
- }
- }
- function flushFrame( onResize : Bool = false ) {
- flushQueries();
- frame.commandList.close();
- frame.commandList.execute();
- currentShader = null;
- Driver.flushMessages();
- frame.fenceValue = fenceValue++;
- Driver.signal(fence, frame.fenceValue);
- }
- override function present() {
- transition(frame.backBuffer, PRESENT);
- flushFrame();
- Driver.present(window.vsync);
- waitForFrame(Driver.getCurrentBackBufferIndex());
- beginFrame();
- if( hasDeviceError ) {
- Sys.println("----------- OnContextLost ----------");
- hasDeviceError = false;
- dispose();
- reset();
- onContextLost();
- }
- }
- function waitForFrame( index : Int ) {
- var frame = frames[index];
- if( fence.getValue() < frame.fenceValue ) {
- fence.setEvent(frame.fenceValue, fenceEvent);
- fenceEvent.wait(-1);
- }
- }
- }
- #end
|