swr/rast: allow early-z if shader uses depth value
[mesa.git] / src / gallium / drivers / swr / rasterizer / core / rasterizer.h
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file rasterizer.h
24 *
25 * @brief Definitions for the rasterizer.
26 *
27 ******************************************************************************/
28 #pragma once
29
30 #include "context.h"
31 #include <type_traits>
32 #include "conservativeRast.h"
33 #include "multisample.h"
34
35 void RasterizeLine(DRAW_CONTEXT *pDC, uint32_t workerId, uint32_t macroTile, void *pData);
36 void RasterizeSimplePoint(DRAW_CONTEXT *pDC, uint32_t workerId, uint32_t macroTile, void *pData);
37 void RasterizeTriPoint(DRAW_CONTEXT *pDC, uint32_t workerId, uint32_t macroTile, void *pData);
38
39 INLINE
40 __m128i fpToFixedPoint(const __m128 vIn)
41 {
42 __m128 vFixed = _mm_mul_ps(vIn, _mm_set1_ps(FIXED_POINT_SCALE));
43 return _mm_cvtps_epi32(vFixed);
44 }
45
46 // Selector for correct templated RasterizeTriangle function
47 PFN_WORK_FUNC GetRasterizerFunc(
48 uint32_t numSamples,
49 bool IsCenter,
50 bool IsConservative,
51 uint32_t InputCoverage,
52 uint32_t EdgeEnable,
53 bool RasterizeScissorEdges);
54
55 enum TriEdgesStates
56 {
57 STATE_NO_VALID_EDGES = 0,
58 STATE_E0_E1_VALID,
59 STATE_E0_E2_VALID,
60 STATE_E1_E2_VALID,
61 STATE_ALL_EDGES_VALID,
62 STATE_VALID_TRI_EDGE_COUNT,
63 };
64
65 enum TriEdgesValues
66 {
67 NO_VALID_EDGES = 0,
68 E0_E1_VALID = 0x3,
69 E0_E2_VALID = 0x5,
70 E1_E2_VALID = 0x6,
71 ALL_EDGES_VALID = 0x7,
72 VALID_TRI_EDGE_COUNT,
73 };
74
75 //////////////////////////////////////////////////////////////////////////
76 /// @brief ValidTriEdges convenience typedefs used for templated function
77 /// specialization supported Fixed Point precisions
78 typedef std::integral_constant<uint32_t, ALL_EDGES_VALID> AllEdgesValidT;
79 typedef std::integral_constant<uint32_t, E0_E1_VALID> E0E1ValidT;
80 typedef std::integral_constant<uint32_t, E0_E2_VALID> E0E2ValidT;
81 typedef std::integral_constant<uint32_t, E1_E2_VALID> E1E2ValidT;
82 typedef std::integral_constant<uint32_t, NO_VALID_EDGES> NoEdgesValidT;
83
84 typedef std::integral_constant<uint32_t, STATE_ALL_EDGES_VALID> StateAllEdgesValidT;
85 typedef std::integral_constant<uint32_t, STATE_E0_E1_VALID> StateE0E1ValidT;
86 typedef std::integral_constant<uint32_t, STATE_E0_E2_VALID> StateE0E2ValidT;
87 typedef std::integral_constant<uint32_t, STATE_E1_E2_VALID> StateE1E2ValidT;
88 typedef std::integral_constant<uint32_t, STATE_NO_VALID_EDGES> StateNoEdgesValidT;
89
90 // some specializations to convert from edge state to edge bitmask values
91 template <typename EdgeMask>
92 struct EdgeMaskVal
93 {
94 static_assert(EdgeMask::value > STATE_ALL_EDGES_VALID, "Primary EdgeMaskVal shouldn't be instantiated");
95 };
96
97 template <>
98 struct EdgeMaskVal<StateAllEdgesValidT>
99 {
100 typedef AllEdgesValidT T;
101 };
102
103 template <>
104 struct EdgeMaskVal<StateE0E1ValidT>
105 {
106 typedef E0E1ValidT T;
107 };
108
109 template <>
110 struct EdgeMaskVal<StateE0E2ValidT>
111 {
112 typedef E0E2ValidT T;
113 };
114
115 template <>
116 struct EdgeMaskVal<StateE1E2ValidT>
117 {
118 typedef E1E2ValidT T;
119 };
120
121 template <>
122 struct EdgeMaskVal<StateNoEdgesValidT>
123 {
124 typedef NoEdgesValidT T;
125 };
126
127 INLINE uint32_t EdgeValToEdgeState(uint32_t val)
128 {
129 SWR_ASSERT(val < VALID_TRI_EDGE_COUNT, "Unexpected tri edge mask");
130 static const uint32_t edgeValToEdgeState[VALID_TRI_EDGE_COUNT] = { 0, 0, 0, 1, 0, 2, 3, 4 };
131 return edgeValToEdgeState[val];
132 }
133
134 //////////////////////////////////////////////////////////////////////////
135 /// @struct RasterScissorEdgesT
136 /// @brief Primary RasterScissorEdgesT templated struct that holds compile
137 /// time information about the number of edges needed to be rasterized,
138 /// If either the scissor rect or conservative rast is enabled,
139 /// the scissor test is enabled and the rasterizer will test
140 /// 3 triangle edges + 4 scissor edges for coverage.
141 /// @tparam RasterScissorEdgesT: number of multisamples
142 /// @tparam ConservativeT: is this a conservative rasterization
143 /// @tparam EdgeMaskT: Which edges are valid(not degenerate)
144 template <typename RasterScissorEdgesT, typename ConservativeT, typename EdgeMaskT>
145 struct RasterEdgeTraits
146 {
147 typedef std::true_type RasterizeScissorEdgesT;
148 typedef std::integral_constant<uint32_t, 7> NumEdgesT;
149 //typedef std::integral_constant<uint32_t, EdgeMaskT::value> ValidEdgeMaskT;
150 typedef typename EdgeMaskVal<EdgeMaskT>::T ValidEdgeMaskT;
151 };
152
153 //////////////////////////////////////////////////////////////////////////
154 /// @brief specialization of RasterEdgeTraits. If neither scissor rect
155 /// nor conservative rast is enabled, only test 3 triangle edges
156 /// for coverage
157 template <typename EdgeMaskT>
158 struct RasterEdgeTraits<std::false_type, std::false_type, EdgeMaskT>
159 {
160 typedef std::false_type RasterizeScissorEdgesT;
161 typedef std::integral_constant<uint32_t, 3> NumEdgesT;
162 // no need for degenerate edge masking in non-conservative case; rasterize all triangle edges
163 typedef std::integral_constant<uint32_t, ALL_EDGES_VALID> ValidEdgeMaskT;
164 };
165
166 //////////////////////////////////////////////////////////////////////////
167 /// @struct RasterizerTraits
168 /// @brief templated struct that holds compile time information used
169 /// during rasterization. Inherits EdgeTraits and ConservativeRastBETraits.
170 /// @tparam NumSamplesT: number of multisamples
171 /// @tparam ConservativeT: is this a conservative rasterization
172 /// @tparam InputCoverageT: what type of input coverage is the PS expecting?
173 /// (only used with conservative rasterization)
174 /// @tparam RasterScissorEdgesT: do we need to rasterize with a scissor?
175 template <typename NumSamplesT, typename CenterPatternT, typename ConservativeT, typename InputCoverageT, typename EdgeEnableT, typename RasterScissorEdgesT>
176 struct RasterizerTraits final : public ConservativeRastBETraits<ConservativeT, InputCoverageT>,
177 public RasterEdgeTraits<RasterScissorEdgesT, ConservativeT, EdgeEnableT>
178 {
179 typedef MultisampleTraits<static_cast<SWR_MULTISAMPLE_COUNT>(NumSamplesT::value), CenterPatternT::value> MT;
180
181 /// Fixed point precision the rasterizer is using
182 typedef FixedPointTraits<Fixed_16_8> PrecisionT;
183 /// Fixed point precision of the edge tests used during rasterization
184 typedef FixedPointTraits<Fixed_X_16> EdgePrecisionT;
185
186 // If conservative rast or MSAA center pattern is enabled, only need a single sample coverage test, with the result copied to all samples
187 typedef std::integral_constant<int, ConservativeT::value ? 1 : MT::numCoverageSamples> NumCoverageSamplesT;
188
189 static_assert(EdgePrecisionT::BitsT::value >= ConservativeRastBETraits<ConservativeT, InputCoverageT>::ConservativePrecisionT::BitsT::value,
190 "Rasterizer edge fixed point precision < required conservative rast precision");
191
192 /// constants used to offset between different types of raster tiles
193 static const int colorRasterTileStep{(KNOB_TILE_X_DIM * KNOB_TILE_Y_DIM * (FormatTraits<KNOB_COLOR_HOT_TILE_FORMAT>::bpp / 8)) * MT::numSamples};
194 static const int depthRasterTileStep{(KNOB_TILE_X_DIM * KNOB_TILE_Y_DIM * (FormatTraits<KNOB_DEPTH_HOT_TILE_FORMAT>::bpp / 8)) * MT::numSamples};
195 static const int stencilRasterTileStep{(KNOB_TILE_X_DIM * KNOB_TILE_Y_DIM * (FormatTraits<KNOB_STENCIL_HOT_TILE_FORMAT>::bpp / 8)) * MT::numSamples};
196 static const int colorRasterTileRowStep{(KNOB_MACROTILE_X_DIM / KNOB_TILE_X_DIM) * colorRasterTileStep};
197 static const int depthRasterTileRowStep{(KNOB_MACROTILE_X_DIM / KNOB_TILE_X_DIM)* depthRasterTileStep};
198 static const int stencilRasterTileRowStep{(KNOB_MACROTILE_X_DIM / KNOB_TILE_X_DIM) * stencilRasterTileStep};
199 };