Bug 601257: Clean up usage of shader constants in D3D9 layers. r=jrmuizel a=joedrew
authorBas Schouten <bschouten@mozilla.com>
Sat, 02 Oct 2010 00:24:58 +0200
changeset 54880 a25c1120d719bbe0c47ac4dd7db243e8e01881fa
parent 54879 b1557040c156335894b02af6d9a395ef0176c56e
child 54881 b5af89c610e27f5f2a1a3eb6cf64ec72a7d03002
push id16055
push userbschouten@mozilla.com
push dateFri, 01 Oct 2010 22:25:17 +0000
treeherdermozilla-central@b5af89c610e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel, joedrew
bugs601257
milestone2.0b7pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 601257: Clean up usage of shader constants in D3D9 layers. r=jrmuizel a=joedrew
gfx/layers/d3d9/CanvasLayerD3D9.cpp
gfx/layers/d3d9/ColorLayerD3D9.cpp
gfx/layers/d3d9/ContainerLayerD3D9.cpp
gfx/layers/d3d9/DeviceManagerD3D9.h
gfx/layers/d3d9/ImageLayerD3D9.cpp
gfx/layers/d3d9/LayerManagerD3D9.cpp
gfx/layers/d3d9/LayerManagerD3D9.h
gfx/layers/d3d9/LayerManagerD3D9Shaders.h
gfx/layers/d3d9/LayerManagerD3D9Shaders.hlsl
gfx/layers/d3d9/ThebesLayerD3D9.cpp
--- a/gfx/layers/d3d9/CanvasLayerD3D9.cpp
+++ b/gfx/layers/d3d9/CanvasLayerD3D9.cpp
@@ -228,49 +228,38 @@ CanvasLayerD3D9::GetLayer()
 
 void
 CanvasLayerD3D9::RenderLayer()
 {
   if (!mTexture) {
     Updated(mBounds);
   }
 
-  float quadTransform[4][4];
   /*
-   * Matrix to transform the <0.0,0.0>, <1.0,1.0> quad to the correct position
-   * and size. To get pixel perfect mapping we offset the quad half a pixel
-   * to the top-left. We also flip the Y axis here, note we can only do this
-   * because we are in CULL_NONE mode!
-   *
-   * See: http://msdn.microsoft.com/en-us/library/bb219690%28VS.85%29.aspx
+   * We flip the Y axis here, note we can only do this because we are in 
+   * CULL_NONE mode!
    */
-  memset(&quadTransform, 0, sizeof(quadTransform));
-  quadTransform[0][0] = (float)mBounds.width;
+
+  ShaderConstantRect quad(0, 0, mBounds.width, mBounds.height);
   if (mNeedsYFlip) {
-    quadTransform[1][1] = (float)-mBounds.height;
-    quadTransform[3][1] = (float)mBounds.height;
-  } else {
-    quadTransform[1][1] = (float)mBounds.height;
-    quadTransform[3][1] = 0.0f;
+    quad.mHeight = (float)-mBounds.height;
+    quad.mY = (float)mBounds.height;
   }
-  quadTransform[2][2] = 1.0f;
-  quadTransform[3][0] = 0.0f;
-  quadTransform[3][3] = 1.0f;
 
-  device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
+  device()->SetVertexShaderConstantF(CBvLayerQuad, quad, 1);
 
-  device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+  device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
   float opacity[4];
   /*
    * We always upload a 4 component float, but the shader will use only the
    * first component since it's declared as a 'float'.
    */
   opacity[0] = GetOpacity();
-  device()->SetPixelShaderConstantF(0, opacity, 1);
+  device()->SetPixelShaderConstantF(CBfLayerOpacity, opacity, 1);
 
   mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
 
   if (!mDataIsPremultiplied) {
     device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
     device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
   }
   device()->SetTexture(0, mTexture);
--- a/gfx/layers/d3d9/ColorLayerD3D9.cpp
+++ b/gfx/layers/d3d9/ColorLayerD3D9.cpp
@@ -48,29 +48,27 @@ ColorLayerD3D9::GetLayer()
 }
 
 void
 ColorLayerD3D9::RenderLayer()
 {
   // XXX we might be able to improve performance by using
   // IDirect3DDevice9::Clear
 
-  float quadTransform[4][4];
   nsIntRect visibleRect = mVisibleRegion.GetBounds();
-  // Transform the quad to the size of the visible area.
-  memset(&quadTransform, 0, sizeof(quadTransform));
-  quadTransform[0][0] = (float)visibleRect.width;
-  quadTransform[1][1] = (float)visibleRect.height;
-  quadTransform[2][2] = 1.0f;
-  quadTransform[3][0] = (float)visibleRect.x;
-  quadTransform[3][1] = (float)visibleRect.y;
-  quadTransform[3][3] = 1.0f;
 
-  device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
-  device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+  device()->SetVertexShaderConstantF(
+    CBvLayerQuad,
+    ShaderConstantRect(visibleRect.x,
+                       visibleRect.y,
+                       visibleRect.width,
+                       visibleRect.height),
+    1);
+
+  device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
   float color[4];
   // color is premultiplied, so we need to adjust all channels
   color[0] = (float)(mColor.r * GetOpacity());
   color[1] = (float)(mColor.g * GetOpacity());
   color[2] = (float)(mColor.b * GetOpacity());
   color[3] = (float)(mColor.a * GetOpacity());
 
--- a/gfx/layers/d3d9/ContainerLayerD3D9.cpp
+++ b/gfx/layers/d3d9/ContainerLayerD3D9.cpp
@@ -154,36 +154,33 @@ ContainerLayerD3D9::RenderLayer()
     device()->CreateTexture(visibleRect.width, visibleRect.height, 1,
                             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
                             D3DPOOL_DEFAULT, getter_AddRefs(renderTexture),
                             NULL);
     nsRefPtr<IDirect3DSurface9> renderSurface;
     renderTexture->GetSurfaceLevel(0, getter_AddRefs(renderSurface));
     device()->SetRenderTarget(0, renderSurface);
     device()->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 0, 0);
