1、Adam SchaefferMicrosoft CorporationSESSION CODE:WPH308锐普PPT论坛chinakui首发rapidbbsConsistent sets of hardware capabilities defined by MicrosoftResolutionTouch InputCPU/GPURAMHardware keyboard is optional锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbsLow level controlStraight to the metalRaw perfor
2、mance tuningHigh level abstractionRely on compiler and runtimeDeveloper productivity锐普PPT论坛chinakui首发:rapidbbsPowerful and expressiveType safety reduces hard-to-track-down bugsReflectionInitializer syntaxGreat tooling(IntelliSense)Similar enough to C that learning and porting are easyBlazingly fast
3、compilesC#锐普PPT论坛chinakui首发:rapidbbsUsually within a few percent of native performanceAwesome generational garbage collectionPerformance shootout:Raymond Chen vs.Rico Marianis.msdn/ricom/archive/2019/05/10/416151.aspx锐普PPT论坛chinakui首发:rapidbbsSignificant delta between managed and native.NET Compact
4、FrameworkSimplistic mark-and-sweep garbage collectionXbox is not a general purpose computerUnforgiving in-order CPU architectureRequires custom VMX instructions for optimal math perfSecurity architecture poses challenges for jitted code锐普PPT论坛chinakui首发:rapidbbsIn between Windows and Xbox 360.NET Co
5、mpact FrameworkKeep an eye on garbage collection!ARMv7 CPUMore forgiving toward jitted codeARM jitter is more mature than PPC锐普PPT论坛chinakui首发:rapidbbsInstance methodInterfaceDelegate/eventReflectionVirtual method锐普PPT论坛chinakui首发:rapidbbsC+allows independent choice of.NET types dictate their alloca
6、tion and usage semanticsData typeThe memory in which a type lives(placement new)How a type instance is referenced(T,T*,T&,const T&)Value typesint,bool,struct,Vector3Reference typesclass,array,string,delegate,boxed value types锐普PPT论坛chinakui首发:rapidbbsOft-repeated wisdomValue types live on the stackR
7、eference types live on the heapValue types live wherever they are declaredReference types have two piecesMemory allocated from the heapA pointer to this heap memoryThat is subtly incorrect锐普PPT论坛chinakui首发:rapidbbsBy default,prefer class over structureUse struct for things that areSmall(=16 bytes)Sh
8、ort livedPass large structures by referenceMatrix a,b,c;c=Matrix.Multiply(a,b);/copies 192 bytes!Matrix.Multiply(ref a,ref b,out c);锐普PPT论坛chinakui首发:rapidbbsC+.NETAllocateInitially fast,becoming slower as fragmentation increasesVery fast,apart from periodic garbage collectionsFreeFastInstantaneousF
9、ragmentationIncreases over timeNoneCache coherencyRequires custom allocatorsThings allocated close in time are also close in physical locationGarbage collection is not optionalCant have type safety without automatic memory management锐普PPT论坛chinakui首发:rapidbbsTriggered per megabyte of allocation1Star
10、ts with root references(stack variables,statics)2Recursively follows all references to see what other objects can be reached3Anything we didnt reach must be garbage4Compacts the heap,sliding live objects down to fill holes5锐普PPT论坛chinakui首发:rapidbbsFrameworks designed for performanceFrameworks desig
11、ned for performanceMake it run Less OftenIf you never allocate,GC will never runMake it Finish QuicklyCollection time is proportional to how many object references must be traversedUse object poolsSimple heap=fast collectionUse value types and integer handles锐普PPT论坛chinakui首发:rapidbbsExplicitly forc
12、es a garbage collectionUse wisely to give yourself more headroomAfter loadingDuring pauses in gameplayDont call every frame!锐普PPT论坛chinakui首发:rapidbbsBeware of boxingstring vs.StringBuilderUse WeakReference to track GC frequencyhttp:/s.msdn/shawnhar/archive/2019/10/12/monitoring-the-garbage-collecto
13、r.aspxUse CLR Profiler on WindowsSee MIX10 talk:“Development and Debugging Tools for Windows Phone 7 Series”Use.NET Reflector to peek behind the curtainhttp:/red-gate/products/reflector/锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbsPlus hardware accelerated 2D sprite drawingBasicEffectSkinnedE
14、ffectEnvironmentMapEffectAlphaTestEffectDualTextureEffect锐普PPT论坛chinakui首发:rapidbbs0-3 directional lightsBlinn-Phong shadingOptional textureOptional fogOptional vertex colorBasicEffectVertex CostPixel CostNo lighting51One vertex light401Three vertex lights601Three pixel lights1850+Texture+1+2+Fog+4+
15、2锐普PPT论坛chinakui首发:rapidbbsDualTextureEffectFor lightmaps,detail textures,decalsBlends two texturesSeparate texture coordinatesModulate 2X combine mode(A*B*2)Good visuals at low pixel costVertex CostPixel CostTwo Textures76+Fog+4+2锐普PPT论坛chinakui首发:rapidbbsFor billboards and impostersAdds alpha test
16、 operations(pixel kill)Standard blending is free with all effectsOnly need alpha test if you want to disable depth/stencil writesAlphaTestEffectVertex CostPixel Cost,=,66=,!=610+Fog+4+2锐普PPT论坛chinakui首发:rapidbbsSkinnedEffectFor animated models and instancingGame code animates bones on CPUVertex skin
17、ning performed by GPUUp to 72 bonesOne,two,or four weights per vertexVertex CostPixel CostOne vertex light554Three vertex lights754Three pixel lights3351+Two bones+7+0+Four bones+13+0+Fog+0+2锐普PPT论坛chinakui首发:rapidbbsEnvironmentMapEffectOooh,shiny!Diffuse texture+cube environment mapCheap way to fak
18、e many complex lightsFresnel term simulates behavior when light reaches a surface and some reflects,some penetratesVertex CostPixel CostOne light326Three lights366+Fresnel+7+0+Specular+0+2+Fog+0+2锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbsFramerateNumberof PixelsPixel Cost锐普PPT论坛chinakui首发:
19、rapidbbsFramerate 30 hz refresh rate No point updating faster than the display!Game.TargetElapsedTime=TimeSpan.FromSeconds(1f/30);锐普PPT论坛chinakui首发:rapidbbsPixel Cost Prefer cheaper effects Minimize overdrawMany known algorithms:Distance,frustum,BSP,sort front to backImplement“overdraw x-ray mode”Dr
20、aw untextured with additive blendingBrighter areas indicate overdraw锐普PPT论坛chinakui首发:rapidbbsNumberof Pixels 800 x480 is 25%more pixels than Xbox 1Great for textToo many pixels for intensive games800 x480=384,000 pixels600 x360=216,000 pixels(56%)Dedicated hardware scaler Does not consume any GPU H
21、igher quality than bilinear upsampling锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbsAvoidPreferRenderTargetUsage.PreserveContentsRenderTargetUsage.DiscardContentsdevice.BlendState=new BlendState.;/At startupstatic BlendState myState=new BlendState.;/Per frameDevice.BlendState=myState;VertexBuf
22、fer.SetData(.)device.DrawUserPrimitives(.);/orDynamicVertexBuffer.SetData(.,SetDataOptions.NoOverwrite);锐普PPT论坛chinakui首发:rapidbbsGreat performance comes from great knowledgeUnderstandActionsValue types vs.reference typesGarbage collectionC#compiler magic(foreach,iterator methods,closures)Cost of the different graphical effect optionsUse CLR Profiler and.NET ReflectorRender smaller than display resolution,rely on scaler锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbs锐普PPT论坛chinakui首发:rapidbbs