blob: 6c3c4fe3ab49ebcada0d7c1f17d6caea0887c1ad [file] [log] [blame]
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
10#include "libGLESv2/Context.h"
11
12#include <algorithm>
13
14#include "libEGL/Display.h"
15
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000019#include "libGLESv2/renderer/renderer9_utils.h" // D3D9_REPLACE
apatrick@chromium.org144f2802012-07-12 01:42:34 +000020#include "libGLESv2/ResourceManager.h"
21#include "libGLESv2/Buffer.h"
22#include "libGLESv2/Fence.h"
daniel@transgaming.com29ab9522012-08-27 16:25:37 +000023#include "libGLESv2/Framebuffer.h"
apatrick@chromium.org144f2802012-07-12 01:42:34 +000024#include "libGLESv2/Program.h"
25#include "libGLESv2/ProgramBinary.h"
26#include "libGLESv2/Query.h"
daniel@transgaming.com29ab9522012-08-27 16:25:37 +000027#include "libGLESv2/Renderbuffer.h"
apatrick@chromium.org144f2802012-07-12 01:42:34 +000028#include "libGLESv2/Shader.h"
29#include "libGLESv2/Texture.h"
30#include "libGLESv2/VertexDataManager.h"
31#include "libGLESv2/IndexDataManager.h"
32
33#undef near
34#undef far
35
36namespace gl
37{
daniel@transgaming.com03d39092012-11-28 19:31:59 +000038Context::Context(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
apatrick@chromium.org144f2802012-07-12 01:42:34 +000039{
40 ASSERT(robustAccess == false); // Unimplemented
41
daniel@transgaming.com03d39092012-11-28 19:31:59 +000042 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL); // D3D9_REPLACE
43 mRenderer = static_cast<rx::Renderer9*>(renderer);
44
apatrick@chromium.org144f2802012-07-12 01:42:34 +000045 mDisplay = NULL;
46 mDevice = NULL;
47
48 mFenceHandleAllocator.setBaseHandle(0);
49
50 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
51
52 mState.depthClearValue = 1.0f;
53 mState.stencilClearValue = 0;
54
daniel@transgaming.comf39967e2012-11-28 19:35:56 +000055 mState.rasterizer.cullFace = false;
56 mState.rasterizer.cullMode = GL_BACK;
57 mState.rasterizer.frontFace = GL_CCW;
58 mState.rasterizer.polygonOffsetFill = false;
59 mState.rasterizer.polygonOffsetFactor = 0.0f;
60 mState.rasterizer.polygonOffsetUnits = 0.0f;
61 mState.rasterizer.scissorTest = false;
62 mState.scissor.x = 0;
63 mState.scissor.y = 0;
64 mState.scissor.width = 0;
65 mState.scissor.height = 0;
66
67 mState.blend.blend = false;
68 mState.blend.sourceBlendRGB = GL_ONE;
69 mState.blend.sourceBlendAlpha = GL_ONE;
70 mState.blend.destBlendRGB = GL_ZERO;
71 mState.blend.destBlendAlpha = GL_ZERO;
72 mState.blend.blendEquationRGB = GL_FUNC_ADD;
73 mState.blend.blendEquationAlpha = GL_FUNC_ADD;
74 mState.blend.sampleAlphaToCoverage = false;
75 mState.blend.dither = true;
76
apatrick@chromium.org144f2802012-07-12 01:42:34 +000077 mState.blendColor.red = 0;
78 mState.blendColor.green = 0;
79 mState.blendColor.blue = 0;
80 mState.blendColor.alpha = 0;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +000081
82 mState.depthStencil.depthTest = false;
83 mState.depthStencil.depthFunc = GL_LESS;
84 mState.depthStencil.depthMask = true;
85 mState.depthStencil.stencilTest = false;
86 mState.depthStencil.stencilFunc = GL_ALWAYS;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +000087 mState.depthStencil.stencilMask = -1;
88 mState.depthStencil.stencilWritemask = -1;
89 mState.depthStencil.stencilBackFunc = GL_ALWAYS;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +000090 mState.depthStencil.stencilBackMask = - 1;
91 mState.depthStencil.stencilBackWritemask = -1;
92 mState.depthStencil.stencilFail = GL_KEEP;
93 mState.depthStencil.stencilPassDepthFail = GL_KEEP;
94 mState.depthStencil.stencilPassDepthPass = GL_KEEP;
95 mState.depthStencil.stencilBackFail = GL_KEEP;
96 mState.depthStencil.stencilBackPassDepthFail = GL_KEEP;
97 mState.depthStencil.stencilBackPassDepthPass = GL_KEEP;
98
daniel@transgaming.com08c331d2012-11-28 19:38:39 +000099 mState.stencilRef = 0;
100 mState.stencilBackRef = 0;
101
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000102 mState.sampleCoverage = false;
103 mState.sampleCoverageValue = 1.0f;
104 mState.sampleCoverageInvert = false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000105 mState.generateMipmapHint = GL_DONT_CARE;
106 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
107
108 mState.lineWidth = 1.0f;
109
110 mState.viewportX = 0;
111 mState.viewportY = 0;
daniel@transgaming.com21290e62012-10-31 18:38:28 +0000112 mState.viewportWidth = 0;
113 mState.viewportHeight = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000114 mState.zNear = 0.0f;
115 mState.zFar = 1.0f;
116
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000117 mState.blend.colorMaskRed = true;
118 mState.blend.colorMaskGreen = true;
119 mState.blend.colorMaskBlue = true;
120 mState.blend.colorMaskAlpha = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000121
122 if (shareContext != NULL)
123 {
124 mResourceManager = shareContext->mResourceManager;
125 mResourceManager->addRef();
126 }
127 else
128 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000129 mResourceManager = new ResourceManager(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000130 }
131
132 // [OpenGL ES 2.0.24] section 3.7 page 83:
133 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
134 // and cube map texture state vectors respectively associated with them.
135 // In order that access to these initial textures not be lost, they are treated as texture
136 // objects all of whose names are 0.
137
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000138 mTexture2DZero.set(new Texture2D(mRenderer, 0));
139 mTextureCubeMapZero.set(new TextureCubeMap(mRenderer, 0));
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000140
141 mState.activeSampler = 0;
142 bindArrayBuffer(0);
143 bindElementArrayBuffer(0);
144 bindTextureCubeMap(0);
145 bindTexture2D(0);
146 bindReadFramebuffer(0);
147 bindDrawFramebuffer(0);
148 bindRenderbuffer(0);
149
150 mState.currentProgram = 0;
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000151 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000152
153 mState.packAlignment = 4;
154 mState.unpackAlignment = 4;
155 mState.packReverseRowOrder = false;
156
157 mVertexDataManager = NULL;
158 mIndexDataManager = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000159 mLineLoopIB = NULL;
160
161 mInvalidEnum = false;
162 mInvalidValue = false;
163 mInvalidOperation = false;
164 mOutOfMemory = false;
165 mInvalidFramebufferOperation = false;
166
167 mHasBeenCurrent = false;
168 mContextLost = false;
169 mResetStatus = GL_NO_ERROR;
170 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
171 mRobustAccess = robustAccess;
172
173 mSupportsDXT1Textures = false;
174 mSupportsDXT3Textures = false;
175 mSupportsDXT5Textures = false;
176 mSupportsEventQueries = false;
177 mSupportsOcclusionQueries = false;
178 mNumCompressedTextureFormats = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000179 mMaskedClearSavedState = NULL;
180 markAllStateDirty();
181}
182
183Context::~Context()
184{
185 if (mState.currentProgram != 0)
186 {
187 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
188 if (programObject)
189 {
190 programObject->release();
191 }
192 mState.currentProgram = 0;
193 }
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000194 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000195
196 while (!mFramebufferMap.empty())
197 {
198 deleteFramebuffer(mFramebufferMap.begin()->first);
199 }
200
201 while (!mFenceMap.empty())
202 {
203 deleteFence(mFenceMap.begin()->first);
204 }
205
206 while (!mQueryMap.empty())
207 {
208 deleteQuery(mQueryMap.begin()->first);
209 }
210
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000211 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
212 {
213 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
214 {
215 mState.samplerTexture[type][sampler].set(NULL);
216 }
217 }
218
219 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
220 {
221 mIncompleteTextures[type].set(NULL);
222 }
223
224 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
225 {
226 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
227 }
228
229 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
230 {
231 mState.activeQuery[i].set(NULL);
232 }
233
234 mState.arrayBuffer.set(NULL);
235 mState.elementArrayBuffer.set(NULL);
236 mState.renderbuffer.set(NULL);
237
238 mTexture2DZero.set(NULL);
239 mTextureCubeMapZero.set(NULL);
240
241 delete mVertexDataManager;
242 delete mIndexDataManager;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000243 delete mLineLoopIB;
244
245 if (mMaskedClearSavedState)
246 {
247 mMaskedClearSavedState->Release();
248 }
249
250 mResourceManager->release();
251}
252
daniel@transgaming.comad629872012-11-28 19:32:06 +0000253void Context::makeCurrent(egl::Surface *surface)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000254{
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000255 mDevice = mRenderer->getDevice(); // D3D9_REMOVE
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000256
257 if (!mHasBeenCurrent)
258 {
daniel@transgaming.com408caa52012-10-31 18:47:01 +0000259 mVertexDataManager = new VertexDataManager(mRenderer);
260 mIndexDataManager = new IndexDataManager(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000261
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000262 mSupportsShaderModel3 = mRenderer->getShaderModel3Support();
263 mMaximumPointSize = mRenderer->getMaxPointSize();
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000264 mSupportsVertexTexture = mRenderer->getVertexTextureSupport();
265 mSupportsNonPower2Texture = mRenderer->getNonPower2TextureSupport();
266 mSupportsInstancing = mRenderer->getInstancingSupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000267
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000268 mMaxTextureDimension = std::min(std::min(mRenderer->getMaxTextureWidth(), mRenderer->getMaxTextureHeight()),
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000269 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
270 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
271 mMaxRenderbufferDimension = mMaxTextureDimension;
272 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000273 mMaxTextureAnisotropy = mRenderer->getTextureMaxAnisotropy();
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000274 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f",
275 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000276
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000277 mSupportsEventQueries = mRenderer->getEventQuerySupport();
278 mSupportsOcclusionQueries = mRenderer->getOcclusionQuerySupport();
279 mSupportsDXT1Textures = mRenderer->getDXT1TextureSupport();
280 mSupportsDXT3Textures = mRenderer->getDXT3TextureSupport();
281 mSupportsDXT5Textures = mRenderer->getDXT5TextureSupport();
282 mSupportsFloat32Textures = mRenderer->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
283 mSupportsFloat16Textures = mRenderer->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
284 mSupportsLuminanceTextures = mRenderer->getLuminanceTextureSupport();
285 mSupportsLuminanceAlphaTextures = mRenderer->getLuminanceAlphaTextureSupport();
286 mSupportsDepthTextures = mRenderer->getDepthTextureSupport();
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000287 mSupportsTextureFilterAnisotropy = mRenderer->getTextureFilterAnisotropySupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000288
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000289 mSupports32bitIndices = mRenderer->get32BitIndexSupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000290
291 mNumCompressedTextureFormats = 0;
292 if (supportsDXT1Textures())
293 {
294 mNumCompressedTextureFormats += 2;
295 }
296 if (supportsDXT3Textures())
297 {
298 mNumCompressedTextureFormats += 1;
299 }
300 if (supportsDXT5Textures())
301 {
302 mNumCompressedTextureFormats += 1;
303 }
304
305 initExtensionString();
306 initRendererString();
307
308 mState.viewportX = 0;
309 mState.viewportY = 0;
310 mState.viewportWidth = surface->getWidth();
311 mState.viewportHeight = surface->getHeight();
312
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000313 mState.scissor.x = 0;
314 mState.scissor.y = 0;
315 mState.scissor.width = surface->getWidth();
316 mState.scissor.height = surface->getHeight();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000317
318 mHasBeenCurrent = true;
319 }
320
daniel@transgaming.com024786d2012-10-31 18:42:55 +0000321 // Wrap the existing swapchain resources into GL objects and assign them to the '0' names
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000322 rx::SwapChain *swapchain = surface->getSwapChain();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000323
daniel@transgaming.com70062c92012-11-28 19:32:30 +0000324 Colorbuffer *colorbufferZero = new Colorbuffer(mRenderer, swapchain);
325 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(mRenderer, swapchain);
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000326 Framebuffer *framebufferZero = new DefaultFramebuffer(mRenderer, colorbufferZero, depthStencilbufferZero);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000327
328 setFramebufferZero(framebufferZero);
329
apatrick@chromium.org909f21c2012-08-17 20:06:02 +0000330 // Reset pixel shader to null to work around a bug that only happens with Intel GPUs.
331 // http://crbug.com/110343
332 mDevice->SetPixelShader(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000333
334 markAllStateDirty();
335}
336
337// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
338void Context::markAllStateDirty()
339{
340 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
341 {
342 mAppliedTextureSerialPS[t] = 0;
343 }
344
345 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
346 {
347 mAppliedTextureSerialVS[t] = 0;
348 }
349
daniel@transgaming.come6af4f92012-07-24 18:31:31 +0000350 mAppliedProgramBinarySerial = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000351 mAppliedRenderTargetSerial = 0;
352 mAppliedDepthbufferSerial = 0;
353 mAppliedStencilbufferSerial = 0;
354 mAppliedIBSerial = 0;
355 mDepthStencilInitialized = false;
356 mViewportInitialized = false;
357 mRenderTargetDescInitialized = false;
358
359 mVertexDeclarationCache.markStateDirty();
360
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000361 mDxUniformsDirty = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000362}
363
364void Context::markDxUniformsDirty()
365{
366 mDxUniformsDirty = true;
367}
368
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000369// NOTE: this function should not assume that this context is current!
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000370void Context::markContextLost()
371{
372 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
373 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
374 mContextLost = true;
375}
376
377bool Context::isContextLost()
378{
379 return mContextLost;
380}
381
382void Context::setClearColor(float red, float green, float blue, float alpha)
383{
384 mState.colorClearValue.red = red;
385 mState.colorClearValue.green = green;
386 mState.colorClearValue.blue = blue;
387 mState.colorClearValue.alpha = alpha;
388}
389
390void Context::setClearDepth(float depth)
391{
392 mState.depthClearValue = depth;
393}
394
395void Context::setClearStencil(int stencil)
396{
397 mState.stencilClearValue = stencil;
398}
399
400void Context::setCullFace(bool enabled)
401{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000402 mState.rasterizer.cullFace = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000403}
404
405bool Context::isCullFaceEnabled() const
406{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000407 return mState.rasterizer.cullFace;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000408}
409
410void Context::setCullMode(GLenum mode)
411{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000412 mState.rasterizer.cullMode = mode;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000413}
414
415void Context::setFrontFace(GLenum front)
416{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000417 mState.rasterizer.frontFace = front;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000418}
419
420void Context::setDepthTest(bool enabled)
421{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000422 mState.depthStencil.depthTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000423}
424
425bool Context::isDepthTestEnabled() const
426{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000427 return mState.depthStencil.depthTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000428}
429
430void Context::setDepthFunc(GLenum depthFunc)
431{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000432 mState.depthStencil.depthFunc = depthFunc;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000433}
434
435void Context::setDepthRange(float zNear, float zFar)
436{
437 mState.zNear = zNear;
438 mState.zFar = zFar;
439}
440
441void Context::setBlend(bool enabled)
442{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000443 mState.blend.blend = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000444}
445
446bool Context::isBlendEnabled() const
447{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000448 return mState.blend.blend;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000449}
450
451void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
452{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000453 mState.blend.sourceBlendRGB = sourceRGB;
454 mState.blend.destBlendRGB = destRGB;
455 mState.blend.sourceBlendAlpha = sourceAlpha;
456 mState.blend.destBlendAlpha = destAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000457}
458
459void Context::setBlendColor(float red, float green, float blue, float alpha)
460{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000461 mState.blendColor.red = red;
462 mState.blendColor.green = green;
463 mState.blendColor.blue = blue;
464 mState.blendColor.alpha = alpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000465}
466
467void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
468{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000469 mState.blend.blendEquationRGB = rgbEquation;
470 mState.blend.blendEquationAlpha = alphaEquation;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000471}
472
473void Context::setStencilTest(bool enabled)
474{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000475 mState.depthStencil.stencilTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000476}
477
478bool Context::isStencilTestEnabled() const
479{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000480 return mState.depthStencil.stencilTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000481}
482
483void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
484{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000485 mState.depthStencil.stencilFunc = stencilFunc;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000486 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000487 mState.depthStencil.stencilMask = stencilMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000488}
489
490void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
491{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000492 mState.depthStencil.stencilBackFunc = stencilBackFunc;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000493 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000494 mState.depthStencil.stencilBackMask = stencilBackMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000495}
496
497void Context::setStencilWritemask(GLuint stencilWritemask)
498{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000499 mState.depthStencil.stencilWritemask = stencilWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000500}
501
502void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
503{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000504 mState.depthStencil.stencilBackWritemask = stencilBackWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000505}
506
507void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
508{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000509 mState.depthStencil.stencilFail = stencilFail;
510 mState.depthStencil.stencilPassDepthFail = stencilPassDepthFail;
511 mState.depthStencil.stencilPassDepthPass = stencilPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000512}
513
514void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
515{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000516 mState.depthStencil.stencilBackFail = stencilBackFail;
517 mState.depthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
518 mState.depthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000519}
520
521void Context::setPolygonOffsetFill(bool enabled)
522{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000523 mState.rasterizer.polygonOffsetFill = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000524}
525
526bool Context::isPolygonOffsetFillEnabled() const
527{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000528 return mState.rasterizer.polygonOffsetFill;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000529}
530
531void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
532{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000533 mState.rasterizer.polygonOffsetFactor = factor;
534 mState.rasterizer.polygonOffsetUnits = units;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000535}
536
537void Context::setSampleAlphaToCoverage(bool enabled)
538{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000539 mState.blend.sampleAlphaToCoverage = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000540}
541
542bool Context::isSampleAlphaToCoverageEnabled() const
543{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000544 return mState.blend.sampleAlphaToCoverage;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000545}
546
547void Context::setSampleCoverage(bool enabled)
548{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000549 mState.sampleCoverage = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000550}
551
552bool Context::isSampleCoverageEnabled() const
553{
554 return mState.sampleCoverage;
555}
556
557void Context::setSampleCoverageParams(GLclampf value, bool invert)
558{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000559 mState.sampleCoverageValue = value;
560 mState.sampleCoverageInvert = invert;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000561}
562
563void Context::setScissorTest(bool enabled)
564{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000565 mState.rasterizer.scissorTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000566}
567
568bool Context::isScissorTestEnabled() const
569{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000570 return mState.rasterizer.scissorTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000571}
572
573void Context::setDither(bool enabled)
574{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000575 mState.blend.dither = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000576}
577
578bool Context::isDitherEnabled() const
579{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000580 return mState.blend.dither;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000581}
582
583void Context::setLineWidth(GLfloat width)
584{
585 mState.lineWidth = width;
586}
587
588void Context::setGenerateMipmapHint(GLenum hint)
589{
590 mState.generateMipmapHint = hint;
591}
592
593void Context::setFragmentShaderDerivativeHint(GLenum hint)
594{
595 mState.fragmentShaderDerivativeHint = hint;
596 // TODO: Propagate the hint to shader translator so we can write
597 // ddx, ddx_coarse, or ddx_fine depending on the hint.
598 // Ignore for now. It is valid for implementations to ignore hint.
599}
600
601void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
602{
603 mState.viewportX = x;
604 mState.viewportY = y;
605 mState.viewportWidth = width;
606 mState.viewportHeight = height;
607}
608
609void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
610{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000611 mState.scissor.x = x;
612 mState.scissor.y = y;
613 mState.scissor.width = width;
614 mState.scissor.height = height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000615}
616
617void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
618{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000619 mState.blend.colorMaskRed = red;
620 mState.blend.colorMaskGreen = green;
621 mState.blend.colorMaskBlue = blue;
622 mState.blend.colorMaskAlpha = alpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000623}
624
625void Context::setDepthMask(bool mask)
626{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000627 mState.depthStencil.depthMask = mask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000628}
629
630void Context::setActiveSampler(unsigned int active)
631{
632 mState.activeSampler = active;
633}
634
635GLuint Context::getReadFramebufferHandle() const
636{
637 return mState.readFramebuffer;
638}
639
640GLuint Context::getDrawFramebufferHandle() const
641{
642 return mState.drawFramebuffer;
643}
644
645GLuint Context::getRenderbufferHandle() const
646{
647 return mState.renderbuffer.id();
648}
649
650GLuint Context::getArrayBufferHandle() const
651{
652 return mState.arrayBuffer.id();
653}
654
655GLuint Context::getActiveQuery(GLenum target) const
656{
657 Query *queryObject = NULL;
658
659 switch (target)
660 {
661 case GL_ANY_SAMPLES_PASSED_EXT:
662 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
663 break;
664 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
665 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
666 break;
667 default:
668 ASSERT(false);
669 }
670
671 if (queryObject)
672 {
673 return queryObject->id();
674 }
675 else
676 {
677 return 0;
678 }
679}
680
681void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
682{
683 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
684}
685
686const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
687{
688 return mState.vertexAttribute[attribNum];
689}
690
691void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
692 GLsizei stride, const void *pointer)
693{
694 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
695 mState.vertexAttribute[attribNum].mSize = size;
696 mState.vertexAttribute[attribNum].mType = type;
697 mState.vertexAttribute[attribNum].mNormalized = normalized;
698 mState.vertexAttribute[attribNum].mStride = stride;
699 mState.vertexAttribute[attribNum].mPointer = pointer;
700}
701
702const void *Context::getVertexAttribPointer(unsigned int attribNum) const
703{
704 return mState.vertexAttribute[attribNum].mPointer;
705}
706
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000707void Context::setPackAlignment(GLint alignment)
708{
709 mState.packAlignment = alignment;
710}
711
712GLint Context::getPackAlignment() const
713{
714 return mState.packAlignment;
715}
716
717void Context::setUnpackAlignment(GLint alignment)
718{
719 mState.unpackAlignment = alignment;
720}
721
722GLint Context::getUnpackAlignment() const
723{
724 return mState.unpackAlignment;
725}
726
727void Context::setPackReverseRowOrder(bool reverseRowOrder)
728{
729 mState.packReverseRowOrder = reverseRowOrder;
730}
731
732bool Context::getPackReverseRowOrder() const
733{
734 return mState.packReverseRowOrder;
735}
736
737GLuint Context::createBuffer()
738{
739 return mResourceManager->createBuffer();
740}
741
742GLuint Context::createProgram()
743{
744 return mResourceManager->createProgram();
745}
746
747GLuint Context::createShader(GLenum type)
748{
749 return mResourceManager->createShader(type);
750}
751
752GLuint Context::createTexture()
753{
754 return mResourceManager->createTexture();
755}
756
757GLuint Context::createRenderbuffer()
758{
759 return mResourceManager->createRenderbuffer();
760}
761
762// Returns an unused framebuffer name
763GLuint Context::createFramebuffer()
764{
765 GLuint handle = mFramebufferHandleAllocator.allocate();
766
767 mFramebufferMap[handle] = NULL;
768
769 return handle;
770}
771
772GLuint Context::createFence()
773{
774 GLuint handle = mFenceHandleAllocator.allocate();
775
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000776 mFenceMap[handle] = new Fence(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000777
778 return handle;
779}
780
781// Returns an unused query name
782GLuint Context::createQuery()
783{
784 GLuint handle = mQueryHandleAllocator.allocate();
785
786 mQueryMap[handle] = NULL;
787
788 return handle;
789}
790
791void Context::deleteBuffer(GLuint buffer)
792{
793 if (mResourceManager->getBuffer(buffer))
794 {
795 detachBuffer(buffer);
796 }
797
798 mResourceManager->deleteBuffer(buffer);
799}
800
801void Context::deleteShader(GLuint shader)
802{
803 mResourceManager->deleteShader(shader);
804}
805
806void Context::deleteProgram(GLuint program)
807{
808 mResourceManager->deleteProgram(program);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000809}
810
811void Context::deleteTexture(GLuint texture)
812{
813 if (mResourceManager->getTexture(texture))
814 {
815 detachTexture(texture);
816 }
817
818 mResourceManager->deleteTexture(texture);
819}
820
821void Context::deleteRenderbuffer(GLuint renderbuffer)
822{
823 if (mResourceManager->getRenderbuffer(renderbuffer))
824 {
825 detachRenderbuffer(renderbuffer);
826 }
827
828 mResourceManager->deleteRenderbuffer(renderbuffer);
829}
830
831void Context::deleteFramebuffer(GLuint framebuffer)
832{
833 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
834
835 if (framebufferObject != mFramebufferMap.end())
836 {
837 detachFramebuffer(framebuffer);
838
839 mFramebufferHandleAllocator.release(framebufferObject->first);
840 delete framebufferObject->second;
841 mFramebufferMap.erase(framebufferObject);
842 }
843}
844
845void Context::deleteFence(GLuint fence)
846{
847 FenceMap::iterator fenceObject = mFenceMap.find(fence);
848
849 if (fenceObject != mFenceMap.end())
850 {
851 mFenceHandleAllocator.release(fenceObject->first);
852 delete fenceObject->second;
853 mFenceMap.erase(fenceObject);
854 }
855}
856
857void Context::deleteQuery(GLuint query)
858{
859 QueryMap::iterator queryObject = mQueryMap.find(query);
860 if (queryObject != mQueryMap.end())
861 {
862 mQueryHandleAllocator.release(queryObject->first);
863 if (queryObject->second)
864 {
865 queryObject->second->release();
866 }
867 mQueryMap.erase(queryObject);
868 }
869}
870
871Buffer *Context::getBuffer(GLuint handle)
872{
873 return mResourceManager->getBuffer(handle);
874}
875
876Shader *Context::getShader(GLuint handle)
877{
878 return mResourceManager->getShader(handle);
879}
880
881Program *Context::getProgram(GLuint handle)
882{
883 return mResourceManager->getProgram(handle);
884}
885
886Texture *Context::getTexture(GLuint handle)
887{
888 return mResourceManager->getTexture(handle);
889}
890
891Renderbuffer *Context::getRenderbuffer(GLuint handle)
892{
893 return mResourceManager->getRenderbuffer(handle);
894}
895
896Framebuffer *Context::getReadFramebuffer()
897{
898 return getFramebuffer(mState.readFramebuffer);
899}
900
901Framebuffer *Context::getDrawFramebuffer()
902{
903 return mBoundDrawFramebuffer;
904}
905
906void Context::bindArrayBuffer(unsigned int buffer)
907{
908 mResourceManager->checkBufferAllocation(buffer);
909
910 mState.arrayBuffer.set(getBuffer(buffer));
911}
912
913void Context::bindElementArrayBuffer(unsigned int buffer)
914{
915 mResourceManager->checkBufferAllocation(buffer);
916
917 mState.elementArrayBuffer.set(getBuffer(buffer));
918}
919
920void Context::bindTexture2D(GLuint texture)
921{
922 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
923
924 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
925}
926
927void Context::bindTextureCubeMap(GLuint texture)
928{
929 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
930
931 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
932}
933
934void Context::bindReadFramebuffer(GLuint framebuffer)
935{
936 if (!getFramebuffer(framebuffer))
937 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000938 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000939 }
940
941 mState.readFramebuffer = framebuffer;
942}
943
944void Context::bindDrawFramebuffer(GLuint framebuffer)
945{
946 if (!getFramebuffer(framebuffer))
947 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000948 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000949 }
950
951 mState.drawFramebuffer = framebuffer;
952
953 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
954}
955
956void Context::bindRenderbuffer(GLuint renderbuffer)
957{
958 mResourceManager->checkRenderbufferAllocation(renderbuffer);
959
960 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
961}
962
963void Context::useProgram(GLuint program)
964{
965 GLuint priorProgram = mState.currentProgram;
966 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
967
968 if (priorProgram != program)
969 {
970 Program *newProgram = mResourceManager->getProgram(program);
971 Program *oldProgram = mResourceManager->getProgram(priorProgram);
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000972 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000973 mDxUniformsDirty = true;
974
975 if (newProgram)
976 {
977 newProgram->addRef();
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000978 mCurrentProgramBinary.set(newProgram->getProgramBinary());
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000979 }
980
981 if (oldProgram)
982 {
983 oldProgram->release();
984 }
985 }
986}
987
daniel@transgaming.com95d29422012-07-24 18:36:10 +0000988void Context::linkProgram(GLuint program)
989{
990 Program *programObject = mResourceManager->getProgram(program);
991
daniel@transgaming.com12394cf2012-07-24 18:37:59 +0000992 bool linked = programObject->link();
993
994 // if the current program was relinked successfully we
995 // need to install the new executables
996 if (linked && program == mState.currentProgram)
997 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000998 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +0000999 mDxUniformsDirty = true;
1000 }
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001001}
1002
1003void Context::setProgramBinary(GLuint program, const void *binary, GLint length)
1004{
1005 Program *programObject = mResourceManager->getProgram(program);
1006
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001007 bool loaded = programObject->setProgramBinary(binary, length);
1008
1009 // if the current program was reloaded successfully we
1010 // need to install the new executables
1011 if (loaded && program == mState.currentProgram)
1012 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001013 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001014 mDxUniformsDirty = true;
1015 }
1016
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001017}
1018
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001019void Context::beginQuery(GLenum target, GLuint query)
1020{
1021 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1022 // of zero, if the active query object name for <target> is non-zero (for the
1023 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1024 // the active query for either target is non-zero), if <id> is the name of an
1025 // existing query object whose type does not match <target>, or if <id> is the
1026 // active query object name for any query type, the error INVALID_OPERATION is
1027 // generated.
1028
1029 // Ensure no other queries are active
1030 // NOTE: If other queries than occlusion are supported, we will need to check
1031 // separately that:
1032 // a) The query ID passed is not the current active query for any target/type
1033 // b) There are no active queries for the requested target (and in the case
1034 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1035 // no query may be active for either if glBeginQuery targets either.
1036 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1037 {
1038 if (mState.activeQuery[i].get() != NULL)
1039 {
1040 return error(GL_INVALID_OPERATION);
1041 }
1042 }
1043
1044 QueryType qType;
1045 switch (target)
1046 {
1047 case GL_ANY_SAMPLES_PASSED_EXT:
1048 qType = QUERY_ANY_SAMPLES_PASSED;
1049 break;
1050 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1051 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1052 break;
1053 default:
1054 ASSERT(false);
1055 return;
1056 }
1057
1058 Query *queryObject = getQuery(query, true, target);
1059
1060 // check that name was obtained with glGenQueries
1061 if (!queryObject)
1062 {
1063 return error(GL_INVALID_OPERATION);
1064 }
1065
1066 // check for type mismatch
1067 if (queryObject->getType() != target)
1068 {
1069 return error(GL_INVALID_OPERATION);
1070 }
1071
1072 // set query as active for specified target
1073 mState.activeQuery[qType].set(queryObject);
1074
1075 // begin query
1076 queryObject->begin();
1077}
1078
1079void Context::endQuery(GLenum target)
1080{
1081 QueryType qType;
1082
1083 switch (target)
1084 {
1085 case GL_ANY_SAMPLES_PASSED_EXT:
1086 qType = QUERY_ANY_SAMPLES_PASSED;
1087 break;
1088 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1089 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1090 break;
1091 default:
1092 ASSERT(false);
1093 return;
1094 }
1095
1096 Query *queryObject = mState.activeQuery[qType].get();
1097
1098 if (queryObject == NULL)
1099 {
1100 return error(GL_INVALID_OPERATION);
1101 }
1102
1103 queryObject->end();
1104
1105 mState.activeQuery[qType].set(NULL);
1106}
1107
1108void Context::setFramebufferZero(Framebuffer *buffer)
1109{
1110 delete mFramebufferMap[0];
1111 mFramebufferMap[0] = buffer;
1112 if (mState.drawFramebuffer == 0)
1113 {
1114 mBoundDrawFramebuffer = buffer;
1115 }
1116}
1117
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001118void Context::setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001119{
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001120 RenderbufferStorage *renderbuffer = NULL;
1121 switch (internalformat)
1122 {
1123 case GL_DEPTH_COMPONENT16:
1124 renderbuffer = new gl::Depthbuffer(mRenderer, width, height, samples);
1125 break;
1126 case GL_RGBA4:
1127 case GL_RGB5_A1:
1128 case GL_RGB565:
1129 case GL_RGB8_OES:
1130 case GL_RGBA8_OES:
1131 renderbuffer = new gl::Colorbuffer(mRenderer,width, height, internalformat, samples);
1132 break;
1133 case GL_STENCIL_INDEX8:
1134 renderbuffer = new gl::Stencilbuffer(mRenderer, width, height, samples);
1135 break;
1136 case GL_DEPTH24_STENCIL8_OES:
1137 renderbuffer = new gl::DepthStencilbuffer(mRenderer, width, height, samples);
1138 break;
1139 default:
1140 UNREACHABLE(); return;
1141 }
1142
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001143 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1144 renderbufferObject->setStorage(renderbuffer);
1145}
1146
1147Framebuffer *Context::getFramebuffer(unsigned int handle)
1148{
1149 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1150
1151 if (framebuffer == mFramebufferMap.end())
1152 {
1153 return NULL;
1154 }
1155 else
1156 {
1157 return framebuffer->second;
1158 }
1159}
1160
1161Fence *Context::getFence(unsigned int handle)
1162{
1163 FenceMap::iterator fence = mFenceMap.find(handle);
1164
1165 if (fence == mFenceMap.end())
1166 {
1167 return NULL;
1168 }
1169 else
1170 {
1171 return fence->second;
1172 }
1173}
1174
1175Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1176{
1177 QueryMap::iterator query = mQueryMap.find(handle);
1178
1179 if (query == mQueryMap.end())
1180 {
1181 return NULL;
1182 }
1183 else
1184 {
1185 if (!query->second && create)
1186 {
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001187 query->second = new Query(mRenderer, handle, type);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001188 query->second->addRef();
1189 }
1190 return query->second;
1191 }
1192}
1193
1194Buffer *Context::getArrayBuffer()
1195{
1196 return mState.arrayBuffer.get();
1197}
1198
1199Buffer *Context::getElementArrayBuffer()
1200{
1201 return mState.elementArrayBuffer.get();
1202}
1203
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001204ProgramBinary *Context::getCurrentProgramBinary()
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001205{
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001206 return mCurrentProgramBinary.get();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001207}
1208
1209Texture2D *Context::getTexture2D()
1210{
1211 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1212}
1213
1214TextureCubeMap *Context::getTextureCubeMap()
1215{
1216 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1217}
1218
1219Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
1220{
1221 GLuint texid = mState.samplerTexture[type][sampler].id();
1222
1223 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
1224 {
1225 switch (type)
1226 {
1227 default: UNREACHABLE();
1228 case TEXTURE_2D: return mTexture2DZero.get();
1229 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
1230 }
1231 }
1232
1233 return mState.samplerTexture[type][sampler].get();
1234}
1235
1236bool Context::getBooleanv(GLenum pname, GLboolean *params)
1237{
1238 switch (pname)
1239 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001240 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1241 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1242 case GL_DEPTH_WRITEMASK: *params = mState.depthStencil.depthMask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001243 case GL_COLOR_WRITEMASK:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001244 params[0] = mState.blend.colorMaskRed;
1245 params[1] = mState.blend.colorMaskGreen;
1246 params[2] = mState.blend.colorMaskBlue;
1247 params[3] = mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001248 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001249 case GL_CULL_FACE: *params = mState.rasterizer.cullFace; break;
1250 case GL_POLYGON_OFFSET_FILL: *params = mState.rasterizer.polygonOffsetFill; break;
1251 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.blend.sampleAlphaToCoverage; break;
1252 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1253 case GL_SCISSOR_TEST: *params = mState.rasterizer.scissorTest; break;
1254 case GL_STENCIL_TEST: *params = mState.depthStencil.stencilTest; break;
1255 case GL_DEPTH_TEST: *params = mState.depthStencil.depthTest; break;
1256 case GL_BLEND: *params = mState.blend.blend; break;
1257 case GL_DITHER: *params = mState.blend.dither; break;
1258 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001259 default:
1260 return false;
1261 }
1262
1263 return true;
1264}
1265
1266bool Context::getFloatv(GLenum pname, GLfloat *params)
1267{
1268 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1269 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1270 // GetIntegerv as its native query function. As it would require conversion in any
1271 // case, this should make no difference to the calling application.
1272 switch (pname)
1273 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001274 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1275 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1276 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1277 case GL_POLYGON_OFFSET_FACTOR: *params = mState.rasterizer.polygonOffsetFactor; break;
1278 case GL_POLYGON_OFFSET_UNITS: *params = mState.rasterizer.polygonOffsetUnits; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001279 case GL_ALIASED_LINE_WIDTH_RANGE:
1280 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1281 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1282 break;
1283 case GL_ALIASED_POINT_SIZE_RANGE:
1284 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1285 params[1] = getMaximumPointSize();
1286 break;
1287 case GL_DEPTH_RANGE:
1288 params[0] = mState.zNear;
1289 params[1] = mState.zFar;
1290 break;
1291 case GL_COLOR_CLEAR_VALUE:
1292 params[0] = mState.colorClearValue.red;
1293 params[1] = mState.colorClearValue.green;
1294 params[2] = mState.colorClearValue.blue;
1295 params[3] = mState.colorClearValue.alpha;
1296 break;
1297 case GL_BLEND_COLOR:
1298 params[0] = mState.blendColor.red;
1299 params[1] = mState.blendColor.green;
1300 params[2] = mState.blendColor.blue;
1301 params[3] = mState.blendColor.alpha;
1302 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001303 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1304 if (!supportsTextureFilterAnisotropy())
1305 {
1306 return false;
1307 }
1308 *params = mMaxTextureAnisotropy;
1309 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001310 default:
1311 return false;
1312 }
1313
1314 return true;
1315}
1316
1317bool Context::getIntegerv(GLenum pname, GLint *params)
1318{
1319 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1320 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1321 // GetIntegerv as its native query function. As it would require conversion in any
1322 // case, this should make no difference to the calling application. You may find it in
1323 // Context::getFloatv.
1324 switch (pname)
1325 {
1326 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1327 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1328 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
1329 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1330 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
1331 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1332 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
1333 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
1334 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1335 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1336 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1337 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
1338 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1339 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1340 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1341 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
1342 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1343 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1344 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
1345 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1346 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1347 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1348 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001349 case GL_STENCIL_FUNC: *params = mState.depthStencil.stencilFunc; break;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001350 case GL_STENCIL_REF: *params = mState.stencilRef; break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001351 case GL_STENCIL_VALUE_MASK: *params = mState.depthStencil.stencilMask; break;
1352 case GL_STENCIL_BACK_FUNC: *params = mState.depthStencil.stencilBackFunc; break;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001353 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001354 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.depthStencil.stencilBackMask; break;
1355 case GL_STENCIL_FAIL: *params = mState.depthStencil.stencilFail; break;
1356 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilPassDepthFail; break;
1357 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilPassDepthPass; break;
1358 case GL_STENCIL_BACK_FAIL: *params = mState.depthStencil.stencilBackFail; break;
1359 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilBackPassDepthFail; break;
1360 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilBackPassDepthPass; break;
1361 case GL_DEPTH_FUNC: *params = mState.depthStencil.depthFunc; break;
1362 case GL_BLEND_SRC_RGB: *params = mState.blend.sourceBlendRGB; break;
1363 case GL_BLEND_SRC_ALPHA: *params = mState.blend.sourceBlendAlpha; break;
1364 case GL_BLEND_DST_RGB: *params = mState.blend.destBlendRGB; break;
1365 case GL_BLEND_DST_ALPHA: *params = mState.blend.destBlendAlpha; break;
1366 case GL_BLEND_EQUATION_RGB: *params = mState.blend.blendEquationRGB; break;
1367 case GL_BLEND_EQUATION_ALPHA: *params = mState.blend.blendEquationAlpha; break;
1368 case GL_STENCIL_WRITEMASK: *params = mState.depthStencil.stencilWritemask; break;
1369 case GL_STENCIL_BACK_WRITEMASK: *params = mState.depthStencil.stencilBackWritemask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001370 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1371 case GL_SUBPIXEL_BITS: *params = 4; break;
1372 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1373 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
1374 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1375 params[0] = mNumCompressedTextureFormats;
1376 break;
1377 case GL_MAX_SAMPLES_ANGLE:
1378 {
1379 GLsizei maxSamples = getMaxSupportedSamples();
1380 if (maxSamples != 0)
1381 {
1382 *params = maxSamples;
1383 }
1384 else
1385 {
1386 return false;
1387 }
1388
1389 break;
1390 }
1391 case GL_SAMPLE_BUFFERS:
1392 case GL_SAMPLES:
1393 {
1394 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1395 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1396 {
1397 switch (pname)
1398 {
1399 case GL_SAMPLE_BUFFERS:
1400 if (framebuffer->getSamples() != 0)
1401 {
1402 *params = 1;
1403 }
1404 else
1405 {
1406 *params = 0;
1407 }
1408 break;
1409 case GL_SAMPLES:
1410 *params = framebuffer->getSamples();
1411 break;
1412 }
1413 }
1414 else
1415 {
1416 *params = 0;
1417 }
1418 }
1419 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00001420 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1421 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1422 {
1423 GLenum format, type;
1424 if (getCurrentReadFormatType(&format, &type))
1425 {
1426 if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT)
1427 *params = format;
1428 else
1429 *params = type;
1430 }
1431 }
1432 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001433 case GL_MAX_VIEWPORT_DIMS:
1434 {
1435 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
1436 params[0] = maxDimension;
1437 params[1] = maxDimension;
1438 }
1439 break;
1440 case GL_COMPRESSED_TEXTURE_FORMATS:
1441 {
1442 if (supportsDXT1Textures())
1443 {
1444 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1445 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1446 }
1447 if (supportsDXT3Textures())
1448 {
1449 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1450 }
1451 if (supportsDXT5Textures())
1452 {
1453 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
1454 }
1455 }
1456 break;
1457 case GL_VIEWPORT:
1458 params[0] = mState.viewportX;
1459 params[1] = mState.viewportY;
1460 params[2] = mState.viewportWidth;
1461 params[3] = mState.viewportHeight;
1462 break;
1463 case GL_SCISSOR_BOX:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001464 params[0] = mState.scissor.x;
1465 params[1] = mState.scissor.y;
1466 params[2] = mState.scissor.width;
1467 params[3] = mState.scissor.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001468 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001469 case GL_CULL_FACE_MODE: *params = mState.rasterizer.cullMode; break;
1470 case GL_FRONT_FACE: *params = mState.rasterizer.frontFace; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001471 case GL_RED_BITS:
1472 case GL_GREEN_BITS:
1473 case GL_BLUE_BITS:
1474 case GL_ALPHA_BITS:
1475 {
1476 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1477 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1478
1479 if (colorbuffer)
1480 {
1481 switch (pname)
1482 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001483 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1484 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1485 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1486 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001487 }
1488 }
1489 else
1490 {
1491 *params = 0;
1492 }
1493 }
1494 break;
1495 case GL_DEPTH_BITS:
1496 {
1497 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1498 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1499
1500 if (depthbuffer)
1501 {
1502 *params = depthbuffer->getDepthSize();
1503 }
1504 else
1505 {
1506 *params = 0;
1507 }
1508 }
1509 break;
1510 case GL_STENCIL_BITS:
1511 {
1512 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1513 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1514
1515 if (stencilbuffer)
1516 {
1517 *params = stencilbuffer->getStencilSize();
1518 }
1519 else
1520 {
1521 *params = 0;
1522 }
1523 }
1524 break;
1525 case GL_TEXTURE_BINDING_2D:
1526 {
1527 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1528 {
1529 error(GL_INVALID_OPERATION);
1530 return false;
1531 }
1532
1533 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
1534 }
1535 break;
1536 case GL_TEXTURE_BINDING_CUBE_MAP:
1537 {
1538 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1539 {
1540 error(GL_INVALID_OPERATION);
1541 return false;
1542 }
1543
1544 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
1545 }
1546 break;
1547 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1548 *params = mResetStrategy;
1549 break;
1550 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1551 *params = 1;
1552 break;
1553 case GL_PROGRAM_BINARY_FORMATS_OES:
1554 *params = GL_PROGRAM_BINARY_ANGLE;
1555 break;
1556 default:
1557 return false;
1558 }
1559
1560 return true;
1561}
1562
1563bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1564{
1565 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1566 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1567 // to the fact that it is stored internally as a float, and so would require conversion
1568 // if returned from Context::getIntegerv. Since this conversion is already implemented
1569 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1570 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1571 // application.
1572 switch (pname)
1573 {
1574 case GL_COMPRESSED_TEXTURE_FORMATS:
1575 {
1576 *type = GL_INT;
1577 *numParams = mNumCompressedTextureFormats;
1578 }
1579 break;
1580 case GL_SHADER_BINARY_FORMATS:
1581 {
1582 *type = GL_INT;
1583 *numParams = 0;
1584 }
1585 break;
1586 case GL_MAX_VERTEX_ATTRIBS:
1587 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1588 case GL_MAX_VARYING_VECTORS:
1589 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1590 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1591 case GL_MAX_TEXTURE_IMAGE_UNITS:
1592 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1593 case GL_MAX_RENDERBUFFER_SIZE:
1594 case GL_NUM_SHADER_BINARY_FORMATS:
1595 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1596 case GL_ARRAY_BUFFER_BINDING:
1597 case GL_FRAMEBUFFER_BINDING:
1598 case GL_RENDERBUFFER_BINDING:
1599 case GL_CURRENT_PROGRAM:
1600 case GL_PACK_ALIGNMENT:
1601 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1602 case GL_UNPACK_ALIGNMENT:
1603 case GL_GENERATE_MIPMAP_HINT:
1604 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1605 case GL_RED_BITS:
1606 case GL_GREEN_BITS:
1607 case GL_BLUE_BITS:
1608 case GL_ALPHA_BITS:
1609 case GL_DEPTH_BITS:
1610 case GL_STENCIL_BITS:
1611 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1612 case GL_CULL_FACE_MODE:
1613 case GL_FRONT_FACE:
1614 case GL_ACTIVE_TEXTURE:
1615 case GL_STENCIL_FUNC:
1616 case GL_STENCIL_VALUE_MASK:
1617 case GL_STENCIL_REF:
1618 case GL_STENCIL_FAIL:
1619 case GL_STENCIL_PASS_DEPTH_FAIL:
1620 case GL_STENCIL_PASS_DEPTH_PASS:
1621 case GL_STENCIL_BACK_FUNC:
1622 case GL_STENCIL_BACK_VALUE_MASK:
1623 case GL_STENCIL_BACK_REF:
1624 case GL_STENCIL_BACK_FAIL:
1625 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1626 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1627 case GL_DEPTH_FUNC:
1628 case GL_BLEND_SRC_RGB:
1629 case GL_BLEND_SRC_ALPHA:
1630 case GL_BLEND_DST_RGB:
1631 case GL_BLEND_DST_ALPHA:
1632 case GL_BLEND_EQUATION_RGB:
1633 case GL_BLEND_EQUATION_ALPHA:
1634 case GL_STENCIL_WRITEMASK:
1635 case GL_STENCIL_BACK_WRITEMASK:
1636 case GL_STENCIL_CLEAR_VALUE:
1637 case GL_SUBPIXEL_BITS:
1638 case GL_MAX_TEXTURE_SIZE:
1639 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1640 case GL_SAMPLE_BUFFERS:
1641 case GL_SAMPLES:
1642 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1643 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1644 case GL_TEXTURE_BINDING_2D:
1645 case GL_TEXTURE_BINDING_CUBE_MAP:
1646 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1647 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1648 case GL_PROGRAM_BINARY_FORMATS_OES:
1649 {
1650 *type = GL_INT;
1651 *numParams = 1;
1652 }
1653 break;
1654 case GL_MAX_SAMPLES_ANGLE:
1655 {
1656 if (getMaxSupportedSamples() != 0)
1657 {
1658 *type = GL_INT;
1659 *numParams = 1;
1660 }
1661 else
1662 {
1663 return false;
1664 }
1665 }
1666 break;
1667 case GL_MAX_VIEWPORT_DIMS:
1668 {
1669 *type = GL_INT;
1670 *numParams = 2;
1671 }
1672 break;
1673 case GL_VIEWPORT:
1674 case GL_SCISSOR_BOX:
1675 {
1676 *type = GL_INT;
1677 *numParams = 4;
1678 }
1679 break;
1680 case GL_SHADER_COMPILER:
1681 case GL_SAMPLE_COVERAGE_INVERT:
1682 case GL_DEPTH_WRITEMASK:
1683 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1684 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1685 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1686 case GL_SAMPLE_COVERAGE:
1687 case GL_SCISSOR_TEST:
1688 case GL_STENCIL_TEST:
1689 case GL_DEPTH_TEST:
1690 case GL_BLEND:
1691 case GL_DITHER:
1692 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1693 {
1694 *type = GL_BOOL;
1695 *numParams = 1;
1696 }
1697 break;
1698 case GL_COLOR_WRITEMASK:
1699 {
1700 *type = GL_BOOL;
1701 *numParams = 4;
1702 }
1703 break;
1704 case GL_POLYGON_OFFSET_FACTOR:
1705 case GL_POLYGON_OFFSET_UNITS:
1706 case GL_SAMPLE_COVERAGE_VALUE:
1707 case GL_DEPTH_CLEAR_VALUE:
1708 case GL_LINE_WIDTH:
1709 {
1710 *type = GL_FLOAT;
1711 *numParams = 1;
1712 }
1713 break;
1714 case GL_ALIASED_LINE_WIDTH_RANGE:
1715 case GL_ALIASED_POINT_SIZE_RANGE:
1716 case GL_DEPTH_RANGE:
1717 {
1718 *type = GL_FLOAT;
1719 *numParams = 2;
1720 }
1721 break;
1722 case GL_COLOR_CLEAR_VALUE:
1723 case GL_BLEND_COLOR:
1724 {
1725 *type = GL_FLOAT;
1726 *numParams = 4;
1727 }
1728 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001729 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1730 if (!supportsTextureFilterAnisotropy())
1731 {
1732 return false;
1733 }
1734 *type = GL_FLOAT;
1735 *numParams = 1;
1736 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001737 default:
1738 return false;
1739 }
1740
1741 return true;
1742}
1743
1744// Applies the render target surface, depth stencil surface, viewport rectangle and
1745// scissor rectangle to the Direct3D 9 device
1746bool Context::applyRenderTarget(bool ignoreViewport)
1747{
1748 Framebuffer *framebufferObject = getDrawFramebuffer();
1749
1750 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1751 {
1752 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1753 }
1754
daniel@transgaming.com8a8b24c2012-11-28 19:36:26 +00001755 mRenderer->applyRenderTarget(framebufferObject);
1756
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001757 // if there is no color attachment we must synthesize a NULL colorattachment
1758 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1759 Renderbuffer *renderbufferObject = NULL;
1760 if (framebufferObject->getColorbufferType() != GL_NONE)
1761 {
1762 renderbufferObject = framebufferObject->getColorbuffer();
1763 }
1764 else
1765 {
1766 renderbufferObject = framebufferObject->getNullColorbuffer();
1767 }
1768 if (!renderbufferObject)
1769 {
1770 ERR("unable to locate renderbuffer for FBO.");
1771 return false;
1772 }
1773
1774 bool renderTargetChanged = false;
1775 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1776 if (renderTargetSerial != mAppliedRenderTargetSerial)
1777 {
1778 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1779 if (!renderTarget)
1780 {
1781 ERR("render target pointer unexpectedly null.");
1782 return false; // Context must be lost
1783 }
1784 mDevice->SetRenderTarget(0, renderTarget);
1785 mAppliedRenderTargetSerial = renderTargetSerial;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001786 renderTargetChanged = true;
1787 renderTarget->Release();
1788 }
1789
1790 IDirect3DSurface9 *depthStencil = NULL;
1791 unsigned int depthbufferSerial = 0;
1792 unsigned int stencilbufferSerial = 0;
1793 if (framebufferObject->getDepthbufferType() != GL_NONE)
1794 {
1795 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1796 depthStencil = depthbuffer->getDepthStencil();
1797 if (!depthStencil)
1798 {
1799 ERR("Depth stencil pointer unexpectedly null.");
1800 return false;
1801 }
1802
1803 depthbufferSerial = depthbuffer->getSerial();
1804 }
1805 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1806 {
1807 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1808 depthStencil = stencilbuffer->getDepthStencil();
1809 if (!depthStencil)
1810 {
1811 ERR("Depth stencil pointer unexpectedly null.");
1812 return false;
1813 }
1814
1815 stencilbufferSerial = stencilbuffer->getSerial();
1816 }
1817
1818 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1819 stencilbufferSerial != mAppliedStencilbufferSerial ||
1820 !mDepthStencilInitialized)
1821 {
1822 mDevice->SetDepthStencilSurface(depthStencil);
1823 mAppliedDepthbufferSerial = depthbufferSerial;
1824 mAppliedStencilbufferSerial = stencilbufferSerial;
1825 mDepthStencilInitialized = true;
1826 }
1827
1828 if (depthStencil)
1829 {
1830 depthStencil->Release();
1831 }
1832
1833 if (!mRenderTargetDescInitialized || renderTargetChanged)
1834 {
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00001835 mRenderTargetDesc.width = renderbufferObject->getWidth();
1836 mRenderTargetDesc.height = renderbufferObject->getHeight();
1837 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001838 mRenderTargetDescInitialized = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001839 }
1840
1841 D3DVIEWPORT9 viewport;
1842
1843 float zNear = clamp01(mState.zNear);
1844 float zFar = clamp01(mState.zFar);
1845
1846 if (ignoreViewport)
1847 {
1848 viewport.X = 0;
1849 viewport.Y = 0;
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00001850 viewport.Width = mRenderTargetDesc.width;
1851 viewport.Height = mRenderTargetDesc.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001852 viewport.MinZ = 0.0f;
1853 viewport.MaxZ = 1.0f;
1854 }
1855 else
1856 {
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00001857 viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.width));
1858 viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.height));
1859 viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.width) - static_cast<LONG>(viewport.X));
1860 viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.height) - static_cast<LONG>(viewport.Y));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001861 viewport.MinZ = zNear;
1862 viewport.MaxZ = zFar;
1863 }
1864
1865 if (viewport.Width <= 0 || viewport.Height <= 0)
1866 {
1867 return false; // Nothing to render
1868 }
1869
1870 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1871 {
1872 mDevice->SetViewport(&viewport);
1873 mSetViewport = viewport;
1874 mViewportInitialized = true;
1875 mDxUniformsDirty = true;
1876 }
1877
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00001878 mRenderer->setScissorRectangle(mState.scissor, static_cast<int>(mRenderTargetDesc.width),
1879 static_cast<int>(mRenderTargetDesc.height));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001880
1881 if (mState.currentProgram && mDxUniformsDirty)
1882 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001883 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001884
1885 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
1886 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
1887 programBinary->setUniform2fv(halfPixelSize, 1, xy);
1888
1889 // These values are used for computing gl_FragCoord in Program::linkVaryings().
1890 GLint coord = programBinary->getDxCoordLocation();
1891 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1892 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1893 (float)mState.viewportY + mState.viewportHeight / 2.0f};
1894 programBinary->setUniform4fv(coord, 1, whxy);
1895
1896 GLint depth = programBinary->getDxDepthLocation();
1897 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
1898 programBinary->setUniform2fv(depth, 1, dz);
1899
1900 GLint depthRange = programBinary->getDxDepthRangeLocation();
1901 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1902 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
1903 mDxUniformsDirty = false;
1904 }
1905
1906 return true;
1907}
1908
1909// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
1910void Context::applyState(GLenum drawMode)
1911{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001912 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001913
1914 Framebuffer *framebufferObject = getDrawFramebuffer();
1915
1916 GLint frontCCW = programBinary->getDxFrontCCWLocation();
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001917 GLint ccw = (mState.rasterizer.frontFace == GL_CCW);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001918 programBinary->setUniform1iv(frontCCW, 1, &ccw);
1919
1920 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
1921 GLint alwaysFront = !isTriangleMode(drawMode);
1922 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1923
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +00001924 const gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001925 unsigned int depthSize = depthbuffer ? depthbuffer->getDepthSize() : 0;
1926
1927 mRenderer->setRasterizerState(mState.rasterizer, depthSize);
1928
1929 unsigned int mask = 0;
1930 if (mState.sampleCoverage)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001931 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001932 if (mState.sampleCoverageValue != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001933 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001934 float threshold = 0.5f;
1935
1936 for (int i = 0; i < framebufferObject->getSamples(); ++i)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001937 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001938 mask <<= 1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001939
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001940 if ((i + 1) * mState.sampleCoverageValue >= threshold)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001941 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001942 threshold += 1.0f;
1943 mask |= 1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001944 }
1945 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001946 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001947
1948 if (mState.sampleCoverageInvert)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001949 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001950 mask = ~mask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001951 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001952 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001953 else
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001954 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001955 mask = 0xFFFFFFFF;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001956 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001957 mRenderer->setBlendState(mState.blend, mState.blendColor, mask);
1958
1959 unsigned int stencilSize = framebufferObject->hasStencil() ? framebufferObject->getStencilbuffer()->getStencilSize() : 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001960 mRenderer->setDepthStencilState(mState.depthStencil, mState.stencilRef, mState.stencilBackRef,
1961 mState.rasterizer.frontFace == GL_CCW, stencilSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001962}
1963
1964GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
1965{
1966 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
1967
daniel@transgaming.com408caa52012-10-31 18:47:01 +00001968 ProgramBinary *programBinary = getCurrentProgramBinary();
1969 GLenum err = mVertexDataManager->prepareVertexData(mState.vertexAttribute, programBinary, first, count, attributes, instances);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001970 if (err != GL_NO_ERROR)
1971 {
1972 return err;
1973 }
daniel@transgaming.com408caa52012-10-31 18:47:01 +00001974
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00001975 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001976}
1977
1978// Applies the indices and element array bindings to the Direct3D 9 device
1979GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
1980{
1981 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
1982
1983 if (err == GL_NO_ERROR)
1984 {
1985 if (indexInfo->serial != mAppliedIBSerial)
1986 {
1987 mDevice->SetIndices(indexInfo->indexBuffer);
1988 mAppliedIBSerial = indexInfo->serial;
1989 }
1990 }
1991
1992 return err;
1993}
1994
1995// Applies the shaders and shader constants to the Direct3D 9 device
1996void Context::applyShaders()
1997{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001998 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001999
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002000 if (programBinary->getSerial() != mAppliedProgramBinarySerial)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002001 {
2002 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2003 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
2004
2005 mDevice->SetPixelShader(pixelShader);
2006 mDevice->SetVertexShader(vertexShader);
2007 programBinary->dirtyAllUniforms();
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002008 mAppliedProgramBinarySerial = programBinary->getSerial();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002009 }
2010
2011 programBinary->applyUniforms();
2012}
2013
2014// Applies the textures and sampler states to the Direct3D 9 device
2015void Context::applyTextures()
2016{
2017 applyTextures(SAMPLER_PIXEL);
2018
2019 if (mSupportsVertexTexture)
2020 {
2021 applyTextures(SAMPLER_VERTEX);
2022 }
2023}
2024
2025// For each Direct3D 9 sampler of either the pixel or vertex stage,
2026// looks up the corresponding OpenGL texture image unit and texture type,
2027// and sets the texture and its addressing/filtering state (or NULL when inactive).
2028void Context::applyTextures(SamplerType type)
2029{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002030 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002031
2032 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
2033 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002034 int samplerRange = programBinary->getUsedSamplerRange(type);
2035
2036 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
2037 {
2038 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002039
2040 if (textureUnit != -1)
2041 {
2042 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
2043
2044 Texture *texture = getSamplerTexture(textureUnit, textureType);
2045 unsigned int texSerial = texture->getTextureSerial();
2046
2047 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
2048 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002049 if (texture->isSamplerComplete())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002050 {
2051 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
2052 {
daniel@transgaming.comebf139f2012-10-31 18:07:32 +00002053 SamplerState samplerState;
2054 texture->getSamplerState(&samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002055
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002056 mRenderer->setSamplerState(type, samplerIndex, samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002057 }
2058
2059 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
2060 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002061 mRenderer->setTexture(type, samplerIndex, texture);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002062 }
2063 }
2064 else
2065 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002066 mRenderer->setTexture(type, samplerIndex, getIncompleteTexture(textureType));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002067 }
2068
2069 appliedTextureSerial[samplerIndex] = texSerial;
2070 texture->resetDirty();
2071 }
2072 }
2073 else
2074 {
2075 if (appliedTextureSerial[samplerIndex] != 0)
2076 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002077 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002078 appliedTextureSerial[samplerIndex] = 0;
2079 }
2080 }
2081 }
2082
2083 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2084 {
2085 if (appliedTextureSerial[samplerIndex] != 0)
2086 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002087 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002088 appliedTextureSerial[samplerIndex] = 0;
2089 }
2090 }
2091}
2092
2093void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2094 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2095{
2096 Framebuffer *framebuffer = getReadFramebuffer();
2097
2098 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2099 {
2100 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2101 }
2102
2103 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2104 {
2105 return error(GL_INVALID_OPERATION);
2106 }
2107
daniel@transgaming.com6452adf2012-10-17 18:22:35 +00002108 GLsizei outputPitch = ComputePitch(width, ConvertSizedInternalFormat(format, type), mState.packAlignment);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002109 // sized query sanity check
2110 if (bufSize)
2111 {
2112 int requiredSize = outputPitch * height;
2113 if (requiredSize > *bufSize)
2114 {
2115 return error(GL_INVALID_OPERATION);
2116 }
2117 }
2118
2119 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2120 if (!renderTarget)
2121 {
2122 return; // Context must be lost, return silently
2123 }
2124
2125 D3DSURFACE_DESC desc;
2126 renderTarget->GetDesc(&desc);
2127
2128 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2129 {
2130 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2131 renderTarget->Release();
2132 return error(GL_OUT_OF_MEMORY);
2133 }
2134
2135 HRESULT result;
2136 IDirect3DSurface9 *systemSurface = NULL;
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002137 bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mRenderer->getShareHandleSupport() &&
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002138 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2139 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2140 if (directToPixels)
2141 {
2142 // Use the pixels ptr as a shared handle to write directly into client's memory
2143 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2144 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2145 if (FAILED(result))
2146 {
2147 // Try again without the shared handle
2148 directToPixels = false;
2149 }
2150 }
2151
2152 if (!directToPixels)
2153 {
2154 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2155 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2156 if (FAILED(result))
2157 {
2158 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2159 renderTarget->Release();
2160 return error(GL_OUT_OF_MEMORY);
2161 }
2162 }
2163
2164 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
2165 renderTarget->Release();
2166 renderTarget = NULL;
2167
2168 if (FAILED(result))
2169 {
2170 systemSurface->Release();
2171
2172 // It turns out that D3D will sometimes produce more error
2173 // codes than those documented.
2174 if (checkDeviceLost(result))
2175 return error(GL_OUT_OF_MEMORY);
2176 else
2177 {
2178 UNREACHABLE();
2179 return;
2180 }
2181
2182 }
2183
2184 if (directToPixels)
2185 {
2186 systemSurface->Release();
2187 return;
2188 }
2189
2190 RECT rect;
2191 rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
2192 rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
2193 rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2194 rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2195
2196 D3DLOCKED_RECT lock;
2197 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2198
2199 if (FAILED(result))
2200 {
2201 UNREACHABLE();
2202 systemSurface->Release();
2203
2204 return; // No sensible error to generate
2205 }
2206
2207 unsigned char *dest = (unsigned char*)pixels;
2208 unsigned short *dest16 = (unsigned short*)pixels;
2209
2210 unsigned char *source;
2211 int inputPitch;
2212 if (getPackReverseRowOrder())
2213 {
2214 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2215 inputPitch = -lock.Pitch;
2216 }
2217 else
2218 {
2219 source = (unsigned char*)lock.pBits;
2220 inputPitch = lock.Pitch;
2221 }
2222
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002223 unsigned int fastPixelSize = 0;
2224
2225 if (desc.Format == D3DFMT_A8R8G8B8 &&
2226 format == GL_BGRA_EXT &&
2227 type == GL_UNSIGNED_BYTE)
2228 {
2229 fastPixelSize = 4;
2230 }
2231 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2232 format == GL_BGRA_EXT &&
2233 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2234 (desc.Format == D3DFMT_A1R5G5B5 &&
2235 format == GL_BGRA_EXT &&
2236 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2237 {
2238 fastPixelSize = 2;
2239 }
2240 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2241 format == GL_RGBA &&
2242 type == GL_HALF_FLOAT_OES)
2243 {
2244 fastPixelSize = 8;
2245 }
2246 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2247 format == GL_RGBA &&
2248 type == GL_FLOAT)
2249 {
2250 fastPixelSize = 16;
2251 }
2252
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002253 for (int j = 0; j < rect.bottom - rect.top; j++)
2254 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002255 if (fastPixelSize != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002256 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002257 // Fast path for formats which require no translation:
2258 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2259 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2260 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2261 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2262 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2263 //
2264 // Note that buffers with no alpha go through the slow path below.
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002265 memcpy(dest + j * outputPitch,
2266 source + j * inputPitch,
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002267 (rect.right - rect.left) * fastPixelSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002268 continue;
2269 }
2270
2271 for (int i = 0; i < rect.right - rect.left; i++)
2272 {
2273 float r;
2274 float g;
2275 float b;
2276 float a;
2277
2278 switch (desc.Format)
2279 {
2280 case D3DFMT_R5G6B5:
2281 {
2282 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2283
2284 a = 1.0f;
2285 b = (rgb & 0x001F) * (1.0f / 0x001F);
2286 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2287 r = (rgb & 0xF800) * (1.0f / 0xF800);
2288 }
2289 break;
2290 case D3DFMT_A1R5G5B5:
2291 {
2292 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2293
2294 a = (argb & 0x8000) ? 1.0f : 0.0f;
2295 b = (argb & 0x001F) * (1.0f / 0x001F);
2296 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2297 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2298 }
2299 break;
2300 case D3DFMT_A8R8G8B8:
2301 {
2302 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2303
2304 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2305 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2306 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2307 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2308 }
2309 break;
2310 case D3DFMT_X8R8G8B8:
2311 {
2312 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2313
2314 a = 1.0f;
2315 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2316 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2317 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2318 }
2319 break;
2320 case D3DFMT_A2R10G10B10:
2321 {
2322 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2323
2324 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2325 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2326 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2327 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2328 }
2329 break;
2330 case D3DFMT_A32B32G32R32F:
2331 {
2332 // float formats in D3D are stored rgba, rather than the other way round
2333 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2334 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2335 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2336 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2337 }
2338 break;
2339 case D3DFMT_A16B16G16R16F:
2340 {
2341 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgaa480672012-09-05 19:32:38 +00002342 r = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2343 g = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2344 b = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2345 a = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002346 }
2347 break;
2348 default:
2349 UNIMPLEMENTED(); // FIXME
2350 UNREACHABLE();
2351 return;
2352 }
2353
2354 switch (format)
2355 {
2356 case GL_RGBA:
2357 switch (type)
2358 {
2359 case GL_UNSIGNED_BYTE:
2360 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2361 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2362 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2363 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2364 break;
2365 default: UNREACHABLE();
2366 }
2367 break;
2368 case GL_BGRA_EXT:
2369 switch (type)
2370 {
2371 case GL_UNSIGNED_BYTE:
2372 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2373 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2374 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2375 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2376 break;
2377 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2378 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2379 // this type is packed as follows:
2380 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2381 // --------------------------------------------------------------------------------
2382 // | 4th | 3rd | 2nd | 1st component |
2383 // --------------------------------------------------------------------------------
2384 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2385 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2386 ((unsigned short)(15 * a + 0.5f) << 12)|
2387 ((unsigned short)(15 * r + 0.5f) << 8) |
2388 ((unsigned short)(15 * g + 0.5f) << 4) |
2389 ((unsigned short)(15 * b + 0.5f) << 0);
2390 break;
2391 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2392 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2393 // this type is packed as follows:
2394 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2395 // --------------------------------------------------------------------------------
2396 // | 4th | 3rd | 2nd | 1st component |
2397 // --------------------------------------------------------------------------------
2398 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2399 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2400 ((unsigned short)( a + 0.5f) << 15) |
2401 ((unsigned short)(31 * r + 0.5f) << 10) |
2402 ((unsigned short)(31 * g + 0.5f) << 5) |
2403 ((unsigned short)(31 * b + 0.5f) << 0);
2404 break;
2405 default: UNREACHABLE();
2406 }
2407 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002408 case GL_RGB:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002409 switch (type)
2410 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002411 case GL_UNSIGNED_SHORT_5_6_5:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002412 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2413 ((unsigned short)(31 * b + 0.5f) << 0) |
2414 ((unsigned short)(63 * g + 0.5f) << 5) |
2415 ((unsigned short)(31 * r + 0.5f) << 11);
2416 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002417 case GL_UNSIGNED_BYTE:
2418 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2419 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2420 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2421 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002422 default: UNREACHABLE();
2423 }
2424 break;
2425 default: UNREACHABLE();
2426 }
2427 }
2428 }
2429
2430 systemSurface->UnlockRect();
2431
2432 systemSurface->Release();
2433}
2434
2435void Context::clear(GLbitfield mask)
2436{
2437 Framebuffer *framebufferObject = getDrawFramebuffer();
2438
2439 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2440 {
2441 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2442 }
2443
2444 DWORD flags = 0;
2445
2446 if (mask & GL_COLOR_BUFFER_BIT)
2447 {
2448 mask &= ~GL_COLOR_BUFFER_BIT;
2449
2450 if (framebufferObject->getColorbufferType() != GL_NONE)
2451 {
2452 flags |= D3DCLEAR_TARGET;
2453 }
2454 }
2455
2456 if (mask & GL_DEPTH_BUFFER_BIT)
2457 {
2458 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002459 if (mState.depthStencil.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002460 {
2461 flags |= D3DCLEAR_ZBUFFER;
2462 }
2463 }
2464
2465 GLuint stencilUnmasked = 0x0;
2466
2467 if (mask & GL_STENCIL_BUFFER_BIT)
2468 {
2469 mask &= ~GL_STENCIL_BUFFER_BIT;
2470 if (framebufferObject->getStencilbufferType() != GL_NONE)
2471 {
2472 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2473 if (!depthStencil)
2474 {
2475 ERR("Depth stencil pointer unexpectedly null.");
2476 return;
2477 }
2478
2479 D3DSURFACE_DESC desc;
2480 depthStencil->GetDesc(&desc);
2481 depthStencil->Release();
2482
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002483 unsigned int stencilSize = d3d9_gl::GetStencilSize(desc.Format);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002484 stencilUnmasked = (0x1 << stencilSize) - 1;
2485
2486 if (stencilUnmasked != 0x0)
2487 {
2488 flags |= D3DCLEAR_STENCIL;
2489 }
2490 }
2491 }
2492
2493 if (mask != 0)
2494 {
2495 return error(GL_INVALID_VALUE);
2496 }
2497
2498 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2499 {
2500 return;
2501 }
2502
2503 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2504 unorm<8>(mState.colorClearValue.red),
2505 unorm<8>(mState.colorClearValue.green),
2506 unorm<8>(mState.colorClearValue.blue));
2507 float depth = clamp01(mState.depthClearValue);
2508 int stencil = mState.stencilClearValue & 0x000000FF;
2509
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002510
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00002511 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002512
2513 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002514 (mState.depthStencil.stencilWritemask & stencilUnmasked) != stencilUnmasked;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002515 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002516 !(mState.blend.colorMaskRed && mState.blend.colorMaskGreen &&
2517 mState.blend.colorMaskBlue && alphaUnmasked);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002518
2519 if (needMaskedColorClear || needMaskedStencilClear)
2520 {
2521 // State which is altered in all paths from this point to the clear call is saved.
2522 // State which is altered in only some paths will be flagged dirty in the case that
2523 // that path is taken.
2524 HRESULT hr;
2525 if (mMaskedClearSavedState == NULL)
2526 {
2527 hr = mDevice->BeginStateBlock();
2528 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2529
2530 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2531 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2532 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2533 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2534 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2535 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2536 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2537 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2538 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2539 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2540 mDevice->SetPixelShader(NULL);
2541 mDevice->SetVertexShader(NULL);
2542 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2543 mDevice->SetStreamSource(0, NULL, 0, 0);
2544 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2545 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2546 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2547 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2548 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2549 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2550 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2551
2552 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2553 {
2554 mDevice->SetStreamSourceFreq(i, 1);
2555 }
2556
2557 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2558 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2559 }
2560
2561 ASSERT(mMaskedClearSavedState != NULL);
2562
2563 if (mMaskedClearSavedState != NULL)
2564 {
2565 hr = mMaskedClearSavedState->Capture();
2566 ASSERT(SUCCEEDED(hr));
2567 }
2568
2569 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2570 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2571 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2572 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2573 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2574 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2575 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2576 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2577
2578 if (flags & D3DCLEAR_TARGET)
2579 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002580 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
2581 gl_d3d9::ConvertColorMask(mState.blend.colorMaskRed,
2582 mState.blend.colorMaskGreen,
2583 mState.blend.colorMaskBlue,
2584 mState.blend.colorMaskAlpha));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002585 }
2586 else
2587 {
2588 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2589 }
2590
2591 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2592 {
2593 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2594 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2595 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2596 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002597 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.depthStencil.stencilWritemask);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002598 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2599 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2600 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002601 }
2602 else
2603 {
2604 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2605 }
2606
2607 mDevice->SetPixelShader(NULL);
2608 mDevice->SetVertexShader(NULL);
2609 mDevice->SetFVF(D3DFVF_XYZRHW);
2610 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2611 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2612 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2613 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2614 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2615 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2616 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2617
2618 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2619 {
2620 mDevice->SetStreamSourceFreq(i, 1);
2621 }
2622
2623 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2624 quad[0][0] = -0.5f;
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00002625 quad[0][1] = mRenderTargetDesc.height - 0.5f;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002626 quad[0][2] = 0.0f;
2627 quad[0][3] = 1.0f;
2628
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00002629 quad[1][0] = mRenderTargetDesc.width - 0.5f;
2630 quad[1][1] = mRenderTargetDesc.height - 0.5f;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002631 quad[1][2] = 0.0f;
2632 quad[1][3] = 1.0f;
2633
2634 quad[2][0] = -0.5f;
2635 quad[2][1] = -0.5f;
2636 quad[2][2] = 0.0f;
2637 quad[2][3] = 1.0f;
2638
daniel@transgaming.com39cee2e2012-11-28 19:39:14 +00002639 quad[3][0] = mRenderTargetDesc.width - 0.5f;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002640 quad[3][1] = -0.5f;
2641 quad[3][2] = 0.0f;
2642 quad[3][3] = 1.0f;
2643
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002644 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002645 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2646
2647 if (flags & D3DCLEAR_ZBUFFER)
2648 {
2649 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2650 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2651 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2652 }
2653
2654 if (mMaskedClearSavedState != NULL)
2655 {
2656 mMaskedClearSavedState->Apply();
2657 }
2658 }
2659 else if (flags)
2660 {
2661 mDevice->Clear(0, NULL, flags, color, depth, stencil);
2662 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00002663
2664 mRenderer->clear(mask, mState.colorClearValue, mState.depthClearValue, mState.stencilClearValue,
2665 framebufferObject);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002666}
2667
2668void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
2669{
2670 if (!mState.currentProgram)
2671 {
2672 return error(GL_INVALID_OPERATION);
2673 }
2674
2675 D3DPRIMITIVETYPE primitiveType;
2676 int primitiveCount;
2677
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002678 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002679 return error(GL_INVALID_ENUM);
2680
2681 if (primitiveCount <= 0)
2682 {
2683 return;
2684 }
2685
2686 if (!applyRenderTarget(false))
2687 {
2688 return;
2689 }
2690
2691 applyState(mode);
2692
2693 GLsizei repeatDraw = 1;
2694 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
2695 if (err != GL_NO_ERROR)
2696 {
2697 return error(err);
2698 }
2699
2700 applyShaders();
2701 applyTextures();
2702
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002703 if (!getCurrentProgramBinary()->validateSamplers(NULL))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002704 {
2705 return error(GL_INVALID_OPERATION);
2706 }
2707
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002708 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002709 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002710 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002711
2712 if (mode == GL_LINE_LOOP)
2713 {
2714 drawLineLoop(count, GL_NONE, NULL, 0);
2715 }
2716 else if (instances > 0)
2717 {
2718 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2719 if (countingIB)
2720 {
2721 if (mAppliedIBSerial != countingIB->getSerial())
2722 {
2723 mDevice->SetIndices(countingIB->getBuffer());
2724 mAppliedIBSerial = countingIB->getSerial();
2725 }
2726
2727 for (int i = 0; i < repeatDraw; i++)
2728 {
2729 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2730 }
2731 }
2732 else
2733 {
2734 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2735 return error(GL_OUT_OF_MEMORY);
2736 }
2737 }
2738 else // Regular case
2739 {
2740 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
2741 }
2742 }
2743}
2744
2745void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
2746{
2747 if (!mState.currentProgram)
2748 {
2749 return error(GL_INVALID_OPERATION);
2750 }
2751
2752 if (!indices && !mState.elementArrayBuffer)
2753 {
2754 return error(GL_INVALID_OPERATION);
2755 }
2756
2757 D3DPRIMITIVETYPE primitiveType;
2758 int primitiveCount;
2759
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002760 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002761 return error(GL_INVALID_ENUM);
2762
2763 if (primitiveCount <= 0)
2764 {
2765 return;
2766 }
2767
2768 if (!applyRenderTarget(false))
2769 {
2770 return;
2771 }
2772
2773 applyState(mode);
2774
2775 TranslatedIndexData indexInfo;
2776 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2777 if (err != GL_NO_ERROR)
2778 {
2779 return error(err);
2780 }
2781
2782 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2783 GLsizei repeatDraw = 1;
2784 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
2785 if (err != GL_NO_ERROR)
2786 {
2787 return error(err);
2788 }
2789
2790 applyShaders();
2791 applyTextures();
2792
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002793 if (!getCurrentProgramBinary()->validateSamplers(false))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002794 {
2795 return error(GL_INVALID_OPERATION);
2796 }
2797
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002798 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002799 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002800 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002801
2802 if (mode == GL_LINE_LOOP)
2803 {
2804 drawLineLoop(count, type, indices, indexInfo.minIndex);
2805 }
2806 else
2807 {
2808 for (int i = 0; i < repeatDraw; i++)
2809 {
2810 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
2811 }
2812 }
2813 }
2814}
2815
2816// Implements glFlush when block is false, glFinish when block is true
2817void Context::sync(bool block)
2818{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002819 mRenderer->sync(block);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002820}
2821
2822void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
2823{
2824 // Get the raw indices for an indexed draw
2825 if (type != GL_NONE && mState.elementArrayBuffer.get())
2826 {
2827 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2828 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2829 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2830 }
2831
2832 UINT startIndex = 0;
2833 bool succeeded = false;
2834
2835 if (supports32bitIndices())
2836 {
2837 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
2838
2839 if (!mLineLoopIB)
2840 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00002841 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002842 }
2843
2844 if (mLineLoopIB)
2845 {
2846 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2847
2848 UINT offset = 0;
2849 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
2850 startIndex = offset / 4;
2851
2852 if (data)
2853 {
2854 switch (type)
2855 {
2856 case GL_NONE: // Non-indexed draw
2857 for (int i = 0; i < count; i++)
2858 {
2859 data[i] = i;
2860 }
2861 data[count] = 0;
2862 break;
2863 case GL_UNSIGNED_BYTE:
2864 for (int i = 0; i < count; i++)
2865 {
2866 data[i] = static_cast<const GLubyte*>(indices)[i];
2867 }
2868 data[count] = static_cast<const GLubyte*>(indices)[0];
2869 break;
2870 case GL_UNSIGNED_SHORT:
2871 for (int i = 0; i < count; i++)
2872 {
2873 data[i] = static_cast<const GLushort*>(indices)[i];
2874 }
2875 data[count] = static_cast<const GLushort*>(indices)[0];
2876 break;
2877 case GL_UNSIGNED_INT:
2878 for (int i = 0; i < count; i++)
2879 {
2880 data[i] = static_cast<const GLuint*>(indices)[i];
2881 }
2882 data[count] = static_cast<const GLuint*>(indices)[0];
2883 break;
2884 default: UNREACHABLE();
2885 }
2886
2887 mLineLoopIB->unmap();
2888 succeeded = true;
2889 }
2890 }
2891 }
2892 else
2893 {
2894 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
2895
2896 if (!mLineLoopIB)
2897 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00002898 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002899 }
2900
2901 if (mLineLoopIB)
2902 {
2903 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2904
2905 UINT offset = 0;
2906 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
2907 startIndex = offset / 2;
2908
2909 if (data)
2910 {
2911 switch (type)
2912 {
2913 case GL_NONE: // Non-indexed draw
2914 for (int i = 0; i < count; i++)
2915 {
2916 data[i] = i;
2917 }
2918 data[count] = 0;
2919 break;
2920 case GL_UNSIGNED_BYTE:
2921 for (int i = 0; i < count; i++)
2922 {
2923 data[i] = static_cast<const GLubyte*>(indices)[i];
2924 }
2925 data[count] = static_cast<const GLubyte*>(indices)[0];
2926 break;
2927 case GL_UNSIGNED_SHORT:
2928 for (int i = 0; i < count; i++)
2929 {
2930 data[i] = static_cast<const GLushort*>(indices)[i];
2931 }
2932 data[count] = static_cast<const GLushort*>(indices)[0];
2933 break;
2934 case GL_UNSIGNED_INT:
2935 for (int i = 0; i < count; i++)
2936 {
2937 data[i] = static_cast<const GLuint*>(indices)[i];
2938 }
2939 data[count] = static_cast<const GLuint*>(indices)[0];
2940 break;
2941 default: UNREACHABLE();
2942 }
2943
2944 mLineLoopIB->unmap();
2945 succeeded = true;
2946 }
2947 }
2948 }
2949
2950 if (succeeded)
2951 {
2952 if (mAppliedIBSerial != mLineLoopIB->getSerial())
2953 {
2954 mDevice->SetIndices(mLineLoopIB->getBuffer());
2955 mAppliedIBSerial = mLineLoopIB->getSerial();
2956 }
2957
2958 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
2959 }
2960 else
2961 {
2962 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
2963 return error(GL_OUT_OF_MEMORY);
2964 }
2965}
2966
2967void Context::recordInvalidEnum()
2968{
2969 mInvalidEnum = true;
2970}
2971
2972void Context::recordInvalidValue()
2973{
2974 mInvalidValue = true;
2975}
2976
2977void Context::recordInvalidOperation()
2978{
2979 mInvalidOperation = true;
2980}
2981
2982void Context::recordOutOfMemory()
2983{
2984 mOutOfMemory = true;
2985}
2986
2987void Context::recordInvalidFramebufferOperation()
2988{
2989 mInvalidFramebufferOperation = true;
2990}
2991
2992// Get one of the recorded errors and clear its flag, if any.
2993// [OpenGL ES 2.0.24] section 2.5 page 13.
2994GLenum Context::getError()
2995{
2996 if (mInvalidEnum)
2997 {
2998 mInvalidEnum = false;
2999
3000 return GL_INVALID_ENUM;
3001 }
3002
3003 if (mInvalidValue)
3004 {
3005 mInvalidValue = false;
3006
3007 return GL_INVALID_VALUE;
3008 }
3009
3010 if (mInvalidOperation)
3011 {
3012 mInvalidOperation = false;
3013
3014 return GL_INVALID_OPERATION;
3015 }
3016
3017 if (mOutOfMemory)
3018 {
3019 mOutOfMemory = false;
3020
3021 return GL_OUT_OF_MEMORY;
3022 }
3023
3024 if (mInvalidFramebufferOperation)
3025 {
3026 mInvalidFramebufferOperation = false;
3027
3028 return GL_INVALID_FRAMEBUFFER_OPERATION;
3029 }
3030
3031 return GL_NO_ERROR;
3032}
3033
3034GLenum Context::getResetStatus()
3035{
3036 if (mResetStatus == GL_NO_ERROR)
3037 {
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00003038 // mResetStatus will be set by the markContextLost callback
3039 // in the case a notification is sent
3040 mRenderer->testDeviceLost(true);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003041 }
3042
3043 GLenum status = mResetStatus;
3044
3045 if (mResetStatus != GL_NO_ERROR)
3046 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003047 if (mRenderer->testDeviceResettable())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003048 {
3049 mResetStatus = GL_NO_ERROR;
3050 }
3051 }
3052
3053 return status;
3054}
3055
3056bool Context::isResetNotificationEnabled()
3057{
3058 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3059}
3060
3061bool Context::supportsShaderModel3() const
3062{
3063 return mSupportsShaderModel3;
3064}
3065
3066float Context::getMaximumPointSize() const
3067{
3068 return mSupportsShaderModel3 ? mMaximumPointSize : ALIASED_POINT_SIZE_RANGE_MAX_SM2;
3069}
3070
3071int Context::getMaximumVaryingVectors() const
3072{
3073 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3074}
3075
3076unsigned int Context::getMaximumVertexTextureImageUnits() const
3077{
3078 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3079}
3080
3081unsigned int Context::getMaximumCombinedTextureImageUnits() const
3082{
3083 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3084}
3085
3086int Context::getMaximumFragmentUniformVectors() const
3087{
3088 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3089}
3090
3091int Context::getMaxSupportedSamples() const
3092{
daniel@transgaming.comb7833982012-10-31 18:31:46 +00003093 return mRenderer->getMaxSupportedSamples();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003094}
3095
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003096bool Context::supportsEventQueries() const
3097{
3098 return mSupportsEventQueries;
3099}
3100
3101bool Context::supportsOcclusionQueries() const
3102{
3103 return mSupportsOcclusionQueries;
3104}
3105
3106bool Context::supportsDXT1Textures() const
3107{
3108 return mSupportsDXT1Textures;
3109}
3110
3111bool Context::supportsDXT3Textures() const
3112{
3113 return mSupportsDXT3Textures;
3114}
3115
3116bool Context::supportsDXT5Textures() const
3117{
3118 return mSupportsDXT5Textures;
3119}
3120
3121bool Context::supportsFloat32Textures() const
3122{
3123 return mSupportsFloat32Textures;
3124}
3125
3126bool Context::supportsFloat32LinearFilter() const
3127{
3128 return mSupportsFloat32LinearFilter;
3129}
3130
3131bool Context::supportsFloat32RenderableTextures() const
3132{
3133 return mSupportsFloat32RenderableTextures;
3134}
3135
3136bool Context::supportsFloat16Textures() const
3137{
3138 return mSupportsFloat16Textures;
3139}
3140
3141bool Context::supportsFloat16LinearFilter() const
3142{
3143 return mSupportsFloat16LinearFilter;
3144}
3145
3146bool Context::supportsFloat16RenderableTextures() const
3147{
3148 return mSupportsFloat16RenderableTextures;
3149}
3150
3151int Context::getMaximumRenderbufferDimension() const
3152{
3153 return mMaxRenderbufferDimension;
3154}
3155
3156int Context::getMaximumTextureDimension() const
3157{
3158 return mMaxTextureDimension;
3159}
3160
3161int Context::getMaximumCubeTextureDimension() const
3162{
3163 return mMaxCubeTextureDimension;
3164}
3165
3166int Context::getMaximumTextureLevel() const
3167{
3168 return mMaxTextureLevel;
3169}
3170
3171bool Context::supportsLuminanceTextures() const
3172{
3173 return mSupportsLuminanceTextures;
3174}
3175
3176bool Context::supportsLuminanceAlphaTextures() const
3177{
3178 return mSupportsLuminanceAlphaTextures;
3179}
3180
3181bool Context::supportsDepthTextures() const
3182{
3183 return mSupportsDepthTextures;
3184}
3185
3186bool Context::supports32bitIndices() const
3187{
3188 return mSupports32bitIndices;
3189}
3190
3191bool Context::supportsNonPower2Texture() const
3192{
3193 return mSupportsNonPower2Texture;
3194}
3195
3196bool Context::supportsInstancing() const
3197{
3198 return mSupportsInstancing;
3199}
3200
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003201bool Context::supportsTextureFilterAnisotropy() const
3202{
3203 return mSupportsTextureFilterAnisotropy;
3204}
3205
3206float Context::getTextureMaxAnisotropy() const
3207{
3208 return mMaxTextureAnisotropy;
3209}
3210
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003211bool Context::getCurrentReadFormatType(GLenum *format, GLenum *type)
3212{
3213 Framebuffer *framebuffer = getReadFramebuffer();
3214 if (!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3215 {
3216 return error(GL_INVALID_OPERATION, false);
3217 }
3218
3219 Renderbuffer *renderbuffer = framebuffer->getColorbuffer();
3220 if (!renderbuffer)
3221 {
3222 return error(GL_INVALID_OPERATION, false);
3223 }
3224
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003225 *format = gl::ExtractFormat(renderbuffer->getActualFormat());
3226 *type = gl::ExtractType(renderbuffer->getActualFormat());
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003227
3228 return true;
3229}
3230
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003231void Context::detachBuffer(GLuint buffer)
3232{
3233 // [OpenGL ES 2.0.24] section 2.9 page 22:
3234 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3235 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3236
3237 if (mState.arrayBuffer.id() == buffer)
3238 {
3239 mState.arrayBuffer.set(NULL);
3240 }
3241
3242 if (mState.elementArrayBuffer.id() == buffer)
3243 {
3244 mState.elementArrayBuffer.set(NULL);
3245 }
3246
3247 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3248 {
3249 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
3250 {
3251 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
3252 }
3253 }
3254}
3255
3256void Context::detachTexture(GLuint texture)
3257{
3258 // [OpenGL ES 2.0.24] section 3.8 page 84:
3259 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3260 // rebound to texture object zero
3261
3262 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3263 {
3264 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
3265 {
3266 if (mState.samplerTexture[type][sampler].id() == texture)
3267 {
3268 mState.samplerTexture[type][sampler].set(NULL);
3269 }
3270 }
3271 }
3272
3273 // [OpenGL ES 2.0.24] section 4.4 page 112:
3274 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3275 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3276 // image was attached in the currently bound framebuffer.
3277
3278 Framebuffer *readFramebuffer = getReadFramebuffer();
3279 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3280
3281 if (readFramebuffer)
3282 {
3283 readFramebuffer->detachTexture(texture);
3284 }
3285
3286 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3287 {
3288 drawFramebuffer->detachTexture(texture);
3289 }
3290}
3291
3292void Context::detachFramebuffer(GLuint framebuffer)
3293{
3294 // [OpenGL ES 2.0.24] section 4.4 page 107:
3295 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3296 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3297
3298 if (mState.readFramebuffer == framebuffer)
3299 {
3300 bindReadFramebuffer(0);
3301 }
3302
3303 if (mState.drawFramebuffer == framebuffer)
3304 {
3305 bindDrawFramebuffer(0);
3306 }
3307}
3308
3309void Context::detachRenderbuffer(GLuint renderbuffer)
3310{
3311 // [OpenGL ES 2.0.24] section 4.4 page 109:
3312 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3313 // had been executed with the target RENDERBUFFER and name of zero.
3314
3315 if (mState.renderbuffer.id() == renderbuffer)
3316 {
3317 bindRenderbuffer(0);
3318 }
3319
3320 // [OpenGL ES 2.0.24] section 4.4 page 111:
3321 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3322 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3323 // point to which this image was attached in the currently bound framebuffer.
3324
3325 Framebuffer *readFramebuffer = getReadFramebuffer();
3326 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3327
3328 if (readFramebuffer)
3329 {
3330 readFramebuffer->detachRenderbuffer(renderbuffer);
3331 }
3332
3333 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3334 {
3335 drawFramebuffer->detachRenderbuffer(renderbuffer);
3336 }
3337}
3338
3339Texture *Context::getIncompleteTexture(TextureType type)
3340{
3341 Texture *t = mIncompleteTextures[type].get();
3342
3343 if (t == NULL)
3344 {
3345 static const GLubyte color[] = { 0, 0, 0, 255 };
3346
3347 switch (type)
3348 {
3349 default:
3350 UNREACHABLE();
3351 // default falls through to TEXTURE_2D
3352
3353 case TEXTURE_2D:
3354 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003355 Texture2D *incomplete2d = new Texture2D(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003356 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3357 t = incomplete2d;
3358 }
3359 break;
3360
3361 case TEXTURE_CUBE:
3362 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003363 TextureCubeMap *incompleteCube = new TextureCubeMap(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003364
3365 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3366 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3367 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3368 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3369 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3370 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3371
3372 t = incompleteCube;
3373 }
3374 break;
3375 }
3376
3377 mIncompleteTextures[type].set(t);
3378 }
3379
3380 return t;
3381}
3382
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003383bool Context::skipDraw(GLenum drawMode)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003384{
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003385 if (drawMode == GL_POINTS)
3386 {
3387 // ProgramBinary assumes non-point rendering if gl_PointSize isn't written,
3388 // which affects varying interpolation. Since the value of gl_PointSize is
3389 // undefined when not written, just skip drawing to avoid unexpected results.
3390 if (!getCurrentProgramBinary()->usesPointSize())
3391 {
3392 // This is stictly speaking not an error, but developers should be
3393 // notified of risking undefined behavior.
3394 ERR("Point rendering without writing to gl_PointSize.");
3395
3396 return true;
3397 }
3398 }
3399 else if (isTriangleMode(drawMode))
3400 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003401 if (mState.rasterizer.cullFace && mState.rasterizer.cullMode == GL_FRONT_AND_BACK)
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003402 {
3403 return true;
3404 }
3405 }
3406
3407 return false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003408}
3409
3410bool Context::isTriangleMode(GLenum drawMode)
3411{
3412 switch (drawMode)
3413 {
3414 case GL_TRIANGLES:
3415 case GL_TRIANGLE_FAN:
3416 case GL_TRIANGLE_STRIP:
3417 return true;
3418 case GL_POINTS:
3419 case GL_LINES:
3420 case GL_LINE_LOOP:
3421 case GL_LINE_STRIP:
3422 return false;
3423 default: UNREACHABLE();
3424 }
3425
3426 return false;
3427}
3428
3429void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3430{
3431 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3432
3433 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3434 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3435 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3436 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
3437
3438 mVertexDataManager->dirtyCurrentValue(index);
3439}
3440
3441void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3442{
3443 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3444
3445 mState.vertexAttribute[index].mDivisor = divisor;
3446}
3447
3448// keep list sorted in following order
3449// OES extensions
3450// EXT extensions
3451// Vendor extensions
3452void Context::initExtensionString()
3453{
3454 mExtensionString = "";
3455
3456 // OES extensions
3457 if (supports32bitIndices())
3458 {
3459 mExtensionString += "GL_OES_element_index_uint ";
3460 }
3461
3462 mExtensionString += "GL_OES_packed_depth_stencil ";
3463 mExtensionString += "GL_OES_get_program_binary ";
3464 mExtensionString += "GL_OES_rgb8_rgba8 ";
3465 mExtensionString += "GL_OES_standard_derivatives ";
3466
3467 if (supportsFloat16Textures())
3468 {
3469 mExtensionString += "GL_OES_texture_half_float ";
3470 }
3471 if (supportsFloat16LinearFilter())
3472 {
3473 mExtensionString += "GL_OES_texture_half_float_linear ";
3474 }
3475 if (supportsFloat32Textures())
3476 {
3477 mExtensionString += "GL_OES_texture_float ";
3478 }
3479 if (supportsFloat32LinearFilter())
3480 {
3481 mExtensionString += "GL_OES_texture_float_linear ";
3482 }
3483
3484 if (supportsNonPower2Texture())
3485 {
3486 mExtensionString += "GL_OES_texture_npot ";
3487 }
3488
3489 // Multi-vendor (EXT) extensions
3490 if (supportsOcclusionQueries())
3491 {
3492 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3493 }
3494
3495 mExtensionString += "GL_EXT_read_format_bgra ";
3496 mExtensionString += "GL_EXT_robustness ";
3497
3498 if (supportsDXT1Textures())
3499 {
3500 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3501 }
3502
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003503 if (supportsTextureFilterAnisotropy())
3504 {
3505 mExtensionString += "GL_EXT_texture_filter_anisotropic ";
3506 }
3507
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003508 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3509 mExtensionString += "GL_EXT_texture_storage ";
3510
3511 // ANGLE-specific extensions
3512 if (supportsDepthTextures())
3513 {
3514 mExtensionString += "GL_ANGLE_depth_texture ";
3515 }
3516
3517 mExtensionString += "GL_ANGLE_framebuffer_blit ";
3518 if (getMaxSupportedSamples() != 0)
3519 {
3520 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3521 }
3522
3523 if (supportsInstancing())
3524 {
3525 mExtensionString += "GL_ANGLE_instanced_arrays ";
3526 }
3527
3528 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3529
3530 if (supportsDXT3Textures())
3531 {
3532 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3533 }
3534 if (supportsDXT5Textures())
3535 {
3536 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3537 }
3538
3539 mExtensionString += "GL_ANGLE_texture_usage ";
3540 mExtensionString += "GL_ANGLE_translated_shader_source ";
3541
3542 // Other vendor-specific extensions
3543 if (supportsEventQueries())
3544 {
3545 mExtensionString += "GL_NV_fence ";
3546 }
3547
3548 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3549 if (end != std::string::npos)
3550 {
3551 mExtensionString.resize(end+1);
3552 }
3553}
3554
3555const char *Context::getExtensionString() const
3556{
3557 return mExtensionString.c_str();
3558}
3559
3560void Context::initRendererString()
3561{
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003562 mRendererString = "ANGLE (";
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00003563 mRendererString += mRenderer->getAdapterDescription();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003564 mRendererString += ")";
3565}
3566
3567const char *Context::getRendererString() const
3568{
3569 return mRendererString.c_str();
3570}
3571
3572void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3573 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3574 GLbitfield mask)
3575{
3576 Framebuffer *readFramebuffer = getReadFramebuffer();
3577 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3578
3579 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3580 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3581 {
3582 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3583 }
3584
3585 if (drawFramebuffer->getSamples() != 0)
3586 {
3587 return error(GL_INVALID_OPERATION);
3588 }
3589
3590 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3591 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3592 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3593 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3594
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003595 Rectangle sourceRect;
3596 Rectangle destRect;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003597
3598 if (srcX0 < srcX1)
3599 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003600 sourceRect.x = srcX0;
3601 destRect.x = dstX0;
3602 sourceRect.width = srcX1 - srcX0;
3603 destRect.width = dstX1 - dstX0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003604 }
3605 else
3606 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003607 sourceRect.x = srcX1;
3608 destRect.x = dstX1;
3609 sourceRect.width = srcX0 - srcX1;
3610 destRect.width = dstX0 - dstX1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003611 }
3612
3613 if (srcY0 < srcY1)
3614 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003615 sourceRect.height = srcY1 - srcY0;
3616 destRect.height = dstY1 - dstY0;
3617 sourceRect.y = srcY0;
3618 destRect.y = dstY0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003619 }
3620 else
3621 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003622 sourceRect.height = srcY0 - srcY1;
3623 destRect.height = dstY0 - srcY1;
3624 sourceRect.y = srcY1;
3625 destRect.y = dstY1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003626 }
3627
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003628 Rectangle sourceScissoredRect = sourceRect;
3629 Rectangle destScissoredRect = destRect;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003630
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003631 if (mState.rasterizer.scissorTest)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003632 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003633 // Only write to parts of the destination framebuffer which pass the scissor test.
3634 if (destRect.x < mState.scissor.x)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003635 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003636 int xDiff = mState.scissor.x - destRect.x;
3637 destScissoredRect.x = mState.scissor.x;
3638 destScissoredRect.width -= xDiff;
3639 sourceScissoredRect.x += xDiff;
3640 sourceScissoredRect.width -= xDiff;
3641
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003642 }
3643
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003644 if (destRect.x + destRect.width > mState.scissor.x + mState.scissor.width)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003645 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003646 int xDiff = (destRect.x + destRect.width) - (mState.scissor.x + mState.scissor.width);
3647 destScissoredRect.width -= xDiff;
3648 sourceScissoredRect.width -= xDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003649 }
3650
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003651 if (destRect.y < mState.scissor.y)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003652 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003653 int yDiff = mState.scissor.y - destRect.y;
3654 destScissoredRect.y = mState.scissor.y;
3655 destScissoredRect.height -= yDiff;
3656 sourceScissoredRect.y += yDiff;
3657 sourceScissoredRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003658 }
3659
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003660 if (destRect.y + destRect.height > mState.scissor.y + mState.scissor.height)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003661 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003662 int yDiff = (destRect.y + destRect.height) - (mState.scissor.y + mState.scissor.height);
3663 destScissoredRect.height -= yDiff;
3664 sourceScissoredRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003665 }
3666 }
3667
3668 bool blitRenderTarget = false;
3669 bool blitDepthStencil = false;
3670
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003671 Rectangle sourceTrimmedRect = sourceScissoredRect;
3672 Rectangle destTrimmedRect = destScissoredRect;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003673
3674 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3675 // the actual draw and read surfaces.
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003676 if (sourceTrimmedRect.x < 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003677 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003678 int xDiff = 0 - sourceTrimmedRect.x;
3679 sourceTrimmedRect.x = 0;
3680 sourceTrimmedRect.width -= xDiff;
3681 destTrimmedRect.x += xDiff;
3682 destTrimmedRect.width -= xDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003683 }
3684
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003685 if (sourceTrimmedRect.x + sourceTrimmedRect.width > readBufferWidth)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003686 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003687 int xDiff = (sourceTrimmedRect.x + sourceTrimmedRect.width) - readBufferWidth;
3688 sourceTrimmedRect.width -= xDiff;
3689 destTrimmedRect.width -= xDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003690 }
3691
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003692 if (sourceTrimmedRect.y < 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003693 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003694 int yDiff = 0 - sourceTrimmedRect.y;
3695 sourceTrimmedRect.y = 0;
3696 sourceTrimmedRect.height -= yDiff;
3697 destTrimmedRect.y += yDiff;
3698 destTrimmedRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003699 }
3700
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003701 if (sourceTrimmedRect.y + sourceTrimmedRect.height > readBufferHeight)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003702 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003703 int yDiff = (sourceTrimmedRect.y + sourceTrimmedRect.height) - readBufferHeight;
3704 sourceTrimmedRect.height -= yDiff;
3705 destTrimmedRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003706 }
3707
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003708 if (destTrimmedRect.x < 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003709 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003710 int xDiff = 0 - destTrimmedRect.x;
3711 destTrimmedRect.x = 0;
3712 destTrimmedRect.width -= xDiff;
3713 sourceTrimmedRect.x += xDiff;
3714 sourceTrimmedRect.width -= xDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003715 }
3716
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003717 if (destTrimmedRect.x + destTrimmedRect.width > drawBufferWidth)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003718 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003719 int xDiff = (destTrimmedRect.x + destTrimmedRect.width) - drawBufferWidth;
3720 destTrimmedRect.width -= xDiff;
3721 sourceTrimmedRect.width -= xDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003722 }
3723
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003724 if (destTrimmedRect.y < 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003725 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003726 int yDiff = 0 - destTrimmedRect.y;
3727 destTrimmedRect.y = 0;
3728 destTrimmedRect.height -= yDiff;
3729 sourceTrimmedRect.y += yDiff;
3730 sourceTrimmedRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003731 }
3732
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003733 if (destTrimmedRect.y + destTrimmedRect.height > drawBufferHeight)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003734 {
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003735 int yDiff = (destTrimmedRect.y + destTrimmedRect.height) - drawBufferHeight;
3736 destTrimmedRect.height -= yDiff;
3737 sourceTrimmedRect.height -= yDiff;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003738 }
3739
3740 bool partialBufferCopy = false;
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003741 if (sourceTrimmedRect.height < readBufferHeight ||
3742 sourceTrimmedRect.width < readBufferWidth ||
3743 destTrimmedRect.height < drawBufferHeight ||
3744 destTrimmedRect.width < drawBufferWidth ||
3745 sourceTrimmedRect.y != 0 || destTrimmedRect.y != 0 || sourceTrimmedRect.x != 0 || destTrimmedRect.x != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003746 {
3747 partialBufferCopy = true;
3748 }
3749
3750 if (mask & GL_COLOR_BUFFER_BIT)
3751 {
3752 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3753 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3754 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3755 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3756 if (!validReadType || !validDrawType ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003757 readFramebuffer->getColorbuffer()->getActualFormat() != drawFramebuffer->getColorbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003758 {
3759 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3760 return error(GL_INVALID_OPERATION);
3761 }
3762
3763 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3764 {
3765 return error(GL_INVALID_OPERATION);
3766 }
3767
3768 blitRenderTarget = true;
3769
3770 }
3771
3772 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3773 {
3774 Renderbuffer *readDSBuffer = NULL;
3775 Renderbuffer *drawDSBuffer = NULL;
3776
3777 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3778 // both a depth and stencil buffer, it will be the same buffer.
3779
3780 if (mask & GL_DEPTH_BUFFER_BIT)
3781 {
3782 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3783 {
3784 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003785 readFramebuffer->getDepthbuffer()->getActualFormat() != drawFramebuffer->getDepthbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003786 {
3787 return error(GL_INVALID_OPERATION);
3788 }
3789
3790 blitDepthStencil = true;
3791 readDSBuffer = readFramebuffer->getDepthbuffer();
3792 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3793 }
3794 }
3795
3796 if (mask & GL_STENCIL_BUFFER_BIT)
3797 {
3798 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3799 {
3800 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003801 readFramebuffer->getStencilbuffer()->getActualFormat() != drawFramebuffer->getStencilbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003802 {
3803 return error(GL_INVALID_OPERATION);
3804 }
3805
3806 blitDepthStencil = true;
3807 readDSBuffer = readFramebuffer->getStencilbuffer();
3808 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3809 }
3810 }
3811
3812 if (partialBufferCopy)
3813 {
3814 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3815 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3816 }
3817
3818 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3819 (readDSBuffer && readDSBuffer->getSamples() != 0))
3820 {
3821 return error(GL_INVALID_OPERATION);
3822 }
3823 }
3824
3825 if (blitRenderTarget || blitDepthStencil)
3826 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003827 mRenderer->endScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003828
3829 if (blitRenderTarget)
3830 {
3831 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
3832 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
3833
daniel@transgaming.com48ab81c2012-11-28 19:39:21 +00003834 RECT finalSrcRect, finalDstRect; // TEMPORARY
3835 finalSrcRect.left = sourceTrimmedRect.x;
3836 finalSrcRect.right = sourceTrimmedRect.x + sourceTrimmedRect.width;
3837 finalSrcRect.top = sourceTrimmedRect.y;
3838 finalSrcRect.bottom = sourceTrimmedRect.y + sourceTrimmedRect.height;
3839
3840 finalDstRect.left = destTrimmedRect.x;
3841 finalDstRect.right = destTrimmedRect.x + destTrimmedRect.width;
3842 finalDstRect.top = destTrimmedRect.y;
3843 finalDstRect.bottom = destTrimmedRect.y + destTrimmedRect.height;
3844
3845 HRESULT result = mDevice->StretchRect(readRenderTarget, &finalSrcRect,
3846 drawRenderTarget, &finalDstRect, D3DTEXF_NONE);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003847
3848 readRenderTarget->Release();
3849 drawRenderTarget->Release();
3850
3851 if (FAILED(result))
3852 {
3853 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3854 return;
3855 }
3856 }
3857
3858 if (blitDepthStencil)
3859 {
3860 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
3861 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
3862
3863 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
3864
3865 readDepthStencil->Release();
3866 drawDepthStencil->Release();
3867
3868 if (FAILED(result))
3869 {
3870 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3871 return;
3872 }
3873 }
3874 }
3875}
3876
3877VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3878{
3879 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3880 {
3881 mVertexDeclCache[i].vertexDeclaration = NULL;
3882 mVertexDeclCache[i].lruCount = 0;
3883 }
3884}
3885
3886VertexDeclarationCache::~VertexDeclarationCache()
3887{
3888 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3889 {
3890 if (mVertexDeclCache[i].vertexDeclaration)
3891 {
3892 mVertexDeclCache[i].vertexDeclaration->Release();
3893 }
3894 }
3895}
3896
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00003897GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003898{
3899 *repeatDraw = 1;
3900
3901 int indexedAttribute = MAX_VERTEX_ATTRIBS;
3902 int instancedAttribute = MAX_VERTEX_ATTRIBS;
3903
3904 if (instances > 0)
3905 {
3906 // Find an indexed attribute to be mapped to D3D stream 0
3907 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3908 {
3909 if (attributes[i].active)
3910 {
3911 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
3912 {
3913 if (attributes[i].divisor == 0)
3914 {
3915 indexedAttribute = i;
3916 }
3917 }
3918 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
3919 {
3920 if (attributes[i].divisor != 0)
3921 {
3922 instancedAttribute = i;
3923 }
3924 }
3925 else break; // Found both an indexed and instanced attribute
3926 }
3927 }
3928
3929 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
3930 {
3931 return GL_INVALID_OPERATION;
3932 }
3933 }
3934
3935 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3936 D3DVERTEXELEMENT9 *element = &elements[0];
3937
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003938 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3939 {
3940 if (attributes[i].active)
3941 {
3942 int stream = i;
3943
3944 if (instances > 0)
3945 {
3946 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
3947 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
3948 {
3949 *repeatDraw = instances;
3950 }
3951 else
3952 {
3953 if (i == indexedAttribute)
3954 {
3955 stream = 0;
3956 }
3957 else if (i == 0)
3958 {
3959 stream = indexedAttribute;
3960 }
3961
3962 UINT frequency = 1;
3963
3964 if (attributes[i].divisor == 0)
3965 {
3966 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
3967 }
3968 else
3969 {
3970 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
3971 }
3972
3973 device->SetStreamSourceFreq(stream, frequency);
3974 mInstancingEnabled = true;
3975 }
3976 }
3977
3978 if (mAppliedVBs[stream].serial != attributes[i].serial ||
3979 mAppliedVBs[stream].stride != attributes[i].stride ||
3980 mAppliedVBs[stream].offset != attributes[i].offset)
3981 {
3982 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3983 mAppliedVBs[stream].serial = attributes[i].serial;
3984 mAppliedVBs[stream].stride = attributes[i].stride;
3985 mAppliedVBs[stream].offset = attributes[i].offset;
3986 }
3987
3988 element->Stream = stream;
3989 element->Offset = 0;
3990 element->Type = attributes[i].type;
3991 element->Method = D3DDECLMETHOD_DEFAULT;
3992 element->Usage = D3DDECLUSAGE_TEXCOORD;
3993 element->UsageIndex = programBinary->getSemanticIndex(i);
3994 element++;
3995 }
3996 }
3997
3998 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
3999 {
4000 if (mInstancingEnabled)
4001 {
4002 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4003 {
4004 device->SetStreamSourceFreq(i, 1);
4005 }
4006
4007 mInstancingEnabled = false;
4008 }
4009 }
4010
4011 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4012 *(element++) = end;
4013
4014 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4015 {
4016 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4017 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4018 {
4019 entry->lruCount = ++mMaxLru;
4020 if(entry->vertexDeclaration != mLastSetVDecl)
4021 {
4022 device->SetVertexDeclaration(entry->vertexDeclaration);
4023 mLastSetVDecl = entry->vertexDeclaration;
4024 }
4025
4026 return GL_NO_ERROR;
4027 }
4028 }
4029
4030 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4031
4032 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4033 {
4034 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4035 {
4036 lastCache = &mVertexDeclCache[i];
4037 }
4038 }
4039
4040 if (lastCache->vertexDeclaration != NULL)
4041 {
4042 lastCache->vertexDeclaration->Release();
4043 lastCache->vertexDeclaration = NULL;
4044 // mLastSetVDecl is set to the replacement, so we don't have to worry
4045 // about it.
4046 }
4047
4048 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4049 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4050 device->SetVertexDeclaration(lastCache->vertexDeclaration);
4051 mLastSetVDecl = lastCache->vertexDeclaration;
4052 lastCache->lruCount = ++mMaxLru;
4053
4054 return GL_NO_ERROR;
4055}
4056
4057void VertexDeclarationCache::markStateDirty()
4058{
4059 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4060 {
4061 mAppliedVBs[i].serial = 0;
4062 }
4063
4064 mLastSetVDecl = NULL;
4065 mInstancingEnabled = true; // Forces it to be disabled when not used
4066}
4067
4068}
4069
4070extern "C"
4071{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004072gl::Context *glCreateContext(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004073{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004074 return new gl::Context(shareContext, renderer, notifyResets, robustAccess);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004075}
4076
4077void glDestroyContext(gl::Context *context)
4078{
4079 delete context;
4080
4081 if (context == gl::getContext())
4082 {
4083 gl::makeCurrent(NULL, NULL, NULL);
4084 }
4085}
4086
4087void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4088{
4089 gl::makeCurrent(context, display, surface);
4090}
4091
4092gl::Context *glGetCurrentContext()
4093{
4094 return gl::getContext();
4095}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004096
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004097}