-    device()->GetVertexShaderConstantF(12, previousRenderTargetOffset, 1);
+    device()->GetVertexShaderConstantF(CBvRenderTargetOffset, previousRenderTargetOffset, 1);
     renderTargetOffset[0] = (float)visibleRect.x;
     renderTargetOffset[1] = (float)visibleRect.y;
-    device()->SetVertexShaderConstantF(12, renderTargetOffset, 1);
+    device()->SetVertexShaderConstantF(CBvRenderTargetOffset, renderTargetOffset, 1);
 
-    float viewMatrix[4][4];
+    gfx3DMatrix viewMatrix;
     /*
      * Matrix to transform to viewport space ( <-1.0, 1.0> topleft,
      * <1.0, -1.0> bottomright)
      */
-    memset(&viewMatrix, 0, sizeof(viewMatrix));
-    viewMatrix[0][0] = 2.0f / visibleRect.width;
-    viewMatrix[1][1] = -2.0f / visibleRect.height;
-    viewMatrix[2][2] = 1.0f;
-    viewMatrix[3][0] = -1.0f;
-    viewMatrix[3][1] = 1.0f;
-    viewMatrix[3][3] = 1.0f;
+    viewMatrix._11 = 2.0f / visibleRect.width;
+    viewMatrix._22 = -2.0f / visibleRect.height;
+    viewMatrix._41 = -1.0f;
+    viewMatrix._42 = 1.0f;
 
-    device()->GetVertexShaderConstantF(8, &oldViewMatrix[0][0], 4);
-    device()->SetVertexShaderConstantF(8, &viewMatrix[0][0], 4);
+    device()->GetVertexShaderConstantF(CBmProjection, &oldViewMatrix[0][0], 4);
+    device()->SetVertexShaderConstantF(CBmProjection, &viewMatrix._11, 4);
   }
 
   /*
    * Render this container's contents.
    */
   LayerD3D9 *layerToRender = GetFirstChildD3D9();
   while (layerToRender) {
     const nsIntRect *clipRect = layerToRender->GetLayer()->GetClipRect();
@@ -223,45 +220,35 @@ ContainerLayerD3D9::RenderLayer()
     layerToRender = nextSibling ? static_cast<LayerD3D9*>(nextSibling->
                                                           ImplData())
                                 : nsnull;
   }
 
   if (useIntermediate) {
     device()->SetRenderTarget(0, previousRenderTarget);
     device()->SetScissorRect(&oldClipRect);
-    device()->SetVertexShaderConstantF(12, previousRenderTargetOffset, 1);
-    device()->SetVertexShaderConstantF(8, &oldViewMatrix[0][0], 4);
+    device()->SetVertexShaderConstantF(CBvRenderTargetOffset, previousRenderTargetOffset, 1);
+    device()->SetVertexShaderConstantF(CBmProjection, &oldViewMatrix[0][0], 4);
 
-    float quadTransform[4][4];
-    /*
-     * Matrix to transform the <0.0,0.0>, <1.0,1.0> quad to the correct position
-     * and size. To get pixel perfect mapping we offset the quad half a pixel
-     * to the top-left.
-     *
-     * See: http://msdn.microsoft.com/en-us/library/bb219690%28VS.85%29.aspx
-     */
-    memset(&quadTransform, 0, sizeof(quadTransform));
-    quadTransform[0][0] = (float)visibleRect.width;
-    quadTransform[1][1] = (float)visibleRect.height;
-    quadTransform[2][2] = 1.0f;
-    quadTransform[3][0] = (float)visibleRect.x;
-    quadTransform[3][1] = (float)visibleRect.y;
-    quadTransform[3][3] = 1.0f;
+    device()->SetVertexShaderConstantF(CBvLayerQuad,
+                                       ShaderConstantRect(visibleRect.x,
+                                                          visibleRect.y,
+                                                          visibleRect.width,
+                                                          visibleRect.height),
+                                       1);
 
-    device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
-    device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+    device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
     float opacityVector[4];
     /*
      * We always upload a 4 component float, but the shader will use only the
      * first component since it's declared as a 'float'.
      */
     opacityVector[0] = opacity;
-    device()->SetPixelShaderConstantF(0, opacityVector, 1);
+    device()->SetPixelShaderConstantF(CBfLayerOpacity, opacityVector, 1);
 
     mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
 
     device()->SetTexture(0, renderTexture);
     device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
   }
 }
 
