Direct3D 12 - Watch out for non-uniform resource index!

15:30
Mon
28
Sep 2015

Direct3D 12 - Watch out for non-uniform resource index!

DirectX 12 allows us to use arrays of resources (descriptor tables) and refer to them from a shader code by an index. The index can be constant or variable. But there is a big trap waiting for shader developers doing that! Microsoft just updated their public documentation in this regard (see Resource Binding in HLSL), so now I can talk about it.

In D3D12, resource index is expected to be uniform (convergent) by default. It means the index, even if dynamic, should be the same across whole draw call (all vertices/pixels/etc.). For example, it can be a result of some calculations, which depend on parameters coming from a constant buffer. Example code:

Texture2D<float4> textures[16] : register(t0);
SamplerState samp : register(s0);
struct SConstantBuffer
{
    uint MaterialId;
};
ConstantBuffer<SConstantBuffer> cb : register(b0);

float4 PS(
    in float2 texCoords : TEXCOORD0 ) : SV_Target
{
    uint materialId = cb.MaterialId;
    return textures[materialId].Sample(samp, texCoords);
}

Why is it this way? That is because on low level, GPU-s are made of SIMD processors. Each of its small processors executes shader instructions over multiple "SIMD threads" (or "warps", or "wavefronts", however you call them), not a single (scalar) value. Because of that, knowing that the resource index will always be the same on all SIMD threads can result in some optimizations and more efficient execution.

Resource indices in Direct3D 12 actually can be non-uniform (divergent) - different in every vertex/pixel/etc., like when they are result of some calculations based on vertex attributes or pixel position. But they must be then surrounded by a special, "pseudo"-function in HLSL, called NonUniformResourceIndex. For example:

Texture2D<float4> textures[16] : register(t0);
SamplerState samp : register(s0);

float4 PS(
    in float2 texCoords : TEXCOORD0,
    in uint materialId : MATERIAL_ID ) : SV_Target
{
    return textures[NonUniformResourceIndex(materialId)].Sample(samp, texCoords);
}

This may look a bit unintuitive, so I expect many developers will make the mistake and omit this function. If you use a non-uniform value as resource index, but forget to mark it with NonUniformResourceIndex, HLSL compiler probably won't warn you about it. It may even work in some cases and on some GPU-s, while it will give invalid (undefined) results on others. So similarly to the issue with reduced precision in normalize/length operations in GLSL, this is a thing to be careful with when coding your shaders in HLSL using new Shader Model 5.1.

Comments (3) | Tags: directx | Author: Adam Sawicki | Share

Comments

Matias N. Goldberg
2015-09-28 16:49:54
Nice catch!

Somehow, I don't think NonUniformResourceIndex is supported by all tiers, since divergent addressing w/ bindless textures in OpenGL was undefined behavior (basically tip was "don't do it" because some HW would go kapput, others would return garbage, others would do the correct thing, particularly the newer models)

