1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef sw_TextureStage_hpp
16 #define sw_TextureStage_hpp
17 
18 #include "Common/Types.hpp"
19 #include "Common/Math.hpp"
20 #include "Renderer/Color.hpp"
21 
22 namespace sw
23 {
24 	class Sampler;
25 	class PixelRoutine;
26 	class Context;
27 
28 	class TextureStage
29 	{
30 		friend class Context;   // FIXME
31 
32 	public:
33 		enum StageOperation
34 		{
35 			STAGE_DISABLE,
36 			STAGE_SELECTARG1,
37 			STAGE_SELECTARG2,
38 			STAGE_SELECTARG3,
39 			STAGE_MODULATE,
40 			STAGE_MODULATE2X,
41 			STAGE_MODULATE4X,
42 			STAGE_ADD,
43 			STAGE_ADDSIGNED,
44 			STAGE_ADDSIGNED2X,
45 			STAGE_SUBTRACT,
46 			STAGE_ADDSMOOTH,
47 			STAGE_MULTIPLYADD,
48 			STAGE_LERP,
49 			STAGE_DOT3,
50 			STAGE_BLENDCURRENTALPHA,
51 			STAGE_BLENDDIFFUSEALPHA,
52 			STAGE_BLENDFACTORALPHA,
53 			STAGE_BLENDTEXTUREALPHA,
54 			STAGE_BLENDTEXTUREALPHAPM,
55 			STAGE_PREMODULATE,
56 			STAGE_MODULATEALPHA_ADDCOLOR,
57 			STAGE_MODULATECOLOR_ADDALPHA,
58 			STAGE_MODULATEINVALPHA_ADDCOLOR,
59 			STAGE_MODULATEINVCOLOR_ADDALPHA,
60 			STAGE_BUMPENVMAP,
61 			STAGE_BUMPENVMAPLUMINANCE,
62 
63 			STAGE_LAST = STAGE_BUMPENVMAPLUMINANCE
64 		};
65 
66 		enum SourceArgument
67 		{
68 			SOURCE_TEXTURE,
69 			SOURCE_CONSTANT,
70 			SOURCE_CURRENT,
71 			SOURCE_DIFFUSE,
72 			SOURCE_SPECULAR,
73 			SOURCE_TEMP,
74 			SOURCE_TFACTOR,
75 
76 			SOURCE_LAST = SOURCE_TFACTOR
77 		};
78 
79 		enum DestinationArgument
80 		{
81 			DESTINATION_CURRENT,
82 			DESTINATION_TEMP,
83 
84 			DESTINATION_LAST = DESTINATION_TEMP
85 		};
86 
87 		enum ArgumentModifier
88 		{
89 			MODIFIER_COLOR,
90 			MODIFIER_INVCOLOR,
91 			MODIFIER_ALPHA,
92 			MODIFIER_INVALPHA,
93 
94 			MODIFIER_LAST = MODIFIER_INVALPHA
95 		};
96 
97 		struct State
98 		{
99 			State();
100 
101 			unsigned int stageOperation			: BITS(STAGE_LAST);
102 			unsigned int firstArgument			: BITS(SOURCE_LAST);
103 			unsigned int secondArgument			: BITS(SOURCE_LAST);
104 			unsigned int thirdArgument			: BITS(SOURCE_LAST);
105 			unsigned int stageOperationAlpha	: BITS(STAGE_LAST);
106 			unsigned int firstArgumentAlpha		: BITS(SOURCE_LAST);
107 			unsigned int secondArgumentAlpha	: BITS(SOURCE_LAST);
108 			unsigned int thirdArgumentAlpha		: BITS(SOURCE_LAST);
109 			unsigned int firstModifier			: BITS(MODIFIER_LAST);
110 			unsigned int secondModifier			: BITS(MODIFIER_LAST);
111 			unsigned int thirdModifier			: BITS(MODIFIER_LAST);
112 			unsigned int firstModifierAlpha		: BITS(MODIFIER_LAST);
113 			unsigned int secondModifierAlpha	: BITS(MODIFIER_LAST);
114 			unsigned int thirdModifierAlpha		: BITS(MODIFIER_LAST);
115 			unsigned int destinationArgument	: BITS(DESTINATION_LAST);
116 			unsigned int texCoordIndex			: BITS(7);
117 
118 			unsigned int cantUnderflow			: 1;
119 			unsigned int usesTexture			: 1;
120 		};
121 
122 		struct Uniforms
123 		{
124 			word4 constantColor4[4];
125 			float4 bumpmapMatrix4F[2][2];
126 			word4 bumpmapMatrix4W[2][2];
127 			word4 luminanceScale4;
128 			word4 luminanceOffset4;
129 		};
130 
131 		TextureStage();
132 
133 		~TextureStage();
134 
135 		void init(int stage, const Sampler *sampler, const TextureStage *previousStage);
136 
137 		State textureStageState() const;
138 
139 		void setConstantColor(const Color<float> &constantColor);
140 		void setBumpmapMatrix(int element, float value);
141 		void setLuminanceScale(float value);
142 		void setLuminanceOffset(float value);
143 
144 		void setTexCoordIndex(unsigned int texCoordIndex);
145 		void setStageOperation(StageOperation stageOperation);
146 		void setFirstArgument(SourceArgument firstArgument);
147 		void setSecondArgument(SourceArgument secondArgument);
148 		void setThirdArgument(SourceArgument thirdArgument);
149 		void setStageOperationAlpha(StageOperation stageOperationAlpha);
150 		void setFirstArgumentAlpha(SourceArgument firstArgumentAlpha);
151 		void setSecondArgumentAlpha(SourceArgument secondArgumentAlpha);
152 		void setThirdArgumentAlpha(SourceArgument thirdArgumentAlpha);
153 		void setFirstModifier(ArgumentModifier firstModifier);
154 		void setSecondModifier(ArgumentModifier secondModifier);
155 		void setThirdModifier(ArgumentModifier thirdModifier);
156 		void setFirstModifierAlpha(ArgumentModifier firstModifierAlpha);
157 		void setSecondModifierAlpha(ArgumentModifier secondModifierAlpha);
158 		void setThirdModifierAlpha(ArgumentModifier thirdModifierAlpha);
159 		void setDestinationArgument(DestinationArgument destinationArgument);
160 
161 		Uniforms uniforms;   // FIXME: Private
162 
163 	private:
164 		bool usesColor(SourceArgument source) const;
165 		bool usesAlpha(SourceArgument source) const;
166 		bool uses(SourceArgument source) const;
167 		bool usesCurrent() const;
168 		bool usesDiffuse() const;
169 		bool usesSpecular() const;
170 		bool usesTexture() const;
171 		bool isStageDisabled() const;
172 		bool writesCurrent() const;
173 
174 		int stage;
175 
176 		StageOperation stageOperation;
177 		SourceArgument firstArgument;
178 		SourceArgument secondArgument;
179 		SourceArgument thirdArgument;
180 		StageOperation stageOperationAlpha;
181 		SourceArgument firstArgumentAlpha;
182 		SourceArgument secondArgumentAlpha;
183 		SourceArgument thirdArgumentAlpha;
184 		ArgumentModifier firstModifier;
185 		ArgumentModifier secondModifier;
186 		ArgumentModifier thirdModifier;
187 		ArgumentModifier firstModifierAlpha;
188 		ArgumentModifier secondModifierAlpha;
189 		ArgumentModifier thirdModifierAlpha;
190 		DestinationArgument destinationArgument;
191 
192 		int texCoordIndex;
193 		const Sampler *sampler;
194 		const TextureStage *previousStage;
195 	};
196 }
197 
198 #endif  // sw_TextureStage_hpp
199