blob: 5548320631d8bcce3b209c3250df050671858658 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.coma469c282012-10-24 18:28:34 +000010#include "GrEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000011#include "GrGLEffect.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000012#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014typedef GrGLUniformManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000015
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016struct GrGpuGL::ProgramCache::Entry {
17 SK_DECLARE_INST_COUNT_ROOT(Entry);
18 Entry() : fProgram(NULL), fLRUStamp(0) {}
19
20 SkAutoTUnref<GrGLProgram> fProgram;
21 unsigned int fLRUStamp;
22};
23
24SK_DEFINE_INST_COUNT(GrGpuGL::ProgramCache::Entry);
25
26struct GrGpuGL::ProgramCache::ProgDescLess {
27 bool operator() (const GrGLProgramDesc& desc, const Entry* entry) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000028 SkASSERT(NULL != entry->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000029 return GrGLProgramDesc::Less(desc, entry->fProgram->getDesc());
30 }
31
32 bool operator() (const Entry* entry, const GrGLProgramDesc& desc) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000033 SkASSERT(NULL != entry->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000034 return GrGLProgramDesc::Less(entry->fProgram->getDesc(), desc);
35 }
36};
junov@google.comf93e7172011-03-31 21:26:24 +000037
robertphillips@google.com6177e692013-02-28 20:16:25 +000038GrGpuGL::ProgramCache::ProgramCache(const GrGLContext& gl)
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000039 : fCount(0)
40 , fCurrLRUStamp(0)
skia.committer@gmail.com91274b92013-03-13 07:01:04 +000041 , fGL(gl)
jvanverth@google.com94878772013-03-12 16:00:54 +000042#ifdef PROGRAM_CACHE_STATS
43 , fTotalRequests(0)
44 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000045 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000046#endif
47{
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000048 for (int i = 0; i < 1 << kHashBits; ++i) {
49 fHashTable[i] = NULL;
50 }
jvanverth@google.com94878772013-03-12 16:00:54 +000051}
52
53GrGpuGL::ProgramCache::~ProgramCache() {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000054 for (int i = 0; i < fCount; ++i){
55 SkDELETE(fEntries[i]);
56 }
jvanverth@google.com94878772013-03-12 16:00:54 +000057 // dump stats
58#ifdef PROGRAM_CACHE_STATS
59 SkDebugf("--- Program Cache ---\n");
60 SkDebugf("Total requests: %d\n", fTotalRequests);
61 SkDebugf("Cache misses: %d\n", fCacheMisses);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000062 SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ?
63 100.f * fCacheMisses / fTotalRequests :
64 0.f);
65 int cacheHits = fTotalRequests - fCacheMisses;
66 SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f);
jvanverth@google.com94878772013-03-12 16:00:54 +000067 SkDebugf("---------------------\n");
68#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000069}
junov@google.comf93e7172011-03-31 21:26:24 +000070
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000071void GrGpuGL::ProgramCache::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000072 for (int i = 0; i < fCount; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000073 SkASSERT(NULL != fEntries[i]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000074 fEntries[i]->fProgram->abandon();
75 fEntries[i]->fProgram.reset(NULL);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000076 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000077 fCount = 0;
78}
79
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000080int GrGpuGL::ProgramCache::search(const GrGLProgramDesc& desc) const {
81 ProgDescLess less;
82 return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less);
83}
84
bsalomon@google.com31ec7982013-03-27 18:14:57 +000085GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000086 const GrEffectStage* colorStages[],
87 const GrEffectStage* coverageStages[]) {
jvanverth@google.com94878772013-03-12 16:00:54 +000088#ifdef PROGRAM_CACHE_STATS
89 ++fTotalRequests;
90#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000091
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000092 Entry* entry = NULL;
93
94 uint32_t hashIdx = desc.getChecksum();
95 hashIdx ^= hashIdx >> 16;
96 if (kHashBits <= 8) {
97 hashIdx ^= hashIdx >> 8;
98 }
99 hashIdx &=((1 << kHashBits) - 1);
100 Entry* hashedEntry = fHashTable[hashIdx];
101 if (NULL != hashedEntry && hashedEntry->fProgram->getDesc() == desc) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(NULL != hashedEntry->fProgram);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000103 entry = hashedEntry;
104 }
105
106 int entryIdx;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000107 if (NULL == entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000108 entryIdx = this->search(desc);
109 if (entryIdx >= 0) {
110 entry = fEntries[entryIdx];
111#ifdef PROGRAM_CACHE_STATS
112 ++fHashMisses;
113#endif
114 }
115 }
116
117 if (NULL == entry) {
118 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +0000119#ifdef PROGRAM_CACHE_STATS
120 ++fCacheMisses;
121#endif
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000122 GrGLProgram* program = GrGLProgram::Create(fGL, desc, colorStages, coverageStages);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000123 if (NULL == program) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000124 return NULL;
125 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000126 int purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000127 if (fCount < kMaxEntries) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000128 entry = SkNEW(Entry);
129 purgeIdx = fCount++;
130 fEntries[purgeIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000131 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000132 SkASSERT(fCount == kMaxEntries);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000133 purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000134 for (int i = 1; i < kMaxEntries; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000135 if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) {
136 purgeIdx = i;
junov@google.comf93e7172011-03-31 21:26:24 +0000137 }
junov@google.comf93e7172011-03-31 21:26:24 +0000138 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000139 entry = fEntries[purgeIdx];
140 int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1);
141 if (fHashTable[purgedHashIdx] == entry) {
142 fHashTable[purgedHashIdx] = NULL;
143 }
junov@google.comf93e7172011-03-31 21:26:24 +0000144 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT(fEntries[purgeIdx] == entry);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000146 entry->fProgram.reset(program);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000147 // We need to shift fEntries around so that the entry currently at purgeIdx is placed
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000148 // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor).
149 entryIdx = ~entryIdx;
150 if (entryIdx < purgeIdx) {
151 // Let E and P be the entries at index entryIdx and purgeIdx, respectively.
152 // If the entries array looks like this:
153 // aaaaEbbbbbPccccc
154 // we rearrange it to look like this:
155 // aaaaPEbbbbbccccc
156 size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*);
157 memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize);
158 fEntries[entryIdx] = entry;
159 } else if (purgeIdx < entryIdx) {
160 // If the entries array looks like this:
161 // aaaaPbbbbbEccccc
162 // we rearrange it to look like this:
163 // aaaabbbbbPEccccc
164 size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*);
165 memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize);
166 fEntries[entryIdx - 1] = entry;
167 }
168#if GR_DEBUG
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000169 SkASSERT(NULL != fEntries[0]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000170 for (int i = 0; i < fCount - 1; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000171 SkASSERT(NULL != fEntries[i + 1]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000172 const GrGLProgramDesc& a = fEntries[i]->fProgram->getDesc();
173 const GrGLProgramDesc& b = fEntries[i + 1]->fProgram->getDesc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000174 SkASSERT(GrGLProgramDesc::Less(a, b));
175 SkASSERT(!GrGLProgramDesc::Less(b, a));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000176 }
177#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000178 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000179
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000180 fHashTable[hashIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000181 entry->fLRUStamp = fCurrLRUStamp;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000182
183 if (SK_MaxU32 == fCurrLRUStamp) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000184 // wrap around! just trash our LRU, one time hit.
185 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000186 fEntries[i]->fLRUStamp = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000187 }
188 }
189 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000190 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000191}
junov@google.comf93e7172011-03-31 21:26:24 +0000192
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000193////////////////////////////////////////////////////////////////////////////////
194
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000195void GrGpuGL::abandonResources(){
196 INHERITED::abandonResources();
197 fProgramCache->abandon();
198 fHWProgramID = 0;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000203#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
204
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000205void GrGpuGL::flushPathStencilMatrix() {
206 const SkMatrix& viewMatrix = this->getDrawState().getViewMatrix();
207 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
208 SkISize size;
209 size.set(rt->width(), rt->height());
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000210 const SkMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000211
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000212 if (fHWPathStencilMatrixState.fRenderTargetOrigin != rt->origin() ||
commit-bot@chromium.org1a165aa2013-08-05 13:30:59 +0000213 !fHWPathStencilMatrixState.fViewMatrix.cheapEqualTo(viewMatrix) ||
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000214 fHWPathStencilMatrixState.fRenderTargetSize!= size) {
215 // rescale the coords from skia's "device" coords to GL's normalized coords,
216 // and perform a y-flip if required.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000217 SkMatrix m;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000218 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000219 m.setScale(SkIntToScalar(2) / rt->width(), SkIntToScalar(-2) / rt->height());
220 m.postTranslate(-SK_Scalar1, SK_Scalar1);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000221 } else {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000222 m.setScale(SkIntToScalar(2) / rt->width(), SkIntToScalar(2) / rt->height());
223 m.postTranslate(-SK_Scalar1, -SK_Scalar1);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000224 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000225 m.preConcat(vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000226
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000227 // GL wants a column-major 4x4.
228 GrGLfloat mv[] = {
229 // col 0
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000230 SkScalarToFloat(m[SkMatrix::kMScaleX]),
231 SkScalarToFloat(m[SkMatrix::kMSkewY]),
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000232 0,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000233 SkScalarToFloat(m[SkMatrix::kMPersp0]),
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000234
235 // col 1
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000236 SkScalarToFloat(m[SkMatrix::kMSkewX]),
237 SkScalarToFloat(m[SkMatrix::kMScaleY]),
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000238 0,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000239 SkScalarToFloat(m[SkMatrix::kMPersp1]),
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000240
241 // col 2
242 0, 0, 0, 0,
243
244 // col3
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000245 SkScalarToFloat(m[SkMatrix::kMTransX]),
246 SkScalarToFloat(m[SkMatrix::kMTransY]),
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000247 0.0f,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000248 SkScalarToFloat(m[SkMatrix::kMPersp2])
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000249 };
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000250 GL_CALL(MatrixMode(GR_GL_PROJECTION));
251 GL_CALL(LoadMatrixf(mv));
252 fHWPathStencilMatrixState.fViewMatrix = vm;
253 fHWPathStencilMatrixState.fRenderTargetSize = size;
254 fHWPathStencilMatrixState.fRenderTargetOrigin = rt->origin();
bsalomon@google.com91961302011-05-09 18:39:58 +0000255 }
junov@google.comf93e7172011-03-31 21:26:24 +0000256}
257
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000258bool GrGpuGL::flushGraphicsState(DrawType type, const GrDeviceCoordTexture* dstCopy) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000259 const GrDrawState& drawState = this->getDrawState();
260
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000261 // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(NULL != drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000263
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000264 if (kStencilPath_DrawType == type) {
265 this->flushPathStencilMatrix();
266 } else {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000267 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000268
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000269 GrBlendCoeff srcCoeff;
270 GrBlendCoeff dstCoeff;
bsalomon@google.com2b446732013-02-12 16:47:41 +0000271 GrDrawState::BlendOptFlags blendOpts = drawState.getBlendOpts(false, &srcCoeff, &dstCoeff);
272 if (GrDrawState::kSkipDraw_BlendOptFlag & blendOpts) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000273 return false;
274 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000275
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000276 SkSTArray<8, const GrEffectStage*, true> colorStages;
277 SkSTArray<8, const GrEffectStage*, true> coverageStages;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000278 GrGLProgramDesc desc;
279 GrGLProgramDesc::Build(this->getDrawState(),
bsalomon@google.com91207482013-02-12 21:45:24 +0000280 kDrawPoints_DrawType == type,
281 blendOpts,
282 srcCoeff,
283 dstCoeff,
284 this,
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000285 dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000286 &colorStages,
287 &coverageStages,
bsalomon@google.com91207482013-02-12 21:45:24 +0000288 &desc);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000289
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000290 fCurrentProgram.reset(fProgramCache->getProgram(desc,
291 colorStages.begin(),
292 coverageStages.begin()));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000293 if (NULL == fCurrentProgram.get()) {
mtklein@google.com330313a2013-08-22 15:37:26 +0000294 SkDEBUGFAIL("Failed to create program!");
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000295 return false;
296 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000297 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000298
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000299 GrGLuint programID = fCurrentProgram->programID();
300 if (fHWProgramID != programID) {
301 GL_CALL(UseProgram(programID));
302 fHWProgramID = programID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000303 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000304
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000305 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000306 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000307
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000308 fCurrentProgram->setData(this,
309 blendOpts,
310 colorStages.begin(),
311 coverageStages.begin(),
312 dstCopy,
313 &fSharedGLProgramState);
junov@google.comf93e7172011-03-31 21:26:24 +0000314 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000315 this->flushStencil(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000316 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000317 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000318
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000319 SkIRect* devRect = NULL;
320 SkIRect devClipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000321 if (drawState.isClipState()) {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000322 this->getClip()->getConservativeBounds(drawState.getRenderTarget(), &devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000323 devRect = &devClipBounds;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000324 }
325 // This must come after textures are flushed because a texture may need
326 // to be msaa-resolved (which will modify bound FBO state).
robertphillips@google.com7b112892012-07-31 15:18:21 +0000327 this->flushRenderTarget(devRect);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000328
junov@google.comf93e7172011-03-31 21:26:24 +0000329 return true;
330}
331
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000332void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) {
junov@google.comf93e7172011-03-31 21:26:24 +0000333
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000334 GrGLsizei stride = this->getDrawState().getVertexSize();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000335
bsalomon@google.com6918d482013-03-07 19:09:11 +0000336 size_t vertexOffsetInBytes = stride * info.startVertex();
337
338 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
339
340 GrGLVertexBuffer* vbuf;
341 switch (this->getGeomSrc().fVertexSrc) {
342 case kBuffer_GeometrySrcType:
343 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
344 break;
345 case kArray_GeometrySrcType:
346 case kReserved_GeometrySrcType:
347 this->finalizeReservedVertices();
348 vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize;
349 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
350 break;
351 default:
352 vbuf = NULL; // suppress warning
353 GrCrash("Unknown geometry src type!");
354 }
355
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000356 SkASSERT(NULL != vbuf);
357 SkASSERT(!vbuf->isLocked());
bsalomon@google.com6918d482013-03-07 19:09:11 +0000358 vertexOffsetInBytes += vbuf->baseOffset();
359
360 GrGLIndexBuffer* ibuf = NULL;
361 if (info.isIndexed()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000362 SkASSERT(NULL != indexOffsetInBytes);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000363
364 switch (this->getGeomSrc().fIndexSrc) {
365 case kBuffer_GeometrySrcType:
366 *indexOffsetInBytes = 0;
367 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
368 break;
369 case kArray_GeometrySrcType:
370 case kReserved_GeometrySrcType:
371 this->finalizeReservedIndices();
372 *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort);
373 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
374 break;
375 default:
376 ibuf = NULL; // suppress warning
377 GrCrash("Unknown geometry src type!");
378 }
379
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000380 SkASSERT(NULL != ibuf);
381 SkASSERT(!ibuf->isLocked());
bsalomon@google.com6918d482013-03-07 19:09:11 +0000382 *indexOffsetInBytes += ibuf->baseOffset();
383 }
384 GrGLAttribArrayState* attribState =
385 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
junov@google.comf93e7172011-03-31 21:26:24 +0000386
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000387 uint32_t usedAttribArraysMask = 0;
388 const GrVertexAttrib* vertexAttrib = this->getDrawState().getVertexAttribs();
389 int vertexAttribCount = this->getDrawState().getVertexAttribCount();
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000390 for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000391 ++vertexAttribIndex, ++vertexAttrib) {
robertphillips@google.comaf3a3b92013-02-28 23:08:28 +0000392
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000393 usedAttribArraysMask |= (1 << vertexAttribIndex);
394 GrVertexAttribType attribType = vertexAttrib->fType;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000395 attribState->set(this,
396 vertexAttribIndex,
397 vbuf,
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000398 GrGLAttribTypeToLayout(attribType).fCount,
399 GrGLAttribTypeToLayout(attribType).fType,
400 GrGLAttribTypeToLayout(attribType).fNormalized,
bsalomon@google.com6918d482013-03-07 19:09:11 +0000401 stride,
402 reinterpret_cast<GrGLvoid*>(
403 vertexOffsetInBytes + vertexAttrib->fOffset));
404 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000405
bsalomon@google.com6918d482013-03-07 19:09:11 +0000406 attribState->disableUnusedAttribArrays(this, usedAttribArraysMask);
junov@google.comf93e7172011-03-31 21:26:24 +0000407}