blob: b7fa89d8a24b849b1f6bf9a3c51324328958c5f7 [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;
87 mState.depthStencil.stencilRef = 0;
88 mState.depthStencil.stencilMask = -1;
89 mState.depthStencil.stencilWritemask = -1;
90 mState.depthStencil.stencilBackFunc = GL_ALWAYS;
91 mState.depthStencil.stencilBackRef = 0;
92 mState.depthStencil.stencilBackMask = - 1;
93 mState.depthStencil.stencilBackWritemask = -1;
94 mState.depthStencil.stencilFail = GL_KEEP;
95 mState.depthStencil.stencilPassDepthFail = GL_KEEP;
96 mState.depthStencil.stencilPassDepthPass = GL_KEEP;
97 mState.depthStencil.stencilBackFail = GL_KEEP;
98 mState.depthStencil.stencilBackPassDepthFail = GL_KEEP;
99 mState.depthStencil.stencilBackPassDepthPass = GL_KEEP;
100
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000101 mState.sampleCoverage = false;
102 mState.sampleCoverageValue = 1.0f;
103 mState.sampleCoverageInvert = false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000104 mState.generateMipmapHint = GL_DONT_CARE;
105 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
106
107 mState.lineWidth = 1.0f;
108
109 mState.viewportX = 0;
110 mState.viewportY = 0;
daniel@transgaming.com21290e62012-10-31 18:38:28 +0000111 mState.viewportWidth = 0;
112 mState.viewportHeight = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000113 mState.zNear = 0.0f;
114 mState.zFar = 1.0f;
115
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000116 mState.blend.colorMaskRed = true;
117 mState.blend.colorMaskGreen = true;
118 mState.blend.colorMaskBlue = true;
119 mState.blend.colorMaskAlpha = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000120
121 if (shareContext != NULL)
122 {
123 mResourceManager = shareContext->mResourceManager;
124 mResourceManager->addRef();
125 }
126 else
127 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000128 mResourceManager = new ResourceManager(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000129 }
130
131 // [OpenGL ES 2.0.24] section 3.7 page 83:
132 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
133 // and cube map texture state vectors respectively associated with them.
134 // In order that access to these initial textures not be lost, they are treated as texture
135 // objects all of whose names are 0.
136
daniel@transgaming.com370482e2012-11-28 19:32:13 +0000137 mTexture2DZero.set(new Texture2D(mRenderer, 0));
138 mTextureCubeMapZero.set(new TextureCubeMap(mRenderer, 0));
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000139
140 mState.activeSampler = 0;
141 bindArrayBuffer(0);
142 bindElementArrayBuffer(0);
143 bindTextureCubeMap(0);
144 bindTexture2D(0);
145 bindReadFramebuffer(0);
146 bindDrawFramebuffer(0);
147 bindRenderbuffer(0);
148
149 mState.currentProgram = 0;
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000150 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000151
152 mState.packAlignment = 4;
153 mState.unpackAlignment = 4;
154 mState.packReverseRowOrder = false;
155
156 mVertexDataManager = NULL;
157 mIndexDataManager = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000158 mLineLoopIB = NULL;
159
160 mInvalidEnum = false;
161 mInvalidValue = false;
162 mInvalidOperation = false;
163 mOutOfMemory = false;
164 mInvalidFramebufferOperation = false;
165
166 mHasBeenCurrent = false;
167 mContextLost = false;
168 mResetStatus = GL_NO_ERROR;
169 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
170 mRobustAccess = robustAccess;
171
172 mSupportsDXT1Textures = false;
173 mSupportsDXT3Textures = false;
174 mSupportsDXT5Textures = false;
175 mSupportsEventQueries = false;
176 mSupportsOcclusionQueries = false;
177 mNumCompressedTextureFormats = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000178 mMaskedClearSavedState = NULL;
179 markAllStateDirty();
180}
181
182Context::~Context()
183{
184 if (mState.currentProgram != 0)
185 {
186 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
187 if (programObject)
188 {
189 programObject->release();
190 }
191 mState.currentProgram = 0;
192 }
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000193 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000194
195 while (!mFramebufferMap.empty())
196 {
197 deleteFramebuffer(mFramebufferMap.begin()->first);
198 }
199
200 while (!mFenceMap.empty())
201 {
202 deleteFence(mFenceMap.begin()->first);
203 }
204
205 while (!mQueryMap.empty())
206 {
207 deleteQuery(mQueryMap.begin()->first);
208 }
209
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000210 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
211 {
212 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
213 {
214 mState.samplerTexture[type][sampler].set(NULL);
215 }
216 }
217
218 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
219 {
220 mIncompleteTextures[type].set(NULL);
221 }
222
223 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
224 {
225 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
226 }
227
228 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
229 {
230 mState.activeQuery[i].set(NULL);
231 }
232
233 mState.arrayBuffer.set(NULL);
234 mState.elementArrayBuffer.set(NULL);
235 mState.renderbuffer.set(NULL);
236
237 mTexture2DZero.set(NULL);
238 mTextureCubeMapZero.set(NULL);
239
240 delete mVertexDataManager;
241 delete mIndexDataManager;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000242 delete mLineLoopIB;
243
244 if (mMaskedClearSavedState)
245 {
246 mMaskedClearSavedState->Release();
247 }
248
249 mResourceManager->release();
250}
251
daniel@transgaming.comad629872012-11-28 19:32:06 +0000252void Context::makeCurrent(egl::Surface *surface)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000253{
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000254 mDevice = mRenderer->getDevice(); // D3D9_REMOVE
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000255
256 if (!mHasBeenCurrent)
257 {
daniel@transgaming.com408caa52012-10-31 18:47:01 +0000258 mVertexDataManager = new VertexDataManager(mRenderer);
259 mIndexDataManager = new IndexDataManager(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000260
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000261 mSupportsShaderModel3 = mRenderer->getShaderModel3Support();
262 mMaximumPointSize = mRenderer->getMaxPointSize();
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000263 mSupportsVertexTexture = mRenderer->getVertexTextureSupport();
264 mSupportsNonPower2Texture = mRenderer->getNonPower2TextureSupport();
265 mSupportsInstancing = mRenderer->getInstancingSupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000266
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000267 mMaxTextureDimension = std::min(std::min(mRenderer->getMaxTextureWidth(), mRenderer->getMaxTextureHeight()),
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000268 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
269 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
270 mMaxRenderbufferDimension = mMaxTextureDimension;
271 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000272 mMaxTextureAnisotropy = mRenderer->getTextureMaxAnisotropy();
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000273 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f",
274 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000275
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000276 mSupportsEventQueries = mRenderer->getEventQuerySupport();
277 mSupportsOcclusionQueries = mRenderer->getOcclusionQuerySupport();
278 mSupportsDXT1Textures = mRenderer->getDXT1TextureSupport();
279 mSupportsDXT3Textures = mRenderer->getDXT3TextureSupport();
280 mSupportsDXT5Textures = mRenderer->getDXT5TextureSupport();
281 mSupportsFloat32Textures = mRenderer->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
282 mSupportsFloat16Textures = mRenderer->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
283 mSupportsLuminanceTextures = mRenderer->getLuminanceTextureSupport();
284 mSupportsLuminanceAlphaTextures = mRenderer->getLuminanceAlphaTextureSupport();
285 mSupportsDepthTextures = mRenderer->getDepthTextureSupport();
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000286 mSupportsTextureFilterAnisotropy = mRenderer->getTextureFilterAnisotropySupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000287
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000288 mSupports32bitIndices = mRenderer->get32BitIndexSupport();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000289
290 mNumCompressedTextureFormats = 0;
291 if (supportsDXT1Textures())
292 {
293 mNumCompressedTextureFormats += 2;
294 }
295 if (supportsDXT3Textures())
296 {
297 mNumCompressedTextureFormats += 1;
298 }
299 if (supportsDXT5Textures())
300 {
301 mNumCompressedTextureFormats += 1;
302 }
303
304 initExtensionString();
305 initRendererString();
306
307 mState.viewportX = 0;
308 mState.viewportY = 0;
309 mState.viewportWidth = surface->getWidth();
310 mState.viewportHeight = surface->getHeight();
311
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000312 mState.scissor.x = 0;
313 mState.scissor.y = 0;
314 mState.scissor.width = surface->getWidth();
315 mState.scissor.height = surface->getHeight();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000316
317 mHasBeenCurrent = true;
318 }
319
daniel@transgaming.com024786d2012-10-31 18:42:55 +0000320 // Wrap the existing swapchain resources into GL objects and assign them to the '0' names
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000321 rx::SwapChain *swapchain = surface->getSwapChain();
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000322
daniel@transgaming.com70062c92012-11-28 19:32:30 +0000323 Colorbuffer *colorbufferZero = new Colorbuffer(mRenderer, swapchain);
324 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(mRenderer, swapchain);
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000325 Framebuffer *framebufferZero = new DefaultFramebuffer(mRenderer, colorbufferZero, depthStencilbufferZero);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000326
327 setFramebufferZero(framebufferZero);
328
apatrick@chromium.org909f21c2012-08-17 20:06:02 +0000329 // Reset pixel shader to null to work around a bug that only happens with Intel GPUs.
330 // http://crbug.com/110343
331 mDevice->SetPixelShader(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000332
333 markAllStateDirty();
334}
335
336// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
337void Context::markAllStateDirty()
338{
339 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
340 {
341 mAppliedTextureSerialPS[t] = 0;
342 }
343
344 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
345 {
346 mAppliedTextureSerialVS[t] = 0;
347 }
348
daniel@transgaming.come6af4f92012-07-24 18:31:31 +0000349 mAppliedProgramBinarySerial = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000350 mAppliedRenderTargetSerial = 0;
351 mAppliedDepthbufferSerial = 0;
352 mAppliedStencilbufferSerial = 0;
353 mAppliedIBSerial = 0;
354 mDepthStencilInitialized = false;
355 mViewportInitialized = false;
356 mRenderTargetDescInitialized = false;
357
358 mVertexDeclarationCache.markStateDirty();
359
360 mClearStateDirty = true;
361 mCullStateDirty = true;
362 mDepthStateDirty = true;
363 mMaskStateDirty = true;
364 mBlendStateDirty = true;
365 mStencilStateDirty = true;
366 mPolygonOffsetStateDirty = true;
367 mScissorStateDirty = true;
368 mSampleStateDirty = true;
369 mDitherStateDirty = true;
370 mFrontFaceDirty = true;
371 mDxUniformsDirty = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000372}
373
374void Context::markDxUniformsDirty()
375{
376 mDxUniformsDirty = true;
377}
378
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000379// NOTE: this function should not assume that this context is current!
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000380void Context::markContextLost()
381{
382 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
383 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
384 mContextLost = true;
385}
386
387bool Context::isContextLost()
388{
389 return mContextLost;
390}
391
392void Context::setClearColor(float red, float green, float blue, float alpha)
393{
394 mState.colorClearValue.red = red;
395 mState.colorClearValue.green = green;
396 mState.colorClearValue.blue = blue;
397 mState.colorClearValue.alpha = alpha;
398}
399
400void Context::setClearDepth(float depth)
401{
402 mState.depthClearValue = depth;
403}
404
405void Context::setClearStencil(int stencil)
406{
407 mState.stencilClearValue = stencil;
408}
409
410void Context::setCullFace(bool enabled)
411{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000412 if (mState.rasterizer.cullFace != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000413 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000414 mState.rasterizer.cullFace = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000415 mCullStateDirty = true;
416 }
417}
418
419bool Context::isCullFaceEnabled() const
420{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000421 return mState.rasterizer.cullFace;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000422}
423
424void Context::setCullMode(GLenum mode)
425{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000426 if (mState.rasterizer.cullMode != mode)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000427 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000428 mState.rasterizer.cullMode = mode;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000429 mCullStateDirty = true;
430 }
431}
432
433void Context::setFrontFace(GLenum front)
434{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000435 if (mState.rasterizer.frontFace != front)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000436 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000437 mState.rasterizer.frontFace = front;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000438 mFrontFaceDirty = true;
439 }
440}
441
442void Context::setDepthTest(bool enabled)
443{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000444 if (mState.depthStencil.depthTest != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000445 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000446 mState.depthStencil.depthTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000447 mDepthStateDirty = true;
448 }
449}
450
451bool Context::isDepthTestEnabled() const
452{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000453 return mState.depthStencil.depthTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000454}
455
456void Context::setDepthFunc(GLenum depthFunc)
457{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000458 if (mState.depthStencil.depthFunc != depthFunc)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000459 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000460 mState.depthStencil.depthFunc = depthFunc;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000461 mDepthStateDirty = true;
462 }
463}
464
465void Context::setDepthRange(float zNear, float zFar)
466{
467 mState.zNear = zNear;
468 mState.zFar = zFar;
469}
470
471void Context::setBlend(bool enabled)
472{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000473 if (mState.blend.blend != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000474 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000475 mState.blend.blend = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000476 mBlendStateDirty = true;
477 }
478}
479
480bool Context::isBlendEnabled() const
481{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000482 return mState.blend.blend;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000483}
484
485void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
486{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000487 if (mState.blend.sourceBlendRGB != sourceRGB ||
488 mState.blend.sourceBlendAlpha != sourceAlpha ||
489 mState.blend.destBlendRGB != destRGB ||
490 mState.blend.destBlendAlpha != destAlpha)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000491 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000492 mState.blend.sourceBlendRGB = sourceRGB;
493 mState.blend.destBlendRGB = destRGB;
494 mState.blend.sourceBlendAlpha = sourceAlpha;
495 mState.blend.destBlendAlpha = destAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000496 mBlendStateDirty = true;
497 }
498}
499
500void Context::setBlendColor(float red, float green, float blue, float alpha)
501{
502 if (mState.blendColor.red != red ||
503 mState.blendColor.green != green ||
504 mState.blendColor.blue != blue ||
505 mState.blendColor.alpha != alpha)
506 {
507 mState.blendColor.red = red;
508 mState.blendColor.green = green;
509 mState.blendColor.blue = blue;
510 mState.blendColor.alpha = alpha;
511 mBlendStateDirty = true;
512 }
513}
514
515void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
516{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000517 if (mState.blend.blendEquationRGB != rgbEquation ||
518 mState.blend.blendEquationAlpha != alphaEquation)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000519 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000520 mState.blend.blendEquationRGB = rgbEquation;
521 mState.blend.blendEquationAlpha = alphaEquation;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000522 mBlendStateDirty = true;
523 }
524}
525
526void Context::setStencilTest(bool enabled)
527{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000528 if (mState.depthStencil.stencilTest != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000529 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000530 mState.depthStencil.stencilTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000531 mStencilStateDirty = true;
532 }
533}
534
535bool Context::isStencilTestEnabled() const
536{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000537 return mState.depthStencil.stencilTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000538}
539
540void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
541{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000542 if (mState.depthStencil.stencilFunc != stencilFunc ||
543 mState.depthStencil.stencilRef != stencilRef ||
544 mState.depthStencil.stencilMask != stencilMask)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000545 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000546 mState.depthStencil.stencilFunc = stencilFunc;
547 mState.depthStencil.stencilRef = (stencilRef > 0) ? stencilRef : 0;
548 mState.depthStencil.stencilMask = stencilMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000549 mStencilStateDirty = true;
550 }
551}
552
553void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
554{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000555 if (mState.depthStencil.stencilBackFunc != stencilBackFunc ||
556 mState.depthStencil.stencilBackRef != stencilBackRef ||
557 mState.depthStencil.stencilBackMask != stencilBackMask)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000558 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000559 mState.depthStencil.stencilBackFunc = stencilBackFunc;
560 mState.depthStencil.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
561 mState.depthStencil.stencilBackMask = stencilBackMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000562 mStencilStateDirty = true;
563 }
564}
565
566void Context::setStencilWritemask(GLuint stencilWritemask)
567{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000568 if (mState.depthStencil.stencilWritemask != stencilWritemask)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000569 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000570 mState.depthStencil.stencilWritemask = stencilWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000571 mStencilStateDirty = true;
572 }
573}
574
575void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
576{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000577 if (mState.depthStencil.stencilBackWritemask != stencilBackWritemask)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000578 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000579 mState.depthStencil.stencilBackWritemask = stencilBackWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000580 mStencilStateDirty = true;
581 }
582}
583
584void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
585{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000586 if (mState.depthStencil.stencilFail != stencilFail ||
587 mState.depthStencil.stencilPassDepthFail != stencilPassDepthFail ||
588 mState.depthStencil.stencilPassDepthPass != stencilPassDepthPass)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000589 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000590 mState.depthStencil.stencilFail = stencilFail;
591 mState.depthStencil.stencilPassDepthFail = stencilPassDepthFail;
592 mState.depthStencil.stencilPassDepthPass = stencilPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000593 mStencilStateDirty = true;
594 }
595}
596
597void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
598{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000599 if (mState.depthStencil.stencilBackFail != stencilBackFail ||
600 mState.depthStencil.stencilBackPassDepthFail != stencilBackPassDepthFail ||
601 mState.depthStencil.stencilBackPassDepthPass != stencilBackPassDepthPass)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000602 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000603 mState.depthStencil.stencilBackFail = stencilBackFail;
604 mState.depthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
605 mState.depthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000606 mStencilStateDirty = true;
607 }
608}
609
610void Context::setPolygonOffsetFill(bool enabled)
611{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000612 if (mState.rasterizer.polygonOffsetFill != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000613 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000614 mState.rasterizer.polygonOffsetFill = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000615 mPolygonOffsetStateDirty = true;
616 }
617}
618
619bool Context::isPolygonOffsetFillEnabled() const
620{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000621 return mState.rasterizer.polygonOffsetFill;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000622
623}
624
625void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
626{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000627 if (mState.rasterizer.polygonOffsetFactor != factor ||
628 mState.rasterizer.polygonOffsetUnits != units)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000629 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000630 mState.rasterizer.polygonOffsetFactor = factor;
631 mState.rasterizer.polygonOffsetUnits = units;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000632 mPolygonOffsetStateDirty = true;
633 }
634}
635
636void Context::setSampleAlphaToCoverage(bool enabled)
637{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000638 if (mState.blend.sampleAlphaToCoverage != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000639 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000640 mState.blend.sampleAlphaToCoverage = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000641 mSampleStateDirty = true;
642 }
643}
644
645bool Context::isSampleAlphaToCoverageEnabled() const
646{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000647 return mState.blend.sampleAlphaToCoverage;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000648}
649
650void Context::setSampleCoverage(bool enabled)
651{
652 if (mState.sampleCoverage != enabled)
653 {
654 mState.sampleCoverage = enabled;
655 mSampleStateDirty = true;
656 }
657}
658
659bool Context::isSampleCoverageEnabled() const
660{
661 return mState.sampleCoverage;
662}
663
664void Context::setSampleCoverageParams(GLclampf value, bool invert)
665{
666 if (mState.sampleCoverageValue != value ||
667 mState.sampleCoverageInvert != invert)
668 {
669 mState.sampleCoverageValue = value;
670 mState.sampleCoverageInvert = invert;
671 mSampleStateDirty = true;
672 }
673}
674
675void Context::setScissorTest(bool enabled)
676{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000677 if (mState.rasterizer.scissorTest != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000678 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000679 mState.rasterizer.scissorTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000680 mScissorStateDirty = true;
681 }
682}
683
684bool Context::isScissorTestEnabled() const
685{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000686 return mState.rasterizer.scissorTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000687}
688
689void Context::setDither(bool enabled)
690{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000691 if (mState.blend.dither != enabled)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000692 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000693 mState.blend.dither = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000694 mDitherStateDirty = true;
695 }
696}
697
698bool Context::isDitherEnabled() const
699{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000700 return mState.blend.dither;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000701}
702
703void Context::setLineWidth(GLfloat width)
704{
705 mState.lineWidth = width;
706}
707
708void Context::setGenerateMipmapHint(GLenum hint)
709{
710 mState.generateMipmapHint = hint;
711}
712
713void Context::setFragmentShaderDerivativeHint(GLenum hint)
714{
715 mState.fragmentShaderDerivativeHint = hint;
716 // TODO: Propagate the hint to shader translator so we can write
717 // ddx, ddx_coarse, or ddx_fine depending on the hint.
718 // Ignore for now. It is valid for implementations to ignore hint.
719}
720
721void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
722{
723 mState.viewportX = x;
724 mState.viewportY = y;
725 mState.viewportWidth = width;
726 mState.viewportHeight = height;
727}
728
729void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
730{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000731 if (mState.scissor.x != x || mState.scissor.y != y ||
732 mState.scissor.width != width || mState.scissor.height != height)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000733 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000734 mState.scissor.x = x;
735 mState.scissor.y = y;
736 mState.scissor.width = width;
737 mState.scissor.height = height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000738 mScissorStateDirty = true;
739 }
740}
741
742void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
743{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000744 if (mState.blend.colorMaskRed != red || mState.blend.colorMaskGreen != green ||
745 mState.blend.colorMaskBlue != blue || mState.blend.colorMaskAlpha != alpha)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000746 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000747 mState.blend.colorMaskRed = red;
748 mState.blend.colorMaskGreen = green;
749 mState.blend.colorMaskBlue = blue;
750 mState.blend.colorMaskAlpha = alpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000751 mMaskStateDirty = true;
752 }
753}
754
755void Context::setDepthMask(bool mask)
756{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000757 if (mState.depthStencil.depthMask != mask)
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000758 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000759 mState.depthStencil.depthMask = mask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000760 mMaskStateDirty = true;
761 }
762}
763
764void Context::setActiveSampler(unsigned int active)
765{
766 mState.activeSampler = active;
767}
768
769GLuint Context::getReadFramebufferHandle() const
770{
771 return mState.readFramebuffer;
772}
773
774GLuint Context::getDrawFramebufferHandle() const
775{
776 return mState.drawFramebuffer;
777}
778
779GLuint Context::getRenderbufferHandle() const
780{
781 return mState.renderbuffer.id();
782}
783
784GLuint Context::getArrayBufferHandle() const
785{
786 return mState.arrayBuffer.id();
787}
788
789GLuint Context::getActiveQuery(GLenum target) const
790{
791 Query *queryObject = NULL;
792
793 switch (target)
794 {
795 case GL_ANY_SAMPLES_PASSED_EXT:
796 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
797 break;
798 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
799 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
800 break;
801 default:
802 ASSERT(false);
803 }
804
805 if (queryObject)
806 {
807 return queryObject->id();
808 }
809 else
810 {
811 return 0;
812 }
813}
814
815void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
816{
817 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
818}
819
820const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
821{
822 return mState.vertexAttribute[attribNum];
823}
824
825void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
826 GLsizei stride, const void *pointer)
827{
828 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
829 mState.vertexAttribute[attribNum].mSize = size;
830 mState.vertexAttribute[attribNum].mType = type;
831 mState.vertexAttribute[attribNum].mNormalized = normalized;
832 mState.vertexAttribute[attribNum].mStride = stride;
833 mState.vertexAttribute[attribNum].mPointer = pointer;
834}
835
836const void *Context::getVertexAttribPointer(unsigned int attribNum) const
837{
838 return mState.vertexAttribute[attribNum].mPointer;
839}
840
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000841void Context::setPackAlignment(GLint alignment)
842{
843 mState.packAlignment = alignment;
844}
845
846GLint Context::getPackAlignment() const
847{
848 return mState.packAlignment;
849}
850
851void Context::setUnpackAlignment(GLint alignment)
852{
853 mState.unpackAlignment = alignment;
854}
855
856GLint Context::getUnpackAlignment() const
857{
858 return mState.unpackAlignment;
859}
860
861void Context::setPackReverseRowOrder(bool reverseRowOrder)
862{
863 mState.packReverseRowOrder = reverseRowOrder;
864}
865
866bool Context::getPackReverseRowOrder() const
867{
868 return mState.packReverseRowOrder;
869}
870
871GLuint Context::createBuffer()
872{
873 return mResourceManager->createBuffer();
874}
875
876GLuint Context::createProgram()
877{
878 return mResourceManager->createProgram();
879}
880
881GLuint Context::createShader(GLenum type)
882{
883 return mResourceManager->createShader(type);
884}
885
886GLuint Context::createTexture()
887{
888 return mResourceManager->createTexture();
889}
890
891GLuint Context::createRenderbuffer()
892{
893 return mResourceManager->createRenderbuffer();
894}
895
896// Returns an unused framebuffer name
897GLuint Context::createFramebuffer()
898{
899 GLuint handle = mFramebufferHandleAllocator.allocate();
900
901 mFramebufferMap[handle] = NULL;
902
903 return handle;
904}
905
906GLuint Context::createFence()
907{
908 GLuint handle = mFenceHandleAllocator.allocate();
909
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000910 mFenceMap[handle] = new Fence(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000911
912 return handle;
913}
914
915// Returns an unused query name
916GLuint Context::createQuery()
917{
918 GLuint handle = mQueryHandleAllocator.allocate();
919
920 mQueryMap[handle] = NULL;
921
922 return handle;
923}
924
925void Context::deleteBuffer(GLuint buffer)
926{
927 if (mResourceManager->getBuffer(buffer))
928 {
929 detachBuffer(buffer);
930 }
931
932 mResourceManager->deleteBuffer(buffer);
933}
934
935void Context::deleteShader(GLuint shader)
936{
937 mResourceManager->deleteShader(shader);
938}
939
940void Context::deleteProgram(GLuint program)
941{
942 mResourceManager->deleteProgram(program);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000943}
944
945void Context::deleteTexture(GLuint texture)
946{
947 if (mResourceManager->getTexture(texture))
948 {
949 detachTexture(texture);
950 }
951
952 mResourceManager->deleteTexture(texture);
953}
954
955void Context::deleteRenderbuffer(GLuint renderbuffer)
956{
957 if (mResourceManager->getRenderbuffer(renderbuffer))
958 {
959 detachRenderbuffer(renderbuffer);
960 }
961
962 mResourceManager->deleteRenderbuffer(renderbuffer);
963}
964
965void Context::deleteFramebuffer(GLuint framebuffer)
966{
967 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
968
969 if (framebufferObject != mFramebufferMap.end())
970 {
971 detachFramebuffer(framebuffer);
972
973 mFramebufferHandleAllocator.release(framebufferObject->first);
974 delete framebufferObject->second;
975 mFramebufferMap.erase(framebufferObject);
976 }
977}
978
979void Context::deleteFence(GLuint fence)
980{
981 FenceMap::iterator fenceObject = mFenceMap.find(fence);
982
983 if (fenceObject != mFenceMap.end())
984 {
985 mFenceHandleAllocator.release(fenceObject->first);
986 delete fenceObject->second;
987 mFenceMap.erase(fenceObject);
988 }
989}
990
991void Context::deleteQuery(GLuint query)
992{
993 QueryMap::iterator queryObject = mQueryMap.find(query);
994 if (queryObject != mQueryMap.end())
995 {
996 mQueryHandleAllocator.release(queryObject->first);
997 if (queryObject->second)
998 {
999 queryObject->second->release();
1000 }
1001 mQueryMap.erase(queryObject);
1002 }
1003}
1004
1005Buffer *Context::getBuffer(GLuint handle)
1006{
1007 return mResourceManager->getBuffer(handle);
1008}
1009
1010Shader *Context::getShader(GLuint handle)
1011{
1012 return mResourceManager->getShader(handle);
1013}
1014
1015Program *Context::getProgram(GLuint handle)
1016{
1017 return mResourceManager->getProgram(handle);
1018}
1019
1020Texture *Context::getTexture(GLuint handle)
1021{
1022 return mResourceManager->getTexture(handle);
1023}
1024
1025Renderbuffer *Context::getRenderbuffer(GLuint handle)
1026{
1027 return mResourceManager->getRenderbuffer(handle);
1028}
1029
1030Framebuffer *Context::getReadFramebuffer()
1031{
1032 return getFramebuffer(mState.readFramebuffer);
1033}
1034
1035Framebuffer *Context::getDrawFramebuffer()
1036{
1037 return mBoundDrawFramebuffer;
1038}
1039
1040void Context::bindArrayBuffer(unsigned int buffer)
1041{
1042 mResourceManager->checkBufferAllocation(buffer);
1043
1044 mState.arrayBuffer.set(getBuffer(buffer));
1045}
1046
1047void Context::bindElementArrayBuffer(unsigned int buffer)
1048{
1049 mResourceManager->checkBufferAllocation(buffer);
1050
1051 mState.elementArrayBuffer.set(getBuffer(buffer));
1052}
1053
1054void Context::bindTexture2D(GLuint texture)
1055{
1056 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
1057
1058 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
1059}
1060
1061void Context::bindTextureCubeMap(GLuint texture)
1062{
1063 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
1064
1065 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
1066}
1067
1068void Context::bindReadFramebuffer(GLuint framebuffer)
1069{
1070 if (!getFramebuffer(framebuffer))
1071 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +00001072 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001073 }
1074
1075 mState.readFramebuffer = framebuffer;
1076}
1077
1078void Context::bindDrawFramebuffer(GLuint framebuffer)
1079{
1080 if (!getFramebuffer(framebuffer))
1081 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +00001082 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001083 }
1084
1085 mState.drawFramebuffer = framebuffer;
1086
1087 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
1088}
1089
1090void Context::bindRenderbuffer(GLuint renderbuffer)
1091{
1092 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1093
1094 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
1095}
1096
1097void Context::useProgram(GLuint program)
1098{
1099 GLuint priorProgram = mState.currentProgram;
1100 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
1101
1102 if (priorProgram != program)
1103 {
1104 Program *newProgram = mResourceManager->getProgram(program);
1105 Program *oldProgram = mResourceManager->getProgram(priorProgram);
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001106 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001107 mDxUniformsDirty = true;
1108
1109 if (newProgram)
1110 {
1111 newProgram->addRef();
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001112 mCurrentProgramBinary.set(newProgram->getProgramBinary());
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001113 }
1114
1115 if (oldProgram)
1116 {
1117 oldProgram->release();
1118 }
1119 }
1120}
1121
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001122void Context::linkProgram(GLuint program)
1123{
1124 Program *programObject = mResourceManager->getProgram(program);
1125
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001126 bool linked = programObject->link();
1127
1128 // if the current program was relinked successfully we
1129 // need to install the new executables
1130 if (linked && program == mState.currentProgram)
1131 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001132 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001133 mDxUniformsDirty = true;
1134 }
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001135}
1136
1137void Context::setProgramBinary(GLuint program, const void *binary, GLint length)
1138{
1139 Program *programObject = mResourceManager->getProgram(program);
1140
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001141 bool loaded = programObject->setProgramBinary(binary, length);
1142
1143 // if the current program was reloaded successfully we
1144 // need to install the new executables
1145 if (loaded && program == mState.currentProgram)
1146 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001147 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001148 mDxUniformsDirty = true;
1149 }
1150
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001151}
1152
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001153void Context::beginQuery(GLenum target, GLuint query)
1154{
1155 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1156 // of zero, if the active query object name for <target> is non-zero (for the
1157 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1158 // the active query for either target is non-zero), if <id> is the name of an
1159 // existing query object whose type does not match <target>, or if <id> is the
1160 // active query object name for any query type, the error INVALID_OPERATION is
1161 // generated.
1162
1163 // Ensure no other queries are active
1164 // NOTE: If other queries than occlusion are supported, we will need to check
1165 // separately that:
1166 // a) The query ID passed is not the current active query for any target/type
1167 // b) There are no active queries for the requested target (and in the case
1168 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1169 // no query may be active for either if glBeginQuery targets either.
1170 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1171 {
1172 if (mState.activeQuery[i].get() != NULL)
1173 {
1174 return error(GL_INVALID_OPERATION);
1175 }
1176 }
1177
1178 QueryType qType;
1179 switch (target)
1180 {
1181 case GL_ANY_SAMPLES_PASSED_EXT:
1182 qType = QUERY_ANY_SAMPLES_PASSED;
1183 break;
1184 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1185 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1186 break;
1187 default:
1188 ASSERT(false);
1189 return;
1190 }
1191
1192 Query *queryObject = getQuery(query, true, target);
1193
1194 // check that name was obtained with glGenQueries
1195 if (!queryObject)
1196 {
1197 return error(GL_INVALID_OPERATION);
1198 }
1199
1200 // check for type mismatch
1201 if (queryObject->getType() != target)
1202 {
1203 return error(GL_INVALID_OPERATION);
1204 }
1205
1206 // set query as active for specified target
1207 mState.activeQuery[qType].set(queryObject);
1208
1209 // begin query
1210 queryObject->begin();
1211}
1212
1213void Context::endQuery(GLenum target)
1214{
1215 QueryType qType;
1216
1217 switch (target)
1218 {
1219 case GL_ANY_SAMPLES_PASSED_EXT:
1220 qType = QUERY_ANY_SAMPLES_PASSED;
1221 break;
1222 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1223 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1224 break;
1225 default:
1226 ASSERT(false);
1227 return;
1228 }
1229
1230 Query *queryObject = mState.activeQuery[qType].get();
1231
1232 if (queryObject == NULL)
1233 {
1234 return error(GL_INVALID_OPERATION);
1235 }
1236
1237 queryObject->end();
1238
1239 mState.activeQuery[qType].set(NULL);
1240}
1241
1242void Context::setFramebufferZero(Framebuffer *buffer)
1243{
1244 delete mFramebufferMap[0];
1245 mFramebufferMap[0] = buffer;
1246 if (mState.drawFramebuffer == 0)
1247 {
1248 mBoundDrawFramebuffer = buffer;
1249 }
1250}
1251
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001252void Context::setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001253{
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001254 RenderbufferStorage *renderbuffer = NULL;
1255 switch (internalformat)
1256 {
1257 case GL_DEPTH_COMPONENT16:
1258 renderbuffer = new gl::Depthbuffer(mRenderer, width, height, samples);
1259 break;
1260 case GL_RGBA4:
1261 case GL_RGB5_A1:
1262 case GL_RGB565:
1263 case GL_RGB8_OES:
1264 case GL_RGBA8_OES:
1265 renderbuffer = new gl::Colorbuffer(mRenderer,width, height, internalformat, samples);
1266 break;
1267 case GL_STENCIL_INDEX8:
1268 renderbuffer = new gl::Stencilbuffer(mRenderer, width, height, samples);
1269 break;
1270 case GL_DEPTH24_STENCIL8_OES:
1271 renderbuffer = new gl::DepthStencilbuffer(mRenderer, width, height, samples);
1272 break;
1273 default:
1274 UNREACHABLE(); return;
1275 }
1276
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001277 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1278 renderbufferObject->setStorage(renderbuffer);
1279}
1280
1281Framebuffer *Context::getFramebuffer(unsigned int handle)
1282{
1283 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1284
1285 if (framebuffer == mFramebufferMap.end())
1286 {
1287 return NULL;
1288 }
1289 else
1290 {
1291 return framebuffer->second;
1292 }
1293}
1294
1295Fence *Context::getFence(unsigned int handle)
1296{
1297 FenceMap::iterator fence = mFenceMap.find(handle);
1298
1299 if (fence == mFenceMap.end())
1300 {
1301 return NULL;
1302 }
1303 else
1304 {
1305 return fence->second;
1306 }
1307}
1308
1309Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1310{
1311 QueryMap::iterator query = mQueryMap.find(handle);
1312
1313 if (query == mQueryMap.end())
1314 {
1315 return NULL;
1316 }
1317 else
1318 {
1319 if (!query->second && create)
1320 {
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001321 query->second = new Query(mRenderer, handle, type);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001322 query->second->addRef();
1323 }
1324 return query->second;
1325 }
1326}
1327
1328Buffer *Context::getArrayBuffer()
1329{
1330 return mState.arrayBuffer.get();
1331}
1332
1333Buffer *Context::getElementArrayBuffer()
1334{
1335 return mState.elementArrayBuffer.get();
1336}
1337
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001338ProgramBinary *Context::getCurrentProgramBinary()
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001339{
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001340 return mCurrentProgramBinary.get();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001341}
1342
1343Texture2D *Context::getTexture2D()
1344{
1345 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1346}
1347
1348TextureCubeMap *Context::getTextureCubeMap()
1349{
1350 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1351}
1352
1353Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
1354{
1355 GLuint texid = mState.samplerTexture[type][sampler].id();
1356
1357 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
1358 {
1359 switch (type)
1360 {
1361 default: UNREACHABLE();
1362 case TEXTURE_2D: return mTexture2DZero.get();
1363 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
1364 }
1365 }
1366
1367 return mState.samplerTexture[type][sampler].get();
1368}
1369
1370bool Context::getBooleanv(GLenum pname, GLboolean *params)
1371{
1372 switch (pname)
1373 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001374 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1375 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1376 case GL_DEPTH_WRITEMASK: *params = mState.depthStencil.depthMask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001377 case GL_COLOR_WRITEMASK:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001378 params[0] = mState.blend.colorMaskRed;
1379 params[1] = mState.blend.colorMaskGreen;
1380 params[2] = mState.blend.colorMaskBlue;
1381 params[3] = mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001382 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001383 case GL_CULL_FACE: *params = mState.rasterizer.cullFace; break;
1384 case GL_POLYGON_OFFSET_FILL: *params = mState.rasterizer.polygonOffsetFill; break;
1385 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.blend.sampleAlphaToCoverage; break;
1386 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1387 case GL_SCISSOR_TEST: *params = mState.rasterizer.scissorTest; break;
1388 case GL_STENCIL_TEST: *params = mState.depthStencil.stencilTest; break;
1389 case GL_DEPTH_TEST: *params = mState.depthStencil.depthTest; break;
1390 case GL_BLEND: *params = mState.blend.blend; break;
1391 case GL_DITHER: *params = mState.blend.dither; break;
1392 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001393 default:
1394 return false;
1395 }
1396
1397 return true;
1398}
1399
1400bool Context::getFloatv(GLenum pname, GLfloat *params)
1401{
1402 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1403 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1404 // GetIntegerv as its native query function. As it would require conversion in any
1405 // case, this should make no difference to the calling application.
1406 switch (pname)
1407 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001408 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1409 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1410 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1411 case GL_POLYGON_OFFSET_FACTOR: *params = mState.rasterizer.polygonOffsetFactor; break;
1412 case GL_POLYGON_OFFSET_UNITS: *params = mState.rasterizer.polygonOffsetUnits; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001413 case GL_ALIASED_LINE_WIDTH_RANGE:
1414 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1415 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1416 break;
1417 case GL_ALIASED_POINT_SIZE_RANGE:
1418 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1419 params[1] = getMaximumPointSize();
1420 break;
1421 case GL_DEPTH_RANGE:
1422 params[0] = mState.zNear;
1423 params[1] = mState.zFar;
1424 break;
1425 case GL_COLOR_CLEAR_VALUE:
1426 params[0] = mState.colorClearValue.red;
1427 params[1] = mState.colorClearValue.green;
1428 params[2] = mState.colorClearValue.blue;
1429 params[3] = mState.colorClearValue.alpha;
1430 break;
1431 case GL_BLEND_COLOR:
1432 params[0] = mState.blendColor.red;
1433 params[1] = mState.blendColor.green;
1434 params[2] = mState.blendColor.blue;
1435 params[3] = mState.blendColor.alpha;
1436 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001437 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1438 if (!supportsTextureFilterAnisotropy())
1439 {
1440 return false;
1441 }
1442 *params = mMaxTextureAnisotropy;
1443 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001444 default:
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool Context::getIntegerv(GLenum pname, GLint *params)
1452{
1453 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1454 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1455 // GetIntegerv as its native query function. As it would require conversion in any
1456 // case, this should make no difference to the calling application. You may find it in
1457 // Context::getFloatv.
1458 switch (pname)
1459 {
1460 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1461 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1462 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
1463 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1464 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
1465 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1466 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
1467 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
1468 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1469 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1470 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1471 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
1472 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1473 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1474 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1475 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
1476 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1477 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1478 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
1479 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1480 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1481 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1482 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001483 case GL_STENCIL_FUNC: *params = mState.depthStencil.stencilFunc; break;
1484 case GL_STENCIL_REF: *params = mState.depthStencil.stencilRef; break;
1485 case GL_STENCIL_VALUE_MASK: *params = mState.depthStencil.stencilMask; break;
1486 case GL_STENCIL_BACK_FUNC: *params = mState.depthStencil.stencilBackFunc; break;
1487 case GL_STENCIL_BACK_REF: *params = mState.depthStencil.stencilBackRef; break;
1488 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.depthStencil.stencilBackMask; break;
1489 case GL_STENCIL_FAIL: *params = mState.depthStencil.stencilFail; break;
1490 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilPassDepthFail; break;
1491 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilPassDepthPass; break;
1492 case GL_STENCIL_BACK_FAIL: *params = mState.depthStencil.stencilBackFail; break;
1493 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilBackPassDepthFail; break;
1494 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilBackPassDepthPass; break;
1495 case GL_DEPTH_FUNC: *params = mState.depthStencil.depthFunc; break;
1496 case GL_BLEND_SRC_RGB: *params = mState.blend.sourceBlendRGB; break;
1497 case GL_BLEND_SRC_ALPHA: *params = mState.blend.sourceBlendAlpha; break;
1498 case GL_BLEND_DST_RGB: *params = mState.blend.destBlendRGB; break;
1499 case GL_BLEND_DST_ALPHA: *params = mState.blend.destBlendAlpha; break;
1500 case GL_BLEND_EQUATION_RGB: *params = mState.blend.blendEquationRGB; break;
1501 case GL_BLEND_EQUATION_ALPHA: *params = mState.blend.blendEquationAlpha; break;
1502 case GL_STENCIL_WRITEMASK: *params = mState.depthStencil.stencilWritemask; break;
1503 case GL_STENCIL_BACK_WRITEMASK: *params = mState.depthStencil.stencilBackWritemask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001504 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1505 case GL_SUBPIXEL_BITS: *params = 4; break;
1506 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1507 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
1508 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1509 params[0] = mNumCompressedTextureFormats;
1510 break;
1511 case GL_MAX_SAMPLES_ANGLE:
1512 {
1513 GLsizei maxSamples = getMaxSupportedSamples();
1514 if (maxSamples != 0)
1515 {
1516 *params = maxSamples;
1517 }
1518 else
1519 {
1520 return false;
1521 }
1522
1523 break;
1524 }
1525 case GL_SAMPLE_BUFFERS:
1526 case GL_SAMPLES:
1527 {
1528 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1529 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1530 {
1531 switch (pname)
1532 {
1533 case GL_SAMPLE_BUFFERS:
1534 if (framebuffer->getSamples() != 0)
1535 {
1536 *params = 1;
1537 }
1538 else
1539 {
1540 *params = 0;
1541 }
1542 break;
1543 case GL_SAMPLES:
1544 *params = framebuffer->getSamples();
1545 break;
1546 }
1547 }
1548 else
1549 {
1550 *params = 0;
1551 }
1552 }
1553 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00001554 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1555 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1556 {
1557 GLenum format, type;
1558 if (getCurrentReadFormatType(&format, &type))
1559 {
1560 if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT)
1561 *params = format;
1562 else
1563 *params = type;
1564 }
1565 }
1566 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001567 case GL_MAX_VIEWPORT_DIMS:
1568 {
1569 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
1570 params[0] = maxDimension;
1571 params[1] = maxDimension;
1572 }
1573 break;
1574 case GL_COMPRESSED_TEXTURE_FORMATS:
1575 {
1576 if (supportsDXT1Textures())
1577 {
1578 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1579 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1580 }
1581 if (supportsDXT3Textures())
1582 {
1583 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1584 }
1585 if (supportsDXT5Textures())
1586 {
1587 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
1588 }
1589 }
1590 break;
1591 case GL_VIEWPORT:
1592 params[0] = mState.viewportX;
1593 params[1] = mState.viewportY;
1594 params[2] = mState.viewportWidth;
1595 params[3] = mState.viewportHeight;
1596 break;
1597 case GL_SCISSOR_BOX:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001598 params[0] = mState.scissor.x;
1599 params[1] = mState.scissor.y;
1600 params[2] = mState.scissor.width;
1601 params[3] = mState.scissor.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001602 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001603 case GL_CULL_FACE_MODE: *params = mState.rasterizer.cullMode; break;
1604 case GL_FRONT_FACE: *params = mState.rasterizer.frontFace; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001605 case GL_RED_BITS:
1606 case GL_GREEN_BITS:
1607 case GL_BLUE_BITS:
1608 case GL_ALPHA_BITS:
1609 {
1610 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1611 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1612
1613 if (colorbuffer)
1614 {
1615 switch (pname)
1616 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001617 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1618 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1619 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1620 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001621 }
1622 }
1623 else
1624 {
1625 *params = 0;
1626 }
1627 }
1628 break;
1629 case GL_DEPTH_BITS:
1630 {
1631 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1632 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1633
1634 if (depthbuffer)
1635 {
1636 *params = depthbuffer->getDepthSize();
1637 }
1638 else
1639 {
1640 *params = 0;
1641 }
1642 }
1643 break;
1644 case GL_STENCIL_BITS:
1645 {
1646 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1647 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1648
1649 if (stencilbuffer)
1650 {
1651 *params = stencilbuffer->getStencilSize();
1652 }
1653 else
1654 {
1655 *params = 0;
1656 }
1657 }
1658 break;
1659 case GL_TEXTURE_BINDING_2D:
1660 {
1661 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1662 {
1663 error(GL_INVALID_OPERATION);
1664 return false;
1665 }
1666
1667 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
1668 }
1669 break;
1670 case GL_TEXTURE_BINDING_CUBE_MAP:
1671 {
1672 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1673 {
1674 error(GL_INVALID_OPERATION);
1675 return false;
1676 }
1677
1678 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
1679 }
1680 break;
1681 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1682 *params = mResetStrategy;
1683 break;
1684 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1685 *params = 1;
1686 break;
1687 case GL_PROGRAM_BINARY_FORMATS_OES:
1688 *params = GL_PROGRAM_BINARY_ANGLE;
1689 break;
1690 default:
1691 return false;
1692 }
1693
1694 return true;
1695}
1696
1697bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1698{
1699 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1700 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1701 // to the fact that it is stored internally as a float, and so would require conversion
1702 // if returned from Context::getIntegerv. Since this conversion is already implemented
1703 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1704 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1705 // application.
1706 switch (pname)
1707 {
1708 case GL_COMPRESSED_TEXTURE_FORMATS:
1709 {
1710 *type = GL_INT;
1711 *numParams = mNumCompressedTextureFormats;
1712 }
1713 break;
1714 case GL_SHADER_BINARY_FORMATS:
1715 {
1716 *type = GL_INT;
1717 *numParams = 0;
1718 }
1719 break;
1720 case GL_MAX_VERTEX_ATTRIBS:
1721 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1722 case GL_MAX_VARYING_VECTORS:
1723 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1724 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1725 case GL_MAX_TEXTURE_IMAGE_UNITS:
1726 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1727 case GL_MAX_RENDERBUFFER_SIZE:
1728 case GL_NUM_SHADER_BINARY_FORMATS:
1729 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1730 case GL_ARRAY_BUFFER_BINDING:
1731 case GL_FRAMEBUFFER_BINDING:
1732 case GL_RENDERBUFFER_BINDING:
1733 case GL_CURRENT_PROGRAM:
1734 case GL_PACK_ALIGNMENT:
1735 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1736 case GL_UNPACK_ALIGNMENT:
1737 case GL_GENERATE_MIPMAP_HINT:
1738 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1739 case GL_RED_BITS:
1740 case GL_GREEN_BITS:
1741 case GL_BLUE_BITS:
1742 case GL_ALPHA_BITS:
1743 case GL_DEPTH_BITS:
1744 case GL_STENCIL_BITS:
1745 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1746 case GL_CULL_FACE_MODE:
1747 case GL_FRONT_FACE:
1748 case GL_ACTIVE_TEXTURE:
1749 case GL_STENCIL_FUNC:
1750 case GL_STENCIL_VALUE_MASK:
1751 case GL_STENCIL_REF:
1752 case GL_STENCIL_FAIL:
1753 case GL_STENCIL_PASS_DEPTH_FAIL:
1754 case GL_STENCIL_PASS_DEPTH_PASS:
1755 case GL_STENCIL_BACK_FUNC:
1756 case GL_STENCIL_BACK_VALUE_MASK:
1757 case GL_STENCIL_BACK_REF:
1758 case GL_STENCIL_BACK_FAIL:
1759 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1760 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1761 case GL_DEPTH_FUNC:
1762 case GL_BLEND_SRC_RGB:
1763 case GL_BLEND_SRC_ALPHA:
1764 case GL_BLEND_DST_RGB:
1765 case GL_BLEND_DST_ALPHA:
1766 case GL_BLEND_EQUATION_RGB:
1767 case GL_BLEND_EQUATION_ALPHA:
1768 case GL_STENCIL_WRITEMASK:
1769 case GL_STENCIL_BACK_WRITEMASK:
1770 case GL_STENCIL_CLEAR_VALUE:
1771 case GL_SUBPIXEL_BITS:
1772 case GL_MAX_TEXTURE_SIZE:
1773 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1774 case GL_SAMPLE_BUFFERS:
1775 case GL_SAMPLES:
1776 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1777 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1778 case GL_TEXTURE_BINDING_2D:
1779 case GL_TEXTURE_BINDING_CUBE_MAP:
1780 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1781 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1782 case GL_PROGRAM_BINARY_FORMATS_OES:
1783 {
1784 *type = GL_INT;
1785 *numParams = 1;
1786 }
1787 break;
1788 case GL_MAX_SAMPLES_ANGLE:
1789 {
1790 if (getMaxSupportedSamples() != 0)
1791 {
1792 *type = GL_INT;
1793 *numParams = 1;
1794 }
1795 else
1796 {
1797 return false;
1798 }
1799 }
1800 break;
1801 case GL_MAX_VIEWPORT_DIMS:
1802 {
1803 *type = GL_INT;
1804 *numParams = 2;
1805 }
1806 break;
1807 case GL_VIEWPORT:
1808 case GL_SCISSOR_BOX:
1809 {
1810 *type = GL_INT;
1811 *numParams = 4;
1812 }
1813 break;
1814 case GL_SHADER_COMPILER:
1815 case GL_SAMPLE_COVERAGE_INVERT:
1816 case GL_DEPTH_WRITEMASK:
1817 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1818 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1819 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1820 case GL_SAMPLE_COVERAGE:
1821 case GL_SCISSOR_TEST:
1822 case GL_STENCIL_TEST:
1823 case GL_DEPTH_TEST:
1824 case GL_BLEND:
1825 case GL_DITHER:
1826 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1827 {
1828 *type = GL_BOOL;
1829 *numParams = 1;
1830 }
1831 break;
1832 case GL_COLOR_WRITEMASK:
1833 {
1834 *type = GL_BOOL;
1835 *numParams = 4;
1836 }
1837 break;
1838 case GL_POLYGON_OFFSET_FACTOR:
1839 case GL_POLYGON_OFFSET_UNITS:
1840 case GL_SAMPLE_COVERAGE_VALUE:
1841 case GL_DEPTH_CLEAR_VALUE:
1842 case GL_LINE_WIDTH:
1843 {
1844 *type = GL_FLOAT;
1845 *numParams = 1;
1846 }
1847 break;
1848 case GL_ALIASED_LINE_WIDTH_RANGE:
1849 case GL_ALIASED_POINT_SIZE_RANGE:
1850 case GL_DEPTH_RANGE:
1851 {
1852 *type = GL_FLOAT;
1853 *numParams = 2;
1854 }
1855 break;
1856 case GL_COLOR_CLEAR_VALUE:
1857 case GL_BLEND_COLOR:
1858 {
1859 *type = GL_FLOAT;
1860 *numParams = 4;
1861 }
1862 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001863 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1864 if (!supportsTextureFilterAnisotropy())
1865 {
1866 return false;
1867 }
1868 *type = GL_FLOAT;
1869 *numParams = 1;
1870 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001871 default:
1872 return false;
1873 }
1874
1875 return true;
1876}
1877
1878// Applies the render target surface, depth stencil surface, viewport rectangle and
1879// scissor rectangle to the Direct3D 9 device
1880bool Context::applyRenderTarget(bool ignoreViewport)
1881{
1882 Framebuffer *framebufferObject = getDrawFramebuffer();
1883
1884 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1885 {
1886 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1887 }
1888
1889 // if there is no color attachment we must synthesize a NULL colorattachment
1890 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1891 Renderbuffer *renderbufferObject = NULL;
1892 if (framebufferObject->getColorbufferType() != GL_NONE)
1893 {
1894 renderbufferObject = framebufferObject->getColorbuffer();
1895 }
1896 else
1897 {
1898 renderbufferObject = framebufferObject->getNullColorbuffer();
1899 }
1900 if (!renderbufferObject)
1901 {
1902 ERR("unable to locate renderbuffer for FBO.");
1903 return false;
1904 }
1905
1906 bool renderTargetChanged = false;
1907 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1908 if (renderTargetSerial != mAppliedRenderTargetSerial)
1909 {
1910 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1911 if (!renderTarget)
1912 {
1913 ERR("render target pointer unexpectedly null.");
1914 return false; // Context must be lost
1915 }
1916 mDevice->SetRenderTarget(0, renderTarget);
1917 mAppliedRenderTargetSerial = renderTargetSerial;
1918 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
1919 renderTargetChanged = true;
1920 renderTarget->Release();
1921 }
1922
1923 IDirect3DSurface9 *depthStencil = NULL;
1924 unsigned int depthbufferSerial = 0;
1925 unsigned int stencilbufferSerial = 0;
1926 if (framebufferObject->getDepthbufferType() != GL_NONE)
1927 {
1928 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1929 depthStencil = depthbuffer->getDepthStencil();
1930 if (!depthStencil)
1931 {
1932 ERR("Depth stencil pointer unexpectedly null.");
1933 return false;
1934 }
1935
1936 depthbufferSerial = depthbuffer->getSerial();
1937 }
1938 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1939 {
1940 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1941 depthStencil = stencilbuffer->getDepthStencil();
1942 if (!depthStencil)
1943 {
1944 ERR("Depth stencil pointer unexpectedly null.");
1945 return false;
1946 }
1947
1948 stencilbufferSerial = stencilbuffer->getSerial();
1949 }
1950
1951 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1952 stencilbufferSerial != mAppliedStencilbufferSerial ||
1953 !mDepthStencilInitialized)
1954 {
1955 mDevice->SetDepthStencilSurface(depthStencil);
1956 mAppliedDepthbufferSerial = depthbufferSerial;
1957 mAppliedStencilbufferSerial = stencilbufferSerial;
1958 mDepthStencilInitialized = true;
1959 }
1960
1961 if (depthStencil)
1962 {
1963 depthStencil->Release();
1964 }
1965
1966 if (!mRenderTargetDescInitialized || renderTargetChanged)
1967 {
1968 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1969 if (!renderTarget)
1970 {
1971 return false; // Context must be lost
1972 }
1973 renderTarget->GetDesc(&mRenderTargetDesc);
1974 mRenderTargetDescInitialized = true;
1975 renderTarget->Release();
1976 }
1977
1978 D3DVIEWPORT9 viewport;
1979
1980 float zNear = clamp01(mState.zNear);
1981 float zFar = clamp01(mState.zFar);
1982
1983 if (ignoreViewport)
1984 {
1985 viewport.X = 0;
1986 viewport.Y = 0;
1987 viewport.Width = mRenderTargetDesc.Width;
1988 viewport.Height = mRenderTargetDesc.Height;
1989 viewport.MinZ = 0.0f;
1990 viewport.MaxZ = 1.0f;
1991 }
1992 else
1993 {
1994 viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1995 viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1996 viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1997 viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
1998 viewport.MinZ = zNear;
1999 viewport.MaxZ = zFar;
2000 }
2001
2002 if (viewport.Width <= 0 || viewport.Height <= 0)
2003 {
2004 return false; // Nothing to render
2005 }
2006
2007 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
2008 {
2009 mDevice->SetViewport(&viewport);
2010 mSetViewport = viewport;
2011 mViewportInitialized = true;
2012 mDxUniformsDirty = true;
2013 }
2014
2015 if (mScissorStateDirty)
2016 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002017 mRenderer->setScissorRectangle(mState.scissor, static_cast<int>(mRenderTargetDesc.Width),
2018 static_cast<int>(mRenderTargetDesc.Height));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002019 mScissorStateDirty = false;
2020 }
2021
2022 if (mState.currentProgram && mDxUniformsDirty)
2023 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002024 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002025
2026 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
2027 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
2028 programBinary->setUniform2fv(halfPixelSize, 1, xy);
2029
2030 // These values are used for computing gl_FragCoord in Program::linkVaryings().
2031 GLint coord = programBinary->getDxCoordLocation();
2032 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
2033 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2034 (float)mState.viewportY + mState.viewportHeight / 2.0f};
2035 programBinary->setUniform4fv(coord, 1, whxy);
2036
2037 GLint depth = programBinary->getDxDepthLocation();
2038 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
2039 programBinary->setUniform2fv(depth, 1, dz);
2040
2041 GLint depthRange = programBinary->getDxDepthRangeLocation();
2042 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2043 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
2044 mDxUniformsDirty = false;
2045 }
2046
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002047 mRenderer->applyRenderTarget(framebufferObject);
2048
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002049 return true;
2050}
2051
2052// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
2053void Context::applyState(GLenum drawMode)
2054{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002055 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002056
2057 Framebuffer *framebufferObject = getDrawFramebuffer();
2058
2059 GLint frontCCW = programBinary->getDxFrontCCWLocation();
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002060 GLint ccw = (mState.rasterizer.frontFace == GL_CCW);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002061 programBinary->setUniform1iv(frontCCW, 1, &ccw);
2062
2063 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
2064 GLint alwaysFront = !isTriangleMode(drawMode);
2065 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2066
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002067 if (mCullStateDirty || mFrontFaceDirty || mPolygonOffsetStateDirty)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002068 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002069 const gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
2070 unsigned int depthSize = depthbuffer ? depthbuffer->getDepthSize() : 0;
2071
2072 mRenderer->setRasterizerState(mState.rasterizer, depthSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002073
2074 mCullStateDirty = false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002075 mPolygonOffsetStateDirty = false;
2076 }
2077
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002078 if (mBlendStateDirty || mMaskStateDirty)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002079 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002080 unsigned int mask = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002081 if (mState.sampleCoverage)
2082 {
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002083 if (mState.sampleCoverageValue != 0)
2084 {
2085 float threshold = 0.5f;
2086
2087 for (int i = 0; i < framebufferObject->getSamples(); ++i)
2088 {
2089 mask <<= 1;
2090
2091 if ((i + 1) * mState.sampleCoverageValue >= threshold)
2092 {
2093 threshold += 1.0f;
2094 mask |= 1;
2095 }
2096 }
2097 }
2098
2099 if (mState.sampleCoverageInvert)
2100 {
2101 mask = ~mask;
2102 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002103 }
2104 else
2105 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002106 mask = 0xFFFFFFFF;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002107 }
2108
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002109 mRenderer->setBlendState(mState.blend, mState.blendColor, mask);
2110 mBlendStateDirty = false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002111 }
2112
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002113 if (mStencilStateDirty || mFrontFaceDirty || mDepthStateDirty)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002114 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002115 unsigned int stencilSize = framebufferObject->hasStencil() ? framebufferObject->getStencilbuffer()->getStencilSize() : 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002116
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002117 mRenderer->setDepthStencilState(mState.depthStencil,
2118 mState.rasterizer.frontFace == GL_CCW,
2119 stencilSize);
2120
2121 mDepthStateDirty = false;
2122 mStencilStateDirty = false;
2123 mFrontFaceDirty = false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002124 }
2125}
2126
2127GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
2128{
2129 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
2130
daniel@transgaming.com408caa52012-10-31 18:47:01 +00002131 ProgramBinary *programBinary = getCurrentProgramBinary();
2132 GLenum err = mVertexDataManager->prepareVertexData(mState.vertexAttribute, programBinary, first, count, attributes, instances);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002133 if (err != GL_NO_ERROR)
2134 {
2135 return err;
2136 }
daniel@transgaming.com408caa52012-10-31 18:47:01 +00002137
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00002138 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002139}
2140
2141// Applies the indices and element array bindings to the Direct3D 9 device
2142GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
2143{
2144 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
2145
2146 if (err == GL_NO_ERROR)
2147 {
2148 if (indexInfo->serial != mAppliedIBSerial)
2149 {
2150 mDevice->SetIndices(indexInfo->indexBuffer);
2151 mAppliedIBSerial = indexInfo->serial;
2152 }
2153 }
2154
2155 return err;
2156}
2157
2158// Applies the shaders and shader constants to the Direct3D 9 device
2159void Context::applyShaders()
2160{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002161 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002162
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002163 if (programBinary->getSerial() != mAppliedProgramBinarySerial)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002164 {
2165 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2166 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
2167
2168 mDevice->SetPixelShader(pixelShader);
2169 mDevice->SetVertexShader(vertexShader);
2170 programBinary->dirtyAllUniforms();
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002171 mAppliedProgramBinarySerial = programBinary->getSerial();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002172 }
2173
2174 programBinary->applyUniforms();
2175}
2176
2177// Applies the textures and sampler states to the Direct3D 9 device
2178void Context::applyTextures()
2179{
2180 applyTextures(SAMPLER_PIXEL);
2181
2182 if (mSupportsVertexTexture)
2183 {
2184 applyTextures(SAMPLER_VERTEX);
2185 }
2186}
2187
2188// For each Direct3D 9 sampler of either the pixel or vertex stage,
2189// looks up the corresponding OpenGL texture image unit and texture type,
2190// and sets the texture and its addressing/filtering state (or NULL when inactive).
2191void Context::applyTextures(SamplerType type)
2192{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002193 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002194
2195 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
2196 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002197 int samplerRange = programBinary->getUsedSamplerRange(type);
2198
2199 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
2200 {
2201 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002202
2203 if (textureUnit != -1)
2204 {
2205 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
2206
2207 Texture *texture = getSamplerTexture(textureUnit, textureType);
2208 unsigned int texSerial = texture->getTextureSerial();
2209
2210 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
2211 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002212 if (texture->isSamplerComplete())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002213 {
2214 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
2215 {
daniel@transgaming.comebf139f2012-10-31 18:07:32 +00002216 SamplerState samplerState;
2217 texture->getSamplerState(&samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002218
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002219 mRenderer->setSamplerState(type, samplerIndex, samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002220 }
2221
2222 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
2223 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002224 mRenderer->setTexture(type, samplerIndex, texture);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002225 }
2226 }
2227 else
2228 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002229 mRenderer->setTexture(type, samplerIndex, getIncompleteTexture(textureType));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002230 }
2231
2232 appliedTextureSerial[samplerIndex] = texSerial;
2233 texture->resetDirty();
2234 }
2235 }
2236 else
2237 {
2238 if (appliedTextureSerial[samplerIndex] != 0)
2239 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002240 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002241 appliedTextureSerial[samplerIndex] = 0;
2242 }
2243 }
2244 }
2245
2246 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2247 {
2248 if (appliedTextureSerial[samplerIndex] != 0)
2249 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002250 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002251 appliedTextureSerial[samplerIndex] = 0;
2252 }
2253 }
2254}
2255
2256void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2257 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2258{
2259 Framebuffer *framebuffer = getReadFramebuffer();
2260
2261 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2262 {
2263 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2264 }
2265
2266 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2267 {
2268 return error(GL_INVALID_OPERATION);
2269 }
2270
daniel@transgaming.com6452adf2012-10-17 18:22:35 +00002271 GLsizei outputPitch = ComputePitch(width, ConvertSizedInternalFormat(format, type), mState.packAlignment);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002272 // sized query sanity check
2273 if (bufSize)
2274 {
2275 int requiredSize = outputPitch * height;
2276 if (requiredSize > *bufSize)
2277 {
2278 return error(GL_INVALID_OPERATION);
2279 }
2280 }
2281
2282 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2283 if (!renderTarget)
2284 {
2285 return; // Context must be lost, return silently
2286 }
2287
2288 D3DSURFACE_DESC desc;
2289 renderTarget->GetDesc(&desc);
2290
2291 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2292 {
2293 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2294 renderTarget->Release();
2295 return error(GL_OUT_OF_MEMORY);
2296 }
2297
2298 HRESULT result;
2299 IDirect3DSurface9 *systemSurface = NULL;
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002300 bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mRenderer->getShareHandleSupport() &&
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002301 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2302 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2303 if (directToPixels)
2304 {
2305 // Use the pixels ptr as a shared handle to write directly into client's memory
2306 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2307 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2308 if (FAILED(result))
2309 {
2310 // Try again without the shared handle
2311 directToPixels = false;
2312 }
2313 }
2314
2315 if (!directToPixels)
2316 {
2317 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2318 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2319 if (FAILED(result))
2320 {
2321 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2322 renderTarget->Release();
2323 return error(GL_OUT_OF_MEMORY);
2324 }
2325 }
2326
2327 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
2328 renderTarget->Release();
2329 renderTarget = NULL;
2330
2331 if (FAILED(result))
2332 {
2333 systemSurface->Release();
2334
2335 // It turns out that D3D will sometimes produce more error
2336 // codes than those documented.
2337 if (checkDeviceLost(result))
2338 return error(GL_OUT_OF_MEMORY);
2339 else
2340 {
2341 UNREACHABLE();
2342 return;
2343 }
2344
2345 }
2346
2347 if (directToPixels)
2348 {
2349 systemSurface->Release();
2350 return;
2351 }
2352
2353 RECT rect;
2354 rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
2355 rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
2356 rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2357 rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2358
2359 D3DLOCKED_RECT lock;
2360 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2361
2362 if (FAILED(result))
2363 {
2364 UNREACHABLE();
2365 systemSurface->Release();
2366
2367 return; // No sensible error to generate
2368 }
2369
2370 unsigned char *dest = (unsigned char*)pixels;
2371 unsigned short *dest16 = (unsigned short*)pixels;
2372
2373 unsigned char *source;
2374 int inputPitch;
2375 if (getPackReverseRowOrder())
2376 {
2377 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2378 inputPitch = -lock.Pitch;
2379 }
2380 else
2381 {
2382 source = (unsigned char*)lock.pBits;
2383 inputPitch = lock.Pitch;
2384 }
2385
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002386 unsigned int fastPixelSize = 0;
2387
2388 if (desc.Format == D3DFMT_A8R8G8B8 &&
2389 format == GL_BGRA_EXT &&
2390 type == GL_UNSIGNED_BYTE)
2391 {
2392 fastPixelSize = 4;
2393 }
2394 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2395 format == GL_BGRA_EXT &&
2396 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2397 (desc.Format == D3DFMT_A1R5G5B5 &&
2398 format == GL_BGRA_EXT &&
2399 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2400 {
2401 fastPixelSize = 2;
2402 }
2403 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2404 format == GL_RGBA &&
2405 type == GL_HALF_FLOAT_OES)
2406 {
2407 fastPixelSize = 8;
2408 }
2409 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2410 format == GL_RGBA &&
2411 type == GL_FLOAT)
2412 {
2413 fastPixelSize = 16;
2414 }
2415
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002416 for (int j = 0; j < rect.bottom - rect.top; j++)
2417 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002418 if (fastPixelSize != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002419 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002420 // Fast path for formats which require no translation:
2421 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2422 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2423 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2424 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2425 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2426 //
2427 // Note that buffers with no alpha go through the slow path below.
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002428 memcpy(dest + j * outputPitch,
2429 source + j * inputPitch,
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002430 (rect.right - rect.left) * fastPixelSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002431 continue;
2432 }
2433
2434 for (int i = 0; i < rect.right - rect.left; i++)
2435 {
2436 float r;
2437 float g;
2438 float b;
2439 float a;
2440
2441 switch (desc.Format)
2442 {
2443 case D3DFMT_R5G6B5:
2444 {
2445 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2446
2447 a = 1.0f;
2448 b = (rgb & 0x001F) * (1.0f / 0x001F);
2449 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2450 r = (rgb & 0xF800) * (1.0f / 0xF800);
2451 }
2452 break;
2453 case D3DFMT_A1R5G5B5:
2454 {
2455 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2456
2457 a = (argb & 0x8000) ? 1.0f : 0.0f;
2458 b = (argb & 0x001F) * (1.0f / 0x001F);
2459 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2460 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2461 }
2462 break;
2463 case D3DFMT_A8R8G8B8:
2464 {
2465 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2466
2467 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2468 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2469 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2470 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2471 }
2472 break;
2473 case D3DFMT_X8R8G8B8:
2474 {
2475 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2476
2477 a = 1.0f;
2478 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2479 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2480 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2481 }
2482 break;
2483 case D3DFMT_A2R10G10B10:
2484 {
2485 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2486
2487 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2488 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2489 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2490 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2491 }
2492 break;
2493 case D3DFMT_A32B32G32R32F:
2494 {
2495 // float formats in D3D are stored rgba, rather than the other way round
2496 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2497 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2498 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2499 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2500 }
2501 break;
2502 case D3DFMT_A16B16G16R16F:
2503 {
2504 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgaa480672012-09-05 19:32:38 +00002505 r = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2506 g = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2507 b = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2508 a = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002509 }
2510 break;
2511 default:
2512 UNIMPLEMENTED(); // FIXME
2513 UNREACHABLE();
2514 return;
2515 }
2516
2517 switch (format)
2518 {
2519 case GL_RGBA:
2520 switch (type)
2521 {
2522 case GL_UNSIGNED_BYTE:
2523 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2524 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2525 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2526 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2527 break;
2528 default: UNREACHABLE();
2529 }
2530 break;
2531 case GL_BGRA_EXT:
2532 switch (type)
2533 {
2534 case GL_UNSIGNED_BYTE:
2535 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2536 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2537 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2538 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2539 break;
2540 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2541 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2542 // this type is packed as follows:
2543 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2544 // --------------------------------------------------------------------------------
2545 // | 4th | 3rd | 2nd | 1st component |
2546 // --------------------------------------------------------------------------------
2547 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2548 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2549 ((unsigned short)(15 * a + 0.5f) << 12)|
2550 ((unsigned short)(15 * r + 0.5f) << 8) |
2551 ((unsigned short)(15 * g + 0.5f) << 4) |
2552 ((unsigned short)(15 * b + 0.5f) << 0);
2553 break;
2554 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2555 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2556 // this type is packed as follows:
2557 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2558 // --------------------------------------------------------------------------------
2559 // | 4th | 3rd | 2nd | 1st component |
2560 // --------------------------------------------------------------------------------
2561 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2562 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2563 ((unsigned short)( a + 0.5f) << 15) |
2564 ((unsigned short)(31 * r + 0.5f) << 10) |
2565 ((unsigned short)(31 * g + 0.5f) << 5) |
2566 ((unsigned short)(31 * b + 0.5f) << 0);
2567 break;
2568 default: UNREACHABLE();
2569 }
2570 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002571 case GL_RGB:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002572 switch (type)
2573 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002574 case GL_UNSIGNED_SHORT_5_6_5:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002575 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2576 ((unsigned short)(31 * b + 0.5f) << 0) |
2577 ((unsigned short)(63 * g + 0.5f) << 5) |
2578 ((unsigned short)(31 * r + 0.5f) << 11);
2579 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002580 case GL_UNSIGNED_BYTE:
2581 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2582 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2583 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2584 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002585 default: UNREACHABLE();
2586 }
2587 break;
2588 default: UNREACHABLE();
2589 }
2590 }
2591 }
2592
2593 systemSurface->UnlockRect();
2594
2595 systemSurface->Release();
2596}
2597
2598void Context::clear(GLbitfield mask)
2599{
2600 Framebuffer *framebufferObject = getDrawFramebuffer();
2601
2602 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2603 {
2604 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2605 }
2606
2607 DWORD flags = 0;
2608
2609 if (mask & GL_COLOR_BUFFER_BIT)
2610 {
2611 mask &= ~GL_COLOR_BUFFER_BIT;
2612
2613 if (framebufferObject->getColorbufferType() != GL_NONE)
2614 {
2615 flags |= D3DCLEAR_TARGET;
2616 }
2617 }
2618
2619 if (mask & GL_DEPTH_BUFFER_BIT)
2620 {
2621 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002622 if (mState.depthStencil.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002623 {
2624 flags |= D3DCLEAR_ZBUFFER;
2625 }
2626 }
2627
2628 GLuint stencilUnmasked = 0x0;
2629
2630 if (mask & GL_STENCIL_BUFFER_BIT)
2631 {
2632 mask &= ~GL_STENCIL_BUFFER_BIT;
2633 if (framebufferObject->getStencilbufferType() != GL_NONE)
2634 {
2635 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2636 if (!depthStencil)
2637 {
2638 ERR("Depth stencil pointer unexpectedly null.");
2639 return;
2640 }
2641
2642 D3DSURFACE_DESC desc;
2643 depthStencil->GetDesc(&desc);
2644 depthStencil->Release();
2645
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002646 unsigned int stencilSize = d3d9_gl::GetStencilSize(desc.Format);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002647 stencilUnmasked = (0x1 << stencilSize) - 1;
2648
2649 if (stencilUnmasked != 0x0)
2650 {
2651 flags |= D3DCLEAR_STENCIL;
2652 }
2653 }
2654 }
2655
2656 if (mask != 0)
2657 {
2658 return error(GL_INVALID_VALUE);
2659 }
2660
2661 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2662 {
2663 return;
2664 }
2665
2666 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2667 unorm<8>(mState.colorClearValue.red),
2668 unorm<8>(mState.colorClearValue.green),
2669 unorm<8>(mState.colorClearValue.blue));
2670 float depth = clamp01(mState.depthClearValue);
2671 int stencil = mState.stencilClearValue & 0x000000FF;
2672
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002673
2674 bool alphaUnmasked = (d3d9_gl::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002675
2676 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002677 (mState.depthStencil.stencilWritemask & stencilUnmasked) != stencilUnmasked;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002678 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002679 !(mState.blend.colorMaskRed && mState.blend.colorMaskGreen &&
2680 mState.blend.colorMaskBlue && alphaUnmasked);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002681
2682 if (needMaskedColorClear || needMaskedStencilClear)
2683 {
2684 // State which is altered in all paths from this point to the clear call is saved.
2685 // State which is altered in only some paths will be flagged dirty in the case that
2686 // that path is taken.
2687 HRESULT hr;
2688 if (mMaskedClearSavedState == NULL)
2689 {
2690 hr = mDevice->BeginStateBlock();
2691 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2692
2693 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2694 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2695 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2696 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2697 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2698 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2699 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2700 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2701 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2702 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2703 mDevice->SetPixelShader(NULL);
2704 mDevice->SetVertexShader(NULL);
2705 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2706 mDevice->SetStreamSource(0, NULL, 0, 0);
2707 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2708 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2709 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2710 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2711 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2712 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2713 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2714
2715 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2716 {
2717 mDevice->SetStreamSourceFreq(i, 1);
2718 }
2719
2720 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2721 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2722 }
2723
2724 ASSERT(mMaskedClearSavedState != NULL);
2725
2726 if (mMaskedClearSavedState != NULL)
2727 {
2728 hr = mMaskedClearSavedState->Capture();
2729 ASSERT(SUCCEEDED(hr));
2730 }
2731
2732 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2733 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2734 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2735 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2736 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2737 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2738 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2739 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2740
2741 if (flags & D3DCLEAR_TARGET)
2742 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002743 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
2744 gl_d3d9::ConvertColorMask(mState.blend.colorMaskRed,
2745 mState.blend.colorMaskGreen,
2746 mState.blend.colorMaskBlue,
2747 mState.blend.colorMaskAlpha));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002748 }
2749 else
2750 {
2751 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2752 }
2753
2754 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2755 {
2756 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2757 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2758 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2759 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002760 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.depthStencil.stencilWritemask);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002761 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2762 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2763 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
2764 mStencilStateDirty = true;
2765 }
2766 else
2767 {
2768 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2769 }
2770
2771 mDevice->SetPixelShader(NULL);
2772 mDevice->SetVertexShader(NULL);
2773 mDevice->SetFVF(D3DFVF_XYZRHW);
2774 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2775 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2776 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2777 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2778 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2779 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2780 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2781
2782 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2783 {
2784 mDevice->SetStreamSourceFreq(i, 1);
2785 }
2786
2787 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2788 quad[0][0] = -0.5f;
2789 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
2790 quad[0][2] = 0.0f;
2791 quad[0][3] = 1.0f;
2792
2793 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2794 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
2795 quad[1][2] = 0.0f;
2796 quad[1][3] = 1.0f;
2797
2798 quad[2][0] = -0.5f;
2799 quad[2][1] = -0.5f;
2800 quad[2][2] = 0.0f;
2801 quad[2][3] = 1.0f;
2802
2803 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
2804 quad[3][1] = -0.5f;
2805 quad[3][2] = 0.0f;
2806 quad[3][3] = 1.0f;
2807
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002808 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002809 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2810
2811 if (flags & D3DCLEAR_ZBUFFER)
2812 {
2813 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2814 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2815 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2816 }
2817
2818 if (mMaskedClearSavedState != NULL)
2819 {
2820 mMaskedClearSavedState->Apply();
2821 }
2822 }
2823 else if (flags)
2824 {
2825 mDevice->Clear(0, NULL, flags, color, depth, stencil);
2826 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00002827
2828 mRenderer->clear(mask, mState.colorClearValue, mState.depthClearValue, mState.stencilClearValue,
2829 framebufferObject);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002830}
2831
2832void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
2833{
2834 if (!mState.currentProgram)
2835 {
2836 return error(GL_INVALID_OPERATION);
2837 }
2838
2839 D3DPRIMITIVETYPE primitiveType;
2840 int primitiveCount;
2841
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002842 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002843 return error(GL_INVALID_ENUM);
2844
2845 if (primitiveCount <= 0)
2846 {
2847 return;
2848 }
2849
2850 if (!applyRenderTarget(false))
2851 {
2852 return;
2853 }
2854
2855 applyState(mode);
2856
2857 GLsizei repeatDraw = 1;
2858 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
2859 if (err != GL_NO_ERROR)
2860 {
2861 return error(err);
2862 }
2863
2864 applyShaders();
2865 applyTextures();
2866
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002867 if (!getCurrentProgramBinary()->validateSamplers(NULL))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002868 {
2869 return error(GL_INVALID_OPERATION);
2870 }
2871
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002872 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002873 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002874 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002875
2876 if (mode == GL_LINE_LOOP)
2877 {
2878 drawLineLoop(count, GL_NONE, NULL, 0);
2879 }
2880 else if (instances > 0)
2881 {
2882 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2883 if (countingIB)
2884 {
2885 if (mAppliedIBSerial != countingIB->getSerial())
2886 {
2887 mDevice->SetIndices(countingIB->getBuffer());
2888 mAppliedIBSerial = countingIB->getSerial();
2889 }
2890
2891 for (int i = 0; i < repeatDraw; i++)
2892 {
2893 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2894 }
2895 }
2896 else
2897 {
2898 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2899 return error(GL_OUT_OF_MEMORY);
2900 }
2901 }
2902 else // Regular case
2903 {
2904 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
2905 }
2906 }
2907}
2908
2909void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
2910{
2911 if (!mState.currentProgram)
2912 {
2913 return error(GL_INVALID_OPERATION);
2914 }
2915
2916 if (!indices && !mState.elementArrayBuffer)
2917 {
2918 return error(GL_INVALID_OPERATION);
2919 }
2920
2921 D3DPRIMITIVETYPE primitiveType;
2922 int primitiveCount;
2923
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002924 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002925 return error(GL_INVALID_ENUM);
2926
2927 if (primitiveCount <= 0)
2928 {
2929 return;
2930 }
2931
2932 if (!applyRenderTarget(false))
2933 {
2934 return;
2935 }
2936
2937 applyState(mode);
2938
2939 TranslatedIndexData indexInfo;
2940 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2941 if (err != GL_NO_ERROR)
2942 {
2943 return error(err);
2944 }
2945
2946 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2947 GLsizei repeatDraw = 1;
2948 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
2949 if (err != GL_NO_ERROR)
2950 {
2951 return error(err);
2952 }
2953
2954 applyShaders();
2955 applyTextures();
2956
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002957 if (!getCurrentProgramBinary()->validateSamplers(false))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002958 {
2959 return error(GL_INVALID_OPERATION);
2960 }
2961
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002962 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002963 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002964 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002965
2966 if (mode == GL_LINE_LOOP)
2967 {
2968 drawLineLoop(count, type, indices, indexInfo.minIndex);
2969 }
2970 else
2971 {
2972 for (int i = 0; i < repeatDraw; i++)
2973 {
2974 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
2975 }
2976 }
2977 }
2978}
2979
2980// Implements glFlush when block is false, glFinish when block is true
2981void Context::sync(bool block)
2982{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002983 mRenderer->sync(block);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002984}
2985
2986void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
2987{
2988 // Get the raw indices for an indexed draw
2989 if (type != GL_NONE && mState.elementArrayBuffer.get())
2990 {
2991 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2992 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2993 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2994 }
2995
2996 UINT startIndex = 0;
2997 bool succeeded = false;
2998
2999 if (supports32bitIndices())
3000 {
3001 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3002
3003 if (!mLineLoopIB)
3004 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00003005 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003006 }
3007
3008 if (mLineLoopIB)
3009 {
3010 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3011
3012 UINT offset = 0;
3013 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3014 startIndex = offset / 4;
3015
3016 if (data)
3017 {
3018 switch (type)
3019 {
3020 case GL_NONE: // Non-indexed draw
3021 for (int i = 0; i < count; i++)
3022 {
3023 data[i] = i;
3024 }
3025 data[count] = 0;
3026 break;
3027 case GL_UNSIGNED_BYTE:
3028 for (int i = 0; i < count; i++)
3029 {
3030 data[i] = static_cast<const GLubyte*>(indices)[i];
3031 }
3032 data[count] = static_cast<const GLubyte*>(indices)[0];
3033 break;
3034 case GL_UNSIGNED_SHORT:
3035 for (int i = 0; i < count; i++)
3036 {
3037 data[i] = static_cast<const GLushort*>(indices)[i];
3038 }
3039 data[count] = static_cast<const GLushort*>(indices)[0];
3040 break;
3041 case GL_UNSIGNED_INT:
3042 for (int i = 0; i < count; i++)
3043 {
3044 data[i] = static_cast<const GLuint*>(indices)[i];
3045 }
3046 data[count] = static_cast<const GLuint*>(indices)[0];
3047 break;
3048 default: UNREACHABLE();
3049 }
3050
3051 mLineLoopIB->unmap();
3052 succeeded = true;
3053 }
3054 }
3055 }
3056 else
3057 {
3058 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3059
3060 if (!mLineLoopIB)
3061 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00003062 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003063 }
3064
3065 if (mLineLoopIB)
3066 {
3067 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3068
3069 UINT offset = 0;
3070 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3071 startIndex = offset / 2;
3072
3073 if (data)
3074 {
3075 switch (type)
3076 {
3077 case GL_NONE: // Non-indexed draw
3078 for (int i = 0; i < count; i++)
3079 {
3080 data[i] = i;
3081 }
3082 data[count] = 0;
3083 break;
3084 case GL_UNSIGNED_BYTE:
3085 for (int i = 0; i < count; i++)
3086 {
3087 data[i] = static_cast<const GLubyte*>(indices)[i];
3088 }
3089 data[count] = static_cast<const GLubyte*>(indices)[0];
3090 break;
3091 case GL_UNSIGNED_SHORT:
3092 for (int i = 0; i < count; i++)
3093 {
3094 data[i] = static_cast<const GLushort*>(indices)[i];
3095 }
3096 data[count] = static_cast<const GLushort*>(indices)[0];
3097 break;
3098 case GL_UNSIGNED_INT:
3099 for (int i = 0; i < count; i++)
3100 {
3101 data[i] = static_cast<const GLuint*>(indices)[i];
3102 }
3103 data[count] = static_cast<const GLuint*>(indices)[0];
3104 break;
3105 default: UNREACHABLE();
3106 }
3107
3108 mLineLoopIB->unmap();
3109 succeeded = true;
3110 }
3111 }
3112 }
3113
3114 if (succeeded)
3115 {
3116 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3117 {
3118 mDevice->SetIndices(mLineLoopIB->getBuffer());
3119 mAppliedIBSerial = mLineLoopIB->getSerial();
3120 }
3121
3122 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3123 }
3124 else
3125 {
3126 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3127 return error(GL_OUT_OF_MEMORY);
3128 }
3129}
3130
3131void Context::recordInvalidEnum()
3132{
3133 mInvalidEnum = true;
3134}
3135
3136void Context::recordInvalidValue()
3137{
3138 mInvalidValue = true;
3139}
3140
3141void Context::recordInvalidOperation()
3142{
3143 mInvalidOperation = true;
3144}
3145
3146void Context::recordOutOfMemory()
3147{
3148 mOutOfMemory = true;
3149}
3150
3151void Context::recordInvalidFramebufferOperation()
3152{
3153 mInvalidFramebufferOperation = true;
3154}
3155
3156// Get one of the recorded errors and clear its flag, if any.
3157// [OpenGL ES 2.0.24] section 2.5 page 13.
3158GLenum Context::getError()
3159{
3160 if (mInvalidEnum)
3161 {
3162 mInvalidEnum = false;
3163
3164 return GL_INVALID_ENUM;
3165 }
3166
3167 if (mInvalidValue)
3168 {
3169 mInvalidValue = false;
3170
3171 return GL_INVALID_VALUE;
3172 }
3173
3174 if (mInvalidOperation)
3175 {
3176 mInvalidOperation = false;
3177
3178 return GL_INVALID_OPERATION;
3179 }
3180
3181 if (mOutOfMemory)
3182 {
3183 mOutOfMemory = false;
3184
3185 return GL_OUT_OF_MEMORY;
3186 }
3187
3188 if (mInvalidFramebufferOperation)
3189 {
3190 mInvalidFramebufferOperation = false;
3191
3192 return GL_INVALID_FRAMEBUFFER_OPERATION;
3193 }
3194
3195 return GL_NO_ERROR;
3196}
3197
3198GLenum Context::getResetStatus()
3199{
3200 if (mResetStatus == GL_NO_ERROR)
3201 {
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00003202 // mResetStatus will be set by the markContextLost callback
3203 // in the case a notification is sent
3204 mRenderer->testDeviceLost(true);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003205 }
3206
3207 GLenum status = mResetStatus;
3208
3209 if (mResetStatus != GL_NO_ERROR)
3210 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003211 if (mRenderer->testDeviceResettable())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003212 {
3213 mResetStatus = GL_NO_ERROR;
3214 }
3215 }
3216
3217 return status;
3218}
3219
3220bool Context::isResetNotificationEnabled()
3221{
3222 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3223}
3224
3225bool Context::supportsShaderModel3() const
3226{
3227 return mSupportsShaderModel3;
3228}
3229
3230float Context::getMaximumPointSize() const
3231{
3232 return mSupportsShaderModel3 ? mMaximumPointSize : ALIASED_POINT_SIZE_RANGE_MAX_SM2;
3233}
3234
3235int Context::getMaximumVaryingVectors() const
3236{
3237 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3238}
3239
3240unsigned int Context::getMaximumVertexTextureImageUnits() const
3241{
3242 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3243}
3244
3245unsigned int Context::getMaximumCombinedTextureImageUnits() const
3246{
3247 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3248}
3249
3250int Context::getMaximumFragmentUniformVectors() const
3251{
3252 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3253}
3254
3255int Context::getMaxSupportedSamples() const
3256{
daniel@transgaming.comb7833982012-10-31 18:31:46 +00003257 return mRenderer->getMaxSupportedSamples();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003258}
3259
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003260bool Context::supportsEventQueries() const
3261{
3262 return mSupportsEventQueries;
3263}
3264
3265bool Context::supportsOcclusionQueries() const
3266{
3267 return mSupportsOcclusionQueries;
3268}
3269
3270bool Context::supportsDXT1Textures() const
3271{
3272 return mSupportsDXT1Textures;
3273}
3274
3275bool Context::supportsDXT3Textures() const
3276{
3277 return mSupportsDXT3Textures;
3278}
3279
3280bool Context::supportsDXT5Textures() const
3281{
3282 return mSupportsDXT5Textures;
3283}
3284
3285bool Context::supportsFloat32Textures() const
3286{
3287 return mSupportsFloat32Textures;
3288}
3289
3290bool Context::supportsFloat32LinearFilter() const
3291{
3292 return mSupportsFloat32LinearFilter;
3293}
3294
3295bool Context::supportsFloat32RenderableTextures() const
3296{
3297 return mSupportsFloat32RenderableTextures;
3298}
3299
3300bool Context::supportsFloat16Textures() const
3301{
3302 return mSupportsFloat16Textures;
3303}
3304
3305bool Context::supportsFloat16LinearFilter() const
3306{
3307 return mSupportsFloat16LinearFilter;
3308}
3309
3310bool Context::supportsFloat16RenderableTextures() const
3311{
3312 return mSupportsFloat16RenderableTextures;
3313}
3314
3315int Context::getMaximumRenderbufferDimension() const
3316{
3317 return mMaxRenderbufferDimension;
3318}
3319
3320int Context::getMaximumTextureDimension() const
3321{
3322 return mMaxTextureDimension;
3323}
3324
3325int Context::getMaximumCubeTextureDimension() const
3326{
3327 return mMaxCubeTextureDimension;
3328}
3329
3330int Context::getMaximumTextureLevel() const
3331{
3332 return mMaxTextureLevel;
3333}
3334
3335bool Context::supportsLuminanceTextures() const
3336{
3337 return mSupportsLuminanceTextures;
3338}
3339
3340bool Context::supportsLuminanceAlphaTextures() const
3341{
3342 return mSupportsLuminanceAlphaTextures;
3343}
3344
3345bool Context::supportsDepthTextures() const
3346{
3347 return mSupportsDepthTextures;
3348}
3349
3350bool Context::supports32bitIndices() const
3351{
3352 return mSupports32bitIndices;
3353}
3354
3355bool Context::supportsNonPower2Texture() const
3356{
3357 return mSupportsNonPower2Texture;
3358}
3359
3360bool Context::supportsInstancing() const
3361{
3362 return mSupportsInstancing;
3363}
3364
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003365bool Context::supportsTextureFilterAnisotropy() const
3366{
3367 return mSupportsTextureFilterAnisotropy;
3368}
3369
3370float Context::getTextureMaxAnisotropy() const
3371{
3372 return mMaxTextureAnisotropy;
3373}
3374
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003375bool Context::getCurrentReadFormatType(GLenum *format, GLenum *type)
3376{
3377 Framebuffer *framebuffer = getReadFramebuffer();
3378 if (!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3379 {
3380 return error(GL_INVALID_OPERATION, false);
3381 }
3382
3383 Renderbuffer *renderbuffer = framebuffer->getColorbuffer();
3384 if (!renderbuffer)
3385 {
3386 return error(GL_INVALID_OPERATION, false);
3387 }
3388
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003389 *format = gl::ExtractFormat(renderbuffer->getActualFormat());
3390 *type = gl::ExtractType(renderbuffer->getActualFormat());
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003391
3392 return true;
3393}
3394
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003395void Context::detachBuffer(GLuint buffer)
3396{
3397 // [OpenGL ES 2.0.24] section 2.9 page 22:
3398 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3399 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3400
3401 if (mState.arrayBuffer.id() == buffer)
3402 {
3403 mState.arrayBuffer.set(NULL);
3404 }
3405
3406 if (mState.elementArrayBuffer.id() == buffer)
3407 {
3408 mState.elementArrayBuffer.set(NULL);
3409 }
3410
3411 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3412 {
3413 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
3414 {
3415 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
3416 }
3417 }
3418}
3419
3420void Context::detachTexture(GLuint texture)
3421{
3422 // [OpenGL ES 2.0.24] section 3.8 page 84:
3423 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3424 // rebound to texture object zero
3425
3426 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3427 {
3428 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
3429 {
3430 if (mState.samplerTexture[type][sampler].id() == texture)
3431 {
3432 mState.samplerTexture[type][sampler].set(NULL);
3433 }
3434 }
3435 }
3436
3437 // [OpenGL ES 2.0.24] section 4.4 page 112:
3438 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3439 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3440 // image was attached in the currently bound framebuffer.
3441
3442 Framebuffer *readFramebuffer = getReadFramebuffer();
3443 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3444
3445 if (readFramebuffer)
3446 {
3447 readFramebuffer->detachTexture(texture);
3448 }
3449
3450 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3451 {
3452 drawFramebuffer->detachTexture(texture);
3453 }
3454}
3455
3456void Context::detachFramebuffer(GLuint framebuffer)
3457{
3458 // [OpenGL ES 2.0.24] section 4.4 page 107:
3459 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3460 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3461
3462 if (mState.readFramebuffer == framebuffer)
3463 {
3464 bindReadFramebuffer(0);
3465 }
3466
3467 if (mState.drawFramebuffer == framebuffer)
3468 {
3469 bindDrawFramebuffer(0);
3470 }
3471}
3472
3473void Context::detachRenderbuffer(GLuint renderbuffer)
3474{
3475 // [OpenGL ES 2.0.24] section 4.4 page 109:
3476 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3477 // had been executed with the target RENDERBUFFER and name of zero.
3478
3479 if (mState.renderbuffer.id() == renderbuffer)
3480 {
3481 bindRenderbuffer(0);
3482 }
3483
3484 // [OpenGL ES 2.0.24] section 4.4 page 111:
3485 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3486 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3487 // point to which this image was attached in the currently bound framebuffer.
3488
3489 Framebuffer *readFramebuffer = getReadFramebuffer();
3490 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3491
3492 if (readFramebuffer)
3493 {
3494 readFramebuffer->detachRenderbuffer(renderbuffer);
3495 }
3496
3497 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3498 {
3499 drawFramebuffer->detachRenderbuffer(renderbuffer);
3500 }
3501}
3502
3503Texture *Context::getIncompleteTexture(TextureType type)
3504{
3505 Texture *t = mIncompleteTextures[type].get();
3506
3507 if (t == NULL)
3508 {
3509 static const GLubyte color[] = { 0, 0, 0, 255 };
3510
3511 switch (type)
3512 {
3513 default:
3514 UNREACHABLE();
3515 // default falls through to TEXTURE_2D
3516
3517 case TEXTURE_2D:
3518 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003519 Texture2D *incomplete2d = new Texture2D(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003520 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3521 t = incomplete2d;
3522 }
3523 break;
3524
3525 case TEXTURE_CUBE:
3526 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003527 TextureCubeMap *incompleteCube = new TextureCubeMap(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003528
3529 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3530 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3531 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3532 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3533 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3534 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3535
3536 t = incompleteCube;
3537 }
3538 break;
3539 }
3540
3541 mIncompleteTextures[type].set(t);
3542 }
3543
3544 return t;
3545}
3546
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003547bool Context::skipDraw(GLenum drawMode)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003548{
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003549 if (drawMode == GL_POINTS)
3550 {
3551 // ProgramBinary assumes non-point rendering if gl_PointSize isn't written,
3552 // which affects varying interpolation. Since the value of gl_PointSize is
3553 // undefined when not written, just skip drawing to avoid unexpected results.
3554 if (!getCurrentProgramBinary()->usesPointSize())
3555 {
3556 // This is stictly speaking not an error, but developers should be
3557 // notified of risking undefined behavior.
3558 ERR("Point rendering without writing to gl_PointSize.");
3559
3560 return true;
3561 }
3562 }
3563 else if (isTriangleMode(drawMode))
3564 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003565 if (mState.rasterizer.cullFace && mState.rasterizer.cullMode == GL_FRONT_AND_BACK)
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003566 {
3567 return true;
3568 }
3569 }
3570
3571 return false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003572}
3573
3574bool Context::isTriangleMode(GLenum drawMode)
3575{
3576 switch (drawMode)
3577 {
3578 case GL_TRIANGLES:
3579 case GL_TRIANGLE_FAN:
3580 case GL_TRIANGLE_STRIP:
3581 return true;
3582 case GL_POINTS:
3583 case GL_LINES:
3584 case GL_LINE_LOOP:
3585 case GL_LINE_STRIP:
3586 return false;
3587 default: UNREACHABLE();
3588 }
3589
3590 return false;
3591}
3592
3593void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3594{
3595 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3596
3597 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3598 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3599 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3600 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
3601
3602 mVertexDataManager->dirtyCurrentValue(index);
3603}
3604
3605void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3606{
3607 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3608
3609 mState.vertexAttribute[index].mDivisor = divisor;
3610}
3611
3612// keep list sorted in following order
3613// OES extensions
3614// EXT extensions
3615// Vendor extensions
3616void Context::initExtensionString()
3617{
3618 mExtensionString = "";
3619
3620 // OES extensions
3621 if (supports32bitIndices())
3622 {
3623 mExtensionString += "GL_OES_element_index_uint ";
3624 }
3625
3626 mExtensionString += "GL_OES_packed_depth_stencil ";
3627 mExtensionString += "GL_OES_get_program_binary ";
3628 mExtensionString += "GL_OES_rgb8_rgba8 ";
3629 mExtensionString += "GL_OES_standard_derivatives ";
3630
3631 if (supportsFloat16Textures())
3632 {
3633 mExtensionString += "GL_OES_texture_half_float ";
3634 }
3635 if (supportsFloat16LinearFilter())
3636 {
3637 mExtensionString += "GL_OES_texture_half_float_linear ";
3638 }
3639 if (supportsFloat32Textures())
3640 {
3641 mExtensionString += "GL_OES_texture_float ";
3642 }
3643 if (supportsFloat32LinearFilter())
3644 {
3645 mExtensionString += "GL_OES_texture_float_linear ";
3646 }
3647
3648 if (supportsNonPower2Texture())
3649 {
3650 mExtensionString += "GL_OES_texture_npot ";
3651 }
3652
3653 // Multi-vendor (EXT) extensions
3654 if (supportsOcclusionQueries())
3655 {
3656 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3657 }
3658
3659 mExtensionString += "GL_EXT_read_format_bgra ";
3660 mExtensionString += "GL_EXT_robustness ";
3661
3662 if (supportsDXT1Textures())
3663 {
3664 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3665 }
3666
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003667 if (supportsTextureFilterAnisotropy())
3668 {
3669 mExtensionString += "GL_EXT_texture_filter_anisotropic ";
3670 }
3671
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003672 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3673 mExtensionString += "GL_EXT_texture_storage ";
3674
3675 // ANGLE-specific extensions
3676 if (supportsDepthTextures())
3677 {
3678 mExtensionString += "GL_ANGLE_depth_texture ";
3679 }
3680
3681 mExtensionString += "GL_ANGLE_framebuffer_blit ";
3682 if (getMaxSupportedSamples() != 0)
3683 {
3684 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3685 }
3686
3687 if (supportsInstancing())
3688 {
3689 mExtensionString += "GL_ANGLE_instanced_arrays ";
3690 }
3691
3692 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3693
3694 if (supportsDXT3Textures())
3695 {
3696 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3697 }
3698 if (supportsDXT5Textures())
3699 {
3700 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3701 }
3702
3703 mExtensionString += "GL_ANGLE_texture_usage ";
3704 mExtensionString += "GL_ANGLE_translated_shader_source ";
3705
3706 // Other vendor-specific extensions
3707 if (supportsEventQueries())
3708 {
3709 mExtensionString += "GL_NV_fence ";
3710 }
3711
3712 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3713 if (end != std::string::npos)
3714 {
3715 mExtensionString.resize(end+1);
3716 }
3717}
3718
3719const char *Context::getExtensionString() const
3720{
3721 return mExtensionString.c_str();
3722}
3723
3724void Context::initRendererString()
3725{
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003726 mRendererString = "ANGLE (";
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00003727 mRendererString += mRenderer->getAdapterDescription();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003728 mRendererString += ")";
3729}
3730
3731const char *Context::getRendererString() const
3732{
3733 return mRendererString.c_str();
3734}
3735
3736void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3737 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3738 GLbitfield mask)
3739{
3740 Framebuffer *readFramebuffer = getReadFramebuffer();
3741 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3742
3743 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3744 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3745 {
3746 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3747 }
3748
3749 if (drawFramebuffer->getSamples() != 0)
3750 {
3751 return error(GL_INVALID_OPERATION);
3752 }
3753
3754 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3755 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3756 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3757 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3758
3759 RECT sourceRect;
3760 RECT destRect;
3761
3762 if (srcX0 < srcX1)
3763 {
3764 sourceRect.left = srcX0;
3765 sourceRect.right = srcX1;
3766 destRect.left = dstX0;
3767 destRect.right = dstX1;
3768 }
3769 else
3770 {
3771 sourceRect.left = srcX1;
3772 destRect.left = dstX1;
3773 sourceRect.right = srcX0;
3774 destRect.right = dstX0;
3775 }
3776
3777 if (srcY0 < srcY1)
3778 {
3779 sourceRect.bottom = srcY1;
3780 destRect.bottom = dstY1;
3781 sourceRect.top = srcY0;
3782 destRect.top = dstY0;
3783 }
3784 else
3785 {
3786 sourceRect.bottom = srcY0;
3787 destRect.bottom = dstY0;
3788 sourceRect.top = srcY1;
3789 destRect.top = dstY1;
3790 }
3791
3792 RECT sourceScissoredRect = sourceRect;
3793 RECT destScissoredRect = destRect;
3794
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003795 if (mState.rasterizer.scissorTest)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003796 {
3797 // Only write to parts of the destination framebuffer which pass the scissor test
3798 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3799 // rect will be checked against scissorY, rather than the bottom.
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003800 if (destRect.left < mState.scissor.x)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003801 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003802 int xDiff = mState.scissor.x - destRect.left;
3803 destScissoredRect.left = mState.scissor.x;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003804 sourceScissoredRect.left += xDiff;
3805 }
3806
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003807 if (destRect.right > mState.scissor.x + mState.scissor.width)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003808 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003809 int xDiff = destRect.right - (mState.scissor.x + mState.scissor.width);
3810 destScissoredRect.right = mState.scissor.x + mState.scissor.width;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003811 sourceScissoredRect.right -= xDiff;
3812 }
3813
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003814 if (destRect.top < mState.scissor.y)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003815 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003816 int yDiff = mState.scissor.y - destRect.top;
3817 destScissoredRect.top = mState.scissor.y;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003818 sourceScissoredRect.top += yDiff;
3819 }
3820
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003821 if (destRect.bottom > mState.scissor.y + mState.scissor.height)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003822 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003823 int yDiff = destRect.bottom - (mState.scissor.y + mState.scissor.height);
3824 destScissoredRect.bottom = mState.scissor.y + mState.scissor.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003825 sourceScissoredRect.bottom -= yDiff;
3826 }
3827 }
3828
3829 bool blitRenderTarget = false;
3830 bool blitDepthStencil = false;
3831
3832 RECT sourceTrimmedRect = sourceScissoredRect;
3833 RECT destTrimmedRect = destScissoredRect;
3834
3835 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3836 // the actual draw and read surfaces.
3837 if (sourceTrimmedRect.left < 0)
3838 {
3839 int xDiff = 0 - sourceTrimmedRect.left;
3840 sourceTrimmedRect.left = 0;
3841 destTrimmedRect.left += xDiff;
3842 }
3843
3844 if (sourceTrimmedRect.right > readBufferWidth)
3845 {
3846 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3847 sourceTrimmedRect.right = readBufferWidth;
3848 destTrimmedRect.right -= xDiff;
3849 }
3850
3851 if (sourceTrimmedRect.top < 0)
3852 {
3853 int yDiff = 0 - sourceTrimmedRect.top;
3854 sourceTrimmedRect.top = 0;
3855 destTrimmedRect.top += yDiff;
3856 }
3857
3858 if (sourceTrimmedRect.bottom > readBufferHeight)
3859 {
3860 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3861 sourceTrimmedRect.bottom = readBufferHeight;
3862 destTrimmedRect.bottom -= yDiff;
3863 }
3864
3865 if (destTrimmedRect.left < 0)
3866 {
3867 int xDiff = 0 - destTrimmedRect.left;
3868 destTrimmedRect.left = 0;
3869 sourceTrimmedRect.left += xDiff;
3870 }
3871
3872 if (destTrimmedRect.right > drawBufferWidth)
3873 {
3874 int xDiff = destTrimmedRect.right - drawBufferWidth;
3875 destTrimmedRect.right = drawBufferWidth;
3876 sourceTrimmedRect.right -= xDiff;
3877 }
3878
3879 if (destTrimmedRect.top < 0)
3880 {
3881 int yDiff = 0 - destTrimmedRect.top;
3882 destTrimmedRect.top = 0;
3883 sourceTrimmedRect.top += yDiff;
3884 }
3885
3886 if (destTrimmedRect.bottom > drawBufferHeight)
3887 {
3888 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3889 destTrimmedRect.bottom = drawBufferHeight;
3890 sourceTrimmedRect.bottom -= yDiff;
3891 }
3892
3893 bool partialBufferCopy = false;
3894 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3895 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3896 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3897 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
3898 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3899 {
3900 partialBufferCopy = true;
3901 }
3902
3903 if (mask & GL_COLOR_BUFFER_BIT)
3904 {
3905 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3906 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3907 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3908 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3909 if (!validReadType || !validDrawType ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003910 readFramebuffer->getColorbuffer()->getActualFormat() != drawFramebuffer->getColorbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003911 {
3912 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3913 return error(GL_INVALID_OPERATION);
3914 }
3915
3916 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3917 {
3918 return error(GL_INVALID_OPERATION);
3919 }
3920
3921 blitRenderTarget = true;
3922
3923 }
3924
3925 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3926 {
3927 Renderbuffer *readDSBuffer = NULL;
3928 Renderbuffer *drawDSBuffer = NULL;
3929
3930 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3931 // both a depth and stencil buffer, it will be the same buffer.
3932
3933 if (mask & GL_DEPTH_BUFFER_BIT)
3934 {
3935 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3936 {
3937 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003938 readFramebuffer->getDepthbuffer()->getActualFormat() != drawFramebuffer->getDepthbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003939 {
3940 return error(GL_INVALID_OPERATION);
3941 }
3942
3943 blitDepthStencil = true;
3944 readDSBuffer = readFramebuffer->getDepthbuffer();
3945 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3946 }
3947 }
3948
3949 if (mask & GL_STENCIL_BUFFER_BIT)
3950 {
3951 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3952 {
3953 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003954 readFramebuffer->getStencilbuffer()->getActualFormat() != drawFramebuffer->getStencilbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003955 {
3956 return error(GL_INVALID_OPERATION);
3957 }
3958
3959 blitDepthStencil = true;
3960 readDSBuffer = readFramebuffer->getStencilbuffer();
3961 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3962 }
3963 }
3964
3965 if (partialBufferCopy)
3966 {
3967 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3968 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3969 }
3970
3971 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3972 (readDSBuffer && readDSBuffer->getSamples() != 0))
3973 {
3974 return error(GL_INVALID_OPERATION);
3975 }
3976 }
3977
3978 if (blitRenderTarget || blitDepthStencil)
3979 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003980 mRenderer->endScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003981
3982 if (blitRenderTarget)
3983 {
3984 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
3985 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
3986
3987 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
3988 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
3989
3990 readRenderTarget->Release();
3991 drawRenderTarget->Release();
3992
3993 if (FAILED(result))
3994 {
3995 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3996 return;
3997 }
3998 }
3999
4000 if (blitDepthStencil)
4001 {
4002 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4003 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4004
4005 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4006
4007 readDepthStencil->Release();
4008 drawDepthStencil->Release();
4009
4010 if (FAILED(result))
4011 {
4012 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4013 return;
4014 }
4015 }
4016 }
4017}
4018
4019VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4020{
4021 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4022 {
4023 mVertexDeclCache[i].vertexDeclaration = NULL;
4024 mVertexDeclCache[i].lruCount = 0;
4025 }
4026}
4027
4028VertexDeclarationCache::~VertexDeclarationCache()
4029{
4030 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4031 {
4032 if (mVertexDeclCache[i].vertexDeclaration)
4033 {
4034 mVertexDeclCache[i].vertexDeclaration->Release();
4035 }
4036 }
4037}
4038
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00004039GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004040{
4041 *repeatDraw = 1;
4042
4043 int indexedAttribute = MAX_VERTEX_ATTRIBS;
4044 int instancedAttribute = MAX_VERTEX_ATTRIBS;
4045
4046 if (instances > 0)
4047 {
4048 // Find an indexed attribute to be mapped to D3D stream 0
4049 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4050 {
4051 if (attributes[i].active)
4052 {
4053 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4054 {
4055 if (attributes[i].divisor == 0)
4056 {
4057 indexedAttribute = i;
4058 }
4059 }
4060 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4061 {
4062 if (attributes[i].divisor != 0)
4063 {
4064 instancedAttribute = i;
4065 }
4066 }
4067 else break; // Found both an indexed and instanced attribute
4068 }
4069 }
4070
4071 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4072 {
4073 return GL_INVALID_OPERATION;
4074 }
4075 }
4076
4077 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4078 D3DVERTEXELEMENT9 *element = &elements[0];
4079
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004080 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4081 {
4082 if (attributes[i].active)
4083 {
4084 int stream = i;
4085
4086 if (instances > 0)
4087 {
4088 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4089 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4090 {
4091 *repeatDraw = instances;
4092 }
4093 else
4094 {
4095 if (i == indexedAttribute)
4096 {
4097 stream = 0;
4098 }
4099 else if (i == 0)
4100 {
4101 stream = indexedAttribute;
4102 }
4103
4104 UINT frequency = 1;
4105
4106 if (attributes[i].divisor == 0)
4107 {
4108 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4109 }
4110 else
4111 {
4112 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4113 }
4114
4115 device->SetStreamSourceFreq(stream, frequency);
4116 mInstancingEnabled = true;
4117 }
4118 }
4119
4120 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4121 mAppliedVBs[stream].stride != attributes[i].stride ||
4122 mAppliedVBs[stream].offset != attributes[i].offset)
4123 {
4124 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4125 mAppliedVBs[stream].serial = attributes[i].serial;
4126 mAppliedVBs[stream].stride = attributes[i].stride;
4127 mAppliedVBs[stream].offset = attributes[i].offset;
4128 }
4129
4130 element->Stream = stream;
4131 element->Offset = 0;
4132 element->Type = attributes[i].type;
4133 element->Method = D3DDECLMETHOD_DEFAULT;
4134 element->Usage = D3DDECLUSAGE_TEXCOORD;
4135 element->UsageIndex = programBinary->getSemanticIndex(i);
4136 element++;
4137 }
4138 }
4139
4140 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
4141 {
4142 if (mInstancingEnabled)
4143 {
4144 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4145 {
4146 device->SetStreamSourceFreq(i, 1);
4147 }
4148
4149 mInstancingEnabled = false;
4150 }
4151 }
4152
4153 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4154 *(element++) = end;
4155
4156 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4157 {
4158 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4159 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4160 {
4161 entry->lruCount = ++mMaxLru;
4162 if(entry->vertexDeclaration != mLastSetVDecl)
4163 {
4164 device->SetVertexDeclaration(entry->vertexDeclaration);
4165 mLastSetVDecl = entry->vertexDeclaration;
4166 }
4167
4168 return GL_NO_ERROR;
4169 }
4170 }
4171
4172 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4173
4174 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4175 {
4176 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4177 {
4178 lastCache = &mVertexDeclCache[i];
4179 }
4180 }
4181
4182 if (lastCache->vertexDeclaration != NULL)
4183 {
4184 lastCache->vertexDeclaration->Release();
4185 lastCache->vertexDeclaration = NULL;
4186 // mLastSetVDecl is set to the replacement, so we don't have to worry
4187 // about it.
4188 }
4189
4190 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4191 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4192 device->SetVertexDeclaration(lastCache->vertexDeclaration);
4193 mLastSetVDecl = lastCache->vertexDeclaration;
4194 lastCache->lruCount = ++mMaxLru;
4195
4196 return GL_NO_ERROR;
4197}
4198
4199void VertexDeclarationCache::markStateDirty()
4200{
4201 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4202 {
4203 mAppliedVBs[i].serial = 0;
4204 }
4205
4206 mLastSetVDecl = NULL;
4207 mInstancingEnabled = true; // Forces it to be disabled when not used
4208}
4209
4210}
4211
4212extern "C"
4213{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004214gl::Context *glCreateContext(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004215{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004216 return new gl::Context(shareContext, renderer, notifyResets, robustAccess);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004217}
4218
4219void glDestroyContext(gl::Context *context)
4220{
4221 delete context;
4222
4223 if (context == gl::getContext())
4224 {
4225 gl::makeCurrent(NULL, NULL, NULL);
4226 }
4227}
4228
4229void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4230{
4231 gl::makeCurrent(context, display, surface);
4232}
4233
4234gl::Context *glGetCurrentContext()
4235{
4236 return gl::getContext();
4237}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004238
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004239rx::SwapChain *glCreateSwapChain(rx::Renderer9 *renderer, HWND window, HANDLE shareHandle,
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004240 GLenum backBufferFormat, GLenum depthBufferFormat)
4241{
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004242 return new rx::SwapChain(renderer, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004243}
4244
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004245void glDestroySwapChain(rx::SwapChain *swapChain)
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004246{
4247 delete swapChain;
4248}
4249
4250
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004251}