--- a/gfx/layers/d3d9/DeviceManagerD3D9.h
+++ b/gfx/layers/d3d9/DeviceManagerD3D9.h
@@ -46,16 +46,24 @@
 
 namespace mozilla {
 namespace layers {
 
 class DeviceManagerD3D9;
 class LayerD3D9;
 class Nv3DVUtils;
 
+// Shader Constant locations
+const int CBmLayerTransform = 0;
+const int CBmProjection = 4;
+const int CBvRenderTargetOffset = 8;
+const int CBvTextureCoords = 9;
+const int CBvLayerQuad = 10;
+const int CBfLayerOpacity = 0;
+
 /**
  * SwapChain class, this class manages the swap chain belonging to a
  * LayerManagerD3D9.
  */
 class THEBES_API SwapChainD3D9
 {
   NS_INLINE_DECL_REFCOUNTING(SwapChainD3D9)
 public:
--- a/gfx/layers/d3d9/ImageLayerD3D9.cpp
+++ b/gfx/layers/d3d9/ImageLayerD3D9.cpp
@@ -160,39 +160,32 @@ ImageLayerD3D9::RenderLayer()
     PlanarYCbCrImageD3D9 *yuvImage =
       static_cast<PlanarYCbCrImageD3D9*>(image.get());
 
     if (!yuvImage->HasData()) {
       return;
     }
     yuvImage->AllocateTextures();
 
-    float quadTransform[4][4];
-    /*
-     * Matrix to transform the <0.0,0.0>, <1.0,1.0> quad to the correct position
-     * and size. To get pixel perfect mapping we extend the quad half a pixel
-     * beyond all edges.
-     */
-    memset(&quadTransform, 0, sizeof(quadTransform));
-    quadTransform[0][0] = (float)yuvImage->mSize.width;
-    quadTransform[1][1] = (float)yuvImage->mSize.height;
-    quadTransform[2][2] = 1.0f;
-    quadTransform[3][3] = 1.0f;
+    device()->SetVertexShaderConstantF(CBvLayerQuad,
+                                       ShaderConstantRect(0,
+                                                          0,
+                                                          yuvImage->mSize.width,
+                                                          yuvImage->mSize.height),
+                                       1);
 
-
-    device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
-    device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+    device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
     float opacity[4];
     /*
      * We always upload a 4 component float, but the shader will
      * only use the the first component since it's declared as a 'float'.
      */
     opacity[0] = GetOpacity();
-    device()->SetPixelShaderConstantF(0, opacity, 1);
+    device()->SetPixelShaderConstantF(CBfLayerOpacity, opacity, 1);
 
     mD3DManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER);
 
     /* 
      * Send 3d control data and metadata 
      */ 
     if (mD3DManager->Is3DEnabled() && mD3DManager->GetNv3DVUtils()) { 
       mD3DManager->GetNv3DVUtils()->SendNv3DVControl(STEREO_MODE_RIGHT_LEFT, true, FIREFOX_3DV_APP_HANDLE); 
@@ -214,39 +207,33 @@ ImageLayerD3D9::RenderLayer()
     device()->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
 
     device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
 
   } else if (image->GetFormat() == Image::CAIRO_SURFACE) {
     CairoImageD3D9 *cairoImage =
       static_cast<CairoImageD3D9*>(image.get());
 
-    float quadTransform[4][4];
-    /*
-     * Matrix to transform the <0.0,0.0>, <1.0,1.0> quad to the correct position
-     * and size. To get pixel perfect mapping we extend the quad half a pixel
-     * beyond all edges.
-     */
-    memset(&quadTransform, 0, sizeof(quadTransform));
-    quadTransform[0][0] = (float)cairoImage->mSize.width;
-    quadTransform[1][1] = (float)cairoImage->mSize.height;
-    quadTransform[2][2] = 1.0f;
-    quadTransform[3][3] = 1.0f;
 
+    device()->SetVertexShaderConstantF(CBvLayerQuad,
+                                       ShaderConstantRect(0,
+                                                          0,
+                                                          cairoImage->mSize.width,
+                                                          cairoImage->mSize.height),
+                                       1);
 
-    device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
-    device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+    device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
     float opacity[4];
     /*
      * We always upload a 4 component float, but the shader will
      * only use the the first component since it's declared as a 'float'.
      */
     opacity[0] = GetOpacity();
-    device()->SetPixelShaderConstantF(0, opacity, 1);
+    device()->SetPixelShaderConstantF(CBfLayerOpacity, opacity, 1);
 
     mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
 
     device()->SetTexture(0, cairoImage->mTexture);
     device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
   }
 }
 
--- a/gfx/layers/d3d9/LayerManagerD3D9.cpp
+++ b/gfx/layers/d3d9/LayerManagerD3D9.cpp
@@ -305,36 +305,36 @@ LayerManagerD3D9::Render()
 }
 
 void
 LayerManagerD3D9::SetupPipeline()
 {
   nsIntRect rect;
   mWidget->GetClientBounds(rect);
 
-  float viewMatrix[4][4];
+  gfx3DMatrix viewMatrix;
   /*
    * Matrix to transform to viewport space ( <-1.0, 1.0> topleft,
    * <1.0, -1.0> bottomright)
    */
-  memset(&viewMatrix, 0, sizeof(viewMatrix));
-  viewMatrix[0][0] = 2.0f / rect.width;
-  viewMatrix[1][1] = -2.0f / rect.height;
-  viewMatrix[2][2] = 1.0f;
-  viewMatrix[3][0] = -1.0f;
-  viewMatrix[3][1] = 1.0f;
-  viewMatrix[3][3] = 1.0f;
+  viewMatrix._11 = 2.0f / rect.width;
+  viewMatrix._22 = -2.0f / rect.height;
+  viewMatrix._41 = -1.0f;
+  viewMatrix._42 = 1.0f;
 
-  HRESULT hr = device()->SetVertexShaderConstantF(8, &viewMatrix[0][0], 4);
+  HRESULT hr = device()->SetVertexShaderConstantF(CBmProjection,
+                                                  &viewMatrix._11, 4);
 
   if (FAILED(hr)) {
     NS_WARNING("Failed to set projection shader constant!");
   }
 
-  hr = device()->SetVertexShaderConstantF(13, ShaderConstantRect(0, 0, 1.0f, 1.0f), 1);
+  hr = device()->SetVertexShaderConstantF(CBvTextureCoords,
+                                          ShaderConstantRect(0, 0, 1.0f, 1.0f),
+                                          1);
 
   if (FAILED(hr)) {
     NS_WARNING("Failed to set texCoords shader constant!");
   }
 }
 
 void
 LayerManagerD3D9::PaintToTarget()