I'm guessing this "NonUniformResourceIndex" qualifier just means "we'll refuse to run if we encounter incompatible hardware". I doubt it will be used to correct rendering even on old HW, but I'll be glad to be mistaken.
Adam Sawicki
2015-09-28 21:18:17
Matias N. Goldberg: No, non-uniform resource indexing is supported even on older hardware and lower tiers, and the flag really is important.
YLQ
2017-02-22 09:52:00
people's http://www.vans-shoes.net/ well-being, http://uggboots.misblackfriday.com/ and http://broncos.nfljersey.us.com/ give http://www.mcm-handbags.org/ them http://www.air-huarache.co.uk/ a http://www.puma-shoes.de/ little http://www.insanity-workout.us.com/ more http://www.soccers-shoes.net/ respect, http://www.zxcoachoutlet.com/ a http://jazz.nba-jersey.com/ little http://www.calvin-kleins.net/ more http://www.oakleys.org.es/ room, http://www.longchamps.us.com/ I http://www.true-religion.com.co/ believe http://www.ralphlauren-polos.com.co/ that http://giants.nfljersey.us.com/ your http://warriors.nba-jersey.com/ friendly http://www.michael-kors-taschen.com.de/ nature http://www.oakleysunglasses2017.com/ of http://www.nikestore.com.de/ the http://www.rayban-pas-cher.fr/ people http://jets.nfljersey.us.com/ will http://www.chiflatiron.net.co/ be http://www.longchamp-bags.us.com/ able http://www.burberrys-outlets.org.uk/ to http://www.nike-airmax.us.com/ exchange http://www.cheap-jerseys.cc/ for http://www.marc-jacobsonsale.com/ a http://www.christian-louboutins.in.net/ gentle http://www.uggsoutlet.net.co/ fragrance. http://www.michael-korssale.us.com/ Man, http://www.new-balance-schuhe.de/ to http://www.abercrombie-andfitch.ca/ learn http://www.ray-bans.net.co/ respectful http://www.rayban-pascher.fr/ and http://www.michaelkors-outletonline.cc/ humble, http://texans.nfljersey.us.com/ with http://www.thomas-sabo.com.de/ people, http://www.michael-korshandbags.us.org/ not http://www.nhl-jerseys.us.com/ easy http://www.the-northfaces.net.co/ to http://www.northfaceoutlet.com.co/ hurt http://www.oakley--sunglasses.com.au/ anyone. http://www.nike-rosherun.com.es/ A http://nuggets.nba-jersey.com/ real http://falcons.nfljersey.us.com/ meaning http://www.oakleyssunglasses.in.net/ people, http://www.hugo-bossoutlet.com/ is http://www.airjordans.us/ never http://www.nikefree-run.net/ so http://www.womenclothes.in.net/ self-righteous, http://www.christianlouboutinshoesoutlet.org/ seeking http://www.nike-max.fr/ only http://eagles.nfljersey.us.com/ their http://www.hermes-bags.net/ feelings http://www.poloralphlaurenoutlet.net.co/ in http://www.nikeskoes.dk/ front http://www.pandorajewellery.com.au/ of http://www.burberry-outletstore.net/ others http://trailblazers.nba-jersey.com/ abrupt http://www.vibram-fivefingers.in.net/ aggressive, http://www.nikeshoes-outlet.com/ assertive http://www.christianlouboutin.org.uk/ and http://www.longchamp.com.co/ even http://www.truereligion-outlet.com.co/ blind http://www.swarovskijewelrys.in.net/ head http://www.ralphslaurenoutlet.us.com/ domineering! http://www.iphone-cases.in.net/ His http://spurs.nba-jersey.com/ cultivation http://www.uggs-boots.com.co/ will http://www.coach-factory.in.net/ tell http://www.celine-bags.org/ him http://www.omegawatches.in.net/ what http://www.ralph-laurens.org.uk/ is http://www.bcbg-dresses.com/ appropriate http://www.longchampoutlet.com.co/ retreat, http://www.michael-kors-australia.com.au/ brave http://www.oakleys-outlet.it/ moment, http://coach.blackfriday.in.net/ I http://pelicans.nba-jersey.com/ can http://www.ferragamoshoes.in.net/ not http://www.hermesbirkin-outlet.com/ be http://www.oakley.com.de/ brave; http://www.mcms-handbags.com/ courageous http://www.swarovskissale.co.uk/ moderate, http://www.michael-korsbags.co.uk/ is http://saints.nfljersey.us.com/ a http://longchamp.blackfriday.in.net/ graceful, http://www.cheap-oakleyssunglasses.com/ courageous http://www.oakleyoutlet.ar.com/ if http://www.hollister-clothing.in.net/ excessive, http://www.ok-em.com/ it http://www.uggsstore.us.com/ appears http://www.michaelkors-canadaoutlet.ca/ arrogant.Life http://www.christian-louboutinshoes.in.net/ is http://lakers.nba-jersey.com/ like http://www.salvatoreferragamo.in.net/ a http://www.nike-schuhe.com.de/ station, http://www.oakley-outletonline.com.co/ gathering http://redskins.nfljersey.us.com/ dispersed; http://www.uggs-boots.net.co/ yesterday, http://www.pradaoutlet.com.co/ like http://www.nikeair--max.fr/ a http://www.coachblackfriday.com/ landscape, http://www.cheapjerseys.net.co/ saw http://www.christianlouboutinoutlet.net.co/ blurred; http://www.burberry-handbagssale.com.co/ time http://www.raybans-outlet.com.co/ is http://www.ralphlaurencanada.ca/ like http://www.raybanoutlet.ca/ a http://dolphins.nfljersey.us.com/ passing, http://www.nike-roshe-run.de/ remember http://www.nike-air-max.com.au/ the http://www.juicycouture.com.co/ forgotten; http://www.burberryoutlet-sale.net/ life http://www.nike-outlet.us.org/ is http://www.christianlouboutin-shoes.ca/ like http://www.converses-outlet.com/ a http://www.adidas-schuheonline.de/ funnel, http://www.kate-spades.com.co/ has http://www.true-religions.com/ been http://www.timberlands-paschere.fr/ He http://www.newoutletonlinemall.com/ lost; http://www.raybans.org.es/ friendship, http://www.toms-shoes.net.co/ like http://www.newbalancecanada.ca/ a http://www.nike-schoenen.co.nl/ banquet http://www.mk-outletonline.us/ table, http://www.ray-bansoutlet.it/ warm http://www.newbalanceshoes.com.es/ up http://www.coach-factoryyoutletonline.net/ the http://coach.misblackfriday.com/ cold. http://azcardinals.nfljersey.us.com/ And http://www.michaelkors.com.se/ love http://www.ugg-australia.com.de/ is http://www.tommy-hilfiger-online.de/ a http://rayban.blackfriday.in.net/ kind http://www.outlet-burberry.net.co/ of http://www.raybans-sunglass.com/ helpless, http://www.ralph-laurenpolosoutlet.com/ the http://www.cheapreplica-watches.in.net/ heart http://www.omegarelojes.es/ will http://www.coach-factoryoutlet.net.co/ be http://www.raybanssunglasses.in.net/ pain; http://www.pandorajewelry.top/ heartbreak, http://www.timberlandbootsoutlet.us.com/ evolved http://www.cheap-michaelkors.in.net/ into http://bengals.nfljersey.us.com/ an http://www.to-coachoutlet.com/ attitude, http://coach.euro-us.net/ indulgence http://www.versaceoutlet.us.com/ convergence; http://www.katespades-outlet.in.net/ life http://www.coachoutlet-online.com.co/ is http://www.burberryoutlets.net.co/ a http://www.valentino-shoesoutlet.us/ calm, http://www.tomsshoes-outlet.us.com/ cried http://www.longchamp-handbagsoutlet.us.com/ laughed.Life, http://www.toms-shoesoutlet.us/ sometimes http://www.oakleys-sunglasses.in.net/ need http://www.vans-schuhe.com.de/ to http://www.cheapmichaelkors.us.org/ precipitation, http://www.cheap-nike-shoes.net/ but http://www.abercrombiefitchs.cc/ also http://www.jordanretro.org/ need http://www.cheap-jerseys.mex.com/ experience. http://www.prada-shoes.com.co/ Have http://www.oakleys-outlet.in.net/ enough http://www.christianlouboutinshoes.jp.net/ time http://www.oakleys-2017.in.net/ to http://www.abercrombie-andfitchs.com/ reflect, http://www.hollisterclothingstore.org/ but http://www.ralph-laurenoutletonline.in.net/ also http://www.prada-handbags.net.co/ has http://www.puma-shoesoutlet.com/ enough http://www.coach-outletonline.ca/ experience http://www.pandoracharms-canada.ca/ to http://www.cheapshoes.net.co/ grow, http://www.toms-outlet.net/ so http://www.bcbg-maxazria.ca/ as http://www.truereligionjeans.net.co/ to http://www.adidasshoes.org.es/ allow http://www.ralph-laurenoutletonline.com/ yourself http://www.ghdhairstraightener.cc/ to http://www.katespadeoutlet.gb.net/ become http://www.polos-outlets.com/ more http://49ers.nfljersey.us.com/ perfect, http://www.barbours.us.com/ more http://www.nike-mercurial.in.net/ wise, http://www.jimmy-choos.com/ more http://www.the-northface.com.co/ mature http://www.michaeljordan.com.de/ indifferent http://www.nike-rosheruns.nl/ charm. http://www.nikerosherun.us/ The http://buccaneers.nfljersey.us.com/ face http://www.nfl-jersey.us.org/ of http://www.co-aol.com/ things, http://cowboys.nfljersey.us.com/ when http://www.michaelkorsoutletonline.net.co/ you http://www.jordanrelease-dates.us.com/ are http://www.barbour-factory.com/ no http://www.hogan.com.de/ longer http://www.designerhandbagsoutlet.net.co/ eager http://ravens.nfljersey.us.com/ to http://www.airmax-90.org/ deny http://www.the-northfacejackets.co.uk/ error, http://www.montres-pascher.fr/ you http://www.raybans-outlet.cc/ first http://www.p90xworkout.in.net/ learned http://www.newbalance-shoes.org/ an http://ralphlauren.blackfriday.in.net/ important http://www.oakleyoutlet.fr/ lesson. http://chiefs.nfljersey.us.com/ Man http://www.nikestore.us/ to http://www.tory-burchsandals.in.net/ dare http://www.mbt-shoes.us.com/ to http://www.cheap-raybansoutlet.in.net/ dare, http://www.michaelkors-bags.com.co/ can http://www.pulseraspandora.com.es/ be http://www.barbour-jacketsoutlet.net/ beneficial http://www.michaelkorsoutlet.ar.com/ to http://www.pandoras-charms.org.uk/ himself, http://www.hermes-outlet.net.co/ but http://www.oakleys2016.com/ to http://www.cheap-rolex-watches.org.uk/ detract http://www.burberrys-bags.net.co/ from http://clippers.nba-jersey.com/ him! http://www.michael-kors.com.es/ The http://www.canadagoosesonline.com/ best http://www.nike-shoescanada.ca/ in http://hawks.nba-jersey.com/ life http://www.tommyhilfigerca.ca/ is http://76ers.nba-jersey.com/ a http://www.ray-bans.co.uk/ pure http://www.handbagsoutlet.in.net/ good http://www.dsquared2.us.com/ heart, http://www.polo-ralph-lauren.de/ honest. http://www.hollisters-canada.ca/ Do http://www.babylisspros.in.net/ more, http://www.air-maxschoenen.nl/ we http://www.tommyhilfiger.net.co/ shall http://www.ralphlauren-au.com/ have http://www.giuseppezanotti.com.co/ less; http://www.soccer-shoesoutlet.com/ not http://www.rosherun.co.uk/ complain, http://coachoutlet.euro-us.net/ often http://bears.nfljersey.us.com/ smiling; http://jaguars.nfljersey.us.com/ not http://coach-outlet.tumblr.com/ greedy, http://wizards.nba-jersey.com/ understand http://www.burberry-outletsonline.org.uk/ contentment!Does http://michaelkors.euro-us.net/ not http://www.adidas.com.se/ float http://www.giuseppe-zanotti.net/ is http://www.swarovski-online-shop.de/ not http://www.converse.com.de/ dry, http://www.lacosteoutlet.us.com/ do http://chargers.nfljersey.us.com/ not http://www.canadagooses-jackets.com/ fight http://www.uhren-shop.com.de/ do http://www.tory-burch.us.org/ not http://www.barbour.in.net/ rob, http://www.nikefree5.net/ do http://www.oakleys.in.net/ not http://www.airyeezy.us.com/ care http://www.ugg-boots-australia.com.au/ about http://www.the-northfaces.us.com/ flashy http://oakley.blackfriday.in.net/ things, http://www.swarovski-australia.com.au/ not http://www.monclerjackets.in.net/ to http://nets.nba-jersey.com/ care http://www.ugg-boots.us.org/ despicable http://www.nike-air-max.com.de/ person! http://airmax.misblackfriday.com/ Never http://www.nike-huaraches.nl/ let http://www.nike-free-run.de/ yourself http://www.adidassuper-star.de/ get http://www.jordan-shoes.com.co/ lost http://www.monclers-outlet.us.com/ in http://www.ferragamo.com.co/ someone http://www.uggs-onsale.net/ else's http://www.converse-shoes.net/ words http://www.chi-flatiron.us.com/ Fun http://www.timberland-boots.com.co/ Fun http://www.cheap-mlbjerseys.us.com/ language, http://cavaliers.nba-jersey.com/ because http://www.pradahandbags.net.co/ no http://mavericks.nba-jersey.com/ one http://www.abercrombie-kid.us.com/ can http://www.tommy-hilfigers.com/ have http://www.uggs-australia.com.co/ a http://www.reebok.com.de/ cute http://www.coach-outletonline.net.co/ flower http://www.jimmy-chooshoes.com/ blooming http://bills.nfljersey.us.com/ invincible http://www.mcm-bags.us.org/ charm, http://www.cheapuggs-boots.com.co/ it http://raiders.nfljersey.us.com/ is http://www.juicycoutureoutlet.net.co/ not http://lions.nfljersey.us.com/ necessary http://www.burberryonlineshop.de/ because http://www.kates-spade.com/ a http://www.thenorthfacejackets.fr/ person http://www.cheap-rayban.com.co/ can http://www.ray-banssunglasses.org/ easily http://www.uggs-online.com/ change http://www.bottega-venetas.cc/ their http://www.newbalance-outlet.org/ word, http://www.nikeshoesoutlet.org.uk/ insist http://www.nike-tnrequinpascher.fr/ on http://suns.nba-jersey.com/ doing http://bucks.nba-jersey.com/ their http://www.cheap-baseballbats.us/ own http://www.prada.com.de/ before http://www.beatsbydrdrephone.com/ It http://www.rayban-sunglasses.co/ is http://www.rayban.com.de/ unique!Through http://www.burberrybags-sale.net/ the http://raptors.nba-jersey.com/ years, http://www.cheapjerseys.us.org/ there http://www.tory-burchoutlet.com.co/ is http://www.rolexwatchesforsale.us.com/ always http://www.mizuno-running.net/ successful, http://www.cheap-jordans.net/ there http://celtics.nba-jersey.com/ is http://www.polos-outletstore.net/ always http://www.abercrombiehollister.nl/ incomplete; http://michaelkors.blackfriday.in.net/ ever http://www.nike-air-force.de/ desire, http://www.longchamp.com.de/ always http://www.thenorthface.com.de/ burning, http://www.ugg-bootscanada.ca/ always http://www.hollister-abercrombie.com.se/ in http://www.soft-ballbats.com/ expansion; http://patriots.nfljersey.us.com/ the http://www.nikeshoes.org.es/ original http://www.bottega-veneta.in.net/ dream, http://browns.nfljersey.us.com/ always http://heat.nba-jersey.com/ burst, http://www.abercrombie-and-fitch.us.com/ always http://www.beatsbydre.com.co/ far http://www.rosheruns.us/ away. http://www.burberryoutlet-sale.com.co/ I http://www.nike-factorys.us/ love http://www.hollisteronlineshop.com.de/ hearts, http://timberwolves.nba-jersey.com/ always http://www.underarmour.us.com/ worried, http://www.tomsoutlet-online.net/ always http://www.toryburchsale.com.co/ thinking http://www.ugg-bootsclearance.com/ about http://www.adidasshoesca.ca/ ......Life http://www.polos-ralphlauren.us.org/ is http://www.the-northfacejackets.net.co/ a http://www.wedding-dresses.cc/ one-way http://www.basketballshoes.com.co/ trip http://www.toms-shoe.us.com/ to http://www.horlogesrolex.nl/ the http://www.uggs.co.nl/ car, http://www.vans-shoes.co.uk/ the http://www.rayban.co.nl/ best http://www.michael-kors-outlet.us.org/ thing http://www.beats-by-dre.com.co/ to http://www.mk-com.com/ do http://www.marc-jacobs.us.com/ is http://www.michaelkorsbags.us.org/ to http://www.fendi-outlet.in.net/ treat http://www.nike-skor.com.se/ yourself http://www.asics-shoesoutlet.com/ well, http://www.michaelkorsoutlet-online.ar.com/ cherish http://www.adidas.us.com/ today, http://www.tommy-hilfiger.com.de/ looking http://www.ralph-laurenspolo.co.uk/ forward http://magic.nba-jersey.com/ to http://www.adidas-superstars.nl/ tomorrow. http://colts.nfljersey.us.com/ Those http://michaelkors.misblackfriday.com/ who http://www.woolrich-clearance.com/ walked, http://www.retro-jordans.com/ miss, http://www.long-champoutlet.com/ have http://www.ralphs-laurens.co.uk/ not http://kings.nba-jersey.com/ come http://www.burberry-outletonline.cc/ back; http://www.nba-jersey.com/ lost, http://www.airmax-2015.org/ and http://www.ralphlaurenonlineshop.de/ lost, http://www.thenorth-face.ca/ are http://www.hollister-clothingstore.com/ not http://grizzlies.nba-jersey.com/ complex http://www.nikeairmaxnc.co.uk/ ownership. http://www.oakleys-frame.net/ So http://www.replica-handbags.net.co/ we http://titans.nfljersey.us.com/ do http://pistons.nba-jersey.com/ not http://www.nikeair-max.es/ go http://www.truereligion-outlet.us.org/ in http://www.uggboots.com.de/ a http://thunder.nba-jersey.com/ hurry, http://panthers.nfljersey.us.com/ the http://www.rolex-watches.net.co/ love http://rockets.nba-jersey.com/ to http://www.guessfatory.net/ Love; http://www.barbour-jackets.us.com/ the http://www.timberlandshoes.net.co/ left, http://www.mcmbackpacks.in.net/ and http://www.mlb-jerseys.us.com/ to http://www.nikefree-run.org.uk/ retain http://www.tommy-hilfiger.co.nl/ good http://www.omega-watches.com.co/ faith; http://www.iphone-cases.net/ to http://www.oakley-outlet.net.co/ fully http://www.nike-air-max.com.se/ experience http://www.toms--outlet.com.co/ the http://www.oakleysunglasses-canada.ca/ feelings, http://celine.blackfriday.in.net/ to http://www.air-huaracheshoes.co.uk/ cherish http://www.michaelkors.so/ the http://www.michael-korsoutlet.cc/ treasure. http://packers.nfljersey.us.com/ Under http://www.nikefreeshoes-inc.co.uk/ remember http://www.michaelkors.co.nl/

