Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_SHADERDEFINITIONSMLGPU_H
7 : #define MOZILLA_GFX_SHADERDEFINITIONSMLGPU_H
8 :
9 : #include "mozilla/gfx/Point.h"
10 : #include "mozilla/gfx/Triangle.h"
11 : #include "mozilla/gfx/Types.h"
12 : #include "mozilla/layers/LayersHelpers.h"
13 : #include "nsTArray.h"
14 :
15 : namespace mozilla {
16 : namespace layers {
17 :
18 : struct ItemInfo;
19 : class ShaderRenderPass;
20 :
21 : namespace mlg {
22 :
23 : // These may need to move into run-time values determined by MLGDevice.
24 : static const size_t kConstantBufferElementSize = 16;
25 : static const size_t kMaxConstantBufferSize = 4096 * kConstantBufferElementSize;
26 :
27 : // Vertex shader slots. We reverse the first two slots across all shaders,
28 : // and the first three slots free across all RenderPass shaders, for
29 : // uniformity.
30 : static const uint32_t kWorldConstantBufferSlot = 0;
31 : static const uint32_t kLayerBufferSlot = 1;
32 : static const uint32_t kMaskBufferSlot = 3;
33 : static const uint32_t kBlendConstantBufferSlot = 4;
34 : static const uint32_t kClearConstantBufferSlot = 2;
35 :
36 : // This is specified in common-ps.hlsl.
37 : static const uint32_t kMaskLayerTextureSlot = 4;
38 : static const uint32_t kDefaultSamplerSlot = 0;
39 : static const uint32_t kMaskSamplerSlot = 1;
40 :
41 : // These are the maximum slot numbers we bind. We assert that no binding
42 : // happens above the max slot, since we try to clear buffer bindings at
43 : // the end of each frame.
44 : static const uint32_t kMaxVertexShaderConstantBuffers = 5;
45 : static const uint32_t kMaxPixelShaderConstantBuffers = 2;
46 :
47 : // Maximum depth in the depth buffer. This must match common-vs.hlsl.
48 : static const int32_t kDepthLimit = 1000000;
49 :
50 0 : struct WorldConstants
51 : {
52 : float projection[4][4];
53 : gfx::Point targetOffset;
54 : int sortIndexOffset;
55 : float padding;
56 : };
57 :
58 : struct ClearConstants
59 : {
60 0 : explicit ClearConstants(int aDepth) : depth(aDepth)
61 0 : {}
62 : int depth;
63 : int padding[3];
64 : };
65 :
66 0 : struct LayerConstants
67 : {
68 : float transform[4][4];
69 : gfx::Rect clipRect;
70 : uint32_t maskIndex;
71 : uint32_t padding[3];
72 : };
73 :
74 : struct MaskCombineInput
75 : {
76 : float texCoords[4];
77 : };
78 :
79 : struct MaskInformation
80 : {
81 0 : MaskInformation(float aOpacity, bool aHasMask)
82 0 : : opacity(aOpacity),
83 0 : hasMask(aHasMask ? 1 : 0)
84 0 : {}
85 : float opacity;
86 : uint32_t hasMask;
87 : uint32_t padding[2];
88 : };
89 :
90 : struct YCbCrShaderConstants {
91 : float yuvColorMatrix[3][4];
92 : };
93 :
94 : struct BlendVertexShaderConstants {
95 : float backdropTransform[4][4];
96 : };
97 :
98 : template <typename T>
99 : static inline nsTArray<gfx::IntRect>
100 0 : ToRectArray(const T& aRegion)
101 : {
102 0 : nsTArray<gfx::IntRect> rects;
103 0 : for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
104 0 : rects.AppendElement(iter.Get().ToUnknownRect());
105 : }
106 0 : return rects;
107 : }
108 :
109 : struct SimpleTraits
110 : {
111 0 : explicit SimpleTraits(const ItemInfo& aItem, const gfx::Rect& aRect)
112 0 : : mItem(aItem),
113 0 : mRect(aRect)
114 0 : {}
115 :
116 : // Helper nonce structs so functions can break vertex data up by each
117 : // triangle in a quad, or return vertex info for a unit quad.
118 : struct AnyTriangle { };
119 : struct FirstTriangle : AnyTriangle { };
120 : struct SecondTriangle : AnyTriangle { };
121 : struct UnitQuad { };
122 :
123 : // This is the base vertex layout used by all unit quad shaders.
124 : struct UnitQuadVertex {
125 : gfx::Rect rect;
126 : uint32_t layerIndex;
127 : int depth;
128 : };
129 :
130 : // This is the base vertex layout used by all unit triangle shaders.
131 : struct TriangleVertices {
132 : gfx::Point p1, p2, p3;
133 : uint32_t layerIndex;
134 : int depth;
135 : };
136 :
137 : // Helper functions for populating a TriangleVertices. The first two use mRect
138 : // to generate triangles, the third function uses coordinates from an already
139 : // computed triangle.
140 : TriangleVertices MakeVertex(const FirstTriangle& aIgnore) const;
141 : TriangleVertices MakeVertex(const SecondTriangle& aIgnore) const;
142 : TriangleVertices MakeVertex(const gfx::Triangle& aTriangle) const;
143 :
144 : UnitQuadVertex MakeUnitQuadVertex() const;
145 :
146 : // This default GenerateTriangles only computes the 3 points of each triangle
147 : // in the polygon. If needed, shaders can override this and return a more
148 : // complex triangle, to encode dependent information in extended vertex data.
149 : //
150 : // AddShaderVertices will deduce this return type. It should be an nsTArray<T>
151 : // where T inherits from Triangle.
152 : nsTArray<gfx::Triangle> GenerateTriangles(const gfx::Polygon& aPolygon) const;
153 :
154 : // Accessors.
155 : const Maybe<gfx::Polygon>& geometry() const;
156 0 : const gfx::Rect& rect() const {
157 0 : return mRect;
158 : }
159 :
160 : const ItemInfo& mItem;
161 : gfx::Rect mRect;
162 : };
163 :
164 : struct ColorTraits : public SimpleTraits
165 : {
166 0 : ColorTraits(const ItemInfo& aItem, const gfx::Rect& aRect, const gfx::Color& aColor)
167 0 : : SimpleTraits(aItem, aRect), mColor(aColor)
168 0 : {}
169 :
170 : // Color data is the same across all vertex types.
171 : template <typename VertexType>
172 0 : const gfx::Color& MakeVertexData(const VertexType& aIgnore) const {
173 0 : return mColor;
174 : }
175 :
176 : gfx::Color mColor;
177 : };
178 :
179 : struct TexturedTraits : public SimpleTraits
180 : {
181 0 : TexturedTraits(const ItemInfo& aItem, const gfx::Rect& aRect, const gfx::Rect& aTexCoords)
182 0 : : SimpleTraits(aItem, aRect), mTexCoords(aTexCoords)
183 0 : {}
184 :
185 : // Textured triangles need to compute a texture coordinate for each vertex.
186 : nsTArray<gfx::TexturedTriangle> GenerateTriangles(const gfx::Polygon& aPolygon) const;
187 :
188 : struct VertexData {
189 : gfx::Point p1, p2, p3;
190 : };
191 : VertexData MakeVertexData(const FirstTriangle& aIgnore) const;
192 : VertexData MakeVertexData(const SecondTriangle& aIgnore) const;
193 : VertexData MakeVertexData(const gfx::TexturedTriangle& aTriangle) const;
194 0 : const gfx::Rect& MakeVertexData(const UnitQuad& aIgnore) const {
195 0 : return mTexCoords;
196 : }
197 :
198 : gfx::Rect mTexCoords;
199 : };
200 :
201 : } // namespace mlg
202 : } // namespace layers
203 : } // namespace mozilla
204 :
205 : #endif
|