--- a/gfx/layers/d3d9/LayerManagerD3D9.h
+++ b/gfx/layers/d3d9/LayerManagerD3D9.h
@@ -61,20 +61,32 @@ class ThebesLayerD3D9;
  * this for passing rectangular areas to SetVertexShaderConstant. In the format
  * of a 4 component float(x,y,width,height). Our vertex shader can then use
  * this to construct rectangular positions from the 0,0-1,1 quad that we source
  * it with.
  */
 struct ShaderConstantRect
 {
   float mX, mY, mWidth, mHeight;
+
+  // Provide all the commonly used argument types to prevent all the local
+  // casts in the code.
   ShaderConstantRect(float aX, float aY, float aWidth, float aHeight)
     : mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight)
   { }
 
+  ShaderConstantRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
+    : mX((float)aX), mY((float)aY)
+    , mWidth((float)aWidth), mHeight((float)aHeight)
+  { }
+
+  ShaderConstantRect(PRInt32 aX, PRInt32 aY, float aWidth, float aHeight)
+    : mX((float)aX), mY((float)aY), mWidth(aWidth), mHeight(aHeight)
+  { }
+
   // For easy passing to SetVertexShaderConstantF.
   operator float* () { return &mX; }
 };
 
 /*
  * This is the LayerManager used for Direct3D 9. For now this will render on
  * the main thread.
  */
