blob: b68f5f19c286515a6b5ae6f6dca81d4d8ab89521 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
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"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000089
daniel@transgaming.com428d1582010-05-04 03:35:25 +000090 mState.viewportX = 0;
91 mState.viewportY = 0;
92 mState.viewportWidth = config->mDisplayMode.Width;
93 mState.viewportHeight = config->mDisplayMode.Height;
94 mState.zNear = 0.0f;
95 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
daniel@transgaming.com428d1582010-05-04 03:35:25 +000097 mState.scissorX = 0;
98 mState.scissorY = 0;
99 mState.scissorWidth = config->mDisplayMode.Width;
100 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.colorMaskRed = true;
103 mState.colorMaskGreen = true;
104 mState.colorMaskBlue = true;
105 mState.colorMaskAlpha = true;
106 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000108 if (shareContext != NULL)
109 {
110 mResourceManager = shareContext->mResourceManager;
111 mResourceManager->addRef();
112 }
113 else
114 {
115 mResourceManager = new ResourceManager();
116 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 // [OpenGL ES 2.0.24] section 3.7 page 83:
119 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
120 // and cube map texture state vectors respectively associated with them.
121 // In order that access to these initial textures not be lost, they are treated as texture
122 // objects all of whose names are 0.
123
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000124 mTexture2DZero = new Texture2D(0);
125 mTextureCubeMapZero = new TextureCubeMap(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
127 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000128 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000130 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000131 bindArrayBuffer(0);
132 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 bindTextureCubeMap(0);
134 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000135 bindReadFramebuffer(0);
136 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 bindRenderbuffer(0);
138
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000139 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000141 mIncompleteTextures[type] = NULL;
142 }
143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.packAlignment = 4;
147 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 mBufferBackEnd = NULL;
150 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000151 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000152 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000159
160 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000161
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsCompressedTextures = false;
163 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
198 {
199 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
206 {
207 delete mIncompleteTextures[type];
208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
217 mState.texture2D.set(NULL);
218 mState.textureCubeMap.set(NULL);
219 mState.renderbuffer.set(NULL);
220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221 delete mTexture2DZero;
222 delete mTextureCubeMapZero;
223
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000224 delete mBufferBackEnd;
225 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000226 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000227 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000228
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000229 if (mMaskedClearSavedState)
230 {
231 mMaskedClearSavedState->Release();
232 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000233
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000234 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
237void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
238{
239 IDirect3DDevice9 *device = display->getDevice();
240
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000241 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000243 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000244
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000245 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000246 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000247 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000248 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000249
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000250 const D3DFORMAT renderBufferFormats[] =
251 {
252 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000253 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000254 D3DFMT_R5G6B5,
255 D3DFMT_D24S8
256 };
257
258 int max = 0;
259 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
260 {
261 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
262 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
263 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
264
265 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
266 {
267 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
268 {
269 max = j;
270 }
271 }
272 }
273
274 mMaxSupportedSamples = max;
275
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000276 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000277 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com0a337e92010-08-28 17:38:27 +0000278 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter);
279 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000280
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000281 initExtensionString();
282
283 mState.viewportX = 0;
284 mState.viewportY = 0;
285 mState.viewportWidth = surface->getWidth();
286 mState.viewportHeight = surface->getHeight();
287
288 mState.scissorX = 0;
289 mState.scissorY = 0;
290 mState.scissorWidth = surface->getWidth();
291 mState.scissorHeight = surface->getHeight();
292
293 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000294 }
295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
297 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000298 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000301 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000302 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
304 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
306 defaultRenderTarget->Release();
307
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000308 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000310 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000312
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000313 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000314
315 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316}
317
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000318// 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 +0000319void Context::markAllStateDirty()
320{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000321 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000322 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000323 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000324
325 mClearStateDirty = true;
326 mCullStateDirty = true;
327 mDepthStateDirty = true;
328 mMaskStateDirty = true;
329 mBlendStateDirty = true;
330 mStencilStateDirty = true;
331 mPolygonOffsetStateDirty = true;
332 mScissorStateDirty = true;
333 mSampleStateDirty = true;
334 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000335 mFrontFaceDirty = true;
336
337 if (mBufferBackEnd != NULL)
338 {
339 mBufferBackEnd->invalidate();
340 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000341}
342
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343void Context::setClearColor(float red, float green, float blue, float alpha)
344{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000345 mState.colorClearValue.red = red;
346 mState.colorClearValue.green = green;
347 mState.colorClearValue.blue = blue;
348 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349}
350
351void Context::setClearDepth(float depth)
352{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000353 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354}
355
356void Context::setClearStencil(int stencil)
357{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000358 mState.stencilClearValue = stencil;
359}
360
361void Context::setCullFace(bool enabled)
362{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000363 if (mState.cullFace != enabled)
364 {
365 mState.cullFace = enabled;
366 mCullStateDirty = true;
367 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000368}
369
370bool Context::isCullFaceEnabled() const
371{
372 return mState.cullFace;
373}
374
375void Context::setCullMode(GLenum mode)
376{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000377 if (mState.cullMode != mode)
378 {
379 mState.cullMode = mode;
380 mCullStateDirty = true;
381 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000382}
383
384void Context::setFrontFace(GLenum front)
385{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000386 if (mState.frontFace != front)
387 {
388 mState.frontFace = front;
389 mFrontFaceDirty = true;
390 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000391}
392
393void Context::setDepthTest(bool enabled)
394{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000395 if (mState.depthTest != enabled)
396 {
397 mState.depthTest = enabled;
398 mDepthStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402bool Context::isDepthTestEnabled() const
403{
404 return mState.depthTest;
405}
406
407void Context::setDepthFunc(GLenum depthFunc)
408{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000409 if (mState.depthFunc != depthFunc)
410 {
411 mState.depthFunc = depthFunc;
412 mDepthStateDirty = true;
413 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000414}
415
416void Context::setDepthRange(float zNear, float zFar)
417{
418 mState.zNear = zNear;
419 mState.zFar = zFar;
420}
421
422void Context::setBlend(bool enabled)
423{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000424 if (mState.blend != enabled)
425 {
426 mState.blend = enabled;
427 mBlendStateDirty = true;
428 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429}
430
431bool Context::isBlendEnabled() const
432{
433 return mState.blend;
434}
435
436void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
437{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000438 if (mState.sourceBlendRGB != sourceRGB ||
439 mState.sourceBlendAlpha != sourceAlpha ||
440 mState.destBlendRGB != destRGB ||
441 mState.destBlendAlpha != destAlpha)
442 {
443 mState.sourceBlendRGB = sourceRGB;
444 mState.destBlendRGB = destRGB;
445 mState.sourceBlendAlpha = sourceAlpha;
446 mState.destBlendAlpha = destAlpha;
447 mBlendStateDirty = true;
448 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000449}
450
451void Context::setBlendColor(float red, float green, float blue, float alpha)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.blendColor.red != red ||
454 mState.blendColor.green != green ||
455 mState.blendColor.blue != blue ||
456 mState.blendColor.alpha != alpha)
457 {
458 mState.blendColor.red = red;
459 mState.blendColor.green = green;
460 mState.blendColor.blue = blue;
461 mState.blendColor.alpha = alpha;
462 mBlendStateDirty = true;
463 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000464}
465
466void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.blendEquationRGB != rgbEquation ||
469 mState.blendEquationAlpha != alphaEquation)
470 {
471 mState.blendEquationRGB = rgbEquation;
472 mState.blendEquationAlpha = alphaEquation;
473 mBlendStateDirty = true;
474 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000475}
476
477void Context::setStencilTest(bool enabled)
478{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000479 if (mState.stencilTest != enabled)
480 {
481 mState.stencilTest = enabled;
482 mStencilStateDirty = true;
483 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000484}
485
486bool Context::isStencilTestEnabled() const
487{
488 return mState.stencilTest;
489}
490
491void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
492{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000493 if (mState.stencilFunc != stencilFunc ||
494 mState.stencilRef != stencilRef ||
495 mState.stencilMask != stencilMask)
496 {
497 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000498 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 mState.stencilMask = stencilMask;
500 mStencilStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
505{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 if (mState.stencilBackFunc != stencilBackFunc ||
507 mState.stencilBackRef != stencilBackRef ||
508 mState.stencilBackMask != stencilBackMask)
509 {
510 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000511 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 mState.stencilBackMask = stencilBackMask;
513 mStencilStateDirty = true;
514 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000515}
516
517void Context::setStencilWritemask(GLuint stencilWritemask)
518{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000519 if (mState.stencilWritemask != stencilWritemask)
520 {
521 mState.stencilWritemask = stencilWritemask;
522 mStencilStateDirty = true;
523 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000524}
525
526void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
527{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000528 if (mState.stencilBackWritemask != stencilBackWritemask)
529 {
530 mState.stencilBackWritemask = stencilBackWritemask;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilFail != stencilFail ||
538 mState.stencilPassDepthFail != stencilPassDepthFail ||
539 mState.stencilPassDepthPass != stencilPassDepthPass)
540 {
541 mState.stencilFail = stencilFail;
542 mState.stencilPassDepthFail = stencilPassDepthFail;
543 mState.stencilPassDepthPass = stencilPassDepthPass;
544 mStencilStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
549{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 if (mState.stencilBackFail != stencilBackFail ||
551 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
552 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
553 {
554 mState.stencilBackFail = stencilBackFail;
555 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
556 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
557 mStencilStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561void Context::setPolygonOffsetFill(bool enabled)
562{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000563 if (mState.polygonOffsetFill != enabled)
564 {
565 mState.polygonOffsetFill = enabled;
566 mPolygonOffsetStateDirty = true;
567 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000568}
569
570bool Context::isPolygonOffsetFillEnabled() const
571{
572 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000573
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
577{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000578 if (mState.polygonOffsetFactor != factor ||
579 mState.polygonOffsetUnits != units)
580 {
581 mState.polygonOffsetFactor = factor;
582 mState.polygonOffsetUnits = units;
583 mPolygonOffsetStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587void Context::setSampleAlphaToCoverage(bool enabled)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.sampleAlphaToCoverage != enabled)
590 {
591 mState.sampleAlphaToCoverage = enabled;
592 mSampleStateDirty = true;
593 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000594}
595
596bool Context::isSampleAlphaToCoverageEnabled() const
597{
598 return mState.sampleAlphaToCoverage;
599}
600
601void Context::setSampleCoverage(bool enabled)
602{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000603 if (mState.sampleCoverage != enabled)
604 {
605 mState.sampleCoverage = enabled;
606 mSampleStateDirty = true;
607 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000608}
609
610bool Context::isSampleCoverageEnabled() const
611{
612 return mState.sampleCoverage;
613}
614
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000615void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000616{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617 if (mState.sampleCoverageValue != value ||
618 mState.sampleCoverageInvert != invert)
619 {
620 mState.sampleCoverageValue = value;
621 mState.sampleCoverageInvert = invert;
622 mSampleStateDirty = true;
623 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000624}
625
626void Context::setScissorTest(bool enabled)
627{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000628 if (mState.scissorTest != enabled)
629 {
630 mState.scissorTest = enabled;
631 mScissorStateDirty = true;
632 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000633}
634
635bool Context::isScissorTestEnabled() const
636{
637 return mState.scissorTest;
638}
639
640void Context::setDither(bool enabled)
641{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000642 if (mState.dither != enabled)
643 {
644 mState.dither = enabled;
645 mDitherStateDirty = true;
646 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000647}
648
649bool Context::isDitherEnabled() const
650{
651 return mState.dither;
652}
653
654void Context::setLineWidth(GLfloat width)
655{
656 mState.lineWidth = width;
657}
658
659void Context::setGenerateMipmapHint(GLenum hint)
660{
661 mState.generateMipmapHint = hint;
662}
663
664void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
665{
666 mState.viewportX = x;
667 mState.viewportY = y;
668 mState.viewportWidth = width;
669 mState.viewportHeight = height;
670}
671
672void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
673{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000674 if (mState.scissorX != x || mState.scissorY != y ||
675 mState.scissorWidth != width || mState.scissorHeight != height)
676 {
677 mState.scissorX = x;
678 mState.scissorY = y;
679 mState.scissorWidth = width;
680 mState.scissorHeight = height;
681 mScissorStateDirty = true;
682 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000683}
684
685void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
686{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000687 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
688 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
689 {
690 mState.colorMaskRed = red;
691 mState.colorMaskGreen = green;
692 mState.colorMaskBlue = blue;
693 mState.colorMaskAlpha = alpha;
694 mMaskStateDirty = true;
695 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000696}
697
698void Context::setDepthMask(bool mask)
699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.depthMask != mask)
701 {
702 mState.depthMask = mask;
703 mMaskStateDirty = true;
704 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000705}
706
707void Context::setActiveSampler(int active)
708{
709 mState.activeSampler = active;
710}
711
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000712GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000713{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000714 return mState.readFramebuffer;
715}
716
717GLuint Context::getDrawFramebufferHandle() const
718{
719 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000720}
721
722GLuint Context::getRenderbufferHandle() const
723{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000724 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000725}
726
727GLuint Context::getArrayBufferHandle() const
728{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000729 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
733{
734 mState.vertexAttribute[attribNum].mEnabled = enabled;
735}
736
737const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
738{
739 return mState.vertexAttribute[attribNum];
740}
741
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000742void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000743 GLsizei stride, const void *pointer)
744{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000745 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746 mState.vertexAttribute[attribNum].mSize = size;
747 mState.vertexAttribute[attribNum].mType = type;
748 mState.vertexAttribute[attribNum].mNormalized = normalized;
749 mState.vertexAttribute[attribNum].mStride = stride;
750 mState.vertexAttribute[attribNum].mPointer = pointer;
751}
752
753const void *Context::getVertexAttribPointer(unsigned int attribNum) const
754{
755 return mState.vertexAttribute[attribNum].mPointer;
756}
757
758// returns entire set of attributes as a block
759const AttributeState *Context::getVertexAttribBlock()
760{
761 return mState.vertexAttribute;
762}
763
764void Context::setPackAlignment(GLint alignment)
765{
766 mState.packAlignment = alignment;
767}
768
769GLint Context::getPackAlignment() const
770{
771 return mState.packAlignment;
772}
773
774void Context::setUnpackAlignment(GLint alignment)
775{
776 mState.unpackAlignment = alignment;
777}
778
779GLint Context::getUnpackAlignment() const
780{
781 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000782}
783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784GLuint Context::createBuffer()
785{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000786 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787}
788
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789GLuint Context::createProgram()
790{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000791 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000794GLuint Context::createShader(GLenum type)
795{
796 return mResourceManager->createShader(type);
797}
798
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799GLuint Context::createTexture()
800{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000801 return mResourceManager->createTexture();
802}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804GLuint Context::createRenderbuffer()
805{
806 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
809// Returns an unused framebuffer name
810GLuint Context::createFramebuffer()
811{
812 unsigned int handle = 1;
813
814 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
815 {
816 handle++;
817 }
818
819 mFramebufferMap[handle] = NULL;
820
821 return handle;
822}
823
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000824GLuint Context::createFence()
825{
826 unsigned int handle = 0;
827
828 while (mFenceMap.find(handle) != mFenceMap.end())
829 {
830 handle++;
831 }
832
833 mFenceMap[handle] = new Fence;
834
835 return handle;
836}
837
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838void Context::deleteBuffer(GLuint buffer)
839{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000840 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841 {
842 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000844
845 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
848void Context::deleteShader(GLuint shader)
849{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853void Context::deleteProgram(GLuint program)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858void Context::deleteTexture(GLuint texture)
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861 {
862 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864
865 mResourceManager->deleteTexture(texture);
866}
867
868void Context::deleteRenderbuffer(GLuint renderbuffer)
869{
870 if (mResourceManager->getRenderbuffer(renderbuffer))
871 {
872 detachRenderbuffer(renderbuffer);
873 }
874
875 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876}
877
878void Context::deleteFramebuffer(GLuint framebuffer)
879{
880 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
881
882 if (framebufferObject != mFramebufferMap.end())
883 {
884 detachFramebuffer(framebuffer);
885
886 delete framebufferObject->second;
887 mFramebufferMap.erase(framebufferObject);
888 }
889}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000890
891void Context::deleteFence(GLuint fence)
892{
893 FenceMap::iterator fenceObject = mFenceMap.find(fence);
894
895 if (fenceObject != mFenceMap.end())
896 {
897 delete fenceObject->second;
898 mFenceMap.erase(fenceObject);
899 }
900}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000902Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000904 return mResourceManager->getBuffer(handle);
905}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907Shader *Context::getShader(GLuint handle)
908{
909 return mResourceManager->getShader(handle);
910}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912Program *Context::getProgram(GLuint handle)
913{
914 return mResourceManager->getProgram(handle);
915}
916
917Texture *Context::getTexture(GLuint handle)
918{
919 return mResourceManager->getTexture(handle);
920}
921
922Renderbuffer *Context::getRenderbuffer(GLuint handle)
923{
924 return mResourceManager->getRenderbuffer(handle);
925}
926
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000927Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000929 return getFramebuffer(mState.readFramebuffer);
930}
931
932Framebuffer *Context::getDrawFramebuffer()
933{
934 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935}
936
937void Context::bindArrayBuffer(unsigned int buffer)
938{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000939 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000941 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
944void Context::bindElementArrayBuffer(unsigned int buffer)
945{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000946 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000948 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindTexture2D(GLuint texture)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::bindTextureCubeMap(GLuint texture)
961{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000964 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000966 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967}
968
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000969void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
971 if (!getFramebuffer(framebuffer))
972 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000973 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974 }
975
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000976 mState.readFramebuffer = framebuffer;
977}
978
979void Context::bindDrawFramebuffer(GLuint framebuffer)
980{
981 if (!getFramebuffer(framebuffer))
982 {
983 mFramebufferMap[framebuffer] = new Framebuffer();
984 }
985
986 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
989void Context::bindRenderbuffer(GLuint renderbuffer)
990{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000991 mResourceManager->checkRenderbufferAllocation(renderbuffer);
992
993 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996void Context::useProgram(GLuint program)
997{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000998 GLuint priorProgram = mState.currentProgram;
999 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001000
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001001 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001003 Program *newProgram = mResourceManager->getProgram(program);
1004 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1005
1006 if (newProgram)
1007 {
1008 newProgram->addRef();
1009 }
1010
1011 if (oldProgram)
1012 {
1013 oldProgram->release();
1014 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018void Context::setFramebufferZero(Framebuffer *buffer)
1019{
1020 delete mFramebufferMap[0];
1021 mFramebufferMap[0] = buffer;
1022}
1023
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001024void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001026 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1027 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030Framebuffer *Context::getFramebuffer(unsigned int handle)
1031{
1032 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001033
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034 if (framebuffer == mFramebufferMap.end())
1035 {
1036 return NULL;
1037 }
1038 else
1039 {
1040 return framebuffer->second;
1041 }
1042}
1043
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001044Fence *Context::getFence(unsigned int handle)
1045{
1046 FenceMap::iterator fence = mFenceMap.find(handle);
1047
1048 if (fence == mFenceMap.end())
1049 {
1050 return NULL;
1051 }
1052 else
1053 {
1054 return fence->second;
1055 }
1056}
1057
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058Buffer *Context::getArrayBuffer()
1059{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001060 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
1063Buffer *Context::getElementArrayBuffer()
1064{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001065 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066}
1067
1068Program *Context::getCurrentProgram()
1069{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001070 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073Texture2D *Context::getTexture2D()
1074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076 {
1077 return mTexture2DZero;
1078 }
1079
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001080 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
1083TextureCubeMap *Context::getTextureCubeMap()
1084{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086 {
1087 return mTextureCubeMapZero;
1088 }
1089
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001093Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001096
1097 if (texid == 0)
1098 {
1099 switch (type)
1100 {
1101 default: UNREACHABLE();
1102 case SAMPLER_2D: return mTexture2DZero;
1103 case SAMPLER_CUBE: return mTextureCubeMapZero;
1104 }
1105 }
1106
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001107 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108}
1109
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001110bool Context::getBooleanv(GLenum pname, GLboolean *params)
1111{
1112 switch (pname)
1113 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001114 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1115 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1116 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001117 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001118 params[0] = mState.colorMaskRed;
1119 params[1] = mState.colorMaskGreen;
1120 params[2] = mState.colorMaskBlue;
1121 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001123 case GL_CULL_FACE: *params = mState.cullFace;
1124 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1125 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1126 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1127 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1128 case GL_STENCIL_TEST: *params = mState.stencilTest;
1129 case GL_DEPTH_TEST: *params = mState.depthTest;
1130 case GL_BLEND: *params = mState.blend;
1131 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001132 default:
1133 return false;
1134 }
1135
1136 return true;
1137}
1138
1139bool Context::getFloatv(GLenum pname, GLfloat *params)
1140{
1141 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1142 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1143 // GetIntegerv as its native query function. As it would require conversion in any
1144 // case, this should make no difference to the calling application.
1145 switch (pname)
1146 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001147 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1148 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1149 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1150 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1151 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 case GL_ALIASED_LINE_WIDTH_RANGE:
1153 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1154 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1155 break;
1156 case GL_ALIASED_POINT_SIZE_RANGE:
1157 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001158 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 +00001159 break;
1160 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.zNear;
1162 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001163 break;
1164 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001165 params[0] = mState.colorClearValue.red;
1166 params[1] = mState.colorClearValue.green;
1167 params[2] = mState.colorClearValue.blue;
1168 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001170 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.blendColor.red;
1172 params[1] = mState.blendColor.green;
1173 params[2] = mState.blendColor.blue;
1174 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001175 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001176 default:
1177 return false;
1178 }
1179
1180 return true;
1181}
1182
1183bool Context::getIntegerv(GLenum pname, GLint *params)
1184{
1185 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1186 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1187 // GetIntegerv as its native query function. As it would require conversion in any
1188 // case, this should make no difference to the calling application. You may find it in
1189 // Context::getFloatv.
1190 switch (pname)
1191 {
1192 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1193 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1194 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1195 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1196 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1197 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1198 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1199 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001200 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001201 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001202 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1203 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001204 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1205 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1206 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001207 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001208 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1209 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1210 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1211 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1212 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1213 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1214 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1215 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1216 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1217 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1218 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1219 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1220 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1221 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1222 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1223 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1224 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1225 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1226 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1227 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1228 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1229 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1230 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1231 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1232 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1233 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1234 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1235 case GL_SUBPIXEL_BITS: *params = 4; break;
1236 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1237 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001238 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1239 {
1240 if (supportsCompressedTextures())
1241 {
1242 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1243 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1244 *params = 2;
1245 }
1246 else
1247 {
1248 *params = 0;
1249 }
1250 }
1251 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001252 case GL_MAX_SAMPLES_ANGLE:
1253 {
1254 GLsizei maxSamples = getMaxSupportedSamples();
1255 if (maxSamples != 0)
1256 {
1257 *params = maxSamples;
1258 }
1259 else
1260 {
1261 return false;
1262 }
1263
1264 break;
1265 }
1266 case GL_SAMPLE_BUFFERS:
1267 case GL_SAMPLES:
1268 {
1269 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1270 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1271 {
1272 switch (pname)
1273 {
1274 case GL_SAMPLE_BUFFERS:
1275 if (framebuffer->getSamples() != 0)
1276 {
1277 *params = 1;
1278 }
1279 else
1280 {
1281 *params = 0;
1282 }
1283 break;
1284 case GL_SAMPLES:
1285 *params = framebuffer->getSamples();
1286 break;
1287 }
1288 }
1289 else
1290 {
1291 *params = 0;
1292 }
1293 }
1294 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001295 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1296 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1297 case GL_MAX_VIEWPORT_DIMS:
1298 {
1299 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1300 params[0] = maxDimension;
1301 params[1] = maxDimension;
1302 }
1303 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001304 case GL_COMPRESSED_TEXTURE_FORMATS:
1305 {
1306 if (supportsCompressedTextures())
1307 {
1308 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1309 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1310 }
1311 }
1312 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001313 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001314 params[0] = mState.viewportX;
1315 params[1] = mState.viewportY;
1316 params[2] = mState.viewportWidth;
1317 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001318 break;
1319 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001320 params[0] = mState.scissorX;
1321 params[1] = mState.scissorY;
1322 params[2] = mState.scissorWidth;
1323 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001324 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001325 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1326 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 case GL_RED_BITS:
1328 case GL_GREEN_BITS:
1329 case GL_BLUE_BITS:
1330 case GL_ALPHA_BITS:
1331 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001332 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001333 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1334
1335 if (colorbuffer)
1336 {
1337 switch (pname)
1338 {
1339 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1340 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1341 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1342 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1343 }
1344 }
1345 else
1346 {
1347 *params = 0;
1348 }
1349 }
1350 break;
1351 case GL_DEPTH_BITS:
1352 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001353 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001354 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001355
1356 if (depthbuffer)
1357 {
1358 *params = depthbuffer->getDepthSize();
1359 }
1360 else
1361 {
1362 *params = 0;
1363 }
1364 }
1365 break;
1366 case GL_STENCIL_BITS:
1367 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001368 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001369 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001370
1371 if (stencilbuffer)
1372 {
1373 *params = stencilbuffer->getStencilSize();
1374 }
1375 else
1376 {
1377 *params = 0;
1378 }
1379 }
1380 break;
1381 case GL_TEXTURE_BINDING_2D:
1382 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001383 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001384 {
1385 error(GL_INVALID_OPERATION);
1386 return false;
1387 }
1388
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001389 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001390 }
1391 break;
1392 case GL_TEXTURE_BINDING_CUBE_MAP:
1393 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001394 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001395 {
1396 error(GL_INVALID_OPERATION);
1397 return false;
1398 }
1399
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001400 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001401 }
1402 break;
1403 default:
1404 return false;
1405 }
1406
1407 return true;
1408}
1409
1410bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1411{
1412 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1413 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1414 // to the fact that it is stored internally as a float, and so would require conversion
1415 // if returned from Context::getIntegerv. Since this conversion is already implemented
1416 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1417 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1418 // application.
1419 switch (pname)
1420 {
1421 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1422 case GL_SHADER_BINARY_FORMATS:
1423 {
1424 *type = GL_INT;
1425 *numParams = 0;
1426 }
1427 break;
1428 case GL_MAX_VERTEX_ATTRIBS:
1429 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1430 case GL_MAX_VARYING_VECTORS:
1431 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1432 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1433 case GL_MAX_TEXTURE_IMAGE_UNITS:
1434 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1435 case GL_MAX_RENDERBUFFER_SIZE:
1436 case GL_NUM_SHADER_BINARY_FORMATS:
1437 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1438 case GL_ARRAY_BUFFER_BINDING:
1439 case GL_FRAMEBUFFER_BINDING:
1440 case GL_RENDERBUFFER_BINDING:
1441 case GL_CURRENT_PROGRAM:
1442 case GL_PACK_ALIGNMENT:
1443 case GL_UNPACK_ALIGNMENT:
1444 case GL_GENERATE_MIPMAP_HINT:
1445 case GL_RED_BITS:
1446 case GL_GREEN_BITS:
1447 case GL_BLUE_BITS:
1448 case GL_ALPHA_BITS:
1449 case GL_DEPTH_BITS:
1450 case GL_STENCIL_BITS:
1451 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1452 case GL_CULL_FACE_MODE:
1453 case GL_FRONT_FACE:
1454 case GL_ACTIVE_TEXTURE:
1455 case GL_STENCIL_FUNC:
1456 case GL_STENCIL_VALUE_MASK:
1457 case GL_STENCIL_REF:
1458 case GL_STENCIL_FAIL:
1459 case GL_STENCIL_PASS_DEPTH_FAIL:
1460 case GL_STENCIL_PASS_DEPTH_PASS:
1461 case GL_STENCIL_BACK_FUNC:
1462 case GL_STENCIL_BACK_VALUE_MASK:
1463 case GL_STENCIL_BACK_REF:
1464 case GL_STENCIL_BACK_FAIL:
1465 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1466 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1467 case GL_DEPTH_FUNC:
1468 case GL_BLEND_SRC_RGB:
1469 case GL_BLEND_SRC_ALPHA:
1470 case GL_BLEND_DST_RGB:
1471 case GL_BLEND_DST_ALPHA:
1472 case GL_BLEND_EQUATION_RGB:
1473 case GL_BLEND_EQUATION_ALPHA:
1474 case GL_STENCIL_WRITEMASK:
1475 case GL_STENCIL_BACK_WRITEMASK:
1476 case GL_STENCIL_CLEAR_VALUE:
1477 case GL_SUBPIXEL_BITS:
1478 case GL_MAX_TEXTURE_SIZE:
1479 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1480 case GL_SAMPLE_BUFFERS:
1481 case GL_SAMPLES:
1482 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1483 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1484 case GL_TEXTURE_BINDING_2D:
1485 case GL_TEXTURE_BINDING_CUBE_MAP:
1486 {
1487 *type = GL_INT;
1488 *numParams = 1;
1489 }
1490 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001491 case GL_MAX_SAMPLES_ANGLE:
1492 {
1493 if (getMaxSupportedSamples() != 0)
1494 {
1495 *type = GL_INT;
1496 *numParams = 1;
1497 }
1498 else
1499 {
1500 return false;
1501 }
1502 }
1503 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001504 case GL_MAX_VIEWPORT_DIMS:
1505 {
1506 *type = GL_INT;
1507 *numParams = 2;
1508 }
1509 break;
1510 case GL_VIEWPORT:
1511 case GL_SCISSOR_BOX:
1512 {
1513 *type = GL_INT;
1514 *numParams = 4;
1515 }
1516 break;
1517 case GL_SHADER_COMPILER:
1518 case GL_SAMPLE_COVERAGE_INVERT:
1519 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001520 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1521 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1522 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1523 case GL_SAMPLE_COVERAGE:
1524 case GL_SCISSOR_TEST:
1525 case GL_STENCIL_TEST:
1526 case GL_DEPTH_TEST:
1527 case GL_BLEND:
1528 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001529 {
1530 *type = GL_BOOL;
1531 *numParams = 1;
1532 }
1533 break;
1534 case GL_COLOR_WRITEMASK:
1535 {
1536 *type = GL_BOOL;
1537 *numParams = 4;
1538 }
1539 break;
1540 case GL_POLYGON_OFFSET_FACTOR:
1541 case GL_POLYGON_OFFSET_UNITS:
1542 case GL_SAMPLE_COVERAGE_VALUE:
1543 case GL_DEPTH_CLEAR_VALUE:
1544 case GL_LINE_WIDTH:
1545 {
1546 *type = GL_FLOAT;
1547 *numParams = 1;
1548 }
1549 break;
1550 case GL_ALIASED_LINE_WIDTH_RANGE:
1551 case GL_ALIASED_POINT_SIZE_RANGE:
1552 case GL_DEPTH_RANGE:
1553 {
1554 *type = GL_FLOAT;
1555 *numParams = 2;
1556 }
1557 break;
1558 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001559 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001560 {
1561 *type = GL_FLOAT;
1562 *numParams = 4;
1563 }
1564 break;
1565 default:
1566 return false;
1567 }
1568
1569 return true;
1570}
1571
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572// Applies the render target surface, depth stencil surface, viewport rectangle and
1573// scissor rectangle to the Direct3D 9 device
1574bool Context::applyRenderTarget(bool ignoreViewport)
1575{
1576 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001577
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001578 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
1580 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1581 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001582 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1583
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584 return false;
1585 }
1586
1587 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001588 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001590 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1591 if (renderTargetSerial != mAppliedRenderTargetSerial)
1592 {
1593 device->SetRenderTarget(0, renderTarget);
1594 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001595 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 +00001596 }
1597
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001598 unsigned int depthbufferSerial = 0;
1599 unsigned int stencilbufferSerial = 0;
1600 if (framebufferObject->getDepthbufferType() != GL_NONE)
1601 {
1602 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1603 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1604 }
1605 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1606 {
1607 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1608 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1609 }
1610
1611 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1612 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001613 {
1614 device->SetDepthStencilSurface(depthStencil);
1615 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001616 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
1619 D3DVIEWPORT9 viewport;
1620 D3DSURFACE_DESC desc;
1621 renderTarget->GetDesc(&desc);
1622
1623 if (ignoreViewport)
1624 {
1625 viewport.X = 0;
1626 viewport.Y = 0;
1627 viewport.Width = desc.Width;
1628 viewport.Height = desc.Height;
1629 viewport.MinZ = 0.0f;
1630 viewport.MaxZ = 1.0f;
1631 }
1632 else
1633 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001634 viewport.X = std::max(mState.viewportX, 0);
1635 viewport.Y = std::max(mState.viewportY, 0);
1636 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1637 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1638 viewport.MinZ = clamp01(mState.zNear);
1639 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640 }
1641
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001642 if (viewport.Width <= 0 || viewport.Height <= 0)
1643 {
1644 return false; // Nothing to render
1645 }
1646
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 device->SetViewport(&viewport);
1648
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001649 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001651 if (mState.scissorTest)
1652 {
1653 RECT rect = {mState.scissorX,
1654 mState.scissorY,
1655 mState.scissorX + mState.scissorWidth,
1656 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001657 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1658 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001659 device->SetScissorRect(&rect);
1660 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1661 }
1662 else
1663 {
1664 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1665 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001666
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001667 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 }
1669
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001670 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 Program *programObject = getCurrentProgram();
1673
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001674 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001675 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001677
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001678 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001679 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1680 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1681 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001682 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1683
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001684 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001685 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001686 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1687
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001688 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001689 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001690
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001691 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001692 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001694 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001696 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 }
1698
1699 return true;
1700}
1701
1702// 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 +00001703void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704{
1705 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001706 Program *programObject = getCurrentProgram();
1707
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001708 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001709 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001710 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001713 GLint alwaysFront = !isTriangleMode(drawMode);
1714 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1715
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001716 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001717
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001718 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001720 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001722 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 }
1724 else
1725 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001726 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 }
1728
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001729 mCullStateDirty = false;
1730 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mDepthStateDirty)
1733 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001734 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1737 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
1739 else
1740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743
1744 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1752
1753 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1754 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1755 {
1756 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1757 }
1758 else
1759 {
1760 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1761 unorm<8>(mState.blendColor.alpha),
1762 unorm<8>(mState.blendColor.alpha),
1763 unorm<8>(mState.blendColor.alpha)));
1764 }
1765
1766 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1767 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1768 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1769
1770 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1771 mState.destBlendRGB != mState.destBlendAlpha ||
1772 mState.blendEquationRGB != mState.blendEquationAlpha)
1773 {
1774 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1775
1776 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1777 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1778 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1779
1780 }
1781 else
1782 {
1783 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1784 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001785 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001786 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001787 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001789 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001790
1791 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792 }
1793
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 if (mStencilStateDirty || mFrontFaceDirty)
1795 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001796 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001797 {
1798 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1799 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1800
1801 // FIXME: Unsupported by D3D9
1802 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1803 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1804 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1805 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1806 mState.stencilRef != mState.stencilBackRef ||
1807 mState.stencilMask != mState.stencilBackMask)
1808 {
1809 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1810 return error(GL_INVALID_OPERATION);
1811 }
1812
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001813 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001814 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001815 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1816
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1818 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1819 es2dx::ConvertComparison(mState.stencilFunc));
1820
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001821 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1823
1824 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1825 es2dx::ConvertStencilOp(mState.stencilFail));
1826 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1827 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1828 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1829 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1830
1831 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1832 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1833 es2dx::ConvertComparison(mState.stencilBackFunc));
1834
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001835 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1837
1838 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1839 es2dx::ConvertStencilOp(mState.stencilBackFail));
1840 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1841 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1842 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1843 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1844 }
1845 else
1846 {
1847 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1848 }
1849
1850 mStencilStateDirty = false;
1851 }
1852
1853 if (mMaskStateDirty)
1854 {
1855 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1856 mState.colorMaskBlue, mState.colorMaskAlpha));
1857 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1858
1859 mMaskStateDirty = false;
1860 }
1861
1862 if (mPolygonOffsetStateDirty)
1863 {
1864 if (mState.polygonOffsetFill)
1865 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001866 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001867 if (depthbuffer)
1868 {
1869 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1870 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1871 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1872 }
1873 }
1874 else
1875 {
1876 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1877 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1878 }
1879
1880 mPolygonOffsetStateDirty = false;
1881 }
1882
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001883 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001885 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001886 {
1887 FIXME("Sample alpha to coverage is unimplemented.");
1888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001890 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001891 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001892 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1893 unsigned int mask = 0;
1894 if (mState.sampleCoverageValue != 0)
1895 {
1896 float threshold = 0.5f;
1897
1898 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1899 {
1900 mask <<= 1;
1901
1902 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1903 {
1904 threshold += 1.0f;
1905 mask |= 1;
1906 }
1907 }
1908 }
1909
1910 if (mState.sampleCoverageInvert)
1911 {
1912 mask = ~mask;
1913 }
1914
1915 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1916 }
1917 else
1918 {
1919 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001920 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921
1922 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 }
1924
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001925 if (mDitherStateDirty)
1926 {
1927 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1928
1929 mDitherStateDirty = false;
1930 }
1931
1932 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933}
1934
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001935// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001936void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001938 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001940 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001942 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 }
1944 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001945}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001947GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001948{
1949 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1950
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001951 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001952 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001953 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001954 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001955 }
1956
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001957 lookupAttributeMapping(translated);
1958
1959 mBufferBackEnd->setupAttributesPreDraw(translated);
1960
1961 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1962 {
1963 if (translated[i].enabled && translated[i].nonArray)
1964 {
1965 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1966 if (err != GL_NO_ERROR)
1967 {
1968 return err;
1969 }
1970
1971 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1972
1973 *useIndexing = true;
1974 return GL_NO_ERROR;
1975 }
1976 }
1977
1978 *useIndexing = false;
1979 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001980}
1981
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001982GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983{
1984 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1985
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001986 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001988 if (err == GL_NO_ERROR)
1989 {
1990 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001991
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001992 mBufferBackEnd->setupAttributesPreDraw(translated);
1993 }
1994
1995 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996}
1997
1998// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001999GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002000{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002001 GLenum err = mIndexDataManager->preRenderValidate(mode, 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 {
2005 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2006 }
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.com4fa08332010-05-11 02:29:27 +00002022 if (programObject->getSerial() != mAppliedProgram)
2023 {
2024 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002025 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002026 mAppliedProgram = programObject->getSerial();
2027 }
2028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 programObject->applyUniforms();
2030}
2031
2032// Applies the textures and sampler states to the Direct3D 9 device
2033void Context::applyTextures()
2034{
2035 IDirect3DDevice9 *device = getDevice();
2036 Program *programObject = getCurrentProgram();
2037
2038 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2039 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002040 int textureUnit = programObject->getSamplerMapping(sampler);
2041 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002042 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002043 SamplerType textureType = programObject->getSamplerType(sampler);
2044
2045 Texture *texture = getSamplerTexture(textureUnit, textureType);
2046
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002047 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002048 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002049 if (texture->isComplete())
2050 {
2051 GLenum wrapS = texture->getWrapS();
2052 GLenum wrapT = texture->getWrapT();
2053 GLenum minFilter = texture->getMinFilter();
2054 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002056 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2057 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002059 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2060 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2061 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2062 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2063 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002065 device->SetTexture(sampler, texture->getTexture());
2066 }
2067 else
2068 {
2069 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2070 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002071 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002072
2073 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002075 else
2076 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002077 if (programObject->isSamplerDirty(sampler))
2078 {
2079 device->SetTexture(sampler, NULL);
2080 programObject->setSamplerDirty(sampler, false);
2081 }
2082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084}
2085
2086void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2087{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002088 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002089
2090 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2091 {
2092 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2093 }
2094
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002095 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2096 {
2097 return error(GL_INVALID_OPERATION);
2098 }
2099
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2101 IDirect3DDevice9 *device = getDevice();
2102
2103 D3DSURFACE_DESC desc;
2104 renderTarget->GetDesc(&desc);
2105
2106 IDirect3DSurface9 *systemSurface;
2107 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2108
2109 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2110 {
2111 return error(GL_OUT_OF_MEMORY);
2112 }
2113
2114 ASSERT(SUCCEEDED(result));
2115
2116 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2117 {
2118 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2119 }
2120
2121 result = device->GetRenderTargetData(renderTarget, systemSurface);
2122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 if (FAILED(result))
2124 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 systemSurface->Release();
2126
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002127 switch (result)
2128 {
2129 case D3DERR_DRIVERINTERNALERROR:
2130 case D3DERR_DEVICELOST:
2131 return error(GL_OUT_OF_MEMORY);
2132 default:
2133 UNREACHABLE();
2134 return; // No sensible error to generate
2135 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 }
2137
2138 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002139 RECT rect = {std::max(x, 0),
2140 std::max(y, 0),
2141 std::min(x + width, (int)desc.Width),
2142 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143
2144 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2145
2146 if (FAILED(result))
2147 {
2148 UNREACHABLE();
2149 systemSurface->Release();
2150
2151 return; // No sensible error to generate
2152 }
2153
2154 unsigned char *source = (unsigned char*)lock.pBits;
2155 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002156 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002158 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002159
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 for (int j = 0; j < rect.bottom - rect.top; j++)
2161 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002162 if (desc.Format == D3DFMT_A8R8G8B8 &&
2163 format == GL_BGRA_EXT &&
2164 type == GL_UNSIGNED_BYTE)
2165 {
2166 // Fast path for EXT_read_format_bgra, given
2167 // an RGBA source buffer. Note that buffers with no
2168 // alpha go through the slow path below.
2169 memcpy(dest + j * outputPitch,
2170 source + j * lock.Pitch,
2171 (rect.right - rect.left) * 4);
2172 continue;
2173 }
2174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 for (int i = 0; i < rect.right - rect.left; i++)
2176 {
2177 float r;
2178 float g;
2179 float b;
2180 float a;
2181
2182 switch (desc.Format)
2183 {
2184 case D3DFMT_R5G6B5:
2185 {
2186 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2187
2188 a = 1.0f;
2189 b = (rgb & 0x001F) * (1.0f / 0x001F);
2190 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2191 r = (rgb & 0xF800) * (1.0f / 0xF800);
2192 }
2193 break;
2194 case D3DFMT_X1R5G5B5:
2195 {
2196 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2197
2198 a = 1.0f;
2199 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2200 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2201 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2202 }
2203 break;
2204 case D3DFMT_A1R5G5B5:
2205 {
2206 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2207
2208 a = (argb & 0x8000) ? 1.0f : 0.0f;
2209 b = (argb & 0x001F) * (1.0f / 0x001F);
2210 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2211 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2212 }
2213 break;
2214 case D3DFMT_A8R8G8B8:
2215 {
2216 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2217
2218 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2219 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2220 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2221 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2222 }
2223 break;
2224 case D3DFMT_X8R8G8B8:
2225 {
2226 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2227
2228 a = 1.0f;
2229 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2230 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2231 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2232 }
2233 break;
2234 case D3DFMT_A2R10G10B10:
2235 {
2236 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2237
2238 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2239 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2240 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2241 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2242 }
2243 break;
2244 default:
2245 UNIMPLEMENTED(); // FIXME
2246 UNREACHABLE();
2247 }
2248
2249 switch (format)
2250 {
2251 case GL_RGBA:
2252 switch (type)
2253 {
2254 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002255 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2256 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2257 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2258 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 break;
2260 default: UNREACHABLE();
2261 }
2262 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002263 case GL_BGRA_EXT:
2264 switch (type)
2265 {
2266 case GL_UNSIGNED_BYTE:
2267 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2268 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2269 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2270 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2271 break;
2272 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2273 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2274 // this type is packed as follows:
2275 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2276 // --------------------------------------------------------------------------------
2277 // | 4th | 3rd | 2nd | 1st component |
2278 // --------------------------------------------------------------------------------
2279 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2280 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2281 ((unsigned short)(15 * a + 0.5f) << 12)|
2282 ((unsigned short)(15 * r + 0.5f) << 8) |
2283 ((unsigned short)(15 * g + 0.5f) << 4) |
2284 ((unsigned short)(15 * b + 0.5f) << 0);
2285 break;
2286 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2287 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2288 // this type is packed as follows:
2289 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2290 // --------------------------------------------------------------------------------
2291 // | 4th | 3rd | 2nd | 1st component |
2292 // --------------------------------------------------------------------------------
2293 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2294 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2295 ((unsigned short)( a + 0.5f) << 15) |
2296 ((unsigned short)(31 * r + 0.5f) << 10) |
2297 ((unsigned short)(31 * g + 0.5f) << 5) |
2298 ((unsigned short)(31 * b + 0.5f) << 0);
2299 break;
2300 default: UNREACHABLE();
2301 }
2302 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002303 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 switch (type)
2305 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002306 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002307 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2308 ((unsigned short)(31 * b + 0.5f) << 0) |
2309 ((unsigned short)(63 * g + 0.5f) << 5) |
2310 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 break;
2312 default: UNREACHABLE();
2313 }
2314 break;
2315 default: UNREACHABLE();
2316 }
2317 }
2318 }
2319
2320 systemSurface->UnlockRect();
2321
2322 systemSurface->Release();
2323}
2324
2325void Context::clear(GLbitfield mask)
2326{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002327 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002328
2329 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2330 {
2331 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2332
2333 return;
2334 }
2335
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002336 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 IDirect3DDevice9 *device = getDevice();
2338 DWORD flags = 0;
2339
2340 if (mask & GL_COLOR_BUFFER_BIT)
2341 {
2342 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002343
2344 if (framebufferObject->getColorbufferType() != GL_NONE)
2345 {
2346 flags |= D3DCLEAR_TARGET;
2347 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 }
2349
2350 if (mask & GL_DEPTH_BUFFER_BIT)
2351 {
2352 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002353 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 {
2355 flags |= D3DCLEAR_ZBUFFER;
2356 }
2357 }
2358
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 GLuint stencilUnmasked = 0x0;
2360
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002361 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002364 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002366 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2367 D3DSURFACE_DESC desc;
2368 depthStencil->GetDesc(&desc);
2369
2370 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2371 stencilUnmasked = (0x1 << stencilSize) - 1;
2372
2373 if (stencilUnmasked != 0x0)
2374 {
2375 flags |= D3DCLEAR_STENCIL;
2376 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 }
2378 }
2379
2380 if (mask != 0)
2381 {
2382 return error(GL_INVALID_VALUE);
2383 }
2384
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002385 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2386 {
2387 return;
2388 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002390 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2391 unorm<8>(mState.colorClearValue.red),
2392 unorm<8>(mState.colorClearValue.green),
2393 unorm<8>(mState.colorClearValue.blue));
2394 float depth = clamp01(mState.depthClearValue);
2395 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396
2397 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2398
2399 D3DSURFACE_DESC desc;
2400 renderTarget->GetDesc(&desc);
2401
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002402 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403
2404 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002405 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002407 !(mState.colorMaskRed && mState.colorMaskGreen &&
2408 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409
2410 if (needMaskedColorClear || needMaskedStencilClear)
2411 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002412 // State which is altered in all paths from this point to the clear call is saved.
2413 // State which is altered in only some paths will be flagged dirty in the case that
2414 // that path is taken.
2415 HRESULT hr;
2416 if (mMaskedClearSavedState == NULL)
2417 {
2418 hr = device->BeginStateBlock();
2419 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2420
2421 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2422 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2423 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2424 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2425 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2426 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2427 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2428 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2429 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2430 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2431 device->SetPixelShader(NULL);
2432 device->SetVertexShader(NULL);
2433 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2434 device->SetStreamSourceFreq(0, 1);
2435
2436 hr = device->EndStateBlock(&mMaskedClearSavedState);
2437 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2438 }
2439
2440 ASSERT(mMaskedClearSavedState != NULL);
2441
2442 if (mMaskedClearSavedState != NULL)
2443 {
2444 hr = mMaskedClearSavedState->Capture();
2445 ASSERT(SUCCEEDED(hr));
2446 }
2447
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2449 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2450 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2451 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2452 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2453 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2454 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2455 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2456
2457 if (flags & D3DCLEAR_TARGET)
2458 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002459 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2460 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2461 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2462 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 }
2464 else
2465 {
2466 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2467 }
2468
2469 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2470 {
2471 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2472 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2473 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2474 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002475 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002476 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2478 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002479 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 }
2481 else
2482 {
2483 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2484 }
2485
2486 device->SetPixelShader(NULL);
2487 device->SetVertexShader(NULL);
2488 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002489 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002491 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 {
2493 float x, y, z, w;
2494 D3DCOLOR diffuse;
2495 };
2496
2497 Vertex quad[4];
2498 quad[0].x = 0.0f;
2499 quad[0].y = (float)desc.Height;
2500 quad[0].z = 0.0f;
2501 quad[0].w = 1.0f;
2502 quad[0].diffuse = color;
2503
2504 quad[1].x = (float)desc.Width;
2505 quad[1].y = (float)desc.Height;
2506 quad[1].z = 0.0f;
2507 quad[1].w = 1.0f;
2508 quad[1].diffuse = color;
2509
2510 quad[2].x = 0.0f;
2511 quad[2].y = 0.0f;
2512 quad[2].z = 0.0f;
2513 quad[2].w = 1.0f;
2514 quad[2].diffuse = color;
2515
2516 quad[3].x = (float)desc.Width;
2517 quad[3].y = 0.0f;
2518 quad[3].z = 0.0f;
2519 quad[3].w = 1.0f;
2520 quad[3].diffuse = color;
2521
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002522 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
2525 if (flags & D3DCLEAR_ZBUFFER)
2526 {
2527 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2528 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2529 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2530 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002531
2532 if (mMaskedClearSavedState != NULL)
2533 {
2534 mMaskedClearSavedState->Apply();
2535 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002537 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 {
2539 device->Clear(0, NULL, flags, color, depth, stencil);
2540 }
2541}
2542
2543void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2544{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002545 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
2547 return error(GL_INVALID_OPERATION);
2548 }
2549
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002550 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 IDirect3DDevice9 *device = getDevice();
2552 D3DPRIMITIVETYPE primitiveType;
2553 int primitiveCount;
2554
2555 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2556 return error(GL_INVALID_ENUM);
2557
2558 if (primitiveCount <= 0)
2559 {
2560 return;
2561 }
2562
2563 if (!applyRenderTarget(false))
2564 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002565 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 }
2567
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002568 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002569
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002570 TranslatedIndexData indexInfo;
2571 bool useIndexing;
2572 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002573 if (err != GL_NO_ERROR)
2574 {
2575 return error(err);
2576 }
2577
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578 applyShaders();
2579 applyTextures();
2580
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002581 if (!getCurrentProgram()->validateSamplers())
2582 {
2583 return error(GL_INVALID_OPERATION);
2584 }
2585
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002586 if (!cullSkipsDraw(mode))
2587 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002588 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002589 if (useIndexing)
2590 {
2591 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2592 }
2593 else
2594 {
2595 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2596 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002597 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598}
2599
2600void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2601{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002602 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 {
2604 return error(GL_INVALID_OPERATION);
2605 }
2606
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002607 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 {
2609 return error(GL_INVALID_OPERATION);
2610 }
2611
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002612 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 IDirect3DDevice9 *device = getDevice();
2614 D3DPRIMITIVETYPE primitiveType;
2615 int primitiveCount;
2616
2617 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2618 return error(GL_INVALID_ENUM);
2619
2620 if (primitiveCount <= 0)
2621 {
2622 return;
2623 }
2624
2625 if (!applyRenderTarget(false))
2626 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002627 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 }
2629
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002630 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002631
2632 TranslatedIndexData indexInfo;
2633 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2634 if (err != GL_NO_ERROR)
2635 {
2636 return error(err);
2637 }
2638
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002639 err = applyVertexBuffer(indexInfo);
2640 if (err != GL_NO_ERROR)
2641 {
2642 return error(err);
2643 }
2644
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645 applyShaders();
2646 applyTextures();
2647
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002648 if (!getCurrentProgram()->validateSamplers())
2649 {
2650 return error(GL_INVALID_OPERATION);
2651 }
2652
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002653 if (!cullSkipsDraw(mode))
2654 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002655 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002656 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658}
2659
2660void Context::finish()
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 IDirect3DQuery9 *occlusionQuery = NULL;
2665
2666 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2667
2668 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2669 {
2670 return error(GL_OUT_OF_MEMORY);
2671 }
2672
2673 ASSERT(SUCCEEDED(result));
2674
2675 if (occlusionQuery)
2676 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002677 IDirect3DStateBlock9 *savedState = NULL;
2678 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2679
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002680 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2681 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682
2683 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002684 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 device->SetPixelShader(NULL);
2686 device->SetVertexShader(NULL);
2687 device->SetFVF(D3DFVF_XYZRHW);
2688 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002689 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002692 result = occlusionQuery->Issue(D3DISSUE_END);
2693 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694
2695 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2696 {
2697 // Keep polling, but allow other threads to do something useful first
2698 Sleep(0);
2699 }
2700
2701 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002702
2703 if (savedState)
2704 {
2705 savedState->Apply();
2706 savedState->Release();
2707 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708 }
2709}
2710
2711void Context::flush()
2712{
2713 IDirect3DDevice9 *device = getDevice();
2714 IDirect3DQuery9 *eventQuery = NULL;
2715
2716 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2717
2718 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2719 {
2720 return error(GL_OUT_OF_MEMORY);
2721 }
2722
2723 ASSERT(SUCCEEDED(result));
2724
2725 if (eventQuery)
2726 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002727 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2728 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002730 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002732
2733 if (result == D3DERR_DEVICELOST)
2734 {
2735 error(GL_OUT_OF_MEMORY);
2736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 }
2738}
2739
2740void Context::recordInvalidEnum()
2741{
2742 mInvalidEnum = true;
2743}
2744
2745void Context::recordInvalidValue()
2746{
2747 mInvalidValue = true;
2748}
2749
2750void Context::recordInvalidOperation()
2751{
2752 mInvalidOperation = true;
2753}
2754
2755void Context::recordOutOfMemory()
2756{
2757 mOutOfMemory = true;
2758}
2759
2760void Context::recordInvalidFramebufferOperation()
2761{
2762 mInvalidFramebufferOperation = true;
2763}
2764
2765// Get one of the recorded errors and clear its flag, if any.
2766// [OpenGL ES 2.0.24] section 2.5 page 13.
2767GLenum Context::getError()
2768{
2769 if (mInvalidEnum)
2770 {
2771 mInvalidEnum = false;
2772
2773 return GL_INVALID_ENUM;
2774 }
2775
2776 if (mInvalidValue)
2777 {
2778 mInvalidValue = false;
2779
2780 return GL_INVALID_VALUE;
2781 }
2782
2783 if (mInvalidOperation)
2784 {
2785 mInvalidOperation = false;
2786
2787 return GL_INVALID_OPERATION;
2788 }
2789
2790 if (mOutOfMemory)
2791 {
2792 mOutOfMemory = false;
2793
2794 return GL_OUT_OF_MEMORY;
2795 }
2796
2797 if (mInvalidFramebufferOperation)
2798 {
2799 mInvalidFramebufferOperation = false;
2800
2801 return GL_INVALID_FRAMEBUFFER_OPERATION;
2802 }
2803
2804 return GL_NO_ERROR;
2805}
2806
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002807bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002808{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002809 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002810}
2811
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002812int Context::getMaxSupportedSamples() const
2813{
2814 return mMaxSupportedSamples;
2815}
2816
2817int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2818{
2819 if (requested == 0)
2820 {
2821 return requested;
2822 }
2823
2824 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2825 if (itr == mMultiSampleSupport.end())
2826 {
2827 return -1;
2828 }
2829
2830 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2831 {
2832 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2833 {
2834 return i;
2835 }
2836 }
2837
2838 return -1;
2839}
2840
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002841bool Context::supportsEventQueries() const
2842{
2843 return mSupportsEventQueries;
2844}
2845
daniel@transgaming.com01868132010-08-24 19:21:17 +00002846bool Context::supportsCompressedTextures() const
2847{
2848 return mSupportsCompressedTextures;
2849}
2850
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002851bool Context::supportsFloatTextures() const
2852{
2853 return mSupportsFloatTextures;
2854}
2855
2856bool Context::supportsFloatLinearFilter() const
2857{
2858 return mSupportsFloatLinearFilter;
2859}
2860
2861bool Context::supportsHalfFloatTextures() const
2862{
2863 return mSupportsHalfFloatTextures;
2864}
2865
2866bool Context::supportsHalfFloatLinearFilter() const
2867{
2868 return mSupportsHalfFloatLinearFilter;
2869}
2870
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871void Context::detachBuffer(GLuint buffer)
2872{
2873 // [OpenGL ES 2.0.24] section 2.9 page 22:
2874 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2875 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2876
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002877 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002879 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880 }
2881
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002882 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002884 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002885 }
2886
2887 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2888 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002889 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002891 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002892 }
2893 }
2894}
2895
2896void Context::detachTexture(GLuint texture)
2897{
2898 // [OpenGL ES 2.0.24] section 3.8 page 84:
2899 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2900 // rebound to texture object zero
2901
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002902 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002904 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002905 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002906 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002907 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002908 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002909 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910 }
2911 }
2912
2913 // [OpenGL ES 2.0.24] section 4.4 page 112:
2914 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2915 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2916 // image was attached in the currently bound framebuffer.
2917
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002918 Framebuffer *readFramebuffer = getReadFramebuffer();
2919 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002921 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002923 readFramebuffer->detachTexture(texture);
2924 }
2925
2926 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2927 {
2928 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002929 }
2930}
2931
2932void Context::detachFramebuffer(GLuint framebuffer)
2933{
2934 // [OpenGL ES 2.0.24] section 4.4 page 107:
2935 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2936 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2937
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002938 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002940 bindReadFramebuffer(0);
2941 }
2942
2943 if (mState.drawFramebuffer == framebuffer)
2944 {
2945 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 }
2947}
2948
2949void Context::detachRenderbuffer(GLuint renderbuffer)
2950{
2951 // [OpenGL ES 2.0.24] section 4.4 page 109:
2952 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2953 // had been executed with the target RENDERBUFFER and name of zero.
2954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002955 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002956 {
2957 bindRenderbuffer(0);
2958 }
2959
2960 // [OpenGL ES 2.0.24] section 4.4 page 111:
2961 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2962 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2963 // point to which this image was attached in the currently bound framebuffer.
2964
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002965 Framebuffer *readFramebuffer = getReadFramebuffer();
2966 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002967
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002968 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002969 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002970 readFramebuffer->detachRenderbuffer(renderbuffer);
2971 }
2972
2973 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2974 {
2975 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002976 }
2977}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002978
2979Texture *Context::getIncompleteTexture(SamplerType type)
2980{
2981 Texture *t = mIncompleteTextures[type];
2982
2983 if (t == NULL)
2984 {
2985 static const GLubyte color[] = { 0, 0, 0, 255 };
2986
2987 switch (type)
2988 {
2989 default:
2990 UNREACHABLE();
2991 // default falls through to SAMPLER_2D
2992
2993 case SAMPLER_2D:
2994 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002995 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002996 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002997 t = incomplete2d;
2998 }
2999 break;
3000
3001 case SAMPLER_CUBE:
3002 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003003 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003004
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003005 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3006 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3007 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3008 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3009 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3010 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003011
3012 t = incompleteCube;
3013 }
3014 break;
3015 }
3016
3017 mIncompleteTextures[type] = t;
3018 }
3019
3020 return t;
3021}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003022
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003023bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003024{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003025 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003026}
3027
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003028bool Context::isTriangleMode(GLenum drawMode)
3029{
3030 switch (drawMode)
3031 {
3032 case GL_TRIANGLES:
3033 case GL_TRIANGLE_FAN:
3034 case GL_TRIANGLE_STRIP:
3035 return true;
3036 case GL_POINTS:
3037 case GL_LINES:
3038 case GL_LINE_LOOP:
3039 case GL_LINE_STRIP:
3040 return false;
3041 default: UNREACHABLE();
3042 }
3043
3044 return false;
3045}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003046
3047void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3048{
3049 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3050
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003051 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3052 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3053 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3054 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003055
3056 mVertexDataManager->dirtyCurrentValues();
3057}
3058
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003059void Context::initExtensionString()
3060{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003061 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003062 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3063 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003064 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003065 mExtensionString += "GL_OES_rgb8_rgba8 ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003066
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003067 if (supportsEventQueries())
3068 {
3069 mExtensionString += "GL_NV_fence ";
3070 }
3071
daniel@transgaming.com01868132010-08-24 19:21:17 +00003072 if (supportsCompressedTextures())
3073 {
3074 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3075 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003076
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003077 if (supportsFloatTextures())
3078 {
3079 mExtensionString += "GL_OES_texture_float ";
3080 }
3081
3082 if (supportsHalfFloatTextures())
3083 {
3084 mExtensionString += "GL_OES_texture_half_float ";
3085 }
3086
3087 if (supportsFloatLinearFilter())
3088 {
3089 mExtensionString += "GL_OES_texture_float_linear ";
3090 }
3091
3092 if (supportsHalfFloatLinearFilter())
3093 {
3094 mExtensionString += "GL_OES_texture_half_float_linear ";
3095 }
3096
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003097 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003098 {
3099 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3100 }
3101
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003102 if (mBufferBackEnd->supportIntIndices())
3103 {
3104 mExtensionString += "GL_OES_element_index_uint ";
3105 }
3106
3107 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3108 if (end != std::string::npos)
3109 {
3110 mExtensionString.resize(end+1);
3111 }
3112}
3113
3114const char *Context::getExtensionString() const
3115{
3116 return mExtensionString.c_str();
3117}
3118
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003119void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3120 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3121 GLbitfield mask)
3122{
3123 IDirect3DDevice9 *device = getDevice();
3124
3125 Framebuffer *readFramebuffer = getReadFramebuffer();
3126 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3127
3128 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3129 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3130 {
3131 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3132 }
3133
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003134 if (drawFramebuffer->getSamples() != 0)
3135 {
3136 return error(GL_INVALID_OPERATION);
3137 }
3138
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003139 RECT sourceRect;
3140 RECT destRect;
3141
3142 if (srcX0 < srcX1)
3143 {
3144 sourceRect.left = srcX0;
3145 sourceRect.right = srcX1;
3146 destRect.left = dstX0;
3147 destRect.right = dstX1;
3148 }
3149 else
3150 {
3151 sourceRect.left = srcX1;
3152 destRect.left = dstX1;
3153 sourceRect.right = srcX0;
3154 destRect.right = dstX0;
3155 }
3156
3157 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3158 // flip our Y-values here
3159 if (srcY0 < srcY1)
3160 {
3161 sourceRect.bottom = srcY1;
3162 destRect.bottom = dstY1;
3163 sourceRect.top = srcY0;
3164 destRect.top = dstY0;
3165 }
3166 else
3167 {
3168 sourceRect.bottom = srcY0;
3169 destRect.bottom = dstY0;
3170 sourceRect.top = srcY1;
3171 destRect.top = dstY1;
3172 }
3173
3174 RECT sourceScissoredRect = sourceRect;
3175 RECT destScissoredRect = destRect;
3176
3177 if (mState.scissorTest)
3178 {
3179 // Only write to parts of the destination framebuffer which pass the scissor test
3180 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3181 // rect will be checked against scissorY, rather than the bottom.
3182 if (destRect.left < mState.scissorX)
3183 {
3184 int xDiff = mState.scissorX - destRect.left;
3185 destScissoredRect.left = mState.scissorX;
3186 sourceScissoredRect.left += xDiff;
3187 }
3188
3189 if (destRect.right > mState.scissorX + mState.scissorWidth)
3190 {
3191 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3192 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3193 sourceScissoredRect.right -= xDiff;
3194 }
3195
3196 if (destRect.top < mState.scissorY)
3197 {
3198 int yDiff = mState.scissorY - destRect.top;
3199 destScissoredRect.top = mState.scissorY;
3200 sourceScissoredRect.top += yDiff;
3201 }
3202
3203 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3204 {
3205 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3206 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3207 sourceScissoredRect.bottom -= yDiff;
3208 }
3209 }
3210
3211 bool blitRenderTarget = false;
3212 bool blitDepthStencil = false;
3213
3214 RECT sourceTrimmedRect = sourceScissoredRect;
3215 RECT destTrimmedRect = destScissoredRect;
3216
3217 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3218 // the actual draw and read surfaces.
3219 if (sourceTrimmedRect.left < 0)
3220 {
3221 int xDiff = 0 - sourceTrimmedRect.left;
3222 sourceTrimmedRect.left = 0;
3223 destTrimmedRect.left += xDiff;
3224 }
3225
3226 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3227 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3228 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3229 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3230
3231 if (sourceTrimmedRect.right > readBufferWidth)
3232 {
3233 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3234 sourceTrimmedRect.right = readBufferWidth;
3235 destTrimmedRect.right -= xDiff;
3236 }
3237
3238 if (sourceTrimmedRect.top < 0)
3239 {
3240 int yDiff = 0 - sourceTrimmedRect.top;
3241 sourceTrimmedRect.top = 0;
3242 destTrimmedRect.top += yDiff;
3243 }
3244
3245 if (sourceTrimmedRect.bottom > readBufferHeight)
3246 {
3247 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3248 sourceTrimmedRect.bottom = readBufferHeight;
3249 destTrimmedRect.bottom -= yDiff;
3250 }
3251
3252 if (destTrimmedRect.left < 0)
3253 {
3254 int xDiff = 0 - destTrimmedRect.left;
3255 destTrimmedRect.left = 0;
3256 sourceTrimmedRect.left += xDiff;
3257 }
3258
3259 if (destTrimmedRect.right > drawBufferWidth)
3260 {
3261 int xDiff = destTrimmedRect.right - drawBufferWidth;
3262 destTrimmedRect.right = drawBufferWidth;
3263 sourceTrimmedRect.right -= xDiff;
3264 }
3265
3266 if (destTrimmedRect.top < 0)
3267 {
3268 int yDiff = 0 - destTrimmedRect.top;
3269 destTrimmedRect.top = 0;
3270 sourceTrimmedRect.top += yDiff;
3271 }
3272
3273 if (destTrimmedRect.bottom > drawBufferHeight)
3274 {
3275 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3276 destTrimmedRect.bottom = drawBufferHeight;
3277 sourceTrimmedRect.bottom -= yDiff;
3278 }
3279
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003280 bool partialBufferCopy = false;
3281 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3282 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3283 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3284 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3285 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3286 {
3287 partialBufferCopy = true;
3288 }
3289
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003290 if (mask & GL_COLOR_BUFFER_BIT)
3291 {
3292 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3293 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3294 {
3295 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3296 return error(GL_INVALID_OPERATION);
3297 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003298
3299 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3300 {
3301 return error(GL_INVALID_OPERATION);
3302 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003303
3304 blitRenderTarget = true;
3305
3306 }
3307
3308 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3309 {
3310 DepthStencilbuffer *readDSBuffer = NULL;
3311 DepthStencilbuffer *drawDSBuffer = NULL;
3312
3313 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3314 // both a depth and stencil buffer, it will be the same buffer.
3315
3316 if (mask & GL_DEPTH_BUFFER_BIT)
3317 {
3318 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3319 {
3320 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3321 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3322 {
3323 return error(GL_INVALID_OPERATION);
3324 }
3325
3326 blitDepthStencil = true;
3327 readDSBuffer = readFramebuffer->getDepthbuffer();
3328 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3329 }
3330 }
3331
3332 if (mask & GL_STENCIL_BUFFER_BIT)
3333 {
3334 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3335 {
3336 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3337 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3338 {
3339 return error(GL_INVALID_OPERATION);
3340 }
3341
3342 blitDepthStencil = true;
3343 readDSBuffer = readFramebuffer->getStencilbuffer();
3344 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3345 }
3346 }
3347
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003348 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003349 {
3350 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3351 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3352 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003353
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003354 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3355 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003356 {
3357 return error(GL_INVALID_OPERATION);
3358 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003359 }
3360
3361 if (blitRenderTarget || blitDepthStencil)
3362 {
3363 egl::Display *display = getDisplay();
3364 display->endScene();
3365
3366 if (blitRenderTarget)
3367 {
3368 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3369 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3370
3371 if (FAILED(result))
3372 {
3373 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3374 return;
3375 }
3376 }
3377
3378 if (blitDepthStencil)
3379 {
3380 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3381
3382 if (FAILED(result))
3383 {
3384 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3385 return;
3386 }
3387 }
3388 }
3389}
3390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003391}
3392
3393extern "C"
3394{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003395gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003396{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003397 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003398}
3399
3400void glDestroyContext(gl::Context *context)
3401{
3402 delete context;
3403
3404 if (context == gl::getContext())
3405 {
3406 gl::makeCurrent(NULL, NULL, NULL);
3407 }
3408}
3409
3410void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3411{
3412 gl::makeCurrent(context, display, surface);
3413}
3414
3415gl::Context *glGetCurrentContext()
3416{
3417 return gl::getContext();
3418}
3419}