blob: 6c973ead2076125cacffcf617f061c16ee23262d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000159 mSupportsCompressedTextures = false;
160 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000183 while (!mFenceMap.empty())
184 {
185 deleteFence(mFenceMap.begin()->first);
186 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000187
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000188 while (!mMultiSampleSupport.empty())
189 {
190 delete [] mMultiSampleSupport.begin()->second;
191 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
192 }
193
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000194 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000196 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000202 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000203 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000204 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 }
206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
208 {
209 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
210 }
211
212 mState.arrayBuffer.set(NULL);
213 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 mState.renderbuffer.set(NULL);
215
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mTexture2DZero.set(NULL);
217 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000220 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000221 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000222 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000223
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000224 if (mMaskedClearSavedState)
225 {
226 mMaskedClearSavedState->Release();
227 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000228
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000229 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
233{
234 IDirect3DDevice9 *device = display->getDevice();
235
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239
daniel@transgaming.com83921382011-01-08 05:46:00 +0000240 mVertexDataManager = new VertexDataManager(this, device);
241 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000244 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000245 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000246 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247
248 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
249 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
250 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
251 mMaxRenderbufferDimension = mMaxTextureDimension;
252 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
253 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
254 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
255
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000256 const D3DFORMAT renderBufferFormats[] =
257 {
258 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000259 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000260 D3DFMT_R5G6B5,
261 D3DFMT_D24S8
262 };
263
264 int max = 0;
265 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
266 {
267 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
268 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
269 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
270
271 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
272 {
273 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
274 {
275 max = j;
276 }
277 }
278 }
279
280 mMaxSupportedSamples = max;
281
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000282 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000283 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000284 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
285 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000286 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
287 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000288
daniel@transgaming.com83921382011-01-08 05:46:00 +0000289 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
290
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000291 initExtensionString();
292
293 mState.viewportX = 0;
294 mState.viewportY = 0;
295 mState.viewportWidth = surface->getWidth();
296 mState.viewportHeight = surface->getHeight();
297
298 mState.scissorX = 0;
299 mState.scissorY = 0;
300 mState.scissorWidth = surface->getWidth();
301 mState.scissorHeight = surface->getHeight();
302
303 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000304 }
305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
307 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000308 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000311 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000312 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
314 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000316 if (defaultRenderTarget)
317 {
318 defaultRenderTarget->Release();
319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000321 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000323 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000325
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000326 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327}
328
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000329// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000330void Context::markAllStateDirty()
331{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000332 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
333 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000334 mAppliedTextureSerialPS[t] = 0;
335 }
336
337 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
338 {
339 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000340 }
341
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000342 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000343 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000344 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000345 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000346 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000347
348 mClearStateDirty = true;
349 mCullStateDirty = true;
350 mDepthStateDirty = true;
351 mMaskStateDirty = true;
352 mBlendStateDirty = true;
353 mStencilStateDirty = true;
354 mPolygonOffsetStateDirty = true;
355 mScissorStateDirty = true;
356 mSampleStateDirty = true;
357 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000358 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000359}
360
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361void Context::setClearColor(float red, float green, float blue, float alpha)
362{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000363 mState.colorClearValue.red = red;
364 mState.colorClearValue.green = green;
365 mState.colorClearValue.blue = blue;
366 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
369void Context::setClearDepth(float depth)
370{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
374void Context::setClearStencil(int stencil)
375{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376 mState.stencilClearValue = stencil;
377}
378
379void Context::setCullFace(bool enabled)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.cullFace != enabled)
382 {
383 mState.cullFace = enabled;
384 mCullStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388bool Context::isCullFaceEnabled() const
389{
390 return mState.cullFace;
391}
392
393void Context::setCullMode(GLenum mode)
394{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000395 if (mState.cullMode != mode)
396 {
397 mState.cullMode = mode;
398 mCullStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402void Context::setFrontFace(GLenum front)
403{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000404 if (mState.frontFace != front)
405 {
406 mState.frontFace = front;
407 mFrontFaceDirty = true;
408 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000409}
410
411void Context::setDepthTest(bool enabled)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.depthTest != enabled)
414 {
415 mState.depthTest = enabled;
416 mDepthStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420bool Context::isDepthTestEnabled() const
421{
422 return mState.depthTest;
423}
424
425void Context::setDepthFunc(GLenum depthFunc)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.depthFunc != depthFunc)
428 {
429 mState.depthFunc = depthFunc;
430 mDepthStateDirty = true;
431 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +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.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.blend != enabled)
443 {
444 mState.blend = enabled;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449bool Context::isBlendEnabled() const
450{
451 return mState.blend;
452}
453
454void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
455{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000456 if (mState.sourceBlendRGB != sourceRGB ||
457 mState.sourceBlendAlpha != sourceAlpha ||
458 mState.destBlendRGB != destRGB ||
459 mState.destBlendAlpha != destAlpha)
460 {
461 mState.sourceBlendRGB = sourceRGB;
462 mState.destBlendRGB = destRGB;
463 mState.sourceBlendAlpha = sourceAlpha;
464 mState.destBlendAlpha = destAlpha;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setBlendColor(float red, float green, float blue, float alpha)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.blendColor.red != red ||
472 mState.blendColor.green != green ||
473 mState.blendColor.blue != blue ||
474 mState.blendColor.alpha != alpha)
475 {
476 mState.blendColor.red = red;
477 mState.blendColor.green = green;
478 mState.blendColor.blue = blue;
479 mState.blendColor.alpha = alpha;
480 mBlendStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
485{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 if (mState.blendEquationRGB != rgbEquation ||
487 mState.blendEquationAlpha != alphaEquation)
488 {
489 mState.blendEquationRGB = rgbEquation;
490 mState.blendEquationAlpha = alphaEquation;
491 mBlendStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setStencilTest(bool enabled)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilTest != enabled)
498 {
499 mState.stencilTest = enabled;
500 mStencilStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504bool Context::isStencilTestEnabled() const
505{
506 return mState.stencilTest;
507}
508
509void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
510{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000511 if (mState.stencilFunc != stencilFunc ||
512 mState.stencilRef != stencilRef ||
513 mState.stencilMask != stencilMask)
514 {
515 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000516 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 mState.stencilMask = stencilMask;
518 mStencilStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilBackFunc != stencilBackFunc ||
525 mState.stencilBackRef != stencilBackRef ||
526 mState.stencilBackMask != stencilBackMask)
527 {
528 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000529 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 mState.stencilBackMask = stencilBackMask;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setStencilWritemask(GLuint stencilWritemask)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilWritemask != stencilWritemask)
538 {
539 mState.stencilWritemask = stencilWritemask;
540 mStencilStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilBackWritemask != stencilBackWritemask)
547 {
548 mState.stencilBackWritemask = stencilBackWritemask;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilFail != stencilFail ||
556 mState.stencilPassDepthFail != stencilPassDepthFail ||
557 mState.stencilPassDepthPass != stencilPassDepthPass)
558 {
559 mState.stencilFail = stencilFail;
560 mState.stencilPassDepthFail = stencilPassDepthFail;
561 mState.stencilPassDepthPass = stencilPassDepthPass;
562 mStencilStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.stencilBackFail != stencilBackFail ||
569 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
570 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
571 {
572 mState.stencilBackFail = stencilBackFail;
573 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
574 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setPolygonOffsetFill(bool enabled)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.polygonOffsetFill != enabled)
582 {
583 mState.polygonOffsetFill = enabled;
584 mPolygonOffsetStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588bool Context::isPolygonOffsetFillEnabled() const
589{
590 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000591
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.polygonOffsetFactor != factor ||
597 mState.polygonOffsetUnits != units)
598 {
599 mState.polygonOffsetFactor = factor;
600 mState.polygonOffsetUnits = units;
601 mPolygonOffsetStateDirty = true;
602 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603}
604
605void Context::setSampleAlphaToCoverage(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.sampleAlphaToCoverage != enabled)
608 {
609 mState.sampleAlphaToCoverage = enabled;
610 mSampleStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isSampleAlphaToCoverageEnabled() const
615{
616 return mState.sampleAlphaToCoverage;
617}
618
619void Context::setSampleCoverage(bool enabled)
620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.sampleCoverage != enabled)
622 {
623 mState.sampleCoverage = enabled;
624 mSampleStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628bool Context::isSampleCoverageEnabled() const
629{
630 return mState.sampleCoverage;
631}
632
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000633void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000635 if (mState.sampleCoverageValue != value ||
636 mState.sampleCoverageInvert != invert)
637 {
638 mState.sampleCoverageValue = value;
639 mState.sampleCoverageInvert = invert;
640 mSampleStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setScissorTest(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.scissorTest != enabled)
647 {
648 mState.scissorTest = enabled;
649 mScissorStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isScissorTestEnabled() const
654{
655 return mState.scissorTest;
656}
657
658void Context::setDither(bool enabled)
659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.dither != enabled)
661 {
662 mState.dither = enabled;
663 mDitherStateDirty = true;
664 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000665}
666
667bool Context::isDitherEnabled() const
668{
669 return mState.dither;
670}
671
672void Context::setLineWidth(GLfloat width)
673{
674 mState.lineWidth = width;
675}
676
677void Context::setGenerateMipmapHint(GLenum hint)
678{
679 mState.generateMipmapHint = hint;
680}
681
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000682void Context::setFragmentShaderDerivativeHint(GLenum hint)
683{
684 mState.fragmentShaderDerivativeHint = hint;
685 // TODO: Propagate the hint to shader translator so we can write
686 // ddx, ddx_coarse, or ddx_fine depending on the hint.
687 // Ignore for now. It is valid for implementations to ignore hint.
688}
689
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000690void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
691{
692 mState.viewportX = x;
693 mState.viewportY = y;
694 mState.viewportWidth = width;
695 mState.viewportHeight = height;
696}
697
698void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.scissorX != x || mState.scissorY != y ||
701 mState.scissorWidth != width || mState.scissorHeight != height)
702 {
703 mState.scissorX = x;
704 mState.scissorY = y;
705 mState.scissorWidth = width;
706 mState.scissorHeight = height;
707 mScissorStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
712{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000713 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
714 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
715 {
716 mState.colorMaskRed = red;
717 mState.colorMaskGreen = green;
718 mState.colorMaskBlue = blue;
719 mState.colorMaskAlpha = alpha;
720 mMaskStateDirty = true;
721 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000722}
723
724void Context::setDepthMask(bool mask)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.depthMask != mask)
727 {
728 mState.depthMask = mask;
729 mMaskStateDirty = true;
730 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000733void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734{
735 mState.activeSampler = active;
736}
737
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000738GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000740 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000741}
742
743GLuint Context::getDrawFramebufferHandle() const
744{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000745 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746}
747
748GLuint Context::getRenderbufferHandle() const
749{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000750 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000751}
752
753GLuint Context::getArrayBufferHandle() const
754{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000755 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756}
757
daniel@transgaming.com83921382011-01-08 05:46:00 +0000758void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000760 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000761}
762
daniel@transgaming.com83921382011-01-08 05:46:00 +0000763const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000764{
765 return mState.vertexAttribute[attribNum];
766}
767
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000768void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000769 GLsizei stride, const void *pointer)
770{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000771 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772 mState.vertexAttribute[attribNum].mSize = size;
773 mState.vertexAttribute[attribNum].mType = type;
774 mState.vertexAttribute[attribNum].mNormalized = normalized;
775 mState.vertexAttribute[attribNum].mStride = stride;
776 mState.vertexAttribute[attribNum].mPointer = pointer;
777}
778
779const void *Context::getVertexAttribPointer(unsigned int attribNum) const
780{
781 return mState.vertexAttribute[attribNum].mPointer;
782}
783
daniel@transgaming.com83921382011-01-08 05:46:00 +0000784const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000785{
786 return mState.vertexAttribute;
787}
788
789void Context::setPackAlignment(GLint alignment)
790{
791 mState.packAlignment = alignment;
792}
793
794GLint Context::getPackAlignment() const
795{
796 return mState.packAlignment;
797}
798
799void Context::setUnpackAlignment(GLint alignment)
800{
801 mState.unpackAlignment = alignment;
802}
803
804GLint Context::getUnpackAlignment() const
805{
806 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809GLuint Context::createBuffer()
810{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000811 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814GLuint Context::createProgram()
815{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000816 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000819GLuint Context::createShader(GLenum type)
820{
821 return mResourceManager->createShader(type);
822}
823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createTexture()
825{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000826 return mResourceManager->createTexture();
827}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000829GLuint Context::createRenderbuffer()
830{
831 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000834// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835GLuint Context::createFramebuffer()
836{
benvanik@google.com1a233342011-04-28 19:44:39 +0000837 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000838
839 mFramebufferMap[handle] = NULL;
840
841 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000844GLuint Context::createFence()
845{
benvanik@google.com1a233342011-04-28 19:44:39 +0000846 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000847
848 mFenceMap[handle] = new Fence;
849
850 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000851}
852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853void Context::deleteBuffer(GLuint buffer)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856 {
857 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000859
860 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteShader(GLuint shader)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868void Context::deleteProgram(GLuint program)
869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871}
872
873void Context::deleteTexture(GLuint texture)
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876 {
877 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879
880 mResourceManager->deleteTexture(texture);
881}
882
883void Context::deleteRenderbuffer(GLuint renderbuffer)
884{
885 if (mResourceManager->getRenderbuffer(renderbuffer))
886 {
887 detachRenderbuffer(renderbuffer);
888 }
889
890 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
893void Context::deleteFramebuffer(GLuint framebuffer)
894{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000895 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
896
897 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898 {
899 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000900
benvanik@google.com1a233342011-04-28 19:44:39 +0000901 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000902 delete framebufferObject->second;
903 mFramebufferMap.erase(framebufferObject);
904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000906
907void Context::deleteFence(GLuint fence)
908{
909 FenceMap::iterator fenceObject = mFenceMap.find(fence);
910
911 if (fenceObject != mFenceMap.end())
912 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000913 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000914 delete fenceObject->second;
915 mFenceMap.erase(fenceObject);
916 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000917}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921 return mResourceManager->getBuffer(handle);
922}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924Shader *Context::getShader(GLuint handle)
925{
926 return mResourceManager->getShader(handle);
927}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929Program *Context::getProgram(GLuint handle)
930{
931 return mResourceManager->getProgram(handle);
932}
933
934Texture *Context::getTexture(GLuint handle)
935{
936 return mResourceManager->getTexture(handle);
937}
938
939Renderbuffer *Context::getRenderbuffer(GLuint handle)
940{
941 return mResourceManager->getRenderbuffer(handle);
942}
943
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000944Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946 return getFramebuffer(mState.readFramebuffer);
947}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000948
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000949Framebuffer *Context::getDrawFramebuffer()
950{
951 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindArrayBuffer(unsigned int buffer)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindElementArrayBuffer(unsigned int buffer)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000965 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindTexture2D(GLuint texture)
969{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000970 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000972 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::bindTextureCubeMap(GLuint texture)
976{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000977 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000979 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000982void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000984 if (!getFramebuffer(framebuffer))
985 {
986 mFramebufferMap[framebuffer] = new Framebuffer();
987 }
988
989 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000990}
991
992void Context::bindDrawFramebuffer(GLuint framebuffer)
993{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994 if (!getFramebuffer(framebuffer))
995 {
996 mFramebufferMap[framebuffer] = new Framebuffer();
997 }
998
999 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::bindRenderbuffer(GLuint renderbuffer)
1003{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001004 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1005
1006 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
1009void Context::useProgram(GLuint program)
1010{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001011 GLuint priorProgram = mState.currentProgram;
1012 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001013
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001014 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001016 Program *newProgram = mResourceManager->getProgram(program);
1017 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1018
1019 if (newProgram)
1020 {
1021 newProgram->addRef();
1022 }
1023
1024 if (oldProgram)
1025 {
1026 oldProgram->release();
1027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
1031void Context::setFramebufferZero(Framebuffer *buffer)
1032{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001033 delete mFramebufferMap[0];
1034 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035}
1036
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001037void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001039 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1040 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001043Framebuffer *Context::getFramebuffer(unsigned int handle)
1044{
1045 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1046
1047 if (framebuffer == mFramebufferMap.end())
1048 {
1049 return NULL;
1050 }
1051 else
1052 {
1053 return framebuffer->second;
1054 }
1055}
1056
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001057Fence *Context::getFence(unsigned int handle)
1058{
1059 FenceMap::iterator fence = mFenceMap.find(handle);
1060
1061 if (fence == mFenceMap.end())
1062 {
1063 return NULL;
1064 }
1065 else
1066 {
1067 return fence->second;
1068 }
1069}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071Buffer *Context::getArrayBuffer()
1072{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
1076Buffer *Context::getElementArrayBuffer()
1077{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081Program *Context::getCurrentProgram()
1082{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086Texture2D *Context::getTexture2D()
1087{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001088 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089}
1090
1091TextureCubeMap *Context::getTextureCubeMap()
1092{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001093 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001099
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001100 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001101 {
1102 switch (type)
1103 {
1104 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 case TEXTURE_2D: return mTexture2DZero.get();
1106 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001107 }
1108 }
1109
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001110 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111}
1112
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113bool Context::getBooleanv(GLenum pname, GLboolean *params)
1114{
1115 switch (pname)
1116 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001117 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1118 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1119 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001121 params[0] = mState.colorMaskRed;
1122 params[1] = mState.colorMaskGreen;
1123 params[2] = mState.colorMaskBlue;
1124 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001125 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001126 case GL_CULL_FACE: *params = mState.cullFace; break;
1127 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1128 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1129 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1130 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1131 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1132 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1133 case GL_BLEND: *params = mState.blend; break;
1134 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001135 default:
1136 return false;
1137 }
1138
1139 return true;
1140}
1141
1142bool Context::getFloatv(GLenum pname, GLfloat *params)
1143{
1144 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1145 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1146 // GetIntegerv as its native query function. As it would require conversion in any
1147 // case, this should make no difference to the calling application.
1148 switch (pname)
1149 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001150 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1151 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1152 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1153 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1154 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001155 case GL_ALIASED_LINE_WIDTH_RANGE:
1156 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1157 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1158 break;
1159 case GL_ALIASED_POINT_SIZE_RANGE:
1160 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001161 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001162 break;
1163 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001164 params[0] = mState.zNear;
1165 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 break;
1167 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001168 params[0] = mState.colorClearValue.red;
1169 params[1] = mState.colorClearValue.green;
1170 params[2] = mState.colorClearValue.blue;
1171 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001173 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001174 params[0] = mState.blendColor.red;
1175 params[1] = mState.blendColor.green;
1176 params[2] = mState.blendColor.blue;
1177 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001178 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 default:
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186bool Context::getIntegerv(GLenum pname, GLint *params)
1187{
1188 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1189 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1190 // GetIntegerv as its native query function. As it would require conversion in any
1191 // case, this should make no difference to the calling application. You may find it in
1192 // Context::getFloatv.
1193 switch (pname)
1194 {
1195 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1196 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001197 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001198 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1199 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001200 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001201 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001202 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001205 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1206 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001207 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1208 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1209 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001210 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001211 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1212 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1213 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1214 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001215 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001216 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1217 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1218 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1219 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1220 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1221 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1222 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1223 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1224 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1225 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1226 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1228 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1229 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1230 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1231 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1232 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1233 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1234 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1235 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1236 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1237 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1238 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1239 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001240 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1241 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001242 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1243 {
1244 if (supportsCompressedTextures())
1245 {
1246 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1247 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1248 *params = 2;
1249 }
1250 else
1251 {
1252 *params = 0;
1253 }
1254 }
1255 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001256 case GL_MAX_SAMPLES_ANGLE:
1257 {
1258 GLsizei maxSamples = getMaxSupportedSamples();
1259 if (maxSamples != 0)
1260 {
1261 *params = maxSamples;
1262 }
1263 else
1264 {
1265 return false;
1266 }
1267
1268 break;
1269 }
1270 case GL_SAMPLE_BUFFERS:
1271 case GL_SAMPLES:
1272 {
1273 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1274 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1275 {
1276 switch (pname)
1277 {
1278 case GL_SAMPLE_BUFFERS:
1279 if (framebuffer->getSamples() != 0)
1280 {
1281 *params = 1;
1282 }
1283 else
1284 {
1285 *params = 0;
1286 }
1287 break;
1288 case GL_SAMPLES:
1289 *params = framebuffer->getSamples();
1290 break;
1291 }
1292 }
1293 else
1294 {
1295 *params = 0;
1296 }
1297 }
1298 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001299 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1300 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1301 case GL_MAX_VIEWPORT_DIMS:
1302 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001303 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001304 params[0] = maxDimension;
1305 params[1] = maxDimension;
1306 }
1307 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001308 case GL_COMPRESSED_TEXTURE_FORMATS:
1309 {
1310 if (supportsCompressedTextures())
1311 {
1312 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1313 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1314 }
1315 }
1316 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001318 params[0] = mState.viewportX;
1319 params[1] = mState.viewportY;
1320 params[2] = mState.viewportWidth;
1321 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 break;
1323 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001324 params[0] = mState.scissorX;
1325 params[1] = mState.scissorY;
1326 params[2] = mState.scissorWidth;
1327 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001329 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1330 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 case GL_RED_BITS:
1332 case GL_GREEN_BITS:
1333 case GL_BLUE_BITS:
1334 case GL_ALPHA_BITS:
1335 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001336 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001337 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1338
1339 if (colorbuffer)
1340 {
1341 switch (pname)
1342 {
1343 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1344 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1345 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1346 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1347 }
1348 }
1349 else
1350 {
1351 *params = 0;
1352 }
1353 }
1354 break;
1355 case GL_DEPTH_BITS:
1356 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001357 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001358 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359
1360 if (depthbuffer)
1361 {
1362 *params = depthbuffer->getDepthSize();
1363 }
1364 else
1365 {
1366 *params = 0;
1367 }
1368 }
1369 break;
1370 case GL_STENCIL_BITS:
1371 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001372 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001373 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374
1375 if (stencilbuffer)
1376 {
1377 *params = stencilbuffer->getStencilSize();
1378 }
1379 else
1380 {
1381 *params = 0;
1382 }
1383 }
1384 break;
1385 case GL_TEXTURE_BINDING_2D:
1386 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001387 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 {
1389 error(GL_INVALID_OPERATION);
1390 return false;
1391 }
1392
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001393 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001394 }
1395 break;
1396 case GL_TEXTURE_BINDING_CUBE_MAP:
1397 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001398 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 {
1400 error(GL_INVALID_OPERATION);
1401 return false;
1402 }
1403
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001404 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 }
1406 break;
1407 default:
1408 return false;
1409 }
1410
1411 return true;
1412}
1413
1414bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1415{
1416 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1417 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1418 // to the fact that it is stored internally as a float, and so would require conversion
1419 // if returned from Context::getIntegerv. Since this conversion is already implemented
1420 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1421 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1422 // application.
1423 switch (pname)
1424 {
1425 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1426 case GL_SHADER_BINARY_FORMATS:
1427 {
1428 *type = GL_INT;
1429 *numParams = 0;
1430 }
1431 break;
1432 case GL_MAX_VERTEX_ATTRIBS:
1433 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1434 case GL_MAX_VARYING_VECTORS:
1435 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_TEXTURE_IMAGE_UNITS:
1438 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1439 case GL_MAX_RENDERBUFFER_SIZE:
1440 case GL_NUM_SHADER_BINARY_FORMATS:
1441 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1442 case GL_ARRAY_BUFFER_BINDING:
1443 case GL_FRAMEBUFFER_BINDING:
1444 case GL_RENDERBUFFER_BINDING:
1445 case GL_CURRENT_PROGRAM:
1446 case GL_PACK_ALIGNMENT:
1447 case GL_UNPACK_ALIGNMENT:
1448 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001449 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001450 case GL_RED_BITS:
1451 case GL_GREEN_BITS:
1452 case GL_BLUE_BITS:
1453 case GL_ALPHA_BITS:
1454 case GL_DEPTH_BITS:
1455 case GL_STENCIL_BITS:
1456 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1457 case GL_CULL_FACE_MODE:
1458 case GL_FRONT_FACE:
1459 case GL_ACTIVE_TEXTURE:
1460 case GL_STENCIL_FUNC:
1461 case GL_STENCIL_VALUE_MASK:
1462 case GL_STENCIL_REF:
1463 case GL_STENCIL_FAIL:
1464 case GL_STENCIL_PASS_DEPTH_FAIL:
1465 case GL_STENCIL_PASS_DEPTH_PASS:
1466 case GL_STENCIL_BACK_FUNC:
1467 case GL_STENCIL_BACK_VALUE_MASK:
1468 case GL_STENCIL_BACK_REF:
1469 case GL_STENCIL_BACK_FAIL:
1470 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1471 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1472 case GL_DEPTH_FUNC:
1473 case GL_BLEND_SRC_RGB:
1474 case GL_BLEND_SRC_ALPHA:
1475 case GL_BLEND_DST_RGB:
1476 case GL_BLEND_DST_ALPHA:
1477 case GL_BLEND_EQUATION_RGB:
1478 case GL_BLEND_EQUATION_ALPHA:
1479 case GL_STENCIL_WRITEMASK:
1480 case GL_STENCIL_BACK_WRITEMASK:
1481 case GL_STENCIL_CLEAR_VALUE:
1482 case GL_SUBPIXEL_BITS:
1483 case GL_MAX_TEXTURE_SIZE:
1484 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1485 case GL_SAMPLE_BUFFERS:
1486 case GL_SAMPLES:
1487 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1488 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1489 case GL_TEXTURE_BINDING_2D:
1490 case GL_TEXTURE_BINDING_CUBE_MAP:
1491 {
1492 *type = GL_INT;
1493 *numParams = 1;
1494 }
1495 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001496 case GL_MAX_SAMPLES_ANGLE:
1497 {
1498 if (getMaxSupportedSamples() != 0)
1499 {
1500 *type = GL_INT;
1501 *numParams = 1;
1502 }
1503 else
1504 {
1505 return false;
1506 }
1507 }
1508 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001509 case GL_MAX_VIEWPORT_DIMS:
1510 {
1511 *type = GL_INT;
1512 *numParams = 2;
1513 }
1514 break;
1515 case GL_VIEWPORT:
1516 case GL_SCISSOR_BOX:
1517 {
1518 *type = GL_INT;
1519 *numParams = 4;
1520 }
1521 break;
1522 case GL_SHADER_COMPILER:
1523 case GL_SAMPLE_COVERAGE_INVERT:
1524 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001525 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1526 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1527 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1528 case GL_SAMPLE_COVERAGE:
1529 case GL_SCISSOR_TEST:
1530 case GL_STENCIL_TEST:
1531 case GL_DEPTH_TEST:
1532 case GL_BLEND:
1533 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001534 {
1535 *type = GL_BOOL;
1536 *numParams = 1;
1537 }
1538 break;
1539 case GL_COLOR_WRITEMASK:
1540 {
1541 *type = GL_BOOL;
1542 *numParams = 4;
1543 }
1544 break;
1545 case GL_POLYGON_OFFSET_FACTOR:
1546 case GL_POLYGON_OFFSET_UNITS:
1547 case GL_SAMPLE_COVERAGE_VALUE:
1548 case GL_DEPTH_CLEAR_VALUE:
1549 case GL_LINE_WIDTH:
1550 {
1551 *type = GL_FLOAT;
1552 *numParams = 1;
1553 }
1554 break;
1555 case GL_ALIASED_LINE_WIDTH_RANGE:
1556 case GL_ALIASED_POINT_SIZE_RANGE:
1557 case GL_DEPTH_RANGE:
1558 {
1559 *type = GL_FLOAT;
1560 *numParams = 2;
1561 }
1562 break;
1563 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001564 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001565 {
1566 *type = GL_FLOAT;
1567 *numParams = 4;
1568 }
1569 break;
1570 default:
1571 return false;
1572 }
1573
1574 return true;
1575}
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577// Applies the render target surface, depth stencil surface, viewport rectangle and
1578// scissor rectangle to the Direct3D 9 device
1579bool Context::applyRenderTarget(bool ignoreViewport)
1580{
1581 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001582
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001583 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
1585 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1586 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001587 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 }
1589
1590 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001591
1592 if (!renderTarget)
1593 {
1594 return false; // Context must be lost
1595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001599 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1600 if (renderTargetSerial != mAppliedRenderTargetSerial)
1601 {
1602 device->SetRenderTarget(0, renderTarget);
1603 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001604 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001605 }
1606
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001607 unsigned int depthbufferSerial = 0;
1608 unsigned int stencilbufferSerial = 0;
1609 if (framebufferObject->getDepthbufferType() != GL_NONE)
1610 {
1611 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001612 if (!depthStencil)
1613 {
1614 ERR("Depth stencil pointer unexpectedly null.");
1615 return false;
1616 }
1617
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001618 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1619 }
1620 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1621 {
1622 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001623 if (!depthStencil)
1624 {
1625 ERR("Depth stencil pointer unexpectedly null.");
1626 return false;
1627 }
1628
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001629 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1630 }
1631
1632 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001633 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001634 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001635 {
1636 device->SetDepthStencilSurface(depthStencil);
1637 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001638 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001639 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
1642 D3DVIEWPORT9 viewport;
1643 D3DSURFACE_DESC desc;
1644 renderTarget->GetDesc(&desc);
1645
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001646 float zNear = clamp01(mState.zNear);
1647 float zFar = clamp01(mState.zFar);
1648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 if (ignoreViewport)
1650 {
1651 viewport.X = 0;
1652 viewport.Y = 0;
1653 viewport.Width = desc.Width;
1654 viewport.Height = desc.Height;
1655 viewport.MinZ = 0.0f;
1656 viewport.MaxZ = 1.0f;
1657 }
1658 else
1659 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001660 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1661 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1662 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1663 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1664 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001665 viewport.MinZ = zNear;
1666 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 }
1668
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001669 if (viewport.Width <= 0 || viewport.Height <= 0)
1670 {
1671 return false; // Nothing to render
1672 }
1673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 device->SetViewport(&viewport);
1675
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 if (mState.scissorTest)
1679 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001680 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1681 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1682 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1683 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1684 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 device->SetScissorRect(&rect);
1686 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1687 }
1688 else
1689 {
1690 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1691 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001692
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001693 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
1695
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001696 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 Program *programObject = getCurrentProgram();
1699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001701 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001703
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1706 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1707 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001708 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001711 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001712 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001713
daniel@transgaming.com31754962010-11-28 02:02:52 +00001714 GLint depthRange = programObject->getDxDepthRangeLocation();
1715 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1716 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 }
1718
1719 return true;
1720}
1721
1722// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001723void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724{
1725 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 Program *programObject = getCurrentProgram();
1727
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001728 Framebuffer *framebufferObject = getDrawFramebuffer();
1729
1730 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001733 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001734 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001736 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001737 GLint alwaysFront = !isTriangleMode(drawMode);
1738 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1739
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001740 egl::Display *display = getDisplay();
1741 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1742 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1743 // Apparently some ATI cards have a bug where a draw with a zero color
1744 // write mask can cause later draws to have incorrect results. Instead,
1745 // set a nonzero color write mask but modify the blend state so that no
1746 // drawing is done.
1747 // http://code.google.com/p/angleproject/issues/detail?id=169
1748
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001753 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 else
1756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
1759
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 mCullStateDirty = false;
1761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mDepthStateDirty)
1764 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001765 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1768 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 }
1770 else
1771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001774
1775 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 }
1777
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001778 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1779 {
1780 mBlendStateDirty = true;
1781 mMaskStateDirty = true;
1782 }
1783
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001786 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001787 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1789
1790 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1791 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1792 {
1793 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1794 }
1795 else
1796 {
1797 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1798 unorm<8>(mState.blendColor.alpha),
1799 unorm<8>(mState.blendColor.alpha),
1800 unorm<8>(mState.blendColor.alpha)));
1801 }
1802
1803 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1804 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1805 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1806
1807 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1808 mState.destBlendRGB != mState.destBlendAlpha ||
1809 mState.blendEquationRGB != mState.blendEquationAlpha)
1810 {
1811 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1812
1813 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1814 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1815 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001816 }
1817 else
1818 {
1819 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1820 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001821 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001823 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001825 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826
1827 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 }
1829
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 if (mStencilStateDirty || mFrontFaceDirty)
1831 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001832 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 {
1834 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1835 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1836
1837 // FIXME: Unsupported by D3D9
1838 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1839 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1840 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1841 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1842 mState.stencilRef != mState.stencilBackRef ||
1843 mState.stencilMask != mState.stencilBackMask)
1844 {
1845 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1846 return error(GL_INVALID_OPERATION);
1847 }
1848
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001849 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001850 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1852
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001853 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1854 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertComparison(mState.stencilFunc));
1856
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001857 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1858 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001860 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001862 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001864 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001865 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1866
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001867 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1868 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001869 es2dx::ConvertComparison(mState.stencilBackFunc));
1870
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001871 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1872 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001873
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001874 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001876 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001878 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001879 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1884 }
1885
1886 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001887 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001888 }
1889
1890 if (mMaskStateDirty)
1891 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001892 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1893 mState.colorMaskBlue, mState.colorMaskAlpha);
1894 if (colorMask == 0 && !zeroColorMaskAllowed)
1895 {
1896 // Enable green channel, but set blending so nothing will be drawn.
1897 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1898 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1899
1900 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1901 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1902 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1903 }
1904 else
1905 {
1906 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1907 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1909
1910 mMaskStateDirty = false;
1911 }
1912
1913 if (mPolygonOffsetStateDirty)
1914 {
1915 if (mState.polygonOffsetFill)
1916 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001917 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918 if (depthbuffer)
1919 {
1920 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1921 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1922 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1923 }
1924 }
1925 else
1926 {
1927 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1928 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1929 }
1930
1931 mPolygonOffsetStateDirty = false;
1932 }
1933
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001934 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001936 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001937 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001938 FIXME("Sample alpha to coverage is unimplemented.");
1939 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001940
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001941 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1942 if (mState.sampleCoverage)
1943 {
1944 unsigned int mask = 0;
1945 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001946 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001947 float threshold = 0.5f;
1948
1949 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001950 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001951 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001952
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001953 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001954 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001955 threshold += 1.0f;
1956 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001957 }
1958 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001959 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001960
1961 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001962 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001963 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001964 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001965
1966 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001967 }
1968 else
1969 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001970 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001971 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972
1973 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 }
1975
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 if (mDitherStateDirty)
1977 {
1978 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1979
1980 mDitherStateDirty = false;
1981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982}
1983
daniel@transgaming.com83921382011-01-08 05:46:00 +00001984GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001985{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001986 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001988 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001989 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001990 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001991 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001992 }
1993
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001994 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001995}
1996
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001997// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001998GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001999{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002000 IDirect3DDevice9 *device = getDevice();
2001 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002002
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002003 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002004 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00002005 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002006 }
2007
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002008 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009}
2010
2011// Applies the shaders and shader constants to the Direct3D 9 device
2012void Context::applyShaders()
2013{
2014 IDirect3DDevice9 *device = getDevice();
2015 Program *programObject = getCurrentProgram();
2016 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2017 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2018
2019 device->SetVertexShader(vertexShader);
2020 device->SetPixelShader(pixelShader);
2021
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002022 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002023 {
2024 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002025 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002026 }
2027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 programObject->applyUniforms();
2029}
2030
2031// Applies the textures and sampler states to the Direct3D 9 device
2032void Context::applyTextures()
2033{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002034 applyTextures(SAMPLER_PIXEL);
2035
2036 if (mSupportsVertexTexture)
2037 {
2038 applyTextures(SAMPLER_VERTEX);
2039 }
2040}
2041
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002042// For each Direct3D 9 sampler of either the pixel or vertex stage,
2043// looks up the corresponding OpenGL texture image unit and texture type,
2044// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002045void Context::applyTextures(SamplerType type)
2046{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 IDirect3DDevice9 *device = getDevice();
2048 Program *programObject = getCurrentProgram();
2049
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002050 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002051
2052 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002054 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002055 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2056 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2057
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002058 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002060 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002061
2062 Texture *texture = getSamplerTexture(textureUnit, textureType);
2063
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002064 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002065 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002066 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2067
2068 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002069 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002070 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002071 {
2072 GLenum wrapS = texture->getWrapS();
2073 GLenum wrapT = texture->getWrapT();
2074 GLenum minFilter = texture->getMinFilter();
2075 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002077 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2078 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002080 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002081 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2082 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002083 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2084 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002087 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002088 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002089 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002090 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 }
2092 else
2093 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002094 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002095 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002096
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002097 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002098 texture->resetDirty();
2099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002101 else
2102 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002103 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002104 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002105 device->SetTexture(d3dSampler, NULL);
2106 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002107 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110}
2111
2112void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2113{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002114 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002115
2116 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2117 {
2118 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2119 }
2120
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002121 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2122 {
2123 return error(GL_INVALID_OPERATION);
2124 }
2125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002127
2128 if (!renderTarget)
2129 {
2130 return; // Context must be lost, return silently
2131 }
2132
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 IDirect3DDevice9 *device = getDevice();
2134
2135 D3DSURFACE_DESC desc;
2136 renderTarget->GetDesc(&desc);
2137
2138 IDirect3DSurface9 *systemSurface;
2139 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2140
2141 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2142 {
2143 return error(GL_OUT_OF_MEMORY);
2144 }
2145
2146 ASSERT(SUCCEEDED(result));
2147
2148 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2149 {
2150 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2151 }
2152
2153 result = device->GetRenderTargetData(renderTarget, systemSurface);
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 if (FAILED(result))
2156 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 systemSurface->Release();
2158
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002159 switch (result)
2160 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002161 case D3DERR_DRIVERINTERNALERROR:
2162 case D3DERR_DEVICELOST:
2163 return error(GL_OUT_OF_MEMORY);
2164 default:
2165 UNREACHABLE();
2166 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 }
2169
2170 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002171 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2172 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2173 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2174 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2175 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176
2177 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2178
2179 if (FAILED(result))
2180 {
2181 UNREACHABLE();
2182 systemSurface->Release();
2183
2184 return; // No sensible error to generate
2185 }
2186
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002187 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002189 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002190 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002191 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193 for (int j = 0; j < rect.bottom - rect.top; j++)
2194 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002195 if (desc.Format == D3DFMT_A8R8G8B8 &&
2196 format == GL_BGRA_EXT &&
2197 type == GL_UNSIGNED_BYTE)
2198 {
2199 // Fast path for EXT_read_format_bgra, given
2200 // an RGBA source buffer. Note that buffers with no
2201 // alpha go through the slow path below.
2202 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002203 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002204 (rect.right - rect.left) * 4);
2205 continue;
2206 }
2207
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 for (int i = 0; i < rect.right - rect.left; i++)
2209 {
2210 float r;
2211 float g;
2212 float b;
2213 float a;
2214
2215 switch (desc.Format)
2216 {
2217 case D3DFMT_R5G6B5:
2218 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002219 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220
2221 a = 1.0f;
2222 b = (rgb & 0x001F) * (1.0f / 0x001F);
2223 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2224 r = (rgb & 0xF800) * (1.0f / 0xF800);
2225 }
2226 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227 case D3DFMT_A1R5G5B5:
2228 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002229 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230
2231 a = (argb & 0x8000) ? 1.0f : 0.0f;
2232 b = (argb & 0x001F) * (1.0f / 0x001F);
2233 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2234 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2235 }
2236 break;
2237 case D3DFMT_A8R8G8B8:
2238 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002239 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240
2241 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2242 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2243 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2244 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2245 }
2246 break;
2247 case D3DFMT_X8R8G8B8:
2248 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002249 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250
2251 a = 1.0f;
2252 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2253 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2254 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2255 }
2256 break;
2257 case D3DFMT_A2R10G10B10:
2258 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002259 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260
2261 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2262 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2263 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2264 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2265 }
2266 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002267 case D3DFMT_A32B32G32R32F:
2268 {
2269 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002270 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2271 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2272 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2273 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002274 }
2275 break;
2276 case D3DFMT_A16B16G16R16F:
2277 {
2278 // float formats in D3D are stored rgba, rather than the other way round
2279 float abgr[4];
2280
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002281 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002282
2283 a = abgr[3];
2284 b = abgr[2];
2285 g = abgr[1];
2286 r = abgr[0];
2287 }
2288 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 default:
2290 UNIMPLEMENTED(); // FIXME
2291 UNREACHABLE();
2292 }
2293
2294 switch (format)
2295 {
2296 case GL_RGBA:
2297 switch (type)
2298 {
2299 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002300 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2301 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2302 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2303 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 break;
2305 default: UNREACHABLE();
2306 }
2307 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002308 case GL_BGRA_EXT:
2309 switch (type)
2310 {
2311 case GL_UNSIGNED_BYTE:
2312 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2313 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2314 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2315 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2316 break;
2317 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2318 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2319 // this type is packed as follows:
2320 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2321 // --------------------------------------------------------------------------------
2322 // | 4th | 3rd | 2nd | 1st component |
2323 // --------------------------------------------------------------------------------
2324 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2325 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2326 ((unsigned short)(15 * a + 0.5f) << 12)|
2327 ((unsigned short)(15 * r + 0.5f) << 8) |
2328 ((unsigned short)(15 * g + 0.5f) << 4) |
2329 ((unsigned short)(15 * b + 0.5f) << 0);
2330 break;
2331 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2332 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2333 // this type is packed as follows:
2334 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2335 // --------------------------------------------------------------------------------
2336 // | 4th | 3rd | 2nd | 1st component |
2337 // --------------------------------------------------------------------------------
2338 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2339 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2340 ((unsigned short)( a + 0.5f) << 15) |
2341 ((unsigned short)(31 * r + 0.5f) << 10) |
2342 ((unsigned short)(31 * g + 0.5f) << 5) |
2343 ((unsigned short)(31 * b + 0.5f) << 0);
2344 break;
2345 default: UNREACHABLE();
2346 }
2347 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002348 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349 switch (type)
2350 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002351 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002352 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2353 ((unsigned short)(31 * b + 0.5f) << 0) |
2354 ((unsigned short)(63 * g + 0.5f) << 5) |
2355 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002356 break;
2357 default: UNREACHABLE();
2358 }
2359 break;
2360 default: UNREACHABLE();
2361 }
2362 }
2363 }
2364
2365 systemSurface->UnlockRect();
2366
2367 systemSurface->Release();
2368}
2369
2370void Context::clear(GLbitfield mask)
2371{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002372 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002373
2374 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2375 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002376 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002377 }
2378
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002379 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 IDirect3DDevice9 *device = getDevice();
2381 DWORD flags = 0;
2382
2383 if (mask & GL_COLOR_BUFFER_BIT)
2384 {
2385 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002386
2387 if (framebufferObject->getColorbufferType() != GL_NONE)
2388 {
2389 flags |= D3DCLEAR_TARGET;
2390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 }
2392
2393 if (mask & GL_DEPTH_BUFFER_BIT)
2394 {
2395 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002396 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397 {
2398 flags |= D3DCLEAR_ZBUFFER;
2399 }
2400 }
2401
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 GLuint stencilUnmasked = 0x0;
2403
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002404 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002407 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002409 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002410 if (!depthStencil)
2411 {
2412 ERR("Depth stencil pointer unexpectedly null.");
2413 return;
2414 }
2415
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002416 D3DSURFACE_DESC desc;
2417 depthStencil->GetDesc(&desc);
2418
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002419 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002420 stencilUnmasked = (0x1 << stencilSize) - 1;
2421
2422 if (stencilUnmasked != 0x0)
2423 {
2424 flags |= D3DCLEAR_STENCIL;
2425 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427 }
2428
2429 if (mask != 0)
2430 {
2431 return error(GL_INVALID_VALUE);
2432 }
2433
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002434 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2435 {
2436 return;
2437 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002439 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002440 unorm<8>(mState.colorClearValue.red),
2441 unorm<8>(mState.colorClearValue.green),
2442 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002443 float depth = clamp01(mState.depthClearValue);
2444 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445
2446 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2447
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002448 if (!renderTarget)
2449 {
2450 return; // Context must be lost, return silently
2451 }
2452
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 D3DSURFACE_DESC desc;
2454 renderTarget->GetDesc(&desc);
2455
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002456 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
2458 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002459 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002461 !(mState.colorMaskRed && mState.colorMaskGreen &&
2462 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
2464 if (needMaskedColorClear || needMaskedStencilClear)
2465 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002466 // State which is altered in all paths from this point to the clear call is saved.
2467 // State which is altered in only some paths will be flagged dirty in the case that
2468 // that path is taken.
2469 HRESULT hr;
2470 if (mMaskedClearSavedState == NULL)
2471 {
2472 hr = device->BeginStateBlock();
2473 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2474
2475 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2476 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2477 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2478 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2479 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2480 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2481 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2482 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2483 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2484 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2485 device->SetPixelShader(NULL);
2486 device->SetVertexShader(NULL);
2487 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002488 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2489 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2490 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2491 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2492 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2493 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2494 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002495
2496 hr = device->EndStateBlock(&mMaskedClearSavedState);
2497 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2498 }
2499
2500 ASSERT(mMaskedClearSavedState != NULL);
2501
2502 if (mMaskedClearSavedState != NULL)
2503 {
2504 hr = mMaskedClearSavedState->Capture();
2505 ASSERT(SUCCEEDED(hr));
2506 }
2507
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2509 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2510 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2511 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2512 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2513 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2514 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2515 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2516
2517 if (flags & D3DCLEAR_TARGET)
2518 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002519 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 }
2521 else
2522 {
2523 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2524 }
2525
2526 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2527 {
2528 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2529 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2530 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2531 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002532 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002533 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2535 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002536 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 }
2538 else
2539 {
2540 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2541 }
2542
2543 device->SetPixelShader(NULL);
2544 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002545 device->SetFVF(D3DFVF_XYZRHW);
2546 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2547 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2548 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2549 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2550 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2551 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2552 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002554 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2555 quad[0][0] = -0.5f;
2556 quad[0][1] = desc.Height - 0.5f;
2557 quad[0][2] = 0.0f;
2558 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002560 quad[1][0] = desc.Width - 0.5f;
2561 quad[1][1] = desc.Height - 0.5f;
2562 quad[1][2] = 0.0f;
2563 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002565 quad[2][0] = -0.5f;
2566 quad[2][1] = -0.5f;
2567 quad[2][2] = 0.0f;
2568 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002570 quad[3][0] = desc.Width - 0.5f;
2571 quad[3][1] = -0.5f;
2572 quad[3][2] = 0.0f;
2573 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002575 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002576 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
2578 if (flags & D3DCLEAR_ZBUFFER)
2579 {
2580 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2581 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2582 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2583 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002584
2585 if (mMaskedClearSavedState != NULL)
2586 {
2587 mMaskedClearSavedState->Apply();
2588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002590 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591 {
2592 device->Clear(0, NULL, flags, color, depth, stencil);
2593 }
2594}
2595
2596void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2597{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002598 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 {
2600 return error(GL_INVALID_OPERATION);
2601 }
2602
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002603 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 IDirect3DDevice9 *device = getDevice();
2605 D3DPRIMITIVETYPE primitiveType;
2606 int primitiveCount;
2607
2608 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2609 return error(GL_INVALID_ENUM);
2610
2611 if (primitiveCount <= 0)
2612 {
2613 return;
2614 }
2615
2616 if (!applyRenderTarget(false))
2617 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002618 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 }
2620
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002621 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002622
daniel@transgaming.com83921382011-01-08 05:46:00 +00002623 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002624 if (err != GL_NO_ERROR)
2625 {
2626 return error(err);
2627 }
2628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 applyShaders();
2630 applyTextures();
2631
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002632 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002633 {
2634 return error(GL_INVALID_OPERATION);
2635 }
2636
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002637 if (!cullSkipsDraw(mode))
2638 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002639 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002640
2641 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002642
2643 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2644 {
2645 drawClosingLine(first, first + count - 1);
2646 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002647 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648}
2649
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002650void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002652 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 {
2654 return error(GL_INVALID_OPERATION);
2655 }
2656
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002657 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658 {
2659 return error(GL_INVALID_OPERATION);
2660 }
2661
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002662 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663 IDirect3DDevice9 *device = getDevice();
2664 D3DPRIMITIVETYPE primitiveType;
2665 int primitiveCount;
2666
2667 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2668 return error(GL_INVALID_ENUM);
2669
2670 if (primitiveCount <= 0)
2671 {
2672 return;
2673 }
2674
2675 if (!applyRenderTarget(false))
2676 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002677 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678 }
2679
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002680 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002681
2682 TranslatedIndexData indexInfo;
2683 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2684 if (err != GL_NO_ERROR)
2685 {
2686 return error(err);
2687 }
2688
daniel@transgaming.com83921382011-01-08 05:46:00 +00002689 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2690 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002691 if (err != GL_NO_ERROR)
2692 {
2693 return error(err);
2694 }
2695
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 applyShaders();
2697 applyTextures();
2698
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002699 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002700 {
2701 return error(GL_INVALID_OPERATION);
2702 }
2703
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002704 if (!cullSkipsDraw(mode))
2705 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002706 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002707
daniel@transgaming.com83921382011-01-08 05:46:00 +00002708 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002709
2710 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2711 {
2712 drawClosingLine(count, type, indices);
2713 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002714 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715}
2716
2717void Context::finish()
2718{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002719 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 IDirect3DDevice9 *device = getDevice();
2721 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002722 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002724 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2725 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002727 ERR("CreateQuery failed hr=%x\n", result);
2728 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2729 {
2730 return error(GL_OUT_OF_MEMORY);
2731 }
2732 ASSERT(false);
2733 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 }
2735
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002736 IDirect3DStateBlock9 *savedState = NULL;
2737 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2738 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002740 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002742
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002743 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002744 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002745 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002746 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002747 ASSERT(false);
2748 return;
2749 }
2750
2751 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2752 if (FAILED(result))
2753 {
2754 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2755 occlusionQuery->Release();
2756 savedState->Release();
2757 ASSERT(false);
2758 return;
2759 }
2760
2761 // Render something outside the render target
2762 device->SetPixelShader(NULL);
2763 device->SetVertexShader(NULL);
2764 device->SetFVF(D3DFVF_XYZRHW);
2765 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2766 display->startScene();
2767 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2768
2769 result = occlusionQuery->Issue(D3DISSUE_END);
2770 if (FAILED(result))
2771 {
2772 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2773 occlusionQuery->Release();
2774 savedState->Apply();
2775 savedState->Release();
2776 ASSERT(false);
2777 return;
2778 }
2779
2780 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2781 {
2782 // Keep polling, but allow other threads to do something useful first
2783 Sleep(0);
2784 }
2785
2786 occlusionQuery->Release();
2787 savedState->Apply();
2788 savedState->Release();
2789
2790 if (result == D3DERR_DEVICELOST)
2791 {
2792 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002793 }
2794}
2795
2796void Context::flush()
2797{
2798 IDirect3DDevice9 *device = getDevice();
2799 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002800 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002802 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2803 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002805 ERR("CreateQuery failed hr=%x\n", result);
2806 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2807 {
2808 return error(GL_OUT_OF_MEMORY);
2809 }
2810 ASSERT(false);
2811 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 }
2813
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002814 result = eventQuery->Issue(D3DISSUE_END);
2815 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002817 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2818 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002820 return;
2821 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002822
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002823 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2824 eventQuery->Release();
2825
2826 if (result == D3DERR_DEVICELOST)
2827 {
2828 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 }
2830}
2831
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002832void Context::drawClosingLine(unsigned int first, unsigned int last)
2833{
2834 IDirect3DDevice9 *device = getDevice();
2835 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002836 bool succeeded = false;
2837 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002838
2839 if (supports32bitIndices())
2840 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002841 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002842
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002843 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002844 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002845 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2846 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002847
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002848 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2849
2850 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2851 if (data)
2852 {
2853 data[0] = last;
2854 data[1] = first;
2855 mClosingIB->unmap();
2856 offset /= 4;
2857 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002858 }
2859 }
2860 else
2861 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002862 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002863
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002864 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002865 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002866 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2867 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002868
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002869 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2870
2871 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2872 if (data)
2873 {
2874 data[0] = last;
2875 data[1] = first;
2876 mClosingIB->unmap();
2877 offset /= 2;
2878 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002879 }
2880 }
2881
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002882 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002883 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002884 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002885
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002886 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002887 }
2888 else
2889 {
2890 ERR("Could not create an index buffer for closing a line loop.");
2891 error(GL_OUT_OF_MEMORY);
2892 }
2893}
2894
2895void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2896{
2897 unsigned int first = 0;
2898 unsigned int last = 0;
2899
2900 if (mState.elementArrayBuffer.get())
2901 {
2902 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2903 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2904 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2905 }
2906
2907 switch (type)
2908 {
2909 case GL_UNSIGNED_BYTE:
2910 first = static_cast<const GLubyte*>(indices)[0];
2911 last = static_cast<const GLubyte*>(indices)[count - 1];
2912 break;
2913 case GL_UNSIGNED_SHORT:
2914 first = static_cast<const GLushort*>(indices)[0];
2915 last = static_cast<const GLushort*>(indices)[count - 1];
2916 break;
2917 case GL_UNSIGNED_INT:
2918 first = static_cast<const GLuint*>(indices)[0];
2919 last = static_cast<const GLuint*>(indices)[count - 1];
2920 break;
2921 default: UNREACHABLE();
2922 }
2923
2924 drawClosingLine(first, last);
2925}
2926
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927void Context::recordInvalidEnum()
2928{
2929 mInvalidEnum = true;
2930}
2931
2932void Context::recordInvalidValue()
2933{
2934 mInvalidValue = true;
2935}
2936
2937void Context::recordInvalidOperation()
2938{
2939 mInvalidOperation = true;
2940}
2941
2942void Context::recordOutOfMemory()
2943{
2944 mOutOfMemory = true;
2945}
2946
2947void Context::recordInvalidFramebufferOperation()
2948{
2949 mInvalidFramebufferOperation = true;
2950}
2951
2952// Get one of the recorded errors and clear its flag, if any.
2953// [OpenGL ES 2.0.24] section 2.5 page 13.
2954GLenum Context::getError()
2955{
2956 if (mInvalidEnum)
2957 {
2958 mInvalidEnum = false;
2959
2960 return GL_INVALID_ENUM;
2961 }
2962
2963 if (mInvalidValue)
2964 {
2965 mInvalidValue = false;
2966
2967 return GL_INVALID_VALUE;
2968 }
2969
2970 if (mInvalidOperation)
2971 {
2972 mInvalidOperation = false;
2973
2974 return GL_INVALID_OPERATION;
2975 }
2976
2977 if (mOutOfMemory)
2978 {
2979 mOutOfMemory = false;
2980
2981 return GL_OUT_OF_MEMORY;
2982 }
2983
2984 if (mInvalidFramebufferOperation)
2985 {
2986 mInvalidFramebufferOperation = false;
2987
2988 return GL_INVALID_FRAMEBUFFER_OPERATION;
2989 }
2990
2991 return GL_NO_ERROR;
2992}
2993
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002994bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002995{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002996 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002997}
2998
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002999int Context::getMaximumVaryingVectors() const
3000{
3001 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3002}
3003
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003004unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003005{
3006 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3007}
3008
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003009unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003010{
3011 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3012}
3013
daniel@transgaming.com458da142010-11-28 02:03:02 +00003014int Context::getMaximumFragmentUniformVectors() const
3015{
3016 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3017}
3018
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003019int Context::getMaxSupportedSamples() const
3020{
3021 return mMaxSupportedSamples;
3022}
3023
3024int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3025{
3026 if (requested == 0)
3027 {
3028 return requested;
3029 }
3030
3031 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3032 if (itr == mMultiSampleSupport.end())
3033 {
3034 return -1;
3035 }
3036
3037 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3038 {
3039 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3040 {
3041 return i;
3042 }
3043 }
3044
3045 return -1;
3046}
3047
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003048bool Context::supportsEventQueries() const
3049{
3050 return mSupportsEventQueries;
3051}
3052
daniel@transgaming.com01868132010-08-24 19:21:17 +00003053bool Context::supportsCompressedTextures() const
3054{
3055 return mSupportsCompressedTextures;
3056}
3057
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003058bool Context::supportsFloatTextures() const
3059{
3060 return mSupportsFloatTextures;
3061}
3062
3063bool Context::supportsFloatLinearFilter() const
3064{
3065 return mSupportsFloatLinearFilter;
3066}
3067
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003068bool Context::supportsFloatRenderableTextures() const
3069{
3070 return mSupportsFloatRenderableTextures;
3071}
3072
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003073bool Context::supportsHalfFloatTextures() const
3074{
3075 return mSupportsHalfFloatTextures;
3076}
3077
3078bool Context::supportsHalfFloatLinearFilter() const
3079{
3080 return mSupportsHalfFloatLinearFilter;
3081}
3082
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003083bool Context::supportsHalfFloatRenderableTextures() const
3084{
3085 return mSupportsHalfFloatRenderableTextures;
3086}
3087
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003088int Context::getMaximumRenderbufferDimension() const
3089{
3090 return mMaxRenderbufferDimension;
3091}
3092
3093int Context::getMaximumTextureDimension() const
3094{
3095 return mMaxTextureDimension;
3096}
3097
3098int Context::getMaximumCubeTextureDimension() const
3099{
3100 return mMaxCubeTextureDimension;
3101}
3102
3103int Context::getMaximumTextureLevel() const
3104{
3105 return mMaxTextureLevel;
3106}
3107
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003108bool Context::supportsLuminanceTextures() const
3109{
3110 return mSupportsLuminanceTextures;
3111}
3112
3113bool Context::supportsLuminanceAlphaTextures() const
3114{
3115 return mSupportsLuminanceAlphaTextures;
3116}
3117
daniel@transgaming.com83921382011-01-08 05:46:00 +00003118bool Context::supports32bitIndices() const
3119{
3120 return mSupports32bitIndices;
3121}
3122
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003123bool Context::supportsNonPower2Texture() const
3124{
3125 return mSupportsNonPower2Texture;
3126}
3127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128void Context::detachBuffer(GLuint buffer)
3129{
3130 // [OpenGL ES 2.0.24] section 2.9 page 22:
3131 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3132 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3133
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003134 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003135 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003136 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003137 }
3138
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003139 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003141 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003142 }
3143
3144 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3145 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003146 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003147 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003148 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003149 }
3150 }
3151}
3152
3153void Context::detachTexture(GLuint texture)
3154{
3155 // [OpenGL ES 2.0.24] section 3.8 page 84:
3156 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3157 // rebound to texture object zero
3158
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003159 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003160 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003161 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003162 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003163 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003164 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003165 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003166 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167 }
3168 }
3169
3170 // [OpenGL ES 2.0.24] section 4.4 page 112:
3171 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3172 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3173 // image was attached in the currently bound framebuffer.
3174
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003175 Framebuffer *readFramebuffer = getReadFramebuffer();
3176 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003178 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003179 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003180 readFramebuffer->detachTexture(texture);
3181 }
3182
3183 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3184 {
3185 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003186 }
3187}
3188
3189void Context::detachFramebuffer(GLuint framebuffer)
3190{
3191 // [OpenGL ES 2.0.24] section 4.4 page 107:
3192 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3193 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3194
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003195 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003196 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003197 bindReadFramebuffer(0);
3198 }
3199
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003200 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003201 {
3202 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003203 }
3204}
3205
3206void Context::detachRenderbuffer(GLuint renderbuffer)
3207{
3208 // [OpenGL ES 2.0.24] section 4.4 page 109:
3209 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3210 // had been executed with the target RENDERBUFFER and name of zero.
3211
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003212 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003213 {
3214 bindRenderbuffer(0);
3215 }
3216
3217 // [OpenGL ES 2.0.24] section 4.4 page 111:
3218 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3219 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3220 // point to which this image was attached in the currently bound framebuffer.
3221
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003222 Framebuffer *readFramebuffer = getReadFramebuffer();
3223 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003224
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003225 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003227 readFramebuffer->detachRenderbuffer(renderbuffer);
3228 }
3229
3230 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3231 {
3232 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233 }
3234}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003235
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003236Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003237{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003238 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003239
3240 if (t == NULL)
3241 {
3242 static const GLubyte color[] = { 0, 0, 0, 255 };
3243
3244 switch (type)
3245 {
3246 default:
3247 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003248 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003249
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003250 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003251 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003252 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003253 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003254 t = incomplete2d;
3255 }
3256 break;
3257
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003258 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003259 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003260 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003261
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003262 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3263 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3264 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3265 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3266 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3267 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003268
3269 t = incompleteCube;
3270 }
3271 break;
3272 }
3273
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003274 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003275 }
3276
3277 return t;
3278}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003279
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003280bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003281{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003282 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003283}
3284
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003285bool Context::isTriangleMode(GLenum drawMode)
3286{
3287 switch (drawMode)
3288 {
3289 case GL_TRIANGLES:
3290 case GL_TRIANGLE_FAN:
3291 case GL_TRIANGLE_STRIP:
3292 return true;
3293 case GL_POINTS:
3294 case GL_LINES:
3295 case GL_LINE_LOOP:
3296 case GL_LINE_STRIP:
3297 return false;
3298 default: UNREACHABLE();
3299 }
3300
3301 return false;
3302}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003303
3304void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3305{
3306 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3307
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003308 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3309 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3310 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3311 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003312
daniel@transgaming.com83921382011-01-08 05:46:00 +00003313 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003314}
3315
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003316void Context::initExtensionString()
3317{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003318 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003319 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3320 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003321 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003322 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003323 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003324
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003325 if (supportsEventQueries())
3326 {
3327 mExtensionString += "GL_NV_fence ";
3328 }
3329
daniel@transgaming.com01868132010-08-24 19:21:17 +00003330 if (supportsCompressedTextures())
3331 {
3332 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3333 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003334
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003335 if (supportsFloatTextures())
3336 {
3337 mExtensionString += "GL_OES_texture_float ";
3338 }
3339
3340 if (supportsHalfFloatTextures())
3341 {
3342 mExtensionString += "GL_OES_texture_half_float ";
3343 }
3344
3345 if (supportsFloatLinearFilter())
3346 {
3347 mExtensionString += "GL_OES_texture_float_linear ";
3348 }
3349
3350 if (supportsHalfFloatLinearFilter())
3351 {
3352 mExtensionString += "GL_OES_texture_half_float_linear ";
3353 }
3354
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003355 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003356 {
3357 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3358 }
3359
daniel@transgaming.com83921382011-01-08 05:46:00 +00003360 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003361 {
3362 mExtensionString += "GL_OES_element_index_uint ";
3363 }
3364
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003365 if (supportsNonPower2Texture())
3366 {
3367 mExtensionString += "GL_OES_texture_npot ";
3368 }
3369
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003370 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3371 if (end != std::string::npos)
3372 {
3373 mExtensionString.resize(end+1);
3374 }
3375}
3376
3377const char *Context::getExtensionString() const
3378{
3379 return mExtensionString.c_str();
3380}
3381
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003382void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3383 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3384 GLbitfield mask)
3385{
3386 IDirect3DDevice9 *device = getDevice();
3387
3388 Framebuffer *readFramebuffer = getReadFramebuffer();
3389 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3390
3391 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3392 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3393 {
3394 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3395 }
3396
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003397 if (drawFramebuffer->getSamples() != 0)
3398 {
3399 return error(GL_INVALID_OPERATION);
3400 }
3401
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003402 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3403 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3404 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3405 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3406
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003407 RECT sourceRect;
3408 RECT destRect;
3409
3410 if (srcX0 < srcX1)
3411 {
3412 sourceRect.left = srcX0;
3413 sourceRect.right = srcX1;
3414 destRect.left = dstX0;
3415 destRect.right = dstX1;
3416 }
3417 else
3418 {
3419 sourceRect.left = srcX1;
3420 destRect.left = dstX1;
3421 sourceRect.right = srcX0;
3422 destRect.right = dstX0;
3423 }
3424
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003425 if (srcY0 < srcY1)
3426 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003427 sourceRect.top = readBufferHeight - srcY1;
3428 destRect.top = drawBufferHeight - dstY1;
3429 sourceRect.bottom = readBufferHeight - srcY0;
3430 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003431 }
3432 else
3433 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003434 sourceRect.top = readBufferHeight - srcY0;
3435 destRect.top = drawBufferHeight - dstY0;
3436 sourceRect.bottom = readBufferHeight - srcY1;
3437 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003438 }
3439
3440 RECT sourceScissoredRect = sourceRect;
3441 RECT destScissoredRect = destRect;
3442
3443 if (mState.scissorTest)
3444 {
3445 // Only write to parts of the destination framebuffer which pass the scissor test
3446 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3447 // rect will be checked against scissorY, rather than the bottom.
3448 if (destRect.left < mState.scissorX)
3449 {
3450 int xDiff = mState.scissorX - destRect.left;
3451 destScissoredRect.left = mState.scissorX;
3452 sourceScissoredRect.left += xDiff;
3453 }
3454
3455 if (destRect.right > mState.scissorX + mState.scissorWidth)
3456 {
3457 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3458 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3459 sourceScissoredRect.right -= xDiff;
3460 }
3461
3462 if (destRect.top < mState.scissorY)
3463 {
3464 int yDiff = mState.scissorY - destRect.top;
3465 destScissoredRect.top = mState.scissorY;
3466 sourceScissoredRect.top += yDiff;
3467 }
3468
3469 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3470 {
3471 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3472 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3473 sourceScissoredRect.bottom -= yDiff;
3474 }
3475 }
3476
3477 bool blitRenderTarget = false;
3478 bool blitDepthStencil = false;
3479
3480 RECT sourceTrimmedRect = sourceScissoredRect;
3481 RECT destTrimmedRect = destScissoredRect;
3482
3483 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3484 // the actual draw and read surfaces.
3485 if (sourceTrimmedRect.left < 0)
3486 {
3487 int xDiff = 0 - sourceTrimmedRect.left;
3488 sourceTrimmedRect.left = 0;
3489 destTrimmedRect.left += xDiff;
3490 }
3491
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003492 if (sourceTrimmedRect.right > readBufferWidth)
3493 {
3494 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3495 sourceTrimmedRect.right = readBufferWidth;
3496 destTrimmedRect.right -= xDiff;
3497 }
3498
3499 if (sourceTrimmedRect.top < 0)
3500 {
3501 int yDiff = 0 - sourceTrimmedRect.top;
3502 sourceTrimmedRect.top = 0;
3503 destTrimmedRect.top += yDiff;
3504 }
3505
3506 if (sourceTrimmedRect.bottom > readBufferHeight)
3507 {
3508 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3509 sourceTrimmedRect.bottom = readBufferHeight;
3510 destTrimmedRect.bottom -= yDiff;
3511 }
3512
3513 if (destTrimmedRect.left < 0)
3514 {
3515 int xDiff = 0 - destTrimmedRect.left;
3516 destTrimmedRect.left = 0;
3517 sourceTrimmedRect.left += xDiff;
3518 }
3519
3520 if (destTrimmedRect.right > drawBufferWidth)
3521 {
3522 int xDiff = destTrimmedRect.right - drawBufferWidth;
3523 destTrimmedRect.right = drawBufferWidth;
3524 sourceTrimmedRect.right -= xDiff;
3525 }
3526
3527 if (destTrimmedRect.top < 0)
3528 {
3529 int yDiff = 0 - destTrimmedRect.top;
3530 destTrimmedRect.top = 0;
3531 sourceTrimmedRect.top += yDiff;
3532 }
3533
3534 if (destTrimmedRect.bottom > drawBufferHeight)
3535 {
3536 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3537 destTrimmedRect.bottom = drawBufferHeight;
3538 sourceTrimmedRect.bottom -= yDiff;
3539 }
3540
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003541 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003542 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3543 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3544 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3545 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003546 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3547 {
3548 partialBufferCopy = true;
3549 }
3550
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003551 if (mask & GL_COLOR_BUFFER_BIT)
3552 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003553 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3554 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3555 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3556 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3557 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003558 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3559 {
3560 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3561 return error(GL_INVALID_OPERATION);
3562 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003563
3564 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3565 {
3566 return error(GL_INVALID_OPERATION);
3567 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003568
3569 blitRenderTarget = true;
3570
3571 }
3572
3573 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3574 {
3575 DepthStencilbuffer *readDSBuffer = NULL;
3576 DepthStencilbuffer *drawDSBuffer = NULL;
3577
3578 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3579 // both a depth and stencil buffer, it will be the same buffer.
3580
3581 if (mask & GL_DEPTH_BUFFER_BIT)
3582 {
3583 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3584 {
3585 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3586 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3587 {
3588 return error(GL_INVALID_OPERATION);
3589 }
3590
3591 blitDepthStencil = true;
3592 readDSBuffer = readFramebuffer->getDepthbuffer();
3593 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3594 }
3595 }
3596
3597 if (mask & GL_STENCIL_BUFFER_BIT)
3598 {
3599 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3600 {
3601 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3602 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3603 {
3604 return error(GL_INVALID_OPERATION);
3605 }
3606
3607 blitDepthStencil = true;
3608 readDSBuffer = readFramebuffer->getStencilbuffer();
3609 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3610 }
3611 }
3612
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003613 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003614 {
3615 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3616 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3617 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003618
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003619 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3620 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003621 {
3622 return error(GL_INVALID_OPERATION);
3623 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003624 }
3625
3626 if (blitRenderTarget || blitDepthStencil)
3627 {
3628 egl::Display *display = getDisplay();
3629 display->endScene();
3630
3631 if (blitRenderTarget)
3632 {
3633 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3634 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3635
3636 if (FAILED(result))
3637 {
3638 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3639 return;
3640 }
3641 }
3642
3643 if (blitDepthStencil)
3644 {
3645 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3646
3647 if (FAILED(result))
3648 {
3649 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3650 return;
3651 }
3652 }
3653 }
3654}
3655
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003656VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3657{
3658 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3659 {
3660 mVertexDeclCache[i].vertexDeclaration = NULL;
3661 mVertexDeclCache[i].lruCount = 0;
3662 }
3663}
3664
3665VertexDeclarationCache::~VertexDeclarationCache()
3666{
3667 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3668 {
3669 if (mVertexDeclCache[i].vertexDeclaration)
3670 {
3671 mVertexDeclCache[i].vertexDeclaration->Release();
3672 }
3673 }
3674}
3675
3676GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3677{
3678 IDirect3DDevice9 *device = getDevice();
3679
3680 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3681 D3DVERTEXELEMENT9 *element = &elements[0];
3682
3683 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3684 {
3685 if (attributes[i].active)
3686 {
3687 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3688
3689 element->Stream = i;
3690 element->Offset = 0;
3691 element->Type = attributes[i].type;
3692 element->Method = D3DDECLMETHOD_DEFAULT;
3693 element->Usage = D3DDECLUSAGE_TEXCOORD;
3694 element->UsageIndex = program->getSemanticIndex(i);
3695 element++;
3696 }
3697 }
3698
3699 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3700 *(element++) = end;
3701
3702 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3703 {
3704 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3705 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3706 {
3707 entry->lruCount = ++mMaxLru;
3708 device->SetVertexDeclaration(entry->vertexDeclaration);
3709
3710 return GL_NO_ERROR;
3711 }
3712 }
3713
3714 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3715
3716 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3717 {
3718 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3719 {
3720 lastCache = &mVertexDeclCache[i];
3721 }
3722 }
3723
3724 if (lastCache->vertexDeclaration != NULL)
3725 {
3726 lastCache->vertexDeclaration->Release();
3727 lastCache->vertexDeclaration = NULL;
3728 }
3729
3730 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3731 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3732 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3733 lastCache->lruCount = ++mMaxLru;
3734
3735 return GL_NO_ERROR;
3736}
3737
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003738}
3739
3740extern "C"
3741{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003742gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003743{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003744 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003745}
3746
3747void glDestroyContext(gl::Context *context)
3748{
3749 delete context;
3750
3751 if (context == gl::getContext())
3752 {
3753 gl::makeCurrent(NULL, NULL, NULL);
3754 }
3755}
3756
3757void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3758{
3759 gl::makeCurrent(context, display, surface);
3760}
3761
3762gl::Context *glGetCurrentContext()
3763{
3764 return gl::getContext();
3765}
3766}