--- a/gfx/layers/d3d9/LayerManagerD3D9Shaders.h
+++ b/gfx/layers/d3d9/LayerManagerD3D9Shaders.h
@@ -3,166 +3,153 @@
 // Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
 //
 //   fxc LayerManagerD3D9Shaders.hlsl -ELayerQuadVS -nologo -FhtmpShaderHeader
 //    -VnLayerQuadVS
 //
 //
 // Parameters:
 //
-//   float4x4 mLayerQuadTransform;
 //   float4x4 mLayerTransform;
 //   float4x4 mProjection;
+//   float4 vLayerQuad;
 //   float4 vRenderTargetOffset;
 //   float4 vTextureCoords;
 //
 //
 // Registers:
 //
 //   Name                Reg   Size
 //   ------------------- ----- ----
-//   mLayerQuadTransform c0       4
-//   mLayerTransform     c4       4
-//   mProjection         c8       4
-//   vRenderTargetOffset c12      1
-//   vTextureCoords      c13      1
+//   mLayerTransform     c0       4
+//   mProjection         c4       4
+//   vRenderTargetOffset c8       1
+//   vTextureCoords      c9       1
+//   vLayerQuad          c10      1
 //
 
     vs_2_0
-    def c14, -0.5, 0, 0, 0
+    def c11, -0.5, 0, 0, 0
     dcl_position v0
-    mul r0, v0.y, c1
-    mad r0, c0, v0.x, r0
+    mad r0.xy, v0, c10.zwzw, c10
+    mul r1, r0.y, c1
+    mad r0, c0, r0.x, r1
     mad r0, c2, v0.z, r0
     mad r0, c3, v0.w, r0
+    add r0, r0, -c8
+    add r0.xy, r0, c11.x
     mul r1, r0.y, c5
     mad r1, c4, r0.x, r1
     mad r1, c6, r0.z, r1
-    mad r0, c7, r0.w, r1
-    add r0, r0, -c12
-    add r0.xy, r0, c14.x
-    mul r1, r0.y, c9
-    mad r1, c8, r0.x, r1
-    mad r1, c10, r0.z, r1
-    mad oPos, c11, r0.w, r1
-    mad oT0.xy, v0, c13.zwzw, c13
+    mad oPos, c7, r0.w, r1
+    mad oT0.xy, v0, c9.zwzw, c9
 
