Bug 1632310 [wpt PR 23186] - Add test for computed versus resolved style., a=testonly
[gecko.git] / gfx / layers / mlgpu / ShaderDefinitionsMLGPU.h
blob9c3d490a281f12b0650d7ca5542c1f889bf646cc
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"
14 #include "nsTArray.h"
16 namespace mozilla {
17 namespace layers {
19 struct ItemInfo;
20 class ShaderRenderPass;
22 namespace mlg {
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
30 // uniformity.
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;
54 int sortIndexOffset;
55 unsigned debugFrameNumber;
58 struct ClearConstants {
59 explicit ClearConstants(int aDepth) : depth(aDepth) {}
60 int depth;
61 int padding[3];
64 struct LayerConstants {
65 float transform[4][4];
66 gfx::Rect clipRect;
67 uint32_t maskIndex;
68 uint32_t padding[3];
71 struct MaskCombineInput {
72 float texCoords[4];
75 struct MaskInformation {
76 MaskInformation(float aOpacity, bool aHasMask)
77 : opacity(aOpacity), hasMask(aHasMask ? 1 : 0) {}
78 float opacity;
79 uint32_t hasMask;
80 uint32_t padding[2];
83 struct YCbCrShaderConstants {
84 float yuvColorMatrix[3][4];
87 struct YCbCrColorDepthConstants {
88 float coefficient;
89 uint32_t padding[3];
92 struct BlendVertexShaderConstants {
93 float backdropTransform[4][4];
96 template <typename T>
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());
102 return rects;
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 {};
114 struct UnitQuad {};
116 // This is the base vertex layout used by all unit quad shaders.
117 struct UnitQuadVertex {
118 gfx::Rect rect;
119 uint32_t layerIndex;
120 int depth;
123 // This is the base vertex layout used by all unit triangle shaders.
124 struct TriangleVertices {
125 gfx::Point p1, p2, p3;
126 uint32_t layerIndex;
127 int depth;
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;
147 // Accessors.
148 const Maybe<gfx::Polygon>& geometry() const;
149 const gfx::Rect& rect() const { return mRect; }
151 const ItemInfo& mItem;
152 gfx::Rect mRect;
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 {
163 return mColor;
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;
178 struct VertexData {
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 {
185 return mTexCoords;
188 gfx::Rect mTexCoords;
191 } // namespace mlg
192 } // namespace layers
193 } // namespace mozilla
195 #endif