http://www.levisjeans.com.co/ http://www.nikeair-max.ca/ http://www.toms-outlets.us.com/ http://seahawks.nfljersey.us.com/ http://www.nfljersey.us.com/ http://www.uggsoutlet.com.co/ http://www.ray-bansoutlet.in.net/ http://www.scarpe-hoganoutlets.it/ http://www.burberry-outletcanada.ca/ http://www.designer-handbagsoutlet.us.com/ http://www.beatsheadphone.in.net/ http://www.salomon-schuhe.com.de/ http://www.rolexwatches-canada.ca/ http://www.converse.net.co/ http://www.cheapnhljerseys.us.com/ http://www.rolex-watches.us.com/ http://www.ralphlaurens-outlet.co.uk/ http://www.ray-banssunglasses.co.uk/ http://www.instylers.us.org/ http://www.philipp-plein.us.com/ http://www.chrome-hearts.in.net/ http://steelers.nfljersey.us.com/ http://www.givenchy.in.net/ http://www.cheap-michaelkors.com/ http://www.asicsoutlet.us.org/ http://www.michael-kors.cc/ http://www.poloralphlauren.cc/ http://www.air-max.com.de/ http://www.armani-exchange.in.net/ http://vikings.nfljersey.us.com/ http://hornets.nba-jersey.com/ http://www.michael-korsbags.org.uk/ http://www.pandora.com.de/ http://pacers.nba-jersey.com/ http://www.truereligions.net/ http://www.mcmhandbags.com.co/ http://www.oakley-sunglasses.mex.com/ http://www.montblancpens-sale.com/ http://www.nba-shoes.com/ http://knicks.nba-jersey.com/ http://www.hollisters.us.com/ http://www.tnf-jackets.us.com/ http://www.sunglasses-outlet.net/ http://www.tommy-hilfiger.in.net/ http://www.supra-shoes.org/ http://www.tracksuits-store.com/ http://www.burberry-handbagsoutlet.net.co/ http://www.cheap-thomassabos.co.uk/ http://www.lauren-ralphsoutlet.co.uk/ http://rams.nfljersey.us.com/ http://www.ed-hardy.us.com/ http://www.swarovski-canada.ca/ http://www.swarovski-crystals.us.com/ http://www.adidas-shoes.cc/ http://bulls.nba-jersey.com/ http://www.michael-kors.net.co/

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki