1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_SHADERDEFINITIONSMLGPU_H
8 #define MOZILLA_GFX_SHADERDEFINITIONSMLGPU_H
10 #include "mozilla/gfx/Point.h"
11 #include "mozilla/gfx/Triangle.h"
12 #include "mozilla/gfx/Types.h"
13 #include "mozilla/layers/LayersHelpers.h"
20 class ShaderRenderPass
;
24 // These may need to move into run-time values determined by MLGDevice.
25 static const size_t kConstantBufferElementSize
= 16;
26 static const size_t kMaxConstantBufferSize
= 4096 * kConstantBufferElementSize
;
28 // Vertex shader slots. We reverse the first two slots across all shaders,
29 // and the first three slots free across all RenderPass shaders, for
31 static const uint32_t kWorldConstantBufferSlot
= 0;
32 static const uint32_t kLayerBufferSlot
= 1;
33 static const uint32_t kMaskBufferSlot
= 3;
34 static const uint32_t kBlendConstantBufferSlot
= 4;
35 static const uint32_t kClearConstantBufferSlot
= 2;
37 // This is specified in common-ps.hlsl.
38 static const uint32_t kMaskLayerTextureSlot
= 4;
39 static const uint32_t kDefaultSamplerSlot
= 0;
40 static const uint32_t kMaskSamplerSlot
= 1;
42 // These are the maximum slot numbers we bind. We assert that no binding
43 // happens above the max slot, since we try to clear buffer bindings at
44 // the end of each frame.
45 static const uint32_t kMaxVertexShaderConstantBuffers
= 5;
46 static const uint32_t kMaxPixelShaderConstantBuffers
= 3;
48 // Maximum depth in the depth buffer. This must match common-vs.hlsl.
49 static const int32_t kDepthLimit
= 1000000;
51 struct WorldConstants
{
52 float projection
[4][4];
53 gfx::Point targetOffset
;
55 unsigned debugFrameNumber
;
58 struct ClearConstants
{
59 explicit ClearConstants(int aDepth
) : depth(aDepth
) {}
64 struct LayerConstants
{
65 float transform
[4][4];
71 struct MaskCombineInput
{
75 struct MaskInformation
{
76 MaskInformation(float aOpacity
, bool aHasMask
)
77 : opacity(aOpacity
), hasMask(aHasMask
? 1 : 0) {}
83 struct YCbCrShaderConstants
{
84 float yuvColorMatrix
[3][4];
87 struct YCbCrColorDepthConstants
{
92 struct BlendVertexShaderConstants
{
93 float backdropTransform
[4][4];
97 static inline nsTArray
<gfx::IntRect
> ToRectArray(const T
& aRegion
) {
98 nsTArray
<gfx::IntRect
> rects
;
99 for (auto iter
= aRegion
.RectIter(); !iter
.Done(); iter
.Next()) {
100 rects
.AppendElement(iter
.Get().ToUnknownRect());
105 struct SimpleTraits
{
106 SimpleTraits(const ItemInfo
& aItem
, const gfx::Rect
& aRect
)
107 : mItem(aItem
), mRect(aRect
) {}
109 // Helper nonce structs so functions can break vertex data up by each
110 // triangle in a quad, or return vertex info for a unit quad.
111 struct AnyTriangle
{};
112 struct FirstTriangle
: AnyTriangle
{};
113 struct SecondTriangle
: AnyTriangle
{};
116 // This is the base vertex layout used by all unit quad shaders.
117 struct UnitQuadVertex
{
123 // This is the base vertex layout used by all unit triangle shaders.
124 struct TriangleVertices
{
125 gfx::Point p1
, p2
, p3
;
130 // Helper functions for populating a TriangleVertices. The first two use mRect
131 // to generate triangles, the third function uses coordinates from an already
132 // computed triangle.
133 TriangleVertices
MakeVertex(const FirstTriangle
& aIgnore
) const;
134 TriangleVertices
MakeVertex(const SecondTriangle
& aIgnore
) const;
135 TriangleVertices
MakeVertex(const gfx::Triangle
& aTriangle
) const;
137 UnitQuadVertex
MakeUnitQuadVertex() const;
139 // This default GenerateTriangles only computes the 3 points of each triangle
140 // in the polygon. If needed, shaders can override this and return a more
141 // complex triangle, to encode dependent information in extended vertex data.
143 // AddShaderVertices will deduce this return type. It should be an nsTArray<T>
144 // where T inherits from Triangle.
145 nsTArray
<gfx::Triangle
> GenerateTriangles(const gfx::Polygon
& aPolygon
) const;
148 const Maybe
<gfx::Polygon
>& geometry() const;
149 const gfx::Rect
& rect() const { return mRect
; }
151 const ItemInfo
& mItem
;
155 struct ColorTraits
: public SimpleTraits
{
156 ColorTraits(const ItemInfo
& aItem
, const gfx::Rect
& aRect
,
157 const gfx::DeviceColor
& aColor
)
158 : SimpleTraits(aItem
, aRect
), mColor(aColor
) {}
160 // Color data is the same across all vertex types.
161 template <typename VertexType
>
162 const gfx::DeviceColor
& MakeVertexData(const VertexType
& aIgnore
) const {
166 gfx::DeviceColor mColor
;
169 struct TexturedTraits
: public SimpleTraits
{
170 TexturedTraits(const ItemInfo
& aItem
, const gfx::Rect
& aRect
,
171 const gfx::Rect
& aTexCoords
)
172 : SimpleTraits(aItem
, aRect
), mTexCoords(aTexCoords
) {}
174 // Textured triangles need to compute a texture coordinate for each vertex.
175 nsTArray
<gfx::TexturedTriangle
> GenerateTriangles(
176 const gfx::Polygon
& aPolygon
) const;
179 gfx::Point p1
, p2
, p3
;
181 VertexData
MakeVertexData(const FirstTriangle
& aIgnore
) const;
182 VertexData
MakeVertexData(const SecondTriangle
& aIgnore
) const;
183 VertexData
MakeVertexData(const gfx::TexturedTriangle
& aTriangle
) const;
184 const gfx::Rect
& MakeVertexData(const UnitQuad
& aIgnore
) const {
188 gfx::Rect mTexCoords
;
192 } // namespace layers
193 } // namespace mozilla