Jump to content

  • Log In with Google      Sign In   
  • Create Account

Shawn Hargreaves' Blog

DirectX Tool Kit now supports Windows Phone 8

  Posted by , 30 October 2012 - - - - - - · 734 views

I suspect this will come as no surprise to anyone who's been paying attention to what I've been working on recently...
The latest version of DirectXTK adds support for Windows Phone 8. In fact, DirectXTK pretty much already worked on WP8, which uses the same D3D11 API as Windows 8. The only changes in this release are:
  • Added project and solution files for use with the newly released Windows Phone SDK
  • Updated WICTextureLoader and SaveWICTextureToFile to indicate that these are not available on Windows Phone (which does not support WIC)
  • Updated the readme to match


Apollo has landed

  Posted by , 30 October 2012 - - - - - - · 470 views

The developer SDK for Windows Phone 8 (codename Apollo) is now available for download from an internet near you. Which means I can finally talk about what I’ve been working on this past year! I am dev lead for graphics, so I’ll probably have most to say about that area, but knowing me I’m sure I’ll find other topics to write about too.
This first post is basically just a bullet list summarizing the graphics capabilities of the platform:
  • Write games in high performance native C++
  • Or you can interop between XAML and C++/D3D, similar to the Silverlight/XNA interop feature in Mango
  • Graphics APIs are a subset of Windows 8, so it’s easy to move code back and forth between phones, tablets, and desktop PCs
  • Direct3D 11.1 API, targeting feature level 9.3 hardware capabilities
  • DirectXMath provides high performance SIMD vector math, optimized for SSE and NEON
  • XAudio2 provides game focused audio playback, mixing, and effects (ok, you got me, this one isn't graphics)
  • Not all Windows graphics components are supported on phone
  • No WIC - use DirectXTex to preconvert textures to .dds format, then DirectXTK to load them
  • No Direct2D or DirectWrite - use DirectXTK instead
  • No legacy features such as GDI, D3DX, or fixed function - parts of these can be replaced with DirectXTK
What does D3D11 feature level 9.3 give you?
  • Shader model 4_0_level_9_3
  • Max texture size 4096
  • BC1-BC3 texture compression
  • Floating point texture formats
  • Conditional non-power-of-2 textures (can’t use wrap addressing or have mipmaps)
  • Multiple rendertargets
  • Hardware instancing
  • Occlusion queries
  • Separate alpha blend
Yes, you read that right. Shader model 4_0_level_9_3 means that programmable shaders are now fully supported on Windows Phone. But what is this 4_0_level_9_3 thing, you may well ask?
  • Not quite the same as D3D9 shader model 2.0
  • Uses HLSL 4.0 syntax
  • Similar features to vs_2_a and ps_2_b (think of it as 2.0 turned up to 11 :-)
  • Long pixel shaders (512 instructions)
  • Dynamic flow control
  • Gradient instructions
So what notable D3D11 features are NOT included in feature level 9.3?
  • No geometry shaders
  • No hull/domain shaders
  • No DirectCompute
  • No resource arrays
  • No BC4–BC7 compression
  • No vertex texture fetch
Another way to think of this: feature level 9.3 is basically the same as the XNA HiDef profile, except without vertex texture fetch.
Anything else important you should know?

  • Windows Phone 8 uses a tiled GPU architecture, so for best performance you'll need to understand how to use the D3D11_MAP_WRITE and D3D11_COPY flags to specify DISCARD and NOOVERWRITE behaviors, and when to call ID3D11DeviceContext1::DiscardView (ooh, good topic for future articles)

  • Native D3D apps handle their own rotation: this isn’t automatic like in XNA.
  • Windows Phone 8 device manufacturers can choose one of three screen resolutions:
    • 480x800
    • 720x1280
    • 768x1280

  • Windows Phone 8 includes the same hardware scalerfeature as WP7. Use this to draw at the same resolution on all devices (simplifying development) or to draw fewer pixels for better performance. Use the new DXGI_SCALING_ASPECT_RATIO_STRETCHED mode.
I think this is a pretty exciting set of capabilities, and am looking forward to seeing what y’all will be able to do with it. To whet your appetite, check out these screenshots from an HDR rendering demo created by my colleague Adrian. It's crazy that this does on a phone what would have been impossible on a high end gaming console of just 10 years ago, and at a higher resolution to boot!

Posted Image
Posted Image
Posted Image



DirectXTK PrimitiveBatch helper makes it easy to draw user primitives with D3D11

  Posted by , 12 October 2012 - - - - - - · 779 views

The latest version of DirectXTK adds a PrimitiveBatch helper, for easily and efficiently drawing dynamically generated geometry such as lines or triangles. This fills the same role as the legacy D3D9 APIs DrawPrimitiveUP and DrawIndexedPrimitiveUP. PrimitiveBatch manages the vertex and index buffers for you, using DISCARD and NO_OVERWRITE hints to avoid stalling the GPU pipeline. It automatically merges adjacent draw requests, so if you call DrawLine 100 times in a row, only a single GPU draw call will be generated.

PrimitiveBatch is responsible for setting the vertex buffer, index buffer, and primitive topology, then issuing the final draw call. Unlike the higher level SpriteBatch helper, it does not provide shaders, set the input layout, or set any state objects. PrimitiveBatch is often used in conjunction with BasicEffect and the structures from VertexTypes.h, but it can work with any other shader or vertex formats of your own.

To initialize a PrimitiveBatch for drawing VertexPositionColor data: std::unique_ptr<PrimitiveBatch<VertexPositionColor>> primitiveBatch(new PrimitiveBatch<VertexPositionColor>(deviceContext));
To draw a line: basicEffect->Apply(deviceContext);


primitiveBatch->DrawLine(VertexPositionColor(...), VertexPositionColor(...));
PrimitiveBatch provides five drawing methods:
  • DrawLine(v1, v2)
  • DrawTriangle(v1, v2, v3)
  • DrawQuad(v1, v2, v3, v4)
  • Draw(topology, vertices, vertexCount)
  • DrawIndexed(topology, indices, indexCount, vertices, vertexCount)


DirectXTK October 2012 release

  Posted by , 02 October 2012 - - - - - - · 600 views

The DirectX Tool Kit has been updated with two new features:
  • Added a ScreenGrab module for easily saving rendertarget contents into .dds or other image formats
  • Extended GeometricPrimitive.h with a new CreateGeoSphere method (thanks to my colleague Adrian Tsai for contributing this code!)
There are also some minor tweaks and bugfixes:
  • Moved DDSTextureLoader and WICTextureLoader into the DirectX C++ namespace
  • Renamed project files for better consistency
  • Extended WICTextureLoader to support Windows 8 96bpp floating point formats
  • Updated Win32 desktop projects to use Windows Vista (0x0600) rather than Windows 7 (0x0601) APIs
  • Tweaked SpriteBatch.cpp to workaround an ARM NEON compiler codegen bug
So what is this GeoSphere thingie, you may well ask, and why might it be better than a plain old fashioned UV sphere primitive? Shamelessly plagiarizing an explanation from Adrian:
Generally speaking, geospheres are more efficient than non-geodesic spheres (which are typically generated by using rings of vertices along the latitudes/longitudes of the sphere). In an ideal geosphere each triangle has the same area, which means an even distribution of vertices around the sphere. This means geospheres can give better visual quality at the same vertex count.
My implementation approximates an ideal geosphere by recursively subdividing a starting shape – in this case an octahedron. Almost any regular polyhedron can be used, but octahedrons or icosahedrons typically give best results. Starting with an icosahedron would give slightly more accurate results (i.e. it would produce an output closer to an ideal geosphere) but would make the implementation more complex.
Each successive subdivision increases the number of vertices by a factor of four. Hence the total number of vertices is approximately 4^(N+1), where N is the number of tessellations specified in CreateGeoSphere. A value of 3 or 4 is usually enough to give good results.

Tessellation = 11
276 vertices

Tessellation = 3
277 vertices

Posted Image Posted Image



October 2012 »