-// approximately 15 instruction slots used
+// approximately 12 instruction slots used
 #endif
 
 const BYTE LayerQuadVS[] =
 {
       0,   2, 254, 255, 254, 255, 
-     80,   0,  67,  84,  65,  66, 
-     28,   0,   0,   0,  11,   1, 
+     78,   0,  67,  84,  65,  66, 
+     28,   0,   0,   0,   2,   1, 
       0,   0,   0,   2, 254, 255, 
       5,   0,   0,   0,  28,   0, 
       0,   0,   0,   1,   0,   0, 
-      4,   1,   0,   0, 128,   0, 
+    251,   0,   0,   0, 128,   0, 
       0,   0,   2,   0,   0,   0, 
-      4,   0,   0,   0, 148,   0, 
+      4,   0,   0,   0, 144,   0, 
       0,   0,   0,   0,   0,   0, 
-    164,   0,   0,   0,   2,   0, 
+    160,   0,   0,   0,   2,   0, 
       4,   0,   4,   0,   0,   0, 
-    148,   0,   0,   0,   0,   0, 
-      0,   0, 180,   0,   0,   0, 
-      2,   0,   8,   0,   4,   0, 
-      0,   0, 148,   0,   0,   0, 
-      0,   0,   0,   0, 192,   0, 
-      0,   0,   2,   0,  12,   0, 
-      1,   0,   0,   0, 212,   0, 
+    144,   0,   0,   0,   0,   0, 
+      0,   0, 172,   0,   0,   0, 
+      2,   0,  10,   0,   1,   0, 
+      0,   0, 184,   0,   0,   0, 
+      0,   0,   0,   0, 200,   0, 
+      0,   0,   2,   0,   8,   0, 
+      1,   0,   0,   0, 220,   0, 
       0,   0,   0,   0,   0,   0, 
-    228,   0,   0,   0,   2,   0, 
-     13,   0,   1,   0,   0,   0, 
-    244,   0,   0,   0,   0,   0, 
-      0,   0, 109,  76,  97, 121, 
-    101, 114,  81, 117,  97, 100, 
-     84, 114,  97, 110, 115, 102, 
-    111, 114, 109,   0,   3,   0, 
-      3,   0,   4,   0,   4,   0, 
-      1,   0,   0,   0,   0,   0, 
+    236,   0,   0,   0,   2,   0, 
+      9,   0,   1,   0,   0,   0, 
+    184,   0,   0,   0,   0,   0, 
       0,   0, 109,  76,  97, 121, 
     101, 114,  84, 114,  97, 110, 
     115, 102, 111, 114, 109,   0, 
-    109,  80, 114, 111, 106, 101, 
-     99, 116, 105, 111, 110,   0, 
-    118,  82, 101, 110, 100, 101, 
-    114,  84,  97, 114, 103, 101, 
-    116,  79, 102, 102, 115, 101, 
-    116,   0,   1,   0,   3,   0, 
-      1,   0,   4,   0,   1,   0, 
-      0,   0,   0,   0,   0,   0, 
-    118,  84, 101, 120, 116, 117, 
-    114, 101,  67, 111, 111, 114, 
-    100, 115,   0, 171,   1,   0, 
+      3,   0,   3,   0,   4,   0, 
+      4,   0,   1,   0,   0,   0, 
+      0,   0,   0,   0, 109,  80, 
+    114, 111, 106, 101,  99, 116, 
+    105, 111, 110,   0, 118,  76, 
+     97, 121, 101, 114,  81, 117, 
+     97, 100,   0, 171,   1,   0, 
+      3,   0,   1,   0,   4,   0, 
+      1,   0,   0,   0,   0,   0, 
+      0,   0, 118,  82, 101, 110, 
+    100, 101, 114,  84,  97, 114, 
+    103, 101, 116,  79, 102, 102, 
+    115, 101, 116,   0,   1,   0, 
       3,   0,   1,   0,   4,   0, 
       1,   0,   0,   0,   0,   0, 
-      0,   0, 118, 115,  95,  50, 
-     95,  48,   0,  77, 105,  99, 
-    114, 111, 115, 111, 102, 116, 
-     32,  40,  82,  41,  32,  72, 
-     76,  83,  76,  32,  83, 104, 
-     97, 100, 101, 114,  32,  67, 
-    111, 109, 112, 105, 108, 101, 
-    114,  32,  57,  46,  50,  57, 
-     46,  57,  53,  50,  46,  51, 
-     49,  49,  49,   0,  81,   0, 
-      0,   5,  14,   0,  15, 160, 
-      0,   0,   0, 191,   0,   0, 
+      0,   0, 118,  84, 101, 120, 
+    116, 117, 114, 101,  67, 111, 
+    111, 114, 100, 115,   0, 118, 
+    115,  95,  50,  95,  48,   0, 
+     77, 105,  99, 114, 111, 115, 
+    111, 102, 116,  32,  40,  82, 
+     41,  32,  72,  76,  83,  76, 
+     32,  83, 104,  97, 100, 101, 
+    114,  32,  67, 111, 109, 112, 
+    105, 108, 101, 114,  32,  57, 
+     46,  50,  57,  46,  57,  53, 
+     50,  46,  51,  49,  49,  49, 
+      0, 171,  81,   0,   0,   5, 
+     11,   0,  15, 160,   0,   0, 
+      0, 191,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
-      0,   0,   0,   0,  31,   0, 
-      0,   2,   0,   0,   0, 128, 
-      0,   0,  15, 144,   5,   0, 
-      0,   3,   0,   0,  15, 128, 
-      0,   0,  85, 144,   1,   0, 
+      0,   0,  31,   0,   0,   2, 
+      0,   0,   0, 128,   0,   0, 
+     15, 144,   4,   0,   0,   4, 
+      0,   0,   3, 128,   0,   0, 
+    228, 144,  10,   0, 238, 160, 
+     10,   0, 228, 160,   5,   0, 
+      0,   3,   1,   0,  15, 128, 
+      0,   0,  85, 128,   1,   0, 
     228, 160,   4,   0,   0,   4, 
       0,   0,  15, 128,   0,   0, 
-    228, 160,   0,   0,   0, 144, 
-      0,   0, 228, 128,   4,   0, 
+    228, 160,   0,   0,   0, 128, 
+      1,   0, 228, 128,   4,   0, 
       0,   4,   0,   0,  15, 128, 
       2,   0, 228, 160,   0,   0, 
     170, 144,   0,   0, 228, 128, 
       4,   0,   0,   4,   0,   0, 
      15, 128,   3,   0, 228, 160, 
       0,   0, 255, 144,   0,   0, 
-    228, 128,   5,   0,   0,   3, 
-      1,   0,  15, 128,   0,   0, 
-     85, 128,   5,   0, 228, 160, 
-      4,   0,   0,   4,   1,   0, 
-     15, 128,   4,   0, 228, 160, 
-      0,   0,   0, 128,   1,   0, 
-    228, 128,   4,   0,   0,   4, 
-      1,   0,  15, 128,   6,   0, 
-    228, 160,   0,   0, 170, 128, 
-      1,   0, 228, 128,   4,   0, 
-      0,   4,   0,   0,  15, 128, 
-      7,   0, 228, 160,   0,   0, 
-    255, 128,   1,   0, 228, 128, 
+    228, 128,   2,   0,   0,   3, 
+      0,   0,  15, 128,   0,   0, 
+    228, 128,   8,   0, 228, 161, 
       2,   0,   0,   3,   0,   0, 
-     15, 128,   0,   0, 228, 128, 
-     12,   0, 228, 161,   2,   0, 
-      0,   3,   0,   0,   3, 128, 
-      0,   0, 228, 128,  14,   0, 
-      0, 160,   5,   0,   0,   3, 
-      1,   0,  15, 128,   0,   0, 
-     85, 128,   9,   0, 228, 160, 
-      4,   0,   0,   4,   1,   0, 
-     15, 128,   8,   0, 228, 160, 
-      0,   0,   0, 128,   1,   0, 
+      3, 128,   0,   0, 228, 128, 
+     11,   0,   0, 160,   5,   0, 
+      0,   3,   1,   0,  15, 128, 
+      0,   0,  85, 128,   5,   0, 
+    228, 160,   4,   0,   0,   4, 
+      1,   0,  15, 128,   4,   0, 
+    228, 160,   0,   0,   0, 128, 
+      1,   0, 228, 128,   4,   0, 
+      0,   4,   1,   0,  15, 128, 
+      6,   0, 228, 160,   0,   0, 
+    170, 128,   1,   0, 228, 128, 
+      4,   0,   0,   4,   0,   0, 
+     15, 192,   7,   0, 228, 160, 
+      0,   0, 255, 128,   1,   0, 
     228, 128,   4,   0,   0,   4, 
-      1,   0,  15, 128,  10,   0, 
-    228, 160,   0,   0, 170, 128, 
-      1,   0, 228, 128,   4,   0, 
-      0,   4,   0,   0,  15, 192, 
-     11,   0, 228, 160,   0,   0, 
-    255, 128,   1,   0, 228, 128, 
-      4,   0,   0,   4,   0,   0, 
-      3, 224,   0,   0, 228, 144, 
-     13,   0, 238, 160,  13,   0, 
-    228, 160, 255, 255,   0,   0
+      0,   0,   3, 224,   0,   0, 
+    228, 144,   9,   0, 238, 160, 
+      9,   0, 228, 160, 255, 255, 
+      0,   0
 };
 #if 0
 //
 // Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
 //
 //   fxc LayerManagerD3D9Shaders.hlsl -ERGBAShader -nologo -Tps_2_0
 //    -FhtmpShaderHeader -VnRGBAShaderPS
 //
