blob: 646baedfb55a29f0dc72dbd1096a65bfb60c10e8 [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
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000360 mDxUniformsDirty = true;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000361}
362
363void Context::markDxUniformsDirty()
364{
365 mDxUniformsDirty = true;
366}
367
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000368// NOTE: this function should not assume that this context is current!
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000369void Context::markContextLost()
370{
371 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
372 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
373 mContextLost = true;
374}
375
376bool Context::isContextLost()
377{
378 return mContextLost;
379}
380
381void Context::setClearColor(float red, float green, float blue, float alpha)
382{
383 mState.colorClearValue.red = red;
384 mState.colorClearValue.green = green;
385 mState.colorClearValue.blue = blue;
386 mState.colorClearValue.alpha = alpha;
387}
388
389void Context::setClearDepth(float depth)
390{
391 mState.depthClearValue = depth;
392}
393
394void Context::setClearStencil(int stencil)
395{
396 mState.stencilClearValue = stencil;
397}
398
399void Context::setCullFace(bool enabled)
400{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000401 mState.rasterizer.cullFace = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000402}
403
404bool Context::isCullFaceEnabled() const
405{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000406 return mState.rasterizer.cullFace;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000407}
408
409void Context::setCullMode(GLenum mode)
410{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000411 mState.rasterizer.cullMode = mode;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000412}
413
414void Context::setFrontFace(GLenum front)
415{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000416 mState.rasterizer.frontFace = front;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000417}
418
419void Context::setDepthTest(bool enabled)
420{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000421 mState.depthStencil.depthTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000422}
423
424bool Context::isDepthTestEnabled() const
425{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000426 return mState.depthStencil.depthTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000427}
428
429void Context::setDepthFunc(GLenum depthFunc)
430{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000431 mState.depthStencil.depthFunc = depthFunc;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000432}
433
434void Context::setDepthRange(float zNear, float zFar)
435{
436 mState.zNear = zNear;
437 mState.zFar = zFar;
438}
439
440void Context::setBlend(bool enabled)
441{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000442 mState.blend.blend = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000443}
444
445bool Context::isBlendEnabled() const
446{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000447 return mState.blend.blend;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000448}
449
450void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
451{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000452 mState.blend.sourceBlendRGB = sourceRGB;
453 mState.blend.destBlendRGB = destRGB;
454 mState.blend.sourceBlendAlpha = sourceAlpha;
455 mState.blend.destBlendAlpha = destAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000456}
457
458void Context::setBlendColor(float red, float green, float blue, float alpha)
459{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000460 mState.blendColor.red = red;
461 mState.blendColor.green = green;
462 mState.blendColor.blue = blue;
463 mState.blendColor.alpha = alpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000464}
465
466void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
467{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000468 mState.blend.blendEquationRGB = rgbEquation;
469 mState.blend.blendEquationAlpha = alphaEquation;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000470}
471
472void Context::setStencilTest(bool enabled)
473{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000474 mState.depthStencil.stencilTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000475}
476
477bool Context::isStencilTestEnabled() const
478{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000479 return mState.depthStencil.stencilTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000480}
481
482void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
483{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000484 mState.depthStencil.stencilFunc = stencilFunc;
485 mState.depthStencil.stencilRef = (stencilRef > 0) ? stencilRef : 0;
486 mState.depthStencil.stencilMask = stencilMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000487}
488
489void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
490{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000491 mState.depthStencil.stencilBackFunc = stencilBackFunc;
492 mState.depthStencil.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
493 mState.depthStencil.stencilBackMask = stencilBackMask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000494}
495
496void Context::setStencilWritemask(GLuint stencilWritemask)
497{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000498 mState.depthStencil.stencilWritemask = stencilWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000499}
500
501void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
502{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000503 mState.depthStencil.stencilBackWritemask = stencilBackWritemask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000504}
505
506void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
507{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000508 mState.depthStencil.stencilFail = stencilFail;
509 mState.depthStencil.stencilPassDepthFail = stencilPassDepthFail;
510 mState.depthStencil.stencilPassDepthPass = stencilPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000511}
512
513void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
514{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000515 mState.depthStencil.stencilBackFail = stencilBackFail;
516 mState.depthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
517 mState.depthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000518}
519
520void Context::setPolygonOffsetFill(bool enabled)
521{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000522 mState.rasterizer.polygonOffsetFill = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000523}
524
525bool Context::isPolygonOffsetFillEnabled() const
526{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000527 return mState.rasterizer.polygonOffsetFill;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000528}
529
530void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
531{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000532 mState.rasterizer.polygonOffsetFactor = factor;
533 mState.rasterizer.polygonOffsetUnits = units;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000534}
535
536void Context::setSampleAlphaToCoverage(bool enabled)
537{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000538 mState.blend.sampleAlphaToCoverage = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000539}
540
541bool Context::isSampleAlphaToCoverageEnabled() const
542{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000543 return mState.blend.sampleAlphaToCoverage;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000544}
545
546void Context::setSampleCoverage(bool enabled)
547{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000548 mState.sampleCoverage = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000549}
550
551bool Context::isSampleCoverageEnabled() const
552{
553 return mState.sampleCoverage;
554}
555
556void Context::setSampleCoverageParams(GLclampf value, bool invert)
557{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000558 mState.sampleCoverageValue = value;
559 mState.sampleCoverageInvert = invert;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000560}
561
562void Context::setScissorTest(bool enabled)
563{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000564 mState.rasterizer.scissorTest = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000565}
566
567bool Context::isScissorTestEnabled() const
568{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000569 return mState.rasterizer.scissorTest;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000570}
571
572void Context::setDither(bool enabled)
573{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000574 mState.blend.dither = enabled;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000575}
576
577bool Context::isDitherEnabled() const
578{
daniel@transgaming.comf39967e2012-11-28 19:35:56 +0000579 return mState.blend.dither;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000580}
581
582void Context::setLineWidth(GLfloat width)
583{
584 mState.lineWidth = width;
585}
586
587void Context::setGenerateMipmapHint(GLenum hint)
588{
589 mState.generateMipmapHint = hint;
590}
591
592void Context::setFragmentShaderDerivativeHint(GLenum hint)
593{
594 mState.fragmentShaderDerivativeHint = hint;
595 // TODO: Propagate the hint to shader translator so we can write
596 // ddx, ddx_coarse, or ddx_fine depending on the hint.
597 // Ignore for now. It is valid for implementations to ignore hint.
598}
599
600void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
601{
602 mState.viewportX = x;
603 mState.viewportY = y;
604 mState.viewportWidth = width;
605 mState.viewportHeight = height;
606}
607
608void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
609{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000610 mState.scissor.x = x;
611 mState.scissor.y = y;
612 mState.scissor.width = width;
613 mState.scissor.height = height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000614}
615
616void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
617{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000618 mState.blend.colorMaskRed = red;
619 mState.blend.colorMaskGreen = green;
620 mState.blend.colorMaskBlue = blue;
621 mState.blend.colorMaskAlpha = alpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000622}
623
624void Context::setDepthMask(bool mask)
625{
daniel@transgaming.com2e258642012-11-28 19:36:18 +0000626 mState.depthStencil.depthMask = mask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000627}
628
629void Context::setActiveSampler(unsigned int active)
630{
631 mState.activeSampler = active;
632}
633
634GLuint Context::getReadFramebufferHandle() const
635{
636 return mState.readFramebuffer;
637}
638
639GLuint Context::getDrawFramebufferHandle() const
640{
641 return mState.drawFramebuffer;
642}
643
644GLuint Context::getRenderbufferHandle() const
645{
646 return mState.renderbuffer.id();
647}
648
649GLuint Context::getArrayBufferHandle() const
650{
651 return mState.arrayBuffer.id();
652}
653
654GLuint Context::getActiveQuery(GLenum target) const
655{
656 Query *queryObject = NULL;
657
658 switch (target)
659 {
660 case GL_ANY_SAMPLES_PASSED_EXT:
661 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
662 break;
663 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
664 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
665 break;
666 default:
667 ASSERT(false);
668 }
669
670 if (queryObject)
671 {
672 return queryObject->id();
673 }
674 else
675 {
676 return 0;
677 }
678}
679
680void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
681{
682 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
683}
684
685const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
686{
687 return mState.vertexAttribute[attribNum];
688}
689
690void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
691 GLsizei stride, const void *pointer)
692{
693 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
694 mState.vertexAttribute[attribNum].mSize = size;
695 mState.vertexAttribute[attribNum].mType = type;
696 mState.vertexAttribute[attribNum].mNormalized = normalized;
697 mState.vertexAttribute[attribNum].mStride = stride;
698 mState.vertexAttribute[attribNum].mPointer = pointer;
699}
700
701const void *Context::getVertexAttribPointer(unsigned int attribNum) const
702{
703 return mState.vertexAttribute[attribNum].mPointer;
704}
705
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000706void Context::setPackAlignment(GLint alignment)
707{
708 mState.packAlignment = alignment;
709}
710
711GLint Context::getPackAlignment() const
712{
713 return mState.packAlignment;
714}
715
716void Context::setUnpackAlignment(GLint alignment)
717{
718 mState.unpackAlignment = alignment;
719}
720
721GLint Context::getUnpackAlignment() const
722{
723 return mState.unpackAlignment;
724}
725
726void Context::setPackReverseRowOrder(bool reverseRowOrder)
727{
728 mState.packReverseRowOrder = reverseRowOrder;
729}
730
731bool Context::getPackReverseRowOrder() const
732{
733 return mState.packReverseRowOrder;
734}
735
736GLuint Context::createBuffer()
737{
738 return mResourceManager->createBuffer();
739}
740
741GLuint Context::createProgram()
742{
743 return mResourceManager->createProgram();
744}
745
746GLuint Context::createShader(GLenum type)
747{
748 return mResourceManager->createShader(type);
749}
750
751GLuint Context::createTexture()
752{
753 return mResourceManager->createTexture();
754}
755
756GLuint Context::createRenderbuffer()
757{
758 return mResourceManager->createRenderbuffer();
759}
760
761// Returns an unused framebuffer name
762GLuint Context::createFramebuffer()
763{
764 GLuint handle = mFramebufferHandleAllocator.allocate();
765
766 mFramebufferMap[handle] = NULL;
767
768 return handle;
769}
770
771GLuint Context::createFence()
772{
773 GLuint handle = mFenceHandleAllocator.allocate();
774
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000775 mFenceMap[handle] = new Fence(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000776
777 return handle;
778}
779
780// Returns an unused query name
781GLuint Context::createQuery()
782{
783 GLuint handle = mQueryHandleAllocator.allocate();
784
785 mQueryMap[handle] = NULL;
786
787 return handle;
788}
789
790void Context::deleteBuffer(GLuint buffer)
791{
792 if (mResourceManager->getBuffer(buffer))
793 {
794 detachBuffer(buffer);
795 }
796
797 mResourceManager->deleteBuffer(buffer);
798}
799
800void Context::deleteShader(GLuint shader)
801{
802 mResourceManager->deleteShader(shader);
803}
804
805void Context::deleteProgram(GLuint program)
806{
807 mResourceManager->deleteProgram(program);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000808}
809
810void Context::deleteTexture(GLuint texture)
811{
812 if (mResourceManager->getTexture(texture))
813 {
814 detachTexture(texture);
815 }
816
817 mResourceManager->deleteTexture(texture);
818}
819
820void Context::deleteRenderbuffer(GLuint renderbuffer)
821{
822 if (mResourceManager->getRenderbuffer(renderbuffer))
823 {
824 detachRenderbuffer(renderbuffer);
825 }
826
827 mResourceManager->deleteRenderbuffer(renderbuffer);
828}
829
830void Context::deleteFramebuffer(GLuint framebuffer)
831{
832 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
833
834 if (framebufferObject != mFramebufferMap.end())
835 {
836 detachFramebuffer(framebuffer);
837
838 mFramebufferHandleAllocator.release(framebufferObject->first);
839 delete framebufferObject->second;
840 mFramebufferMap.erase(framebufferObject);
841 }
842}
843
844void Context::deleteFence(GLuint fence)
845{
846 FenceMap::iterator fenceObject = mFenceMap.find(fence);
847
848 if (fenceObject != mFenceMap.end())
849 {
850 mFenceHandleAllocator.release(fenceObject->first);
851 delete fenceObject->second;
852 mFenceMap.erase(fenceObject);
853 }
854}
855
856void Context::deleteQuery(GLuint query)
857{
858 QueryMap::iterator queryObject = mQueryMap.find(query);
859 if (queryObject != mQueryMap.end())
860 {
861 mQueryHandleAllocator.release(queryObject->first);
862 if (queryObject->second)
863 {
864 queryObject->second->release();
865 }
866 mQueryMap.erase(queryObject);
867 }
868}
869
870Buffer *Context::getBuffer(GLuint handle)
871{
872 return mResourceManager->getBuffer(handle);
873}
874
875Shader *Context::getShader(GLuint handle)
876{
877 return mResourceManager->getShader(handle);
878}
879
880Program *Context::getProgram(GLuint handle)
881{
882 return mResourceManager->getProgram(handle);
883}
884
885Texture *Context::getTexture(GLuint handle)
886{
887 return mResourceManager->getTexture(handle);
888}
889
890Renderbuffer *Context::getRenderbuffer(GLuint handle)
891{
892 return mResourceManager->getRenderbuffer(handle);
893}
894
895Framebuffer *Context::getReadFramebuffer()
896{
897 return getFramebuffer(mState.readFramebuffer);
898}
899
900Framebuffer *Context::getDrawFramebuffer()
901{
902 return mBoundDrawFramebuffer;
903}
904
905void Context::bindArrayBuffer(unsigned int buffer)
906{
907 mResourceManager->checkBufferAllocation(buffer);
908
909 mState.arrayBuffer.set(getBuffer(buffer));
910}
911
912void Context::bindElementArrayBuffer(unsigned int buffer)
913{
914 mResourceManager->checkBufferAllocation(buffer);
915
916 mState.elementArrayBuffer.set(getBuffer(buffer));
917}
918
919void Context::bindTexture2D(GLuint texture)
920{
921 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
922
923 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
924}
925
926void Context::bindTextureCubeMap(GLuint texture)
927{
928 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
929
930 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
931}
932
933void Context::bindReadFramebuffer(GLuint framebuffer)
934{
935 if (!getFramebuffer(framebuffer))
936 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000937 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000938 }
939
940 mState.readFramebuffer = framebuffer;
941}
942
943void Context::bindDrawFramebuffer(GLuint framebuffer)
944{
945 if (!getFramebuffer(framebuffer))
946 {
daniel@transgaming.com16418b12012-11-28 19:32:22 +0000947 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000948 }
949
950 mState.drawFramebuffer = framebuffer;
951
952 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
953}
954
955void Context::bindRenderbuffer(GLuint renderbuffer)
956{
957 mResourceManager->checkRenderbufferAllocation(renderbuffer);
958
959 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
960}
961
962void Context::useProgram(GLuint program)
963{
964 GLuint priorProgram = mState.currentProgram;
965 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
966
967 if (priorProgram != program)
968 {
969 Program *newProgram = mResourceManager->getProgram(program);
970 Program *oldProgram = mResourceManager->getProgram(priorProgram);
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000971 mCurrentProgramBinary.set(NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000972 mDxUniformsDirty = true;
973
974 if (newProgram)
975 {
976 newProgram->addRef();
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000977 mCurrentProgramBinary.set(newProgram->getProgramBinary());
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000978 }
979
980 if (oldProgram)
981 {
982 oldProgram->release();
983 }
984 }
985}
986
daniel@transgaming.com95d29422012-07-24 18:36:10 +0000987void Context::linkProgram(GLuint program)
988{
989 Program *programObject = mResourceManager->getProgram(program);
990
daniel@transgaming.com12394cf2012-07-24 18:37:59 +0000991 bool linked = programObject->link();
992
993 // if the current program was relinked successfully we
994 // need to install the new executables
995 if (linked && program == mState.currentProgram)
996 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +0000997 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +0000998 mDxUniformsDirty = true;
999 }
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001000}
1001
1002void Context::setProgramBinary(GLuint program, const void *binary, GLint length)
1003{
1004 Program *programObject = mResourceManager->getProgram(program);
1005
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001006 bool loaded = programObject->setProgramBinary(binary, length);
1007
1008 // if the current program was reloaded successfully we
1009 // need to install the new executables
1010 if (loaded && program == mState.currentProgram)
1011 {
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001012 mCurrentProgramBinary.set(programObject->getProgramBinary());
daniel@transgaming.com12394cf2012-07-24 18:37:59 +00001013 mDxUniformsDirty = true;
1014 }
1015
daniel@transgaming.com95d29422012-07-24 18:36:10 +00001016}
1017
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001018void Context::beginQuery(GLenum target, GLuint query)
1019{
1020 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1021 // of zero, if the active query object name for <target> is non-zero (for the
1022 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1023 // the active query for either target is non-zero), if <id> is the name of an
1024 // existing query object whose type does not match <target>, or if <id> is the
1025 // active query object name for any query type, the error INVALID_OPERATION is
1026 // generated.
1027
1028 // Ensure no other queries are active
1029 // NOTE: If other queries than occlusion are supported, we will need to check
1030 // separately that:
1031 // a) The query ID passed is not the current active query for any target/type
1032 // b) There are no active queries for the requested target (and in the case
1033 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1034 // no query may be active for either if glBeginQuery targets either.
1035 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1036 {
1037 if (mState.activeQuery[i].get() != NULL)
1038 {
1039 return error(GL_INVALID_OPERATION);
1040 }
1041 }
1042
1043 QueryType qType;
1044 switch (target)
1045 {
1046 case GL_ANY_SAMPLES_PASSED_EXT:
1047 qType = QUERY_ANY_SAMPLES_PASSED;
1048 break;
1049 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1050 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1051 break;
1052 default:
1053 ASSERT(false);
1054 return;
1055 }
1056
1057 Query *queryObject = getQuery(query, true, target);
1058
1059 // check that name was obtained with glGenQueries
1060 if (!queryObject)
1061 {
1062 return error(GL_INVALID_OPERATION);
1063 }
1064
1065 // check for type mismatch
1066 if (queryObject->getType() != target)
1067 {
1068 return error(GL_INVALID_OPERATION);
1069 }
1070
1071 // set query as active for specified target
1072 mState.activeQuery[qType].set(queryObject);
1073
1074 // begin query
1075 queryObject->begin();
1076}
1077
1078void Context::endQuery(GLenum target)
1079{
1080 QueryType qType;
1081
1082 switch (target)
1083 {
1084 case GL_ANY_SAMPLES_PASSED_EXT:
1085 qType = QUERY_ANY_SAMPLES_PASSED;
1086 break;
1087 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1088 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1089 break;
1090 default:
1091 ASSERT(false);
1092 return;
1093 }
1094
1095 Query *queryObject = mState.activeQuery[qType].get();
1096
1097 if (queryObject == NULL)
1098 {
1099 return error(GL_INVALID_OPERATION);
1100 }
1101
1102 queryObject->end();
1103
1104 mState.activeQuery[qType].set(NULL);
1105}
1106
1107void Context::setFramebufferZero(Framebuffer *buffer)
1108{
1109 delete mFramebufferMap[0];
1110 mFramebufferMap[0] = buffer;
1111 if (mState.drawFramebuffer == 0)
1112 {
1113 mBoundDrawFramebuffer = buffer;
1114 }
1115}
1116
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001117void Context::setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001118{
daniel@transgaming.com70062c92012-11-28 19:32:30 +00001119 RenderbufferStorage *renderbuffer = NULL;
1120 switch (internalformat)
1121 {
1122 case GL_DEPTH_COMPONENT16:
1123 renderbuffer = new gl::Depthbuffer(mRenderer, width, height, samples);
1124 break;
1125 case GL_RGBA4:
1126 case GL_RGB5_A1:
1127 case GL_RGB565:
1128 case GL_RGB8_OES:
1129 case GL_RGBA8_OES:
1130 renderbuffer = new gl::Colorbuffer(mRenderer,width, height, internalformat, samples);
1131 break;
1132 case GL_STENCIL_INDEX8:
1133 renderbuffer = new gl::Stencilbuffer(mRenderer, width, height, samples);
1134 break;
1135 case GL_DEPTH24_STENCIL8_OES:
1136 renderbuffer = new gl::DepthStencilbuffer(mRenderer, width, height, samples);
1137 break;
1138 default:
1139 UNREACHABLE(); return;
1140 }
1141
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001142 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1143 renderbufferObject->setStorage(renderbuffer);
1144}
1145
1146Framebuffer *Context::getFramebuffer(unsigned int handle)
1147{
1148 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1149
1150 if (framebuffer == mFramebufferMap.end())
1151 {
1152 return NULL;
1153 }
1154 else
1155 {
1156 return framebuffer->second;
1157 }
1158}
1159
1160Fence *Context::getFence(unsigned int handle)
1161{
1162 FenceMap::iterator fence = mFenceMap.find(handle);
1163
1164 if (fence == mFenceMap.end())
1165 {
1166 return NULL;
1167 }
1168 else
1169 {
1170 return fence->second;
1171 }
1172}
1173
1174Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1175{
1176 QueryMap::iterator query = mQueryMap.find(handle);
1177
1178 if (query == mQueryMap.end())
1179 {
1180 return NULL;
1181 }
1182 else
1183 {
1184 if (!query->second && create)
1185 {
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001186 query->second = new Query(mRenderer, handle, type);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001187 query->second->addRef();
1188 }
1189 return query->second;
1190 }
1191}
1192
1193Buffer *Context::getArrayBuffer()
1194{
1195 return mState.arrayBuffer.get();
1196}
1197
1198Buffer *Context::getElementArrayBuffer()
1199{
1200 return mState.elementArrayBuffer.get();
1201}
1202
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001203ProgramBinary *Context::getCurrentProgramBinary()
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001204{
daniel@transgaming.com989c1c82012-07-24 18:40:38 +00001205 return mCurrentProgramBinary.get();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001206}
1207
1208Texture2D *Context::getTexture2D()
1209{
1210 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1211}
1212
1213TextureCubeMap *Context::getTextureCubeMap()
1214{
1215 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1216}
1217
1218Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
1219{
1220 GLuint texid = mState.samplerTexture[type][sampler].id();
1221
1222 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
1223 {
1224 switch (type)
1225 {
1226 default: UNREACHABLE();
1227 case TEXTURE_2D: return mTexture2DZero.get();
1228 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
1229 }
1230 }
1231
1232 return mState.samplerTexture[type][sampler].get();
1233}
1234
1235bool Context::getBooleanv(GLenum pname, GLboolean *params)
1236{
1237 switch (pname)
1238 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001239 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1240 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1241 case GL_DEPTH_WRITEMASK: *params = mState.depthStencil.depthMask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001242 case GL_COLOR_WRITEMASK:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001243 params[0] = mState.blend.colorMaskRed;
1244 params[1] = mState.blend.colorMaskGreen;
1245 params[2] = mState.blend.colorMaskBlue;
1246 params[3] = mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001247 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001248 case GL_CULL_FACE: *params = mState.rasterizer.cullFace; break;
1249 case GL_POLYGON_OFFSET_FILL: *params = mState.rasterizer.polygonOffsetFill; break;
1250 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.blend.sampleAlphaToCoverage; break;
1251 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1252 case GL_SCISSOR_TEST: *params = mState.rasterizer.scissorTest; break;
1253 case GL_STENCIL_TEST: *params = mState.depthStencil.stencilTest; break;
1254 case GL_DEPTH_TEST: *params = mState.depthStencil.depthTest; break;
1255 case GL_BLEND: *params = mState.blend.blend; break;
1256 case GL_DITHER: *params = mState.blend.dither; break;
1257 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001258 default:
1259 return false;
1260 }
1261
1262 return true;
1263}
1264
1265bool Context::getFloatv(GLenum pname, GLfloat *params)
1266{
1267 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1268 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1269 // GetIntegerv as its native query function. As it would require conversion in any
1270 // case, this should make no difference to the calling application.
1271 switch (pname)
1272 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001273 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1274 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1275 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1276 case GL_POLYGON_OFFSET_FACTOR: *params = mState.rasterizer.polygonOffsetFactor; break;
1277 case GL_POLYGON_OFFSET_UNITS: *params = mState.rasterizer.polygonOffsetUnits; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001278 case GL_ALIASED_LINE_WIDTH_RANGE:
1279 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1280 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1281 break;
1282 case GL_ALIASED_POINT_SIZE_RANGE:
1283 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1284 params[1] = getMaximumPointSize();
1285 break;
1286 case GL_DEPTH_RANGE:
1287 params[0] = mState.zNear;
1288 params[1] = mState.zFar;
1289 break;
1290 case GL_COLOR_CLEAR_VALUE:
1291 params[0] = mState.colorClearValue.red;
1292 params[1] = mState.colorClearValue.green;
1293 params[2] = mState.colorClearValue.blue;
1294 params[3] = mState.colorClearValue.alpha;
1295 break;
1296 case GL_BLEND_COLOR:
1297 params[0] = mState.blendColor.red;
1298 params[1] = mState.blendColor.green;
1299 params[2] = mState.blendColor.blue;
1300 params[3] = mState.blendColor.alpha;
1301 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001302 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1303 if (!supportsTextureFilterAnisotropy())
1304 {
1305 return false;
1306 }
1307 *params = mMaxTextureAnisotropy;
1308 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001309 default:
1310 return false;
1311 }
1312
1313 return true;
1314}
1315
1316bool Context::getIntegerv(GLenum pname, GLint *params)
1317{
1318 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1319 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1320 // GetIntegerv as its native query function. As it would require conversion in any
1321 // case, this should make no difference to the calling application. You may find it in
1322 // Context::getFloatv.
1323 switch (pname)
1324 {
1325 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1326 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1327 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
1328 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1329 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
1330 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1331 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
1332 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
1333 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1334 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1335 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1336 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
1337 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1338 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1339 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1340 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
1341 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1342 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1343 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
1344 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1345 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1346 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1347 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001348 case GL_STENCIL_FUNC: *params = mState.depthStencil.stencilFunc; break;
1349 case GL_STENCIL_REF: *params = mState.depthStencil.stencilRef; break;
1350 case GL_STENCIL_VALUE_MASK: *params = mState.depthStencil.stencilMask; break;
1351 case GL_STENCIL_BACK_FUNC: *params = mState.depthStencil.stencilBackFunc; break;
1352 case GL_STENCIL_BACK_REF: *params = mState.depthStencil.stencilBackRef; break;
1353 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.depthStencil.stencilBackMask; break;
1354 case GL_STENCIL_FAIL: *params = mState.depthStencil.stencilFail; break;
1355 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilPassDepthFail; break;
1356 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilPassDepthPass; break;
1357 case GL_STENCIL_BACK_FAIL: *params = mState.depthStencil.stencilBackFail; break;
1358 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilBackPassDepthFail; break;
1359 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilBackPassDepthPass; break;
1360 case GL_DEPTH_FUNC: *params = mState.depthStencil.depthFunc; break;
1361 case GL_BLEND_SRC_RGB: *params = mState.blend.sourceBlendRGB; break;
1362 case GL_BLEND_SRC_ALPHA: *params = mState.blend.sourceBlendAlpha; break;
1363 case GL_BLEND_DST_RGB: *params = mState.blend.destBlendRGB; break;
1364 case GL_BLEND_DST_ALPHA: *params = mState.blend.destBlendAlpha; break;
1365 case GL_BLEND_EQUATION_RGB: *params = mState.blend.blendEquationRGB; break;
1366 case GL_BLEND_EQUATION_ALPHA: *params = mState.blend.blendEquationAlpha; break;
1367 case GL_STENCIL_WRITEMASK: *params = mState.depthStencil.stencilWritemask; break;
1368 case GL_STENCIL_BACK_WRITEMASK: *params = mState.depthStencil.stencilBackWritemask; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001369 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1370 case GL_SUBPIXEL_BITS: *params = 4; break;
1371 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1372 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
1373 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1374 params[0] = mNumCompressedTextureFormats;
1375 break;
1376 case GL_MAX_SAMPLES_ANGLE:
1377 {
1378 GLsizei maxSamples = getMaxSupportedSamples();
1379 if (maxSamples != 0)
1380 {
1381 *params = maxSamples;
1382 }
1383 else
1384 {
1385 return false;
1386 }
1387
1388 break;
1389 }
1390 case GL_SAMPLE_BUFFERS:
1391 case GL_SAMPLES:
1392 {
1393 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1394 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1395 {
1396 switch (pname)
1397 {
1398 case GL_SAMPLE_BUFFERS:
1399 if (framebuffer->getSamples() != 0)
1400 {
1401 *params = 1;
1402 }
1403 else
1404 {
1405 *params = 0;
1406 }
1407 break;
1408 case GL_SAMPLES:
1409 *params = framebuffer->getSamples();
1410 break;
1411 }
1412 }
1413 else
1414 {
1415 *params = 0;
1416 }
1417 }
1418 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00001419 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1420 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1421 {
1422 GLenum format, type;
1423 if (getCurrentReadFormatType(&format, &type))
1424 {
1425 if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT)
1426 *params = format;
1427 else
1428 *params = type;
1429 }
1430 }
1431 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001432 case GL_MAX_VIEWPORT_DIMS:
1433 {
1434 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
1435 params[0] = maxDimension;
1436 params[1] = maxDimension;
1437 }
1438 break;
1439 case GL_COMPRESSED_TEXTURE_FORMATS:
1440 {
1441 if (supportsDXT1Textures())
1442 {
1443 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1444 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1445 }
1446 if (supportsDXT3Textures())
1447 {
1448 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1449 }
1450 if (supportsDXT5Textures())
1451 {
1452 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
1453 }
1454 }
1455 break;
1456 case GL_VIEWPORT:
1457 params[0] = mState.viewportX;
1458 params[1] = mState.viewportY;
1459 params[2] = mState.viewportWidth;
1460 params[3] = mState.viewportHeight;
1461 break;
1462 case GL_SCISSOR_BOX:
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001463 params[0] = mState.scissor.x;
1464 params[1] = mState.scissor.y;
1465 params[2] = mState.scissor.width;
1466 params[3] = mState.scissor.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001467 break;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001468 case GL_CULL_FACE_MODE: *params = mState.rasterizer.cullMode; break;
1469 case GL_FRONT_FACE: *params = mState.rasterizer.frontFace; break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001470 case GL_RED_BITS:
1471 case GL_GREEN_BITS:
1472 case GL_BLUE_BITS:
1473 case GL_ALPHA_BITS:
1474 {
1475 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1476 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1477
1478 if (colorbuffer)
1479 {
1480 switch (pname)
1481 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001482 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1483 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1484 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1485 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001486 }
1487 }
1488 else
1489 {
1490 *params = 0;
1491 }
1492 }
1493 break;
1494 case GL_DEPTH_BITS:
1495 {
1496 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1497 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1498
1499 if (depthbuffer)
1500 {
1501 *params = depthbuffer->getDepthSize();
1502 }
1503 else
1504 {
1505 *params = 0;
1506 }
1507 }
1508 break;
1509 case GL_STENCIL_BITS:
1510 {
1511 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1512 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1513
1514 if (stencilbuffer)
1515 {
1516 *params = stencilbuffer->getStencilSize();
1517 }
1518 else
1519 {
1520 *params = 0;
1521 }
1522 }
1523 break;
1524 case GL_TEXTURE_BINDING_2D:
1525 {
1526 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1527 {
1528 error(GL_INVALID_OPERATION);
1529 return false;
1530 }
1531
1532 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
1533 }
1534 break;
1535 case GL_TEXTURE_BINDING_CUBE_MAP:
1536 {
1537 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1538 {
1539 error(GL_INVALID_OPERATION);
1540 return false;
1541 }
1542
1543 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
1544 }
1545 break;
1546 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1547 *params = mResetStrategy;
1548 break;
1549 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1550 *params = 1;
1551 break;
1552 case GL_PROGRAM_BINARY_FORMATS_OES:
1553 *params = GL_PROGRAM_BINARY_ANGLE;
1554 break;
1555 default:
1556 return false;
1557 }
1558
1559 return true;
1560}
1561
1562bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1563{
1564 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1565 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1566 // to the fact that it is stored internally as a float, and so would require conversion
1567 // if returned from Context::getIntegerv. Since this conversion is already implemented
1568 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1569 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1570 // application.
1571 switch (pname)
1572 {
1573 case GL_COMPRESSED_TEXTURE_FORMATS:
1574 {
1575 *type = GL_INT;
1576 *numParams = mNumCompressedTextureFormats;
1577 }
1578 break;
1579 case GL_SHADER_BINARY_FORMATS:
1580 {
1581 *type = GL_INT;
1582 *numParams = 0;
1583 }
1584 break;
1585 case GL_MAX_VERTEX_ATTRIBS:
1586 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1587 case GL_MAX_VARYING_VECTORS:
1588 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1589 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1590 case GL_MAX_TEXTURE_IMAGE_UNITS:
1591 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1592 case GL_MAX_RENDERBUFFER_SIZE:
1593 case GL_NUM_SHADER_BINARY_FORMATS:
1594 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1595 case GL_ARRAY_BUFFER_BINDING:
1596 case GL_FRAMEBUFFER_BINDING:
1597 case GL_RENDERBUFFER_BINDING:
1598 case GL_CURRENT_PROGRAM:
1599 case GL_PACK_ALIGNMENT:
1600 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1601 case GL_UNPACK_ALIGNMENT:
1602 case GL_GENERATE_MIPMAP_HINT:
1603 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1604 case GL_RED_BITS:
1605 case GL_GREEN_BITS:
1606 case GL_BLUE_BITS:
1607 case GL_ALPHA_BITS:
1608 case GL_DEPTH_BITS:
1609 case GL_STENCIL_BITS:
1610 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1611 case GL_CULL_FACE_MODE:
1612 case GL_FRONT_FACE:
1613 case GL_ACTIVE_TEXTURE:
1614 case GL_STENCIL_FUNC:
1615 case GL_STENCIL_VALUE_MASK:
1616 case GL_STENCIL_REF:
1617 case GL_STENCIL_FAIL:
1618 case GL_STENCIL_PASS_DEPTH_FAIL:
1619 case GL_STENCIL_PASS_DEPTH_PASS:
1620 case GL_STENCIL_BACK_FUNC:
1621 case GL_STENCIL_BACK_VALUE_MASK:
1622 case GL_STENCIL_BACK_REF:
1623 case GL_STENCIL_BACK_FAIL:
1624 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1625 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1626 case GL_DEPTH_FUNC:
1627 case GL_BLEND_SRC_RGB:
1628 case GL_BLEND_SRC_ALPHA:
1629 case GL_BLEND_DST_RGB:
1630 case GL_BLEND_DST_ALPHA:
1631 case GL_BLEND_EQUATION_RGB:
1632 case GL_BLEND_EQUATION_ALPHA:
1633 case GL_STENCIL_WRITEMASK:
1634 case GL_STENCIL_BACK_WRITEMASK:
1635 case GL_STENCIL_CLEAR_VALUE:
1636 case GL_SUBPIXEL_BITS:
1637 case GL_MAX_TEXTURE_SIZE:
1638 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1639 case GL_SAMPLE_BUFFERS:
1640 case GL_SAMPLES:
1641 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1642 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1643 case GL_TEXTURE_BINDING_2D:
1644 case GL_TEXTURE_BINDING_CUBE_MAP:
1645 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1646 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1647 case GL_PROGRAM_BINARY_FORMATS_OES:
1648 {
1649 *type = GL_INT;
1650 *numParams = 1;
1651 }
1652 break;
1653 case GL_MAX_SAMPLES_ANGLE:
1654 {
1655 if (getMaxSupportedSamples() != 0)
1656 {
1657 *type = GL_INT;
1658 *numParams = 1;
1659 }
1660 else
1661 {
1662 return false;
1663 }
1664 }
1665 break;
1666 case GL_MAX_VIEWPORT_DIMS:
1667 {
1668 *type = GL_INT;
1669 *numParams = 2;
1670 }
1671 break;
1672 case GL_VIEWPORT:
1673 case GL_SCISSOR_BOX:
1674 {
1675 *type = GL_INT;
1676 *numParams = 4;
1677 }
1678 break;
1679 case GL_SHADER_COMPILER:
1680 case GL_SAMPLE_COVERAGE_INVERT:
1681 case GL_DEPTH_WRITEMASK:
1682 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1683 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1684 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1685 case GL_SAMPLE_COVERAGE:
1686 case GL_SCISSOR_TEST:
1687 case GL_STENCIL_TEST:
1688 case GL_DEPTH_TEST:
1689 case GL_BLEND:
1690 case GL_DITHER:
1691 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1692 {
1693 *type = GL_BOOL;
1694 *numParams = 1;
1695 }
1696 break;
1697 case GL_COLOR_WRITEMASK:
1698 {
1699 *type = GL_BOOL;
1700 *numParams = 4;
1701 }
1702 break;
1703 case GL_POLYGON_OFFSET_FACTOR:
1704 case GL_POLYGON_OFFSET_UNITS:
1705 case GL_SAMPLE_COVERAGE_VALUE:
1706 case GL_DEPTH_CLEAR_VALUE:
1707 case GL_LINE_WIDTH:
1708 {
1709 *type = GL_FLOAT;
1710 *numParams = 1;
1711 }
1712 break;
1713 case GL_ALIASED_LINE_WIDTH_RANGE:
1714 case GL_ALIASED_POINT_SIZE_RANGE:
1715 case GL_DEPTH_RANGE:
1716 {
1717 *type = GL_FLOAT;
1718 *numParams = 2;
1719 }
1720 break;
1721 case GL_COLOR_CLEAR_VALUE:
1722 case GL_BLEND_COLOR:
1723 {
1724 *type = GL_FLOAT;
1725 *numParams = 4;
1726 }
1727 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001728 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1729 if (!supportsTextureFilterAnisotropy())
1730 {
1731 return false;
1732 }
1733 *type = GL_FLOAT;
1734 *numParams = 1;
1735 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001736 default:
1737 return false;
1738 }
1739
1740 return true;
1741}
1742
1743// Applies the render target surface, depth stencil surface, viewport rectangle and
1744// scissor rectangle to the Direct3D 9 device
1745bool Context::applyRenderTarget(bool ignoreViewport)
1746{
1747 Framebuffer *framebufferObject = getDrawFramebuffer();
1748
1749 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1750 {
1751 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1752 }
1753
daniel@transgaming.com8a8b24c2012-11-28 19:36:26 +00001754 mRenderer->applyRenderTarget(framebufferObject);
1755
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001756 // if there is no color attachment we must synthesize a NULL colorattachment
1757 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1758 Renderbuffer *renderbufferObject = NULL;
1759 if (framebufferObject->getColorbufferType() != GL_NONE)
1760 {
1761 renderbufferObject = framebufferObject->getColorbuffer();
1762 }
1763 else
1764 {
1765 renderbufferObject = framebufferObject->getNullColorbuffer();
1766 }
1767 if (!renderbufferObject)
1768 {
1769 ERR("unable to locate renderbuffer for FBO.");
1770 return false;
1771 }
1772
1773 bool renderTargetChanged = false;
1774 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1775 if (renderTargetSerial != mAppliedRenderTargetSerial)
1776 {
1777 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1778 if (!renderTarget)
1779 {
1780 ERR("render target pointer unexpectedly null.");
1781 return false; // Context must be lost
1782 }
1783 mDevice->SetRenderTarget(0, renderTarget);
1784 mAppliedRenderTargetSerial = renderTargetSerial;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001785 renderTargetChanged = true;
1786 renderTarget->Release();
1787 }
1788
1789 IDirect3DSurface9 *depthStencil = NULL;
1790 unsigned int depthbufferSerial = 0;
1791 unsigned int stencilbufferSerial = 0;
1792 if (framebufferObject->getDepthbufferType() != GL_NONE)
1793 {
1794 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1795 depthStencil = depthbuffer->getDepthStencil();
1796 if (!depthStencil)
1797 {
1798 ERR("Depth stencil pointer unexpectedly null.");
1799 return false;
1800 }
1801
1802 depthbufferSerial = depthbuffer->getSerial();
1803 }
1804 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1805 {
1806 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1807 depthStencil = stencilbuffer->getDepthStencil();
1808 if (!depthStencil)
1809 {
1810 ERR("Depth stencil pointer unexpectedly null.");
1811 return false;
1812 }
1813
1814 stencilbufferSerial = stencilbuffer->getSerial();
1815 }
1816
1817 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1818 stencilbufferSerial != mAppliedStencilbufferSerial ||
1819 !mDepthStencilInitialized)
1820 {
1821 mDevice->SetDepthStencilSurface(depthStencil);
1822 mAppliedDepthbufferSerial = depthbufferSerial;
1823 mAppliedStencilbufferSerial = stencilbufferSerial;
1824 mDepthStencilInitialized = true;
1825 }
1826
1827 if (depthStencil)
1828 {
1829 depthStencil->Release();
1830 }
1831
1832 if (!mRenderTargetDescInitialized || renderTargetChanged)
1833 {
1834 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1835 if (!renderTarget)
1836 {
1837 return false; // Context must be lost
1838 }
1839 renderTarget->GetDesc(&mRenderTargetDesc);
1840 mRenderTargetDescInitialized = true;
1841 renderTarget->Release();
1842 }
1843
1844 D3DVIEWPORT9 viewport;
1845
1846 float zNear = clamp01(mState.zNear);
1847 float zFar = clamp01(mState.zFar);
1848
1849 if (ignoreViewport)
1850 {
1851 viewport.X = 0;
1852 viewport.Y = 0;
1853 viewport.Width = mRenderTargetDesc.Width;
1854 viewport.Height = mRenderTargetDesc.Height;
1855 viewport.MinZ = 0.0f;
1856 viewport.MaxZ = 1.0f;
1857 }
1858 else
1859 {
1860 viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1861 viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1862 viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1863 viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
1864 viewport.MinZ = zNear;
1865 viewport.MaxZ = zFar;
1866 }
1867
1868 if (viewport.Width <= 0 || viewport.Height <= 0)
1869 {
1870 return false; // Nothing to render
1871 }
1872
1873 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1874 {
1875 mDevice->SetViewport(&viewport);
1876 mSetViewport = viewport;
1877 mViewportInitialized = true;
1878 mDxUniformsDirty = true;
1879 }
1880
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001881 mRenderer->setScissorRectangle(mState.scissor, static_cast<int>(mRenderTargetDesc.Width),
1882 static_cast<int>(mRenderTargetDesc.Height));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001883
1884 if (mState.currentProgram && mDxUniformsDirty)
1885 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001886 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001887
1888 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
1889 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
1890 programBinary->setUniform2fv(halfPixelSize, 1, xy);
1891
1892 // These values are used for computing gl_FragCoord in Program::linkVaryings().
1893 GLint coord = programBinary->getDxCoordLocation();
1894 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1895 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1896 (float)mState.viewportY + mState.viewportHeight / 2.0f};
1897 programBinary->setUniform4fv(coord, 1, whxy);
1898
1899 GLint depth = programBinary->getDxDepthLocation();
1900 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
1901 programBinary->setUniform2fv(depth, 1, dz);
1902
1903 GLint depthRange = programBinary->getDxDepthRangeLocation();
1904 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1905 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
1906 mDxUniformsDirty = false;
1907 }
1908
1909 return true;
1910}
1911
1912// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
1913void Context::applyState(GLenum drawMode)
1914{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001915 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001916
1917 Framebuffer *framebufferObject = getDrawFramebuffer();
1918
1919 GLint frontCCW = programBinary->getDxFrontCCWLocation();
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00001920 GLint ccw = (mState.rasterizer.frontFace == GL_CCW);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001921 programBinary->setUniform1iv(frontCCW, 1, &ccw);
1922
1923 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
1924 GLint alwaysFront = !isTriangleMode(drawMode);
1925 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1926
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001927 const gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1928 unsigned int depthSize = depthbuffer ? depthbuffer->getDepthSize() : 0;
1929
1930 mRenderer->setRasterizerState(mState.rasterizer, depthSize);
1931
1932 unsigned int mask = 0;
1933 if (mState.sampleCoverage)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001934 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001935 if (mState.sampleCoverageValue != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001936 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001937 float threshold = 0.5f;
1938
1939 for (int i = 0; i < framebufferObject->getSamples(); ++i)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001940 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001941 mask <<= 1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001942
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001943 if ((i + 1) * mState.sampleCoverageValue >= threshold)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001944 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001945 threshold += 1.0f;
1946 mask |= 1;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001947 }
1948 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001949 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001950
1951 if (mState.sampleCoverageInvert)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001952 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001953 mask = ~mask;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001954 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001955 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001956 else
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001957 {
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001958 mask = 0xFFFFFFFF;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001959 }
daniel@transgaming.com2e258642012-11-28 19:36:18 +00001960 mRenderer->setBlendState(mState.blend, mState.blendColor, mask);
1961
1962 unsigned int stencilSize = framebufferObject->hasStencil() ? framebufferObject->getStencilbuffer()->getStencilSize() : 0;
1963 mRenderer->setDepthStencilState(mState.depthStencil,
1964 mState.rasterizer.frontFace == GL_CCW,
1965 stencilSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001966}
1967
1968GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
1969{
1970 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
1971
daniel@transgaming.com408caa52012-10-31 18:47:01 +00001972 ProgramBinary *programBinary = getCurrentProgramBinary();
1973 GLenum err = mVertexDataManager->prepareVertexData(mState.vertexAttribute, programBinary, first, count, attributes, instances);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001974 if (err != GL_NO_ERROR)
1975 {
1976 return err;
1977 }
daniel@transgaming.com408caa52012-10-31 18:47:01 +00001978
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00001979 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001980}
1981
1982// Applies the indices and element array bindings to the Direct3D 9 device
1983GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
1984{
1985 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
1986
1987 if (err == GL_NO_ERROR)
1988 {
1989 if (indexInfo->serial != mAppliedIBSerial)
1990 {
1991 mDevice->SetIndices(indexInfo->indexBuffer);
1992 mAppliedIBSerial = indexInfo->serial;
1993 }
1994 }
1995
1996 return err;
1997}
1998
1999// Applies the shaders and shader constants to the Direct3D 9 device
2000void Context::applyShaders()
2001{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002002 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002003
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002004 if (programBinary->getSerial() != mAppliedProgramBinarySerial)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002005 {
2006 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2007 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
2008
2009 mDevice->SetPixelShader(pixelShader);
2010 mDevice->SetVertexShader(vertexShader);
2011 programBinary->dirtyAllUniforms();
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002012 mAppliedProgramBinarySerial = programBinary->getSerial();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002013 }
2014
2015 programBinary->applyUniforms();
2016}
2017
2018// Applies the textures and sampler states to the Direct3D 9 device
2019void Context::applyTextures()
2020{
2021 applyTextures(SAMPLER_PIXEL);
2022
2023 if (mSupportsVertexTexture)
2024 {
2025 applyTextures(SAMPLER_VERTEX);
2026 }
2027}
2028
2029// For each Direct3D 9 sampler of either the pixel or vertex stage,
2030// looks up the corresponding OpenGL texture image unit and texture type,
2031// and sets the texture and its addressing/filtering state (or NULL when inactive).
2032void Context::applyTextures(SamplerType type)
2033{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002034 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002035
2036 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
2037 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002038 int samplerRange = programBinary->getUsedSamplerRange(type);
2039
2040 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
2041 {
2042 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002043
2044 if (textureUnit != -1)
2045 {
2046 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
2047
2048 Texture *texture = getSamplerTexture(textureUnit, textureType);
2049 unsigned int texSerial = texture->getTextureSerial();
2050
2051 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
2052 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002053 if (texture->isSamplerComplete())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002054 {
2055 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
2056 {
daniel@transgaming.comebf139f2012-10-31 18:07:32 +00002057 SamplerState samplerState;
2058 texture->getSamplerState(&samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002059
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002060 mRenderer->setSamplerState(type, samplerIndex, samplerState);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002061 }
2062
2063 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
2064 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002065 mRenderer->setTexture(type, samplerIndex, texture);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002066 }
2067 }
2068 else
2069 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002070 mRenderer->setTexture(type, samplerIndex, getIncompleteTexture(textureType));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002071 }
2072
2073 appliedTextureSerial[samplerIndex] = texSerial;
2074 texture->resetDirty();
2075 }
2076 }
2077 else
2078 {
2079 if (appliedTextureSerial[samplerIndex] != 0)
2080 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002081 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002082 appliedTextureSerial[samplerIndex] = 0;
2083 }
2084 }
2085 }
2086
2087 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2088 {
2089 if (appliedTextureSerial[samplerIndex] != 0)
2090 {
daniel@transgaming.coma734f272012-10-31 18:07:48 +00002091 mRenderer->setTexture(type, samplerIndex, NULL);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002092 appliedTextureSerial[samplerIndex] = 0;
2093 }
2094 }
2095}
2096
2097void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2098 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2099{
2100 Framebuffer *framebuffer = getReadFramebuffer();
2101
2102 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2103 {
2104 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2105 }
2106
2107 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2108 {
2109 return error(GL_INVALID_OPERATION);
2110 }
2111
daniel@transgaming.com6452adf2012-10-17 18:22:35 +00002112 GLsizei outputPitch = ComputePitch(width, ConvertSizedInternalFormat(format, type), mState.packAlignment);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002113 // sized query sanity check
2114 if (bufSize)
2115 {
2116 int requiredSize = outputPitch * height;
2117 if (requiredSize > *bufSize)
2118 {
2119 return error(GL_INVALID_OPERATION);
2120 }
2121 }
2122
2123 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2124 if (!renderTarget)
2125 {
2126 return; // Context must be lost, return silently
2127 }
2128
2129 D3DSURFACE_DESC desc;
2130 renderTarget->GetDesc(&desc);
2131
2132 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2133 {
2134 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2135 renderTarget->Release();
2136 return error(GL_OUT_OF_MEMORY);
2137 }
2138
2139 HRESULT result;
2140 IDirect3DSurface9 *systemSurface = NULL;
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002141 bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mRenderer->getShareHandleSupport() &&
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002142 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2143 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2144 if (directToPixels)
2145 {
2146 // Use the pixels ptr as a shared handle to write directly into client's memory
2147 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2148 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2149 if (FAILED(result))
2150 {
2151 // Try again without the shared handle
2152 directToPixels = false;
2153 }
2154 }
2155
2156 if (!directToPixels)
2157 {
2158 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2159 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2160 if (FAILED(result))
2161 {
2162 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2163 renderTarget->Release();
2164 return error(GL_OUT_OF_MEMORY);
2165 }
2166 }
2167
2168 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
2169 renderTarget->Release();
2170 renderTarget = NULL;
2171
2172 if (FAILED(result))
2173 {
2174 systemSurface->Release();
2175
2176 // It turns out that D3D will sometimes produce more error
2177 // codes than those documented.
2178 if (checkDeviceLost(result))
2179 return error(GL_OUT_OF_MEMORY);
2180 else
2181 {
2182 UNREACHABLE();
2183 return;
2184 }
2185
2186 }
2187
2188 if (directToPixels)
2189 {
2190 systemSurface->Release();
2191 return;
2192 }
2193
2194 RECT rect;
2195 rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
2196 rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
2197 rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2198 rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2199
2200 D3DLOCKED_RECT lock;
2201 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2202
2203 if (FAILED(result))
2204 {
2205 UNREACHABLE();
2206 systemSurface->Release();
2207
2208 return; // No sensible error to generate
2209 }
2210
2211 unsigned char *dest = (unsigned char*)pixels;
2212 unsigned short *dest16 = (unsigned short*)pixels;
2213
2214 unsigned char *source;
2215 int inputPitch;
2216 if (getPackReverseRowOrder())
2217 {
2218 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2219 inputPitch = -lock.Pitch;
2220 }
2221 else
2222 {
2223 source = (unsigned char*)lock.pBits;
2224 inputPitch = lock.Pitch;
2225 }
2226
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002227 unsigned int fastPixelSize = 0;
2228
2229 if (desc.Format == D3DFMT_A8R8G8B8 &&
2230 format == GL_BGRA_EXT &&
2231 type == GL_UNSIGNED_BYTE)
2232 {
2233 fastPixelSize = 4;
2234 }
2235 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2236 format == GL_BGRA_EXT &&
2237 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2238 (desc.Format == D3DFMT_A1R5G5B5 &&
2239 format == GL_BGRA_EXT &&
2240 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2241 {
2242 fastPixelSize = 2;
2243 }
2244 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2245 format == GL_RGBA &&
2246 type == GL_HALF_FLOAT_OES)
2247 {
2248 fastPixelSize = 8;
2249 }
2250 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2251 format == GL_RGBA &&
2252 type == GL_FLOAT)
2253 {
2254 fastPixelSize = 16;
2255 }
2256
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002257 for (int j = 0; j < rect.bottom - rect.top; j++)
2258 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002259 if (fastPixelSize != 0)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002260 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002261 // Fast path for formats which require no translation:
2262 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2263 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2264 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2265 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2266 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2267 //
2268 // Note that buffers with no alpha go through the slow path below.
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002269 memcpy(dest + j * outputPitch,
2270 source + j * inputPitch,
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002271 (rect.right - rect.left) * fastPixelSize);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002272 continue;
2273 }
2274
2275 for (int i = 0; i < rect.right - rect.left; i++)
2276 {
2277 float r;
2278 float g;
2279 float b;
2280 float a;
2281
2282 switch (desc.Format)
2283 {
2284 case D3DFMT_R5G6B5:
2285 {
2286 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2287
2288 a = 1.0f;
2289 b = (rgb & 0x001F) * (1.0f / 0x001F);
2290 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2291 r = (rgb & 0xF800) * (1.0f / 0xF800);
2292 }
2293 break;
2294 case D3DFMT_A1R5G5B5:
2295 {
2296 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2297
2298 a = (argb & 0x8000) ? 1.0f : 0.0f;
2299 b = (argb & 0x001F) * (1.0f / 0x001F);
2300 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2301 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2302 }
2303 break;
2304 case D3DFMT_A8R8G8B8:
2305 {
2306 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2307
2308 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2309 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2310 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2311 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2312 }
2313 break;
2314 case D3DFMT_X8R8G8B8:
2315 {
2316 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2317
2318 a = 1.0f;
2319 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2320 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2321 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2322 }
2323 break;
2324 case D3DFMT_A2R10G10B10:
2325 {
2326 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2327
2328 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2329 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2330 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2331 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2332 }
2333 break;
2334 case D3DFMT_A32B32G32R32F:
2335 {
2336 // float formats in D3D are stored rgba, rather than the other way round
2337 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2338 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2339 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2340 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2341 }
2342 break;
2343 case D3DFMT_A16B16G16R16F:
2344 {
2345 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgaa480672012-09-05 19:32:38 +00002346 r = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2347 g = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2348 b = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2349 a = float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002350 }
2351 break;
2352 default:
2353 UNIMPLEMENTED(); // FIXME
2354 UNREACHABLE();
2355 return;
2356 }
2357
2358 switch (format)
2359 {
2360 case GL_RGBA:
2361 switch (type)
2362 {
2363 case GL_UNSIGNED_BYTE:
2364 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2365 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2366 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2367 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2368 break;
2369 default: UNREACHABLE();
2370 }
2371 break;
2372 case GL_BGRA_EXT:
2373 switch (type)
2374 {
2375 case GL_UNSIGNED_BYTE:
2376 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2377 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2378 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2379 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2380 break;
2381 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2382 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2383 // this type is packed as follows:
2384 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2385 // --------------------------------------------------------------------------------
2386 // | 4th | 3rd | 2nd | 1st component |
2387 // --------------------------------------------------------------------------------
2388 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2389 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2390 ((unsigned short)(15 * a + 0.5f) << 12)|
2391 ((unsigned short)(15 * r + 0.5f) << 8) |
2392 ((unsigned short)(15 * g + 0.5f) << 4) |
2393 ((unsigned short)(15 * b + 0.5f) << 0);
2394 break;
2395 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2396 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2397 // this type is packed as follows:
2398 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2399 // --------------------------------------------------------------------------------
2400 // | 4th | 3rd | 2nd | 1st component |
2401 // --------------------------------------------------------------------------------
2402 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2403 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2404 ((unsigned short)( a + 0.5f) << 15) |
2405 ((unsigned short)(31 * r + 0.5f) << 10) |
2406 ((unsigned short)(31 * g + 0.5f) << 5) |
2407 ((unsigned short)(31 * b + 0.5f) << 0);
2408 break;
2409 default: UNREACHABLE();
2410 }
2411 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002412 case GL_RGB:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002413 switch (type)
2414 {
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002415 case GL_UNSIGNED_SHORT_5_6_5:
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002416 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2417 ((unsigned short)(31 * b + 0.5f) << 0) |
2418 ((unsigned short)(63 * g + 0.5f) << 5) |
2419 ((unsigned short)(31 * r + 0.5f) << 11);
2420 break;
daniel@transgaming.com42944b02012-09-27 17:45:57 +00002421 case GL_UNSIGNED_BYTE:
2422 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2423 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2424 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2425 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002426 default: UNREACHABLE();
2427 }
2428 break;
2429 default: UNREACHABLE();
2430 }
2431 }
2432 }
2433
2434 systemSurface->UnlockRect();
2435
2436 systemSurface->Release();
2437}
2438
2439void Context::clear(GLbitfield mask)
2440{
2441 Framebuffer *framebufferObject = getDrawFramebuffer();
2442
2443 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2444 {
2445 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2446 }
2447
2448 DWORD flags = 0;
2449
2450 if (mask & GL_COLOR_BUFFER_BIT)
2451 {
2452 mask &= ~GL_COLOR_BUFFER_BIT;
2453
2454 if (framebufferObject->getColorbufferType() != GL_NONE)
2455 {
2456 flags |= D3DCLEAR_TARGET;
2457 }
2458 }
2459
2460 if (mask & GL_DEPTH_BUFFER_BIT)
2461 {
2462 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002463 if (mState.depthStencil.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002464 {
2465 flags |= D3DCLEAR_ZBUFFER;
2466 }
2467 }
2468
2469 GLuint stencilUnmasked = 0x0;
2470
2471 if (mask & GL_STENCIL_BUFFER_BIT)
2472 {
2473 mask &= ~GL_STENCIL_BUFFER_BIT;
2474 if (framebufferObject->getStencilbufferType() != GL_NONE)
2475 {
2476 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2477 if (!depthStencil)
2478 {
2479 ERR("Depth stencil pointer unexpectedly null.");
2480 return;
2481 }
2482
2483 D3DSURFACE_DESC desc;
2484 depthStencil->GetDesc(&desc);
2485 depthStencil->Release();
2486
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002487 unsigned int stencilSize = d3d9_gl::GetStencilSize(desc.Format);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002488 stencilUnmasked = (0x1 << stencilSize) - 1;
2489
2490 if (stencilUnmasked != 0x0)
2491 {
2492 flags |= D3DCLEAR_STENCIL;
2493 }
2494 }
2495 }
2496
2497 if (mask != 0)
2498 {
2499 return error(GL_INVALID_VALUE);
2500 }
2501
2502 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2503 {
2504 return;
2505 }
2506
2507 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2508 unorm<8>(mState.colorClearValue.red),
2509 unorm<8>(mState.colorClearValue.green),
2510 unorm<8>(mState.colorClearValue.blue));
2511 float depth = clamp01(mState.depthClearValue);
2512 int stencil = mState.stencilClearValue & 0x000000FF;
2513
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002514
2515 bool alphaUnmasked = (d3d9_gl::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.blend.colorMaskAlpha;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002516
2517 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002518 (mState.depthStencil.stencilWritemask & stencilUnmasked) != stencilUnmasked;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002519 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002520 !(mState.blend.colorMaskRed && mState.blend.colorMaskGreen &&
2521 mState.blend.colorMaskBlue && alphaUnmasked);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002522
2523 if (needMaskedColorClear || needMaskedStencilClear)
2524 {
2525 // State which is altered in all paths from this point to the clear call is saved.
2526 // State which is altered in only some paths will be flagged dirty in the case that
2527 // that path is taken.
2528 HRESULT hr;
2529 if (mMaskedClearSavedState == NULL)
2530 {
2531 hr = mDevice->BeginStateBlock();
2532 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2533
2534 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2535 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2536 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2537 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2538 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2539 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2540 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2541 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2542 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2543 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2544 mDevice->SetPixelShader(NULL);
2545 mDevice->SetVertexShader(NULL);
2546 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2547 mDevice->SetStreamSource(0, NULL, 0, 0);
2548 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2549 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2550 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2551 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2552 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2553 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2554 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2555
2556 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2557 {
2558 mDevice->SetStreamSourceFreq(i, 1);
2559 }
2560
2561 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2562 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2563 }
2564
2565 ASSERT(mMaskedClearSavedState != NULL);
2566
2567 if (mMaskedClearSavedState != NULL)
2568 {
2569 hr = mMaskedClearSavedState->Capture();
2570 ASSERT(SUCCEEDED(hr));
2571 }
2572
2573 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2574 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2575 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2576 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2577 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2578 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2579 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2580 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2581
2582 if (flags & D3DCLEAR_TARGET)
2583 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002584 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
2585 gl_d3d9::ConvertColorMask(mState.blend.colorMaskRed,
2586 mState.blend.colorMaskGreen,
2587 mState.blend.colorMaskBlue,
2588 mState.blend.colorMaskAlpha));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002589 }
2590 else
2591 {
2592 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2593 }
2594
2595 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2596 {
2597 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2598 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2599 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2600 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00002601 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.depthStencil.stencilWritemask);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002602 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2603 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2604 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002605 }
2606 else
2607 {
2608 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2609 }
2610
2611 mDevice->SetPixelShader(NULL);
2612 mDevice->SetVertexShader(NULL);
2613 mDevice->SetFVF(D3DFVF_XYZRHW);
2614 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2615 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2616 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2617 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2618 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2619 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2620 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2621
2622 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2623 {
2624 mDevice->SetStreamSourceFreq(i, 1);
2625 }
2626
2627 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2628 quad[0][0] = -0.5f;
2629 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
2630 quad[0][2] = 0.0f;
2631 quad[0][3] = 1.0f;
2632
2633 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2634 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
2635 quad[1][2] = 0.0f;
2636 quad[1][3] = 1.0f;
2637
2638 quad[2][0] = -0.5f;
2639 quad[2][1] = -0.5f;
2640 quad[2][2] = 0.0f;
2641 quad[2][3] = 1.0f;
2642
2643 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
2644 quad[3][1] = -0.5f;
2645 quad[3][2] = 0.0f;
2646 quad[3][3] = 1.0f;
2647
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002648 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002649 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2650
2651 if (flags & D3DCLEAR_ZBUFFER)
2652 {
2653 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2654 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2655 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2656 }
2657
2658 if (mMaskedClearSavedState != NULL)
2659 {
2660 mMaskedClearSavedState->Apply();
2661 }
2662 }
2663 else if (flags)
2664 {
2665 mDevice->Clear(0, NULL, flags, color, depth, stencil);
2666 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00002667
2668 mRenderer->clear(mask, mState.colorClearValue, mState.depthClearValue, mState.stencilClearValue,
2669 framebufferObject);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002670}
2671
2672void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
2673{
2674 if (!mState.currentProgram)
2675 {
2676 return error(GL_INVALID_OPERATION);
2677 }
2678
2679 D3DPRIMITIVETYPE primitiveType;
2680 int primitiveCount;
2681
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002682 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002683 return error(GL_INVALID_ENUM);
2684
2685 if (primitiveCount <= 0)
2686 {
2687 return;
2688 }
2689
2690 if (!applyRenderTarget(false))
2691 {
2692 return;
2693 }
2694
2695 applyState(mode);
2696
2697 GLsizei repeatDraw = 1;
2698 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
2699 if (err != GL_NO_ERROR)
2700 {
2701 return error(err);
2702 }
2703
2704 applyShaders();
2705 applyTextures();
2706
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002707 if (!getCurrentProgramBinary()->validateSamplers(NULL))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002708 {
2709 return error(GL_INVALID_OPERATION);
2710 }
2711
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002712 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002713 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002714 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002715
2716 if (mode == GL_LINE_LOOP)
2717 {
2718 drawLineLoop(count, GL_NONE, NULL, 0);
2719 }
2720 else if (instances > 0)
2721 {
2722 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2723 if (countingIB)
2724 {
2725 if (mAppliedIBSerial != countingIB->getSerial())
2726 {
2727 mDevice->SetIndices(countingIB->getBuffer());
2728 mAppliedIBSerial = countingIB->getSerial();
2729 }
2730
2731 for (int i = 0; i < repeatDraw; i++)
2732 {
2733 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2734 }
2735 }
2736 else
2737 {
2738 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2739 return error(GL_OUT_OF_MEMORY);
2740 }
2741 }
2742 else // Regular case
2743 {
2744 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
2745 }
2746 }
2747}
2748
2749void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
2750{
2751 if (!mState.currentProgram)
2752 {
2753 return error(GL_INVALID_OPERATION);
2754 }
2755
2756 if (!indices && !mState.elementArrayBuffer)
2757 {
2758 return error(GL_INVALID_OPERATION);
2759 }
2760
2761 D3DPRIMITIVETYPE primitiveType;
2762 int primitiveCount;
2763
daniel@transgaming.com682a37c2012-11-28 19:34:44 +00002764 if(!gl_d3d9::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002765 return error(GL_INVALID_ENUM);
2766
2767 if (primitiveCount <= 0)
2768 {
2769 return;
2770 }
2771
2772 if (!applyRenderTarget(false))
2773 {
2774 return;
2775 }
2776
2777 applyState(mode);
2778
2779 TranslatedIndexData indexInfo;
2780 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2781 if (err != GL_NO_ERROR)
2782 {
2783 return error(err);
2784 }
2785
2786 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2787 GLsizei repeatDraw = 1;
2788 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
2789 if (err != GL_NO_ERROR)
2790 {
2791 return error(err);
2792 }
2793
2794 applyShaders();
2795 applyTextures();
2796
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002797 if (!getCurrentProgramBinary()->validateSamplers(false))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002798 {
2799 return error(GL_INVALID_OPERATION);
2800 }
2801
daniel@transgaming.com087e5782012-09-17 21:28:47 +00002802 if (!skipDraw(mode))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002803 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002804 mRenderer->startScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002805
2806 if (mode == GL_LINE_LOOP)
2807 {
2808 drawLineLoop(count, type, indices, indexInfo.minIndex);
2809 }
2810 else
2811 {
2812 for (int i = 0; i < repeatDraw; i++)
2813 {
2814 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
2815 }
2816 }
2817 }
2818}
2819
2820// Implements glFlush when block is false, glFinish when block is true
2821void Context::sync(bool block)
2822{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002823 mRenderer->sync(block);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002824}
2825
2826void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
2827{
2828 // Get the raw indices for an indexed draw
2829 if (type != GL_NONE && mState.elementArrayBuffer.get())
2830 {
2831 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2832 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2833 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2834 }
2835
2836 UINT startIndex = 0;
2837 bool succeeded = false;
2838
2839 if (supports32bitIndices())
2840 {
2841 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
2842
2843 if (!mLineLoopIB)
2844 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00002845 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002846 }
2847
2848 if (mLineLoopIB)
2849 {
2850 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2851
2852 UINT offset = 0;
2853 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
2854 startIndex = offset / 4;
2855
2856 if (data)
2857 {
2858 switch (type)
2859 {
2860 case GL_NONE: // Non-indexed draw
2861 for (int i = 0; i < count; i++)
2862 {
2863 data[i] = i;
2864 }
2865 data[count] = 0;
2866 break;
2867 case GL_UNSIGNED_BYTE:
2868 for (int i = 0; i < count; i++)
2869 {
2870 data[i] = static_cast<const GLubyte*>(indices)[i];
2871 }
2872 data[count] = static_cast<const GLubyte*>(indices)[0];
2873 break;
2874 case GL_UNSIGNED_SHORT:
2875 for (int i = 0; i < count; i++)
2876 {
2877 data[i] = static_cast<const GLushort*>(indices)[i];
2878 }
2879 data[count] = static_cast<const GLushort*>(indices)[0];
2880 break;
2881 case GL_UNSIGNED_INT:
2882 for (int i = 0; i < count; i++)
2883 {
2884 data[i] = static_cast<const GLuint*>(indices)[i];
2885 }
2886 data[count] = static_cast<const GLuint*>(indices)[0];
2887 break;
2888 default: UNREACHABLE();
2889 }
2890
2891 mLineLoopIB->unmap();
2892 succeeded = true;
2893 }
2894 }
2895 }
2896 else
2897 {
2898 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
2899
2900 if (!mLineLoopIB)
2901 {
daniel@transgaming.com6716a272012-10-31 18:31:39 +00002902 mLineLoopIB = new StreamingIndexBuffer(mRenderer, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002903 }
2904
2905 if (mLineLoopIB)
2906 {
2907 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2908
2909 UINT offset = 0;
2910 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
2911 startIndex = offset / 2;
2912
2913 if (data)
2914 {
2915 switch (type)
2916 {
2917 case GL_NONE: // Non-indexed draw
2918 for (int i = 0; i < count; i++)
2919 {
2920 data[i] = i;
2921 }
2922 data[count] = 0;
2923 break;
2924 case GL_UNSIGNED_BYTE:
2925 for (int i = 0; i < count; i++)
2926 {
2927 data[i] = static_cast<const GLubyte*>(indices)[i];
2928 }
2929 data[count] = static_cast<const GLubyte*>(indices)[0];
2930 break;
2931 case GL_UNSIGNED_SHORT:
2932 for (int i = 0; i < count; i++)
2933 {
2934 data[i] = static_cast<const GLushort*>(indices)[i];
2935 }
2936 data[count] = static_cast<const GLushort*>(indices)[0];
2937 break;
2938 case GL_UNSIGNED_INT:
2939 for (int i = 0; i < count; i++)
2940 {
2941 data[i] = static_cast<const GLuint*>(indices)[i];
2942 }
2943 data[count] = static_cast<const GLuint*>(indices)[0];
2944 break;
2945 default: UNREACHABLE();
2946 }
2947
2948 mLineLoopIB->unmap();
2949 succeeded = true;
2950 }
2951 }
2952 }
2953
2954 if (succeeded)
2955 {
2956 if (mAppliedIBSerial != mLineLoopIB->getSerial())
2957 {
2958 mDevice->SetIndices(mLineLoopIB->getBuffer());
2959 mAppliedIBSerial = mLineLoopIB->getSerial();
2960 }
2961
2962 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
2963 }
2964 else
2965 {
2966 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
2967 return error(GL_OUT_OF_MEMORY);
2968 }
2969}
2970
2971void Context::recordInvalidEnum()
2972{
2973 mInvalidEnum = true;
2974}
2975
2976void Context::recordInvalidValue()
2977{
2978 mInvalidValue = true;
2979}
2980
2981void Context::recordInvalidOperation()
2982{
2983 mInvalidOperation = true;
2984}
2985
2986void Context::recordOutOfMemory()
2987{
2988 mOutOfMemory = true;
2989}
2990
2991void Context::recordInvalidFramebufferOperation()
2992{
2993 mInvalidFramebufferOperation = true;
2994}
2995
2996// Get one of the recorded errors and clear its flag, if any.
2997// [OpenGL ES 2.0.24] section 2.5 page 13.
2998GLenum Context::getError()
2999{
3000 if (mInvalidEnum)
3001 {
3002 mInvalidEnum = false;
3003
3004 return GL_INVALID_ENUM;
3005 }
3006
3007 if (mInvalidValue)
3008 {
3009 mInvalidValue = false;
3010
3011 return GL_INVALID_VALUE;
3012 }
3013
3014 if (mInvalidOperation)
3015 {
3016 mInvalidOperation = false;
3017
3018 return GL_INVALID_OPERATION;
3019 }
3020
3021 if (mOutOfMemory)
3022 {
3023 mOutOfMemory = false;
3024
3025 return GL_OUT_OF_MEMORY;
3026 }
3027
3028 if (mInvalidFramebufferOperation)
3029 {
3030 mInvalidFramebufferOperation = false;
3031
3032 return GL_INVALID_FRAMEBUFFER_OPERATION;
3033 }
3034
3035 return GL_NO_ERROR;
3036}
3037
3038GLenum Context::getResetStatus()
3039{
3040 if (mResetStatus == GL_NO_ERROR)
3041 {
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00003042 // mResetStatus will be set by the markContextLost callback
3043 // in the case a notification is sent
3044 mRenderer->testDeviceLost(true);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003045 }
3046
3047 GLenum status = mResetStatus;
3048
3049 if (mResetStatus != GL_NO_ERROR)
3050 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003051 if (mRenderer->testDeviceResettable())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003052 {
3053 mResetStatus = GL_NO_ERROR;
3054 }
3055 }
3056
3057 return status;
3058}
3059
3060bool Context::isResetNotificationEnabled()
3061{
3062 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3063}
3064
3065bool Context::supportsShaderModel3() const
3066{
3067 return mSupportsShaderModel3;
3068}
3069
3070float Context::getMaximumPointSize() const
3071{
3072 return mSupportsShaderModel3 ? mMaximumPointSize : ALIASED_POINT_SIZE_RANGE_MAX_SM2;
3073}
3074
3075int Context::getMaximumVaryingVectors() const
3076{
3077 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3078}
3079
3080unsigned int Context::getMaximumVertexTextureImageUnits() const
3081{
3082 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3083}
3084
3085unsigned int Context::getMaximumCombinedTextureImageUnits() const
3086{
3087 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3088}
3089
3090int Context::getMaximumFragmentUniformVectors() const
3091{
3092 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3093}
3094
3095int Context::getMaxSupportedSamples() const
3096{
daniel@transgaming.comb7833982012-10-31 18:31:46 +00003097 return mRenderer->getMaxSupportedSamples();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003098}
3099
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003100bool Context::supportsEventQueries() const
3101{
3102 return mSupportsEventQueries;
3103}
3104
3105bool Context::supportsOcclusionQueries() const
3106{
3107 return mSupportsOcclusionQueries;
3108}
3109
3110bool Context::supportsDXT1Textures() const
3111{
3112 return mSupportsDXT1Textures;
3113}
3114
3115bool Context::supportsDXT3Textures() const
3116{
3117 return mSupportsDXT3Textures;
3118}
3119
3120bool Context::supportsDXT5Textures() const
3121{
3122 return mSupportsDXT5Textures;
3123}
3124
3125bool Context::supportsFloat32Textures() const
3126{
3127 return mSupportsFloat32Textures;
3128}
3129
3130bool Context::supportsFloat32LinearFilter() const
3131{
3132 return mSupportsFloat32LinearFilter;
3133}
3134
3135bool Context::supportsFloat32RenderableTextures() const
3136{
3137 return mSupportsFloat32RenderableTextures;
3138}
3139
3140bool Context::supportsFloat16Textures() const
3141{
3142 return mSupportsFloat16Textures;
3143}
3144
3145bool Context::supportsFloat16LinearFilter() const
3146{
3147 return mSupportsFloat16LinearFilter;
3148}
3149
3150bool Context::supportsFloat16RenderableTextures() const
3151{
3152 return mSupportsFloat16RenderableTextures;
3153}
3154
3155int Context::getMaximumRenderbufferDimension() const
3156{
3157 return mMaxRenderbufferDimension;
3158}
3159
3160int Context::getMaximumTextureDimension() const
3161{
3162 return mMaxTextureDimension;
3163}
3164
3165int Context::getMaximumCubeTextureDimension() const
3166{
3167 return mMaxCubeTextureDimension;
3168}
3169
3170int Context::getMaximumTextureLevel() const
3171{
3172 return mMaxTextureLevel;
3173}
3174
3175bool Context::supportsLuminanceTextures() const
3176{
3177 return mSupportsLuminanceTextures;
3178}
3179
3180bool Context::supportsLuminanceAlphaTextures() const
3181{
3182 return mSupportsLuminanceAlphaTextures;
3183}
3184
3185bool Context::supportsDepthTextures() const
3186{
3187 return mSupportsDepthTextures;
3188}
3189
3190bool Context::supports32bitIndices() const
3191{
3192 return mSupports32bitIndices;
3193}
3194
3195bool Context::supportsNonPower2Texture() const
3196{
3197 return mSupportsNonPower2Texture;
3198}
3199
3200bool Context::supportsInstancing() const
3201{
3202 return mSupportsInstancing;
3203}
3204
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003205bool Context::supportsTextureFilterAnisotropy() const
3206{
3207 return mSupportsTextureFilterAnisotropy;
3208}
3209
3210float Context::getTextureMaxAnisotropy() const
3211{
3212 return mMaxTextureAnisotropy;
3213}
3214
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003215bool Context::getCurrentReadFormatType(GLenum *format, GLenum *type)
3216{
3217 Framebuffer *framebuffer = getReadFramebuffer();
3218 if (!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3219 {
3220 return error(GL_INVALID_OPERATION, false);
3221 }
3222
3223 Renderbuffer *renderbuffer = framebuffer->getColorbuffer();
3224 if (!renderbuffer)
3225 {
3226 return error(GL_INVALID_OPERATION, false);
3227 }
3228
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003229 *format = gl::ExtractFormat(renderbuffer->getActualFormat());
3230 *type = gl::ExtractType(renderbuffer->getActualFormat());
daniel@transgaming.com42944b02012-09-27 17:45:57 +00003231
3232 return true;
3233}
3234
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003235void Context::detachBuffer(GLuint buffer)
3236{
3237 // [OpenGL ES 2.0.24] section 2.9 page 22:
3238 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3239 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3240
3241 if (mState.arrayBuffer.id() == buffer)
3242 {
3243 mState.arrayBuffer.set(NULL);
3244 }
3245
3246 if (mState.elementArrayBuffer.id() == buffer)
3247 {
3248 mState.elementArrayBuffer.set(NULL);
3249 }
3250
3251 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3252 {
3253 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
3254 {
3255 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
3256 }
3257 }
3258}
3259
3260void Context::detachTexture(GLuint texture)
3261{
3262 // [OpenGL ES 2.0.24] section 3.8 page 84:
3263 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3264 // rebound to texture object zero
3265
3266 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3267 {
3268 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
3269 {
3270 if (mState.samplerTexture[type][sampler].id() == texture)
3271 {
3272 mState.samplerTexture[type][sampler].set(NULL);
3273 }
3274 }
3275 }
3276
3277 // [OpenGL ES 2.0.24] section 4.4 page 112:
3278 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3279 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3280 // image was attached in the currently bound framebuffer.
3281
3282 Framebuffer *readFramebuffer = getReadFramebuffer();
3283 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3284
3285 if (readFramebuffer)
3286 {
3287 readFramebuffer->detachTexture(texture);
3288 }
3289
3290 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3291 {
3292 drawFramebuffer->detachTexture(texture);
3293 }
3294}
3295
3296void Context::detachFramebuffer(GLuint framebuffer)
3297{
3298 // [OpenGL ES 2.0.24] section 4.4 page 107:
3299 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3300 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3301
3302 if (mState.readFramebuffer == framebuffer)
3303 {
3304 bindReadFramebuffer(0);
3305 }
3306
3307 if (mState.drawFramebuffer == framebuffer)
3308 {
3309 bindDrawFramebuffer(0);
3310 }
3311}
3312
3313void Context::detachRenderbuffer(GLuint renderbuffer)
3314{
3315 // [OpenGL ES 2.0.24] section 4.4 page 109:
3316 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3317 // had been executed with the target RENDERBUFFER and name of zero.
3318
3319 if (mState.renderbuffer.id() == renderbuffer)
3320 {
3321 bindRenderbuffer(0);
3322 }
3323
3324 // [OpenGL ES 2.0.24] section 4.4 page 111:
3325 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3326 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3327 // point to which this image was attached in the currently bound framebuffer.
3328
3329 Framebuffer *readFramebuffer = getReadFramebuffer();
3330 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3331
3332 if (readFramebuffer)
3333 {
3334 readFramebuffer->detachRenderbuffer(renderbuffer);
3335 }
3336
3337 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3338 {
3339 drawFramebuffer->detachRenderbuffer(renderbuffer);
3340 }
3341}
3342
3343Texture *Context::getIncompleteTexture(TextureType type)
3344{
3345 Texture *t = mIncompleteTextures[type].get();
3346
3347 if (t == NULL)
3348 {
3349 static const GLubyte color[] = { 0, 0, 0, 255 };
3350
3351 switch (type)
3352 {
3353 default:
3354 UNREACHABLE();
3355 // default falls through to TEXTURE_2D
3356
3357 case TEXTURE_2D:
3358 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003359 Texture2D *incomplete2d = new Texture2D(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003360 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3361 t = incomplete2d;
3362 }
3363 break;
3364
3365 case TEXTURE_CUBE:
3366 {
daniel@transgaming.com370482e2012-11-28 19:32:13 +00003367 TextureCubeMap *incompleteCube = new TextureCubeMap(mRenderer, Texture::INCOMPLETE_TEXTURE_ID);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003368
3369 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3370 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3371 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3372 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3373 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3374 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3375
3376 t = incompleteCube;
3377 }
3378 break;
3379 }
3380
3381 mIncompleteTextures[type].set(t);
3382 }
3383
3384 return t;
3385}
3386
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003387bool Context::skipDraw(GLenum drawMode)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003388{
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003389 if (drawMode == GL_POINTS)
3390 {
3391 // ProgramBinary assumes non-point rendering if gl_PointSize isn't written,
3392 // which affects varying interpolation. Since the value of gl_PointSize is
3393 // undefined when not written, just skip drawing to avoid unexpected results.
3394 if (!getCurrentProgramBinary()->usesPointSize())
3395 {
3396 // This is stictly speaking not an error, but developers should be
3397 // notified of risking undefined behavior.
3398 ERR("Point rendering without writing to gl_PointSize.");
3399
3400 return true;
3401 }
3402 }
3403 else if (isTriangleMode(drawMode))
3404 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003405 if (mState.rasterizer.cullFace && mState.rasterizer.cullMode == GL_FRONT_AND_BACK)
daniel@transgaming.com087e5782012-09-17 21:28:47 +00003406 {
3407 return true;
3408 }
3409 }
3410
3411 return false;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003412}
3413
3414bool Context::isTriangleMode(GLenum drawMode)
3415{
3416 switch (drawMode)
3417 {
3418 case GL_TRIANGLES:
3419 case GL_TRIANGLE_FAN:
3420 case GL_TRIANGLE_STRIP:
3421 return true;
3422 case GL_POINTS:
3423 case GL_LINES:
3424 case GL_LINE_LOOP:
3425 case GL_LINE_STRIP:
3426 return false;
3427 default: UNREACHABLE();
3428 }
3429
3430 return false;
3431}
3432
3433void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3434{
3435 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3436
3437 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3438 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3439 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3440 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
3441
3442 mVertexDataManager->dirtyCurrentValue(index);
3443}
3444
3445void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3446{
3447 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3448
3449 mState.vertexAttribute[index].mDivisor = divisor;
3450}
3451
3452// keep list sorted in following order
3453// OES extensions
3454// EXT extensions
3455// Vendor extensions
3456void Context::initExtensionString()
3457{
3458 mExtensionString = "";
3459
3460 // OES extensions
3461 if (supports32bitIndices())
3462 {
3463 mExtensionString += "GL_OES_element_index_uint ";
3464 }
3465
3466 mExtensionString += "GL_OES_packed_depth_stencil ";
3467 mExtensionString += "GL_OES_get_program_binary ";
3468 mExtensionString += "GL_OES_rgb8_rgba8 ";
3469 mExtensionString += "GL_OES_standard_derivatives ";
3470
3471 if (supportsFloat16Textures())
3472 {
3473 mExtensionString += "GL_OES_texture_half_float ";
3474 }
3475 if (supportsFloat16LinearFilter())
3476 {
3477 mExtensionString += "GL_OES_texture_half_float_linear ";
3478 }
3479 if (supportsFloat32Textures())
3480 {
3481 mExtensionString += "GL_OES_texture_float ";
3482 }
3483 if (supportsFloat32LinearFilter())
3484 {
3485 mExtensionString += "GL_OES_texture_float_linear ";
3486 }
3487
3488 if (supportsNonPower2Texture())
3489 {
3490 mExtensionString += "GL_OES_texture_npot ";
3491 }
3492
3493 // Multi-vendor (EXT) extensions
3494 if (supportsOcclusionQueries())
3495 {
3496 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3497 }
3498
3499 mExtensionString += "GL_EXT_read_format_bgra ";
3500 mExtensionString += "GL_EXT_robustness ";
3501
3502 if (supportsDXT1Textures())
3503 {
3504 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3505 }
3506
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003507 if (supportsTextureFilterAnisotropy())
3508 {
3509 mExtensionString += "GL_EXT_texture_filter_anisotropic ";
3510 }
3511
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003512 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3513 mExtensionString += "GL_EXT_texture_storage ";
3514
3515 // ANGLE-specific extensions
3516 if (supportsDepthTextures())
3517 {
3518 mExtensionString += "GL_ANGLE_depth_texture ";
3519 }
3520
3521 mExtensionString += "GL_ANGLE_framebuffer_blit ";
3522 if (getMaxSupportedSamples() != 0)
3523 {
3524 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3525 }
3526
3527 if (supportsInstancing())
3528 {
3529 mExtensionString += "GL_ANGLE_instanced_arrays ";
3530 }
3531
3532 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3533
3534 if (supportsDXT3Textures())
3535 {
3536 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3537 }
3538 if (supportsDXT5Textures())
3539 {
3540 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3541 }
3542
3543 mExtensionString += "GL_ANGLE_texture_usage ";
3544 mExtensionString += "GL_ANGLE_translated_shader_source ";
3545
3546 // Other vendor-specific extensions
3547 if (supportsEventQueries())
3548 {
3549 mExtensionString += "GL_NV_fence ";
3550 }
3551
3552 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3553 if (end != std::string::npos)
3554 {
3555 mExtensionString.resize(end+1);
3556 }
3557}
3558
3559const char *Context::getExtensionString() const
3560{
3561 return mExtensionString.c_str();
3562}
3563
3564void Context::initRendererString()
3565{
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003566 mRendererString = "ANGLE (";
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00003567 mRendererString += mRenderer->getAdapterDescription();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003568 mRendererString += ")";
3569}
3570
3571const char *Context::getRendererString() const
3572{
3573 return mRendererString.c_str();
3574}
3575
3576void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3577 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3578 GLbitfield mask)
3579{
3580 Framebuffer *readFramebuffer = getReadFramebuffer();
3581 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3582
3583 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3584 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3585 {
3586 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3587 }
3588
3589 if (drawFramebuffer->getSamples() != 0)
3590 {
3591 return error(GL_INVALID_OPERATION);
3592 }
3593
3594 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3595 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3596 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3597 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3598
3599 RECT sourceRect;
3600 RECT destRect;
3601
3602 if (srcX0 < srcX1)
3603 {
3604 sourceRect.left = srcX0;
3605 sourceRect.right = srcX1;
3606 destRect.left = dstX0;
3607 destRect.right = dstX1;
3608 }
3609 else
3610 {
3611 sourceRect.left = srcX1;
3612 destRect.left = dstX1;
3613 sourceRect.right = srcX0;
3614 destRect.right = dstX0;
3615 }
3616
3617 if (srcY0 < srcY1)
3618 {
3619 sourceRect.bottom = srcY1;
3620 destRect.bottom = dstY1;
3621 sourceRect.top = srcY0;
3622 destRect.top = dstY0;
3623 }
3624 else
3625 {
3626 sourceRect.bottom = srcY0;
3627 destRect.bottom = dstY0;
3628 sourceRect.top = srcY1;
3629 destRect.top = dstY1;
3630 }
3631
3632 RECT sourceScissoredRect = sourceRect;
3633 RECT destScissoredRect = destRect;
3634
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003635 if (mState.rasterizer.scissorTest)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003636 {
3637 // Only write to parts of the destination framebuffer which pass the scissor test
3638 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3639 // rect will be checked against scissorY, rather than the bottom.
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003640 if (destRect.left < mState.scissor.x)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003641 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003642 int xDiff = mState.scissor.x - destRect.left;
3643 destScissoredRect.left = mState.scissor.x;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003644 sourceScissoredRect.left += xDiff;
3645 }
3646
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003647 if (destRect.right > mState.scissor.x + mState.scissor.width)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003648 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003649 int xDiff = destRect.right - (mState.scissor.x + mState.scissor.width);
3650 destScissoredRect.right = mState.scissor.x + mState.scissor.width;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003651 sourceScissoredRect.right -= xDiff;
3652 }
3653
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003654 if (destRect.top < mState.scissor.y)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003655 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003656 int yDiff = mState.scissor.y - destRect.top;
3657 destScissoredRect.top = mState.scissor.y;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003658 sourceScissoredRect.top += yDiff;
3659 }
3660
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003661 if (destRect.bottom > mState.scissor.y + mState.scissor.height)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003662 {
daniel@transgaming.comf39967e2012-11-28 19:35:56 +00003663 int yDiff = destRect.bottom - (mState.scissor.y + mState.scissor.height);
3664 destScissoredRect.bottom = mState.scissor.y + mState.scissor.height;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003665 sourceScissoredRect.bottom -= yDiff;
3666 }
3667 }
3668
3669 bool blitRenderTarget = false;
3670 bool blitDepthStencil = false;
3671
3672 RECT sourceTrimmedRect = sourceScissoredRect;
3673 RECT destTrimmedRect = destScissoredRect;
3674
3675 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3676 // the actual draw and read surfaces.
3677 if (sourceTrimmedRect.left < 0)
3678 {
3679 int xDiff = 0 - sourceTrimmedRect.left;
3680 sourceTrimmedRect.left = 0;
3681 destTrimmedRect.left += xDiff;
3682 }
3683
3684 if (sourceTrimmedRect.right > readBufferWidth)
3685 {
3686 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3687 sourceTrimmedRect.right = readBufferWidth;
3688 destTrimmedRect.right -= xDiff;
3689 }
3690
3691 if (sourceTrimmedRect.top < 0)
3692 {
3693 int yDiff = 0 - sourceTrimmedRect.top;
3694 sourceTrimmedRect.top = 0;
3695 destTrimmedRect.top += yDiff;
3696 }
3697
3698 if (sourceTrimmedRect.bottom > readBufferHeight)
3699 {
3700 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3701 sourceTrimmedRect.bottom = readBufferHeight;
3702 destTrimmedRect.bottom -= yDiff;
3703 }
3704
3705 if (destTrimmedRect.left < 0)
3706 {
3707 int xDiff = 0 - destTrimmedRect.left;
3708 destTrimmedRect.left = 0;
3709 sourceTrimmedRect.left += xDiff;
3710 }
3711
3712 if (destTrimmedRect.right > drawBufferWidth)
3713 {
3714 int xDiff = destTrimmedRect.right - drawBufferWidth;
3715 destTrimmedRect.right = drawBufferWidth;
3716 sourceTrimmedRect.right -= xDiff;
3717 }
3718
3719 if (destTrimmedRect.top < 0)
3720 {
3721 int yDiff = 0 - destTrimmedRect.top;
3722 destTrimmedRect.top = 0;
3723 sourceTrimmedRect.top += yDiff;
3724 }
3725
3726 if (destTrimmedRect.bottom > drawBufferHeight)
3727 {
3728 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3729 destTrimmedRect.bottom = drawBufferHeight;
3730 sourceTrimmedRect.bottom -= yDiff;
3731 }
3732
3733 bool partialBufferCopy = false;
3734 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3735 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3736 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3737 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
3738 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3739 {
3740 partialBufferCopy = true;
3741 }
3742
3743 if (mask & GL_COLOR_BUFFER_BIT)
3744 {
3745 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3746 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3747 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3748 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3749 if (!validReadType || !validDrawType ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003750 readFramebuffer->getColorbuffer()->getActualFormat() != drawFramebuffer->getColorbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003751 {
3752 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3753 return error(GL_INVALID_OPERATION);
3754 }
3755
3756 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3757 {
3758 return error(GL_INVALID_OPERATION);
3759 }
3760
3761 blitRenderTarget = true;
3762
3763 }
3764
3765 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3766 {
3767 Renderbuffer *readDSBuffer = NULL;
3768 Renderbuffer *drawDSBuffer = NULL;
3769
3770 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3771 // both a depth and stencil buffer, it will be the same buffer.
3772
3773 if (mask & GL_DEPTH_BUFFER_BIT)
3774 {
3775 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3776 {
3777 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003778 readFramebuffer->getDepthbuffer()->getActualFormat() != drawFramebuffer->getDepthbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003779 {
3780 return error(GL_INVALID_OPERATION);
3781 }
3782
3783 blitDepthStencil = true;
3784 readDSBuffer = readFramebuffer->getDepthbuffer();
3785 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3786 }
3787 }
3788
3789 if (mask & GL_STENCIL_BUFFER_BIT)
3790 {
3791 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3792 {
3793 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
daniel@transgaming.com20d36662012-10-31 19:51:43 +00003794 readFramebuffer->getStencilbuffer()->getActualFormat() != drawFramebuffer->getStencilbuffer()->getActualFormat())
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003795 {
3796 return error(GL_INVALID_OPERATION);
3797 }
3798
3799 blitDepthStencil = true;
3800 readDSBuffer = readFramebuffer->getStencilbuffer();
3801 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3802 }
3803 }
3804
3805 if (partialBufferCopy)
3806 {
3807 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3808 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3809 }
3810
3811 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3812 (readDSBuffer && readDSBuffer->getSamples() != 0))
3813 {
3814 return error(GL_INVALID_OPERATION);
3815 }
3816 }
3817
3818 if (blitRenderTarget || blitDepthStencil)
3819 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003820 mRenderer->endScene();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003821
3822 if (blitRenderTarget)
3823 {
3824 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
3825 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
3826
3827 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
3828 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
3829
3830 readRenderTarget->Release();
3831 drawRenderTarget->Release();
3832
3833 if (FAILED(result))
3834 {
3835 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3836 return;
3837 }
3838 }
3839
3840 if (blitDepthStencil)
3841 {
3842 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
3843 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
3844
3845 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
3846
3847 readDepthStencil->Release();
3848 drawDepthStencil->Release();
3849
3850 if (FAILED(result))
3851 {
3852 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3853 return;
3854 }
3855 }
3856 }
3857}
3858
3859VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3860{
3861 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3862 {
3863 mVertexDeclCache[i].vertexDeclaration = NULL;
3864 mVertexDeclCache[i].lruCount = 0;
3865 }
3866}
3867
3868VertexDeclarationCache::~VertexDeclarationCache()
3869{
3870 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3871 {
3872 if (mVertexDeclCache[i].vertexDeclaration)
3873 {
3874 mVertexDeclCache[i].vertexDeclaration->Release();
3875 }
3876 }
3877}
3878
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00003879GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003880{
3881 *repeatDraw = 1;
3882
3883 int indexedAttribute = MAX_VERTEX_ATTRIBS;
3884 int instancedAttribute = MAX_VERTEX_ATTRIBS;
3885
3886 if (instances > 0)
3887 {
3888 // Find an indexed attribute to be mapped to D3D stream 0
3889 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3890 {
3891 if (attributes[i].active)
3892 {
3893 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
3894 {
3895 if (attributes[i].divisor == 0)
3896 {
3897 indexedAttribute = i;
3898 }
3899 }
3900 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
3901 {
3902 if (attributes[i].divisor != 0)
3903 {
3904 instancedAttribute = i;
3905 }
3906 }
3907 else break; // Found both an indexed and instanced attribute
3908 }
3909 }
3910
3911 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
3912 {
3913 return GL_INVALID_OPERATION;
3914 }
3915 }
3916
3917 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3918 D3DVERTEXELEMENT9 *element = &elements[0];
3919
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003920 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3921 {
3922 if (attributes[i].active)
3923 {
3924 int stream = i;
3925
3926 if (instances > 0)
3927 {
3928 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
3929 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
3930 {
3931 *repeatDraw = instances;
3932 }
3933 else
3934 {
3935 if (i == indexedAttribute)
3936 {
3937 stream = 0;
3938 }
3939 else if (i == 0)
3940 {
3941 stream = indexedAttribute;
3942 }
3943
3944 UINT frequency = 1;
3945
3946 if (attributes[i].divisor == 0)
3947 {
3948 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
3949 }
3950 else
3951 {
3952 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
3953 }
3954
3955 device->SetStreamSourceFreq(stream, frequency);
3956 mInstancingEnabled = true;
3957 }
3958 }
3959
3960 if (mAppliedVBs[stream].serial != attributes[i].serial ||
3961 mAppliedVBs[stream].stride != attributes[i].stride ||
3962 mAppliedVBs[stream].offset != attributes[i].offset)
3963 {
3964 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3965 mAppliedVBs[stream].serial = attributes[i].serial;
3966 mAppliedVBs[stream].stride = attributes[i].stride;
3967 mAppliedVBs[stream].offset = attributes[i].offset;
3968 }
3969
3970 element->Stream = stream;
3971 element->Offset = 0;
3972 element->Type = attributes[i].type;
3973 element->Method = D3DDECLMETHOD_DEFAULT;
3974 element->Usage = D3DDECLUSAGE_TEXCOORD;
3975 element->UsageIndex = programBinary->getSemanticIndex(i);
3976 element++;
3977 }
3978 }
3979
3980 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
3981 {
3982 if (mInstancingEnabled)
3983 {
3984 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3985 {
3986 device->SetStreamSourceFreq(i, 1);
3987 }
3988
3989 mInstancingEnabled = false;
3990 }
3991 }
3992
3993 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3994 *(element++) = end;
3995
3996 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3997 {
3998 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3999 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4000 {
4001 entry->lruCount = ++mMaxLru;
4002 if(entry->vertexDeclaration != mLastSetVDecl)
4003 {
4004 device->SetVertexDeclaration(entry->vertexDeclaration);
4005 mLastSetVDecl = entry->vertexDeclaration;
4006 }
4007
4008 return GL_NO_ERROR;
4009 }
4010 }
4011
4012 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4013
4014 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4015 {
4016 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4017 {
4018 lastCache = &mVertexDeclCache[i];
4019 }
4020 }
4021
4022 if (lastCache->vertexDeclaration != NULL)
4023 {
4024 lastCache->vertexDeclaration->Release();
4025 lastCache->vertexDeclaration = NULL;
4026 // mLastSetVDecl is set to the replacement, so we don't have to worry
4027 // about it.
4028 }
4029
4030 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4031 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4032 device->SetVertexDeclaration(lastCache->vertexDeclaration);
4033 mLastSetVDecl = lastCache->vertexDeclaration;
4034 lastCache->lruCount = ++mMaxLru;
4035
4036 return GL_NO_ERROR;
4037}
4038
4039void VertexDeclarationCache::markStateDirty()
4040{
4041 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4042 {
4043 mAppliedVBs[i].serial = 0;
4044 }
4045
4046 mLastSetVDecl = NULL;
4047 mInstancingEnabled = true; // Forces it to be disabled when not used
4048}
4049
4050}
4051
4052extern "C"
4053{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004054gl::Context *glCreateContext(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004055{
daniel@transgaming.com03d39092012-11-28 19:31:59 +00004056 return new gl::Context(shareContext, renderer, notifyResets, robustAccess);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004057}
4058
4059void glDestroyContext(gl::Context *context)
4060{
4061 delete context;
4062
4063 if (context == gl::getContext())
4064 {
4065 gl::makeCurrent(NULL, NULL, NULL);
4066 }
4067}
4068
4069void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4070{
4071 gl::makeCurrent(context, display, surface);
4072}
4073
4074gl::Context *glGetCurrentContext()
4075{
4076 return gl::getContext();
4077}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00004078
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004079rx::SwapChain *glCreateSwapChain(rx::Renderer9 *renderer, HWND window, HANDLE shareHandle,
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004080 GLenum backBufferFormat, GLenum depthBufferFormat)
4081{
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004082 return new rx::SwapChain(renderer, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004083}
4084
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +00004085void glDestroySwapChain(rx::SwapChain *swapChain)
daniel@transgaming.com3c720782012-10-31 18:42:34 +00004086{
4087 delete swapChain;
4088}
4089
4090
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004091}