blob: df0f0be23b49cbf1765cd410c31381ac6395c654 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#include "GrDrawTarget.h"
19#include "GrGpuVertex.h"
20
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// recursive helper for creating mask with all the tex coord bits set for
22// one stage
23template <int N>
24static int stage_mask_recur(int stage) {
25 return GrDrawTarget::StageTexCoordVertexLayoutBit(stage, N) |
26 stage_mask_recur<N+1>(stage);
27}
reed@google.comd728f6e2011-01-13 20:02:47 +000028template<> // linux build doesn't like static on specializations
29int stage_mask_recur<GrDrawTarget::kNumStages>(int) { return 0; }
reed@google.comac10a2d2010-12-22 21:39:39 +000030
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000031// mask of all tex coord indices for one stage
32static int stage_tex_coord_mask(int stage) {
33 return stage_mask_recur<0>(stage);
reed@google.comac10a2d2010-12-22 21:39:39 +000034}
35
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000036// mask of all bits relevant to one stage
37static int stage_mask(int stage) {
38 return stage_tex_coord_mask(stage) |
39 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(stage);
40}
41
42// recursive helper for creating mask of with all bits set relevant to one
43// texture coordinate index
44template <int N>
45static int tex_coord_mask_recur(int texCoordIdx) {
46 return GrDrawTarget::StageTexCoordVertexLayoutBit(N, texCoordIdx) |
47 tex_coord_mask_recur<N+1>(texCoordIdx);
48}
reed@google.comd728f6e2011-01-13 20:02:47 +000049template<> // linux build doesn't like static on specializations
50int tex_coord_mask_recur<GrDrawTarget::kMaxTexCoords>(int) { return 0; }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000051
52// mask of all bits relevant to one texture coordinate index
53static int tex_coord_idx_mask(int texCoordIdx) {
54 return tex_coord_mask_recur<0>(texCoordIdx);
55}
56
57bool check_layout(GrVertexLayout layout) {
58 // can only have 1 or 0 bits set for each stage.
59 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
60 int stageBits = layout & stage_mask(s);
61 if (stageBits && !GrIsPow2(stageBits)) {
62 return false;
63 }
64 }
65 return true;
66}
67
68size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) {
69 GrAssert(check_layout(vertexLayout));
70
71 size_t vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
72 sizeof(GrGpuTextVertex) :
73 sizeof(GrPoint);
74
75 size_t size = vecSize; // position
76 for (int t = 0; t < kMaxTexCoords; ++t) {
77 if (tex_coord_idx_mask(t) & vertexLayout) {
78 size += vecSize;
79 }
80 }
81 if (vertexLayout & kColor_VertexLayoutBit) {
82 size += sizeof(GrColor);
83 }
84 return size;
85}
86
87int GrDrawTarget::VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout) {
88 GrAssert(check_layout(vertexLayout));
89 if (StagePosAsTexCoordVertexLayoutBit(stage) & vertexLayout) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 return 0;
91 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000092 int tcIdx = VertexTexCoordsForStage(stage, vertexLayout);
93 if (tcIdx >= 0) {
94
95 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
96 sizeof(GrGpuTextVertex) :
97 sizeof(GrPoint);
98 int offset = vecSize; // position
99 // figure out how many tex coordinates are present and precede this one.
100 for (int t = 0; t < tcIdx; ++t) {
101 if (tex_coord_idx_mask(t) & vertexLayout) {
102 offset += vecSize;
103 }
104 }
105 return offset;
106 }
107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 return -1;
109}
110
111int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000112 GrAssert(check_layout(vertexLayout));
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 if (vertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000115 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
116 sizeof(GrGpuTextVertex) :
117 sizeof(GrPoint);
118 int offset = vecSize; // position
119 // figure out how many tex coordinates are present and precede this one.
120 for (int t = 0; t < kMaxTexCoords; ++t) {
121 if (tex_coord_idx_mask(t) & vertexLayout) {
122 offset += vecSize;
123 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000125 return offset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 }
127 return -1;
128}
129
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000130int GrDrawTarget::VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout,
131 int texCoordOffsetsByIdx[kMaxTexCoords],
132 int* colorOffset) {
133 GrAssert(check_layout(vertexLayout));
134
135 GrAssert(NULL != texCoordOffsetsByIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 GrAssert(NULL != colorOffset);
137
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000138 int vecSize = (vertexLayout & kTextFormat_VertexLayoutBit) ?
139 sizeof(GrGpuTextVertex) :
140 sizeof(GrPoint);
141 int size = vecSize; // position
142
143 for (int t = 0; t < kMaxTexCoords; ++t) {
144 if (tex_coord_idx_mask(t) & vertexLayout) {
145 texCoordOffsetsByIdx[t] = size;
146 size += vecSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 } else {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000148 texCoordOffsetsByIdx[t] = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000151 if (kColor_VertexLayoutBit & vertexLayout) {
152 *colorOffset = size;
153 size += sizeof(GrColor);
154 } else {
155 *colorOffset = -1;
156 }
157 return size;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000160int GrDrawTarget::VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout,
161 int texCoordOffsetsByStage[kNumStages],
162 int* colorOffset) {
163 GrAssert(check_layout(vertexLayout));
164
165 GrAssert(NULL != texCoordOffsetsByStage);
166 GrAssert(NULL != colorOffset);
167
168 int texCoordOffsetsByIdx[kMaxTexCoords];
169 int size = VertexSizeAndOffsetsByIdx(vertexLayout,
170 texCoordOffsetsByIdx,
171 colorOffset);
172 for (int s = 0; s < kNumStages; ++s) {
173 int tcIdx;
174 if (StagePosAsTexCoordVertexLayoutBit(s) & vertexLayout) {
175 texCoordOffsetsByStage[s] = 0;
176 } else if ((tcIdx = VertexTexCoordsForStage(s, vertexLayout)) >= 0) {
177 texCoordOffsetsByStage[s] = texCoordOffsetsByIdx[tcIdx];
178 } else {
179 texCoordOffsetsByStage[s] = -1;
180 }
181 }
182 return size;
183}
184
185bool GrDrawTarget::VertexUsesStage(int stage, GrVertexLayout vertexLayout) {
186 GrAssert(stage < kNumStages);
187 GrAssert(check_layout(vertexLayout));
188 return !!(stage_mask(stage) & vertexLayout);
189}
190
191bool GrDrawTarget::VertexUsesTexCoordIdx(int coordIndex,
192 GrVertexLayout vertexLayout) {
193 GrAssert(coordIndex < kMaxTexCoords);
194 GrAssert(check_layout(vertexLayout));
195 return !!(tex_coord_idx_mask(coordIndex) & vertexLayout);
196}
197
198int GrDrawTarget::VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout) {
199 GrAssert(stage < kNumStages);
200 GrAssert(check_layout(vertexLayout));
201 int bit = vertexLayout & stage_tex_coord_mask(stage);
202 if (bit) {
203 // figure out which set of texture coordates is used
204 // bits are ordered T0S0, T0S1, T0S2, ..., T1S0, T1S1, ...
205 // and start at bit 0.
206 GR_STATIC_ASSERT(sizeof(GrVertexLayout) <= sizeof(uint32_t));
207 return (32 - Gr_clz(bit) - 1) / kNumStages;
208 }
209 return -1;
210}
211
212void GrDrawTarget::VertexLayoutUnitTest() {
213 // not necessarily exhaustive
214 static bool run;
215 if (!run) {
216 run = true;
217 for (int s = 0; s < kNumStages; ++s) {
218
219 GrAssert(!VertexUsesStage(s, 0));
220 GrAssert(-1 == VertexStageCoordOffset(s, 0));
221 GrVertexLayout stageMask = 0;
222 for (int t = 0; t < kMaxTexCoords; ++t) {
223 stageMask |= StageTexCoordVertexLayoutBit(s,t);
224 }
225 GrAssert(1 == kMaxTexCoords || !check_layout(stageMask));
226 GrAssert(stage_tex_coord_mask(s) == stageMask);
227 stageMask |= StagePosAsTexCoordVertexLayoutBit(s);
228 GrAssert(stage_mask(s) == stageMask);
229 GrAssert(!check_layout(stageMask));
230 }
231 for (int t = 0; t < kMaxTexCoords; ++t) {
232 GrVertexLayout tcMask = 0;
233 GrAssert(!VertexUsesTexCoordIdx(t, 0));
234 for (int s = 0; s < kNumStages; ++s) {
235 tcMask |= StageTexCoordVertexLayoutBit(s,t);
236 GrAssert(VertexUsesStage(s, tcMask));
237 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
238 GrAssert(VertexUsesTexCoordIdx(t, tcMask));
239 GrAssert(2*sizeof(GrPoint) == VertexSize(tcMask));
240 GrAssert(t == VertexTexCoordsForStage(s, tcMask));
241 for (int s2 = s + 1; s2 < kNumStages; ++s2) {
242 GrAssert(-1 == VertexStageCoordOffset(s2, tcMask));
243 GrAssert(!VertexUsesStage(s2, tcMask));
244 GrAssert(-1 == VertexTexCoordsForStage(s2, tcMask));
245
246 GrVertexLayout posAsTex = tcMask | StagePosAsTexCoordVertexLayoutBit(s2);
247 GrAssert(0 == VertexStageCoordOffset(s2, posAsTex));
248 GrAssert(VertexUsesStage(s2, posAsTex));
249 GrAssert(2*sizeof(GrPoint) == VertexSize(posAsTex));
250 GrAssert(-1 == VertexTexCoordsForStage(s2, posAsTex));
251 }
252 GrVertexLayout withColor = tcMask | kColor_VertexLayoutBit;
253 GrAssert(2*sizeof(GrPoint) == VertexColorOffset(withColor));
254 GrAssert(2*sizeof(GrPoint) + sizeof(GrColor) == VertexSize(withColor));
255 }
256 GrAssert(tex_coord_idx_mask(t) == tcMask);
257 GrAssert(check_layout(tcMask));
258
259 int stageOffsets[kNumStages];
260 int colorOffset;
261 int size;
262 size = VertexSizeAndOffsetsByStage(tcMask, stageOffsets, &colorOffset);
263 GrAssert(2*sizeof(GrPoint) == size);
264 GrAssert(-1 == colorOffset);
265 for (int s = 0; s < kNumStages; ++s) {
266 GrAssert(VertexUsesStage(s, tcMask));
267 GrAssert(sizeof(GrPoint) == stageOffsets[s]);
268 GrAssert(sizeof(GrPoint) == VertexStageCoordOffset(s, tcMask));
269 }
270 }
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
274////////////////////////////////////////////////////////////////////////////////
275
276GrDrawTarget::GrDrawTarget() {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000277#if GR_DEBUG
278 VertexLayoutUnitTest();
279#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 fReservedGeometry.fLocked = false;
281#if GR_DEBUG
282 fReservedGeometry.fVertexCount = ~0;
283 fReservedGeometry.fIndexCount = ~0;
284#endif
285 fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType;
286 fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType;
287}
288
289void GrDrawTarget::setClip(const GrClip& clip) {
290 clipWillChange(clip);
291 fClip = clip;
292}
293
294const GrClip& GrDrawTarget::getClip() const {
295 return fClip;
296}
297
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000298void GrDrawTarget::setTexture(int stage, GrTexture* tex) {
299 GrAssert(stage >= 0 && stage < kNumStages);
300 fCurrDrawState.fTextures[stage] = tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000301}
302
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000303GrTexture* GrDrawTarget::currentTexture(int stage) const {
304 GrAssert(stage >= 0 && stage < kNumStages);
305 return fCurrDrawState.fTextures[stage];
reed@google.comac10a2d2010-12-22 21:39:39 +0000306}
307
308void GrDrawTarget::setRenderTarget(GrRenderTarget* target) {
309 fCurrDrawState.fRenderTarget = target;
310}
311
312GrRenderTarget* GrDrawTarget::currentRenderTarget() const {
313 return fCurrDrawState.fRenderTarget;
314}
315
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000316void GrDrawTarget::setViewMatrix(const GrMatrix& m) {
317 fCurrDrawState.fViewMatrix = m;
reed@google.comac10a2d2010-12-22 21:39:39 +0000318}
319
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000320void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) {
321 fCurrDrawState.fViewMatrix.preConcat(matrix);
322}
323
324// Can't this just return a const&
reed@google.comac10a2d2010-12-22 21:39:39 +0000325void GrDrawTarget::getViewMatrix(GrMatrix* matrix) const {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000326 *matrix = fCurrDrawState.fViewMatrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000327}
328
329bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000330 // Mike: Can we cache this somewhere?
331 // Brian: Sure, do we use it often?
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
333 GrMatrix inverse;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000334 if (fCurrDrawState.fViewMatrix.invert(&inverse)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 if (matrix) {
336 *matrix = inverse;
337 }
338 return true;
339 }
340 return false;
341}
342
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000343void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) {
344 GrAssert(stage >= 0 && stage < kNumStages);
345 fCurrDrawState.fSamplerStates[stage] = state;
346}
347
348void GrDrawTarget::setTextureMatrix(int stage, const GrMatrix& m) {
349 GrAssert(stage >= 0 && stage < kNumStages);
350 fCurrDrawState.fTextureMatrices[stage] = m;
reed@google.comac10a2d2010-12-22 21:39:39 +0000351}
352
353void GrDrawTarget::setStencilPass(StencilPass pass) {
354 fCurrDrawState.fStencilPass = pass;
355}
356
357void GrDrawTarget::setReverseFill(bool reverse) {
358 fCurrDrawState.fReverseFill = reverse;
359}
360
361void GrDrawTarget::enableState(uint32_t bits) {
362 fCurrDrawState.fFlagBits |= bits;
363}
364
365void GrDrawTarget::disableState(uint32_t bits) {
366 fCurrDrawState.fFlagBits &= ~(bits);
367}
368
reed@google.comac10a2d2010-12-22 21:39:39 +0000369void GrDrawTarget::setPointSize(float size) {
370 fCurrDrawState.fPointSize = size;
371}
372
373void GrDrawTarget::setBlendFunc(BlendCoeff srcCoef,
374 BlendCoeff dstCoef) {
375 fCurrDrawState.fSrcBlend = srcCoef;
376 fCurrDrawState.fDstBlend = dstCoef;
377}
378
379void GrDrawTarget::setColor(GrColor c) {
380 fCurrDrawState.fColor = c;
381}
382
383void GrDrawTarget::setAlpha(uint8_t a) {
384 this->setColor((a << 24) | (a << 16) | (a << 8) | a);
385}
386
387void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const {
388 state->fState = fCurrDrawState;
389}
390
391void GrDrawTarget::restoreDrawState(const SavedDrawState& state) {
392 fCurrDrawState = state.fState;
393}
394
395void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) {
396 fCurrDrawState = srcTarget.fCurrDrawState;
397}
398
399
400bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout,
401 uint32_t vertexCount,
402 uint32_t indexCount,
403 void** vertices,
404 void** indices) {
405 GrAssert(!fReservedGeometry.fLocked);
406 fReservedGeometry.fVertexCount = vertexCount;
407 fReservedGeometry.fIndexCount = indexCount;
408
409 fReservedGeometry.fLocked = acquireGeometryHelper(vertexLayout,
410 vertices,
411 indices);
412 if (fReservedGeometry.fLocked) {
413 if (vertexCount) {
414 fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType;
415 fGeometrySrc.fVertexLayout = vertexLayout;
416 }
417 if (indexCount) {
418 fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType;
419 }
420 }
421 return fReservedGeometry.fLocked;
422}
423
424bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout,
425 int32_t* vertexCount,
426 int32_t* indexCount) const {
427 GrAssert(!fReservedGeometry.fLocked);
428 if (NULL != vertexCount) {
429 *vertexCount = -1;
430 }
431 if (NULL != indexCount) {
432 *indexCount = -1;
433 }
434 return false;
435}
436
437void GrDrawTarget::releaseReservedGeometry() {
438 GrAssert(fReservedGeometry.fLocked);
439 releaseGeometryHelper();
440 fReservedGeometry.fLocked = false;
441}
442
443void GrDrawTarget::setVertexSourceToArray(const void* array,
444 GrVertexLayout vertexLayout) {
445 fGeometrySrc.fVertexSrc = kArray_GeometrySrcType;
446 fGeometrySrc.fVertexArray = array;
447 fGeometrySrc.fVertexLayout = vertexLayout;
448}
449
450void GrDrawTarget::setIndexSourceToArray(const void* array) {
451 fGeometrySrc.fIndexSrc = kArray_GeometrySrcType;
452 fGeometrySrc.fIndexArray = array;
453}
454
455void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer,
456 GrVertexLayout vertexLayout) {
457 fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType;
458 fGeometrySrc.fVertexBuffer = buffer;
459 fGeometrySrc.fVertexLayout = vertexLayout;
460}
461
462void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
463 fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType;
464 fGeometrySrc.fIndexBuffer = buffer;
465}
466
467////////////////////////////////////////////////////////////////////////////////
468
469GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) {
470 fDrawTarget = target;
471 fDrawTarget->saveCurrentDrawState(&fDrawState);
472}
473
474GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
475 fDrawTarget->restoreDrawState(fDrawState);
476}