--- a/gfx/layers/d3d9/LayerManagerD3D9Shaders.hlsl
+++ b/gfx/layers/d3d9/LayerManagerD3D9Shaders.hlsl
@@ -1,15 +1,15 @@
-float4x4 mLayerQuadTransform;
 float4x4 mLayerTransform;
 float4 vRenderTargetOffset;
 float4x4 mProjection;
 
 typedef float4 rect;
 rect vTextureCoords;
+rect vLayerQuad;
 
 texture tex0;
 sampler s2D;
 sampler s2DY;
 sampler s2DCb;
 sampler s2DCr;
 
 float fLayerOpacity;
@@ -23,36 +23,42 @@ struct VS_OUTPUT {
   float4 vPosition : POSITION;
   float2 vTexCoords : TEXCOORD0;
 };
 
 VS_OUTPUT LayerQuadVS(const VS_INPUT aVertex)
 {
   VS_OUTPUT outp;
   outp.vPosition = aVertex.vPosition;
-  outp.vPosition = mul(mLayerQuadTransform, outp.vPosition);
+  
+  // We use 4 component floats to uniquely describe a rectangle, by the structure
+  // of x, y, width, height. This allows us to easily generate the 4 corners
+  // of any rectangle from the 4 corners of the 0,0-1,1 quad that we use as the
+  // stream source for our LayerQuad vertex shader. We do this by doing:
+  // Xout = x + Xin * width
+  // Yout = y + Yin * height
+  float2 position = vLayerQuad.xy;
+  float2 size = vLayerQuad.zw;
+  outp.vPosition.x = position.x + outp.vPosition.x * size.x;
+  outp.vPosition.y = position.y + outp.vPosition.y * size.y;
+  
   outp.vPosition = mul(mLayerTransform, outp.vPosition);
   outp.vPosition = outp.vPosition - vRenderTargetOffset;
   
   // adjust our vertices to match d3d9's pixel coordinate system
   // which has pixel centers at integer locations
   outp.vPosition.xy -= 0.5;
   
   outp.vPosition = mul(mProjection, outp.vPosition);
-  
-  // We use 4 component floats to uniquely describe a rectangle, by the structure
-  // of x, y, width, height. This allows us to easily generate the 4 corners
-  // of any rectangle from the 4 corners of the 0,0-1,1 quad that we use as the
-  // stream source for our LayerQuad vertex shader. We do this by doing:
-  // Xout = x + Xin * width
-  // Yout = y + Yin * height
-  float2 position = vTextureCoords.xy;
-  float2 size = vTextureCoords.zw;
+
+  position = vTextureCoords.xy;
+  size = vTextureCoords.zw;
   outp.vTexCoords.x = position.x + aVertex.vPosition.x * size.x;
   outp.vTexCoords.y = position.y + aVertex.vPosition.y * size.y;
+
   return outp;
 }
 
 float4 RGBAShader(const VS_OUTPUT aVertex) : COLOR
 {
   return tex2D(s2D, aVertex.vTexCoords) * fLayerOpacity;
 }
 
