3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Reference Renderer
5 * -----------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Shader interfaces.
24 *//*--------------------------------------------------------------------*/
27 #include "rrVertexAttrib.hpp"
28 #include "rrVertexPacket.hpp"
29 #include "rrFragmentPacket.hpp"
30 #include "rrPrimitivePacket.hpp"
31 #include "rrShadingContext.hpp"
37 /*--------------------------------------------------------------------*//*!
38 * \brief Vertex shader input information
39 *//*--------------------------------------------------------------------*/
40 struct VertexInputInfo
42 VertexInputInfo (void)
45 type = GENERICVECTYPE_LAST;
51 /*--------------------------------------------------------------------*//*!
52 * \brief Shader varying information
53 *//*--------------------------------------------------------------------*/
54 struct VertexVaryingInfo
56 VertexVaryingInfo (void)
59 type = GENERICVECTYPE_LAST;
63 // \note used by std::vector<T>::operator==() const
64 bool operator== (const VertexVaryingInfo& other) const
66 return type == other.type &&
67 flatshade == other.flatshade;
74 typedef VertexVaryingInfo VertexOutputInfo;
75 typedef VertexVaryingInfo FragmentInputInfo;
76 typedef VertexVaryingInfo GeometryInputInfo;
77 typedef VertexVaryingInfo GeometryOutputInfo;
79 /*--------------------------------------------------------------------*//*!
80 * \brief Fragment shader output information
81 *//*--------------------------------------------------------------------*/
82 struct FragmentOutputInfo
84 FragmentOutputInfo (void)
87 type = GENERICVECTYPE_LAST;
93 /*--------------------------------------------------------------------*//*!
94 * \brief Vertex shader interface
96 * Vertex shaders execute shading for set of vertex packets. See VertexPacket
97 * documentation for more details on shading API.
98 *//*--------------------------------------------------------------------*/
102 VertexShader (size_t numInputs, size_t numOutputs) : m_inputs(numInputs), m_outputs(numOutputs) {}
104 virtual void shadeVertices (const VertexAttrib* inputs, VertexPacket* const* packets, const int numPackets) const = 0;
106 const std::vector<VertexInputInfo>& getInputs (void) const { return m_inputs; }
107 const std::vector<VertexOutputInfo>& getOutputs (void) const { return m_outputs; }
110 ~VertexShader (void) {}; // \note Renderer will not delete any objects passed in.
112 std::vector<VertexInputInfo> m_inputs;
113 std::vector<VertexOutputInfo> m_outputs;
114 } DE_WARN_UNUSED_TYPE;
116 /*--------------------------------------------------------------------*//*!
117 * \brief Fragment shader interface
119 * Fragment shader executes shading for list of fragment packets. See
120 * FragmentPacket documentation for more details on shading API.
121 *//*--------------------------------------------------------------------*/
125 FragmentShader (size_t numInputs, size_t numOutputs) : m_inputs(numInputs), m_outputs(numOutputs) {}
127 const std::vector<FragmentInputInfo>& getInputs (void) const { return m_inputs; }
128 const std::vector<FragmentOutputInfo>& getOutputs (void) const { return m_outputs; }
130 virtual void shadeFragments (FragmentPacket* packets, const int numPackets, const FragmentShadingContext& context) const = 0; // \note numPackets must be greater than zero.
133 ~FragmentShader (void) {}; // \note Renderer will not delete any objects passed in.
135 std::vector<FragmentInputInfo> m_inputs;
136 std::vector<FragmentOutputInfo> m_outputs;
137 } DE_WARN_UNUSED_TYPE;
139 /*--------------------------------------------------------------------*//*!
140 * \brief Geometry shader input primitive type
141 *//*--------------------------------------------------------------------*/
142 enum GeometryShaderInputType
144 GEOMETRYSHADERINPUTTYPE_POINTS = 0,
145 GEOMETRYSHADERINPUTTYPE_LINES,
146 GEOMETRYSHADERINPUTTYPE_LINES_ADJACENCY,
147 GEOMETRYSHADERINPUTTYPE_TRIANGLES,
148 GEOMETRYSHADERINPUTTYPE_TRIANGLES_ADJACENCY,
150 GEOMETRYSHADERINPUTTYPE_LAST
153 /*--------------------------------------------------------------------*//*!
154 * \brief Geometry shader output primitive type
155 *//*--------------------------------------------------------------------*/
156 enum GeometryShaderOutputType
158 GEOMETRYSHADEROUTPUTTYPE_POINTS = 0,
159 GEOMETRYSHADEROUTPUTTYPE_LINE_STRIP,
160 GEOMETRYSHADEROUTPUTTYPE_TRIANGLE_STRIP,
162 GEOMETRYSHADEROUTPUTTYPE_LAST
165 /*--------------------------------------------------------------------*//*!
166 * \brief Geometry shader interface
168 * Geometry shader executes a list of primitive packets and outputs
169 * a new set of vertex packets for new primitives.
170 *//*--------------------------------------------------------------------*/
174 GeometryShader (size_t numVaryingInputs,
175 size_t numVaryingOutputs,
176 GeometryShaderInputType inputType,
177 GeometryShaderOutputType outputType,
178 size_t numVerticesOut,
179 size_t numInvocations);
181 virtual void shadePrimitives (GeometryEmitter& output, int verticesIn, const PrimitivePacket* packets, const int numPackets, int invocationID) const = 0;
183 const std::vector<GeometryInputInfo>& getInputs (void) const { return m_inputs; }
184 const std::vector<GeometryOutputInfo>& getOutputs (void) const { return m_outputs; }
185 inline GeometryShaderInputType getInputType (void) const { return m_inputType; }
186 inline GeometryShaderOutputType getOutputType (void) const { return m_outputType; }
187 inline size_t getNumVerticesOut (void) const { return m_numVerticesOut; }
188 inline size_t getNumInvocations (void) const { return m_numInvocations; }
191 const GeometryShaderInputType m_inputType;
192 const GeometryShaderOutputType m_outputType;
193 const size_t m_numVerticesOut;
194 const size_t m_numInvocations;
196 std::vector<GeometryInputInfo> m_inputs;
197 std::vector<GeometryOutputInfo> m_outputs;
198 } DE_WARN_UNUSED_TYPE;
200 // Helpers for shader implementations.
202 template<class Shader>
203 class VertexShaderLoop : public VertexShader
206 VertexShaderLoop (const Shader& shader) : m_shader(shader) {}
208 void shadeVertices (const VertexAttrib* inputs, VertexPacket* packets, const int numPackets) const;
211 const Shader& m_shader;
214 template<class Shader>
215 void VertexShaderLoop<Shader>::shadeVertices (const VertexAttrib* inputs, VertexPacket* packets, const int numPackets) const
217 for (int ndx = 0; ndx < numPackets; ndx++)
218 m_shader.shadeVertex(inputs, packets[ndx]);
221 template<class Shader>
222 class FragmentShaderLoop : public FragmentShader
225 FragmentShaderLoop (const Shader& shader) : m_shader(shader) {}
227 void shadeFragments (FragmentPacket* packets, const int numPackets) const;
230 const Shader& m_shader;
233 template<class Shader>
234 void FragmentShaderLoop<Shader>::shadeFragments (FragmentPacket* packets, const int numPackets) const
236 for (int ndx = 0; ndx < numPackets; ndx++)
237 m_shader.shadeFragment(packets[ndx]);
242 #endif // _RRSHADERS_HPP