--- a/gfx/layers/d3d9/ThebesLayerD3D9.cpp
+++ b/gfx/layers/d3d9/ThebesLayerD3D9.cpp
@@ -220,26 +220,17 @@ ThebesLayerD3D9::RenderLayer()
     nsIntRegion region;
     region.Sub(mVisibleRegion, mValidRegion);
 
     DrawRegion(region);
 
     mValidRegion = mVisibleRegion;
   }
 
-  float quadTransform[4][4];
-  /*
-   * Matrix to transform the <0.0,0.0>, <1.0,1.0> quad to the correct position
-   * and size.
-   */
-  memset(&quadTransform, 0, sizeof(quadTransform));
-  quadTransform[2][2] = 1.0f;
-  quadTransform[3][3] = 1.0f;
-
-  device()->SetVertexShaderConstantF(4, &mTransform._11, 4);
+  device()->SetVertexShaderConstantF(CBmLayerTransform, &mTransform._11, 4);
 
   float opacity[4];
   /*
    * We always upload a 4 component float, but the shader will use only the
    * first component since it's declared as a 'float'.
    */
   opacity[0] = GetOpacity();
   device()->SetPixelShaderConstantF(0, opacity, 1);
@@ -252,32 +243,37 @@ ThebesLayerD3D9::RenderLayer()
   mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
 
   device()->SetTexture(0, mTexture);
 
   nsIntRegionRectIterator iter(mVisibleRegion);
 
   const nsIntRect *iterRect;
   while ((iterRect = iter.Next())) {
-    quadTransform[0][0] = (float)iterRect->width;
-    quadTransform[1][1] = (float)iterRect->height;
-    quadTransform[3][0] = (float)iterRect->x;
-    quadTransform[3][1] = (float)iterRect->y;
-    
-    device()->SetVertexShaderConstantF(0, &quadTransform[0][0], 4);
-    device()->SetVertexShaderConstantF(13, ShaderConstantRect(
+    device()->SetVertexShaderConstantF(CBvLayerQuad,
+                                       ShaderConstantRect(iterRect->x,
+                                                          iterRect->y,
+                                                          iterRect->width,
+                                                          iterRect->height),
+                                       1);
+
+    device()->SetVertexShaderConstantF(CBvTextureCoords,
+      ShaderConstantRect(
         (float)(iterRect->x - visibleRect.x) / (float)visibleRect.width,
         (float)(iterRect->y - visibleRect.y) / (float)visibleRect.height,
         (float)iterRect->width / (float)visibleRect.width,
         (float)iterRect->height / (float)visibleRect.height), 1);
+
     device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
   }
 
   // Set back to default.
-  device()->SetVertexShaderConstantF(13, ShaderConstantRect(0, 0, 1.0f, 1.0f), 1);
+  device()->SetVertexShaderConstantF(CBvTextureCoords,
+                                     ShaderConstantRect(0, 0, 1.0f, 1.0f),
+                                     1);
 }
 
 void
 ThebesLayerD3D9::CleanResources()
 {
   mTexture = nsnull;
 }