blob: 851b0618821c24bc24401a7594683f7cd82938cf [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"
20#include "libGLESv2/Buffer.h"
21#include "libGLESv2/FrameBuffer.h"
22#include "libGLESv2/Program.h"
23#include "libGLESv2/RenderBuffer.h"
24#include "libGLESv2/Shader.h"
25#include "libGLESv2/Texture.h"
26#include "libGLESv2/geometry/backend.h"
27#include "libGLESv2/geometry/VertexDataManager.h"
28#include "libGLESv2/geometry/IndexDataManager.h"
29#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000036Context::Context(const egl::Config *config)
37 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
39 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com428d1582010-05-04 03:35:25 +000040 mState.depthClearValue = 1.0f;
41 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.cullFace = false;
44 mState.cullMode = GL_BACK;
45 mState.frontFace = GL_CCW;
46 mState.depthTest = false;
47 mState.depthFunc = GL_LESS;
48 mState.blend = false;
49 mState.sourceBlendRGB = GL_ONE;
50 mState.sourceBlendAlpha = GL_ONE;
51 mState.destBlendRGB = GL_ZERO;
52 mState.destBlendAlpha = GL_ZERO;
53 mState.blendEquationRGB = GL_FUNC_ADD;
54 mState.blendEquationAlpha = GL_FUNC_ADD;
55 mState.blendColor.red = 0;
56 mState.blendColor.green = 0;
57 mState.blendColor.blue = 0;
58 mState.blendColor.alpha = 0;
59 mState.stencilTest = false;
60 mState.stencilFunc = GL_ALWAYS;
61 mState.stencilRef = 0;
62 mState.stencilMask = -1;
63 mState.stencilWritemask = -1;
64 mState.stencilBackFunc = GL_ALWAYS;
65 mState.stencilBackRef = 0;
66 mState.stencilBackMask = - 1;
67 mState.stencilBackWritemask = -1;
68 mState.stencilFail = GL_KEEP;
69 mState.stencilPassDepthFail = GL_KEEP;
70 mState.stencilPassDepthPass = GL_KEEP;
71 mState.stencilBackFail = GL_KEEP;
72 mState.stencilBackPassDepthFail = GL_KEEP;
73 mState.stencilBackPassDepthPass = GL_KEEP;
74 mState.polygonOffsetFill = false;
75 mState.polygonOffsetFactor = 0.0f;
76 mState.polygonOffsetUnits = 0.0f;
77 mState.sampleAlphaToCoverage = false;
78 mState.sampleCoverage = false;
79 mState.sampleCoverageValue = 1.0f;
80 mState.sampleCoverageInvert = GL_FALSE;
81 mState.scissorTest = false;
82 mState.dither = true;
83 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
daniel@transgaming.com428d1582010-05-04 03:35:25 +000085 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.viewportX = 0;
88 mState.viewportY = 0;
89 mState.viewportWidth = config->mDisplayMode.Width;
90 mState.viewportHeight = config->mDisplayMode.Height;
91 mState.zNear = 0.0f;
92 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
daniel@transgaming.com428d1582010-05-04 03:35:25 +000094 mState.scissorX = 0;
95 mState.scissorY = 0;
96 mState.scissorWidth = config->mDisplayMode.Width;
97 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098
daniel@transgaming.com428d1582010-05-04 03:35:25 +000099 mState.colorMaskRed = true;
100 mState.colorMaskGreen = true;
101 mState.colorMaskBlue = true;
102 mState.colorMaskAlpha = true;
103 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104
105 // [OpenGL ES 2.0.24] section 3.7 page 83:
106 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
107 // and cube map texture state vectors respectively associated with them.
108 // In order that access to these initial textures not be lost, they are treated as texture
109 // objects all of whose names are 0.
110
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000111 mTexture2DZero = new Texture2D(this);
112 mTextureCubeMapZero = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
114 mColorbufferZero = NULL;
115 mDepthbufferZero = NULL;
116 mStencilbufferZero = NULL;
117
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000118 mState.activeSampler = 0;
119 mState.arrayBuffer = 0;
120 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121 bindTextureCubeMap(0);
122 bindTexture2D(0);
123 bindFramebuffer(0);
124 bindRenderbuffer(0);
125
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000126 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000128 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
129 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000130 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000131 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 }
133
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000134 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
135 {
136 mIncompleteTextures[type] = NULL;
137 }
138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.packAlignment = 4;
142 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000143
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000144 mBufferBackEnd = NULL;
145 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000146 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000147 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149 mInvalidEnum = false;
150 mInvalidValue = false;
151 mInvalidOperation = false;
152 mOutOfMemory = false;
153 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000154
155 mHasBeenCurrent = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156}
157
158Context::~Context()
159{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000160 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000162 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
163 {
164 delete mIncompleteTextures[type];
165 }
166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167 delete mTexture2DZero;
168 delete mTextureCubeMapZero;
169
170 delete mColorbufferZero;
171 delete mDepthbufferZero;
172 delete mStencilbufferZero;
173
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000174 delete mBufferBackEnd;
175 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000176 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000177 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 while (!mBufferMap.empty())
180 {
181 deleteBuffer(mBufferMap.begin()->first);
182 }
183
184 while (!mProgramMap.empty())
185 {
186 deleteProgram(mProgramMap.begin()->first);
187 }
188
189 while (!mShaderMap.empty())
190 {
191 deleteShader(mShaderMap.begin()->first);
192 }
193
194 while (!mFramebufferMap.empty())
195 {
196 deleteFramebuffer(mFramebufferMap.begin()->first);
197 }
198
199 while (!mRenderbufferMap.empty())
200 {
201 deleteRenderbuffer(mRenderbufferMap.begin()->first);
202 }
203
204 while (!mTextureMap.empty())
205 {
206 deleteTexture(mTextureMap.begin()->first);
207 }
208}
209
210void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
211{
212 IDirect3DDevice9 *device = display->getDevice();
213
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000214 if (!mBufferBackEnd)
215 {
216 mBufferBackEnd = new Dx9BackEnd(device);
217 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000218 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000219 mBlit = new Blit(this);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000220 }
221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
223 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000224 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
226 Framebuffer *framebufferZero = new Framebuffer();
227 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000228 Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil);
229 Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230
231 setFramebufferZero(framebufferZero);
232 setColorbufferZero(colorbufferZero);
233 setDepthbufferZero(depthbufferZero);
234 setStencilbufferZero(stencilbufferZero);
235
236 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
237 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
238 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
239
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000240 if (!mHasBeenCurrent)
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000241 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000242 mState.viewportX = 0;
243 mState.viewportY = 0;
244 mState.viewportWidth = surface->getWidth();
245 mState.viewportHeight = surface->getHeight();
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000246
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000247 mState.scissorX = 0;
248 mState.scissorY = 0;
249 mState.scissorWidth = surface->getWidth();
250 mState.scissorHeight = surface->getHeight();
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000251
252 mHasBeenCurrent = true;
253 }
254
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 defaultRenderTarget->Release();
256
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000257 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000259 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000261
262 D3DCAPS9 capabilities;
263 device->GetDeviceCaps(&capabilities);
264
265 if (capabilities.PixelShaderVersion == D3DPS_VERSION(3, 0))
266 {
267 mPsProfile = "ps_3_0";
268 mVsProfile = "vs_3_0";
269 }
270 else // egl::Display guarantees support for at least 2.0
271 {
272 mPsProfile = "ps_2_0";
273 mVsProfile = "vs_2_0";
274 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275}
276
277void Context::setClearColor(float red, float green, float blue, float alpha)
278{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000279 mState.colorClearValue.red = red;
280 mState.colorClearValue.green = green;
281 mState.colorClearValue.blue = blue;
282 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283}
284
285void Context::setClearDepth(float depth)
286{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000287 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288}
289
290void Context::setClearStencil(int stencil)
291{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000292 mState.stencilClearValue = stencil;
293}
294
295void Context::setCullFace(bool enabled)
296{
297 mState.cullFace = enabled;
298}
299
300bool Context::isCullFaceEnabled() const
301{
302 return mState.cullFace;
303}
304
305void Context::setCullMode(GLenum mode)
306{
307 mState.cullMode = mode;
308}
309
310void Context::setFrontFace(GLenum front)
311{
312 mState.frontFace = front;
313}
314
315void Context::setDepthTest(bool enabled)
316{
317 mState.depthTest = enabled;
318}
319
320bool Context::isDepthTestEnabled() const
321{
322 return mState.depthTest;
323}
324
325void Context::setDepthFunc(GLenum depthFunc)
326{
327 mState.depthFunc = depthFunc;
328}
329
330void Context::setDepthRange(float zNear, float zFar)
331{
332 mState.zNear = zNear;
333 mState.zFar = zFar;
334}
335
336void Context::setBlend(bool enabled)
337{
338 mState.blend = enabled;
339}
340
341bool Context::isBlendEnabled() const
342{
343 return mState.blend;
344}
345
346void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
347{
348 mState.sourceBlendRGB = sourceRGB;
349 mState.destBlendRGB = destRGB;
350 mState.sourceBlendAlpha = sourceAlpha;
351 mState.destBlendAlpha = destAlpha;
352}
353
354void Context::setBlendColor(float red, float green, float blue, float alpha)
355{
356 mState.blendColor.red = red;
357 mState.blendColor.green = green;
358 mState.blendColor.blue = blue;
359 mState.blendColor.alpha = alpha;
360}
361
362void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
363{
364 mState.blendEquationRGB = rgbEquation;
365 mState.blendEquationAlpha = alphaEquation;
366}
367
368void Context::setStencilTest(bool enabled)
369{
370 mState.stencilTest = enabled;
371}
372
373bool Context::isStencilTestEnabled() const
374{
375 return mState.stencilTest;
376}
377
378void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
379{
380 mState.stencilFunc = stencilFunc;
381 mState.stencilRef = stencilRef;
382 mState.stencilMask = stencilMask;
383}
384
385void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
386{
387 mState.stencilBackFunc = stencilBackFunc;
388 mState.stencilBackRef = stencilBackRef;
389 mState.stencilBackMask = stencilBackMask;
390}
391
392void Context::setStencilWritemask(GLuint stencilWritemask)
393{
394 mState.stencilWritemask = stencilWritemask;
395}
396
397void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
398{
399 mState.stencilBackWritemask = stencilBackWritemask;
400}
401
402void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
403{
404 mState.stencilFail = stencilFail;
405 mState.stencilPassDepthFail = stencilPassDepthFail;
406 mState.stencilPassDepthPass = stencilPassDepthPass;
407}
408
409void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
410{
411 mState.stencilBackFail = stencilBackFail;
412 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
413 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
414}
415
416void Context::setPolygonOffsetFill(bool enabled)
417{
418 mState.polygonOffsetFill = enabled;
419}
420
421bool Context::isPolygonOffsetFillEnabled() const
422{
423 return mState.polygonOffsetFill;
424}
425
426void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
427{
428 mState.polygonOffsetFactor = factor;
429 mState.polygonOffsetUnits = units;
430}
431
432void Context::setSampleAlphaToCoverage(bool enabled)
433{
434 mState.sampleAlphaToCoverage = enabled;
435}
436
437bool Context::isSampleAlphaToCoverageEnabled() const
438{
439 return mState.sampleAlphaToCoverage;
440}
441
442void Context::setSampleCoverage(bool enabled)
443{
444 mState.sampleCoverage = enabled;
445}
446
447bool Context::isSampleCoverageEnabled() const
448{
449 return mState.sampleCoverage;
450}
451
452void Context::setSampleCoverageParams(GLclampf value, GLboolean invert)
453{
454 mState.sampleCoverageValue = value;
455 mState.sampleCoverageInvert = invert;
456}
457
458void Context::setScissorTest(bool enabled)
459{
460 mState.scissorTest = enabled;
461}
462
463bool Context::isScissorTestEnabled() const
464{
465 return mState.scissorTest;
466}
467
468void Context::setDither(bool enabled)
469{
470 mState.dither = enabled;
471}
472
473bool Context::isDitherEnabled() const
474{
475 return mState.dither;
476}
477
478void Context::setLineWidth(GLfloat width)
479{
480 mState.lineWidth = width;
481}
482
483void Context::setGenerateMipmapHint(GLenum hint)
484{
485 mState.generateMipmapHint = hint;
486}
487
488void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
489{
490 mState.viewportX = x;
491 mState.viewportY = y;
492 mState.viewportWidth = width;
493 mState.viewportHeight = height;
494}
495
496void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
497{
498 mState.scissorX = x;
499 mState.scissorY = y;
500 mState.scissorWidth = width;
501 mState.scissorHeight = height;
502}
503
504void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
505{
506 mState.colorMaskRed = red;
507 mState.colorMaskGreen = green;
508 mState.colorMaskBlue = blue;
509 mState.colorMaskAlpha = alpha;
510}
511
512void Context::setDepthMask(bool mask)
513{
514 mState.depthMask = mask;
515}
516
517void Context::setActiveSampler(int active)
518{
519 mState.activeSampler = active;
520}
521
522GLuint Context::getFramebufferHandle() const
523{
524 return mState.framebuffer;
525}
526
527GLuint Context::getRenderbufferHandle() const
528{
529 return mState.renderbuffer;
530}
531
532GLuint Context::getArrayBufferHandle() const
533{
534 return mState.arrayBuffer;
535}
536
537void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
538{
539 mState.vertexAttribute[attribNum].mEnabled = enabled;
540}
541
542const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
543{
544 return mState.vertexAttribute[attribNum];
545}
546
547void Context::setVertexAttribState(unsigned int attribNum, GLuint boundBuffer, GLint size, GLenum type, bool normalized,
548 GLsizei stride, const void *pointer)
549{
550 mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer;
551 mState.vertexAttribute[attribNum].mSize = size;
552 mState.vertexAttribute[attribNum].mType = type;
553 mState.vertexAttribute[attribNum].mNormalized = normalized;
554 mState.vertexAttribute[attribNum].mStride = stride;
555 mState.vertexAttribute[attribNum].mPointer = pointer;
556}
557
558const void *Context::getVertexAttribPointer(unsigned int attribNum) const
559{
560 return mState.vertexAttribute[attribNum].mPointer;
561}
562
563// returns entire set of attributes as a block
564const AttributeState *Context::getVertexAttribBlock()
565{
566 return mState.vertexAttribute;
567}
568
569void Context::setPackAlignment(GLint alignment)
570{
571 mState.packAlignment = alignment;
572}
573
574GLint Context::getPackAlignment() const
575{
576 return mState.packAlignment;
577}
578
579void Context::setUnpackAlignment(GLint alignment)
580{
581 mState.unpackAlignment = alignment;
582}
583
584GLint Context::getUnpackAlignment() const
585{
586 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000587}
588
589// Returns an unused buffer name
590GLuint Context::createBuffer()
591{
592 unsigned int handle = 1;
593
594 while (mBufferMap.find(handle) != mBufferMap.end())
595 {
596 handle++;
597 }
598
599 mBufferMap[handle] = NULL;
600
601 return handle;
602}
603
604// Returns an unused shader/program name
605GLuint Context::createShader(GLenum type)
606{
607 unsigned int handle = 1;
608
609 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
610 {
611 handle++;
612 }
613
614 if (type == GL_VERTEX_SHADER)
615 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000616 mShaderMap[handle] = new VertexShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617 }
618 else if (type == GL_FRAGMENT_SHADER)
619 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000620 mShaderMap[handle] = new FragmentShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621 }
622 else UNREACHABLE();
623
624 return handle;
625}
626
627// Returns an unused program/shader name
628GLuint Context::createProgram()
629{
630 unsigned int handle = 1;
631
632 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
633 {
634 handle++;
635 }
636
637 mProgramMap[handle] = new Program();
638
639 return handle;
640}
641
642// Returns an unused texture name
643GLuint Context::createTexture()
644{
645 unsigned int handle = 1;
646
647 while (mTextureMap.find(handle) != mTextureMap.end())
648 {
649 handle++;
650 }
651
652 mTextureMap[handle] = NULL;
653
654 return handle;
655}
656
657// Returns an unused framebuffer name
658GLuint Context::createFramebuffer()
659{
660 unsigned int handle = 1;
661
662 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
663 {
664 handle++;
665 }
666
667 mFramebufferMap[handle] = NULL;
668
669 return handle;
670}
671
672// Returns an unused renderbuffer name
673GLuint Context::createRenderbuffer()
674{
675 unsigned int handle = 1;
676
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000677 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 {
679 handle++;
680 }
681
682 mRenderbufferMap[handle] = NULL;
683
684 return handle;
685}
686
687void Context::deleteBuffer(GLuint buffer)
688{
689 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
690
691 if (bufferObject != mBufferMap.end())
692 {
693 detachBuffer(buffer);
694
695 delete bufferObject->second;
696 mBufferMap.erase(bufferObject);
697 }
698}
699
700void Context::deleteShader(GLuint shader)
701{
702 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
703
704 if (shaderObject != mShaderMap.end())
705 {
706 if (!shaderObject->second->isAttached())
707 {
708 delete shaderObject->second;
709 mShaderMap.erase(shaderObject);
710 }
711 else
712 {
713 shaderObject->second->flagForDeletion();
714 }
715 }
716}
717
718void Context::deleteProgram(GLuint program)
719{
720 ProgramMap::iterator programObject = mProgramMap.find(program);
721
722 if (programObject != mProgramMap.end())
723 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000724 if (program != mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725 {
726 delete programObject->second;
727 mProgramMap.erase(programObject);
728 }
729 else
730 {
731 programObject->second->flagForDeletion();
732 }
733 }
734}
735
736void Context::deleteTexture(GLuint texture)
737{
738 TextureMap::iterator textureObject = mTextureMap.find(texture);
739
740 if (textureObject != mTextureMap.end())
741 {
742 detachTexture(texture);
743
744 if (texture != 0)
745 {
746 delete textureObject->second;
747 }
748
749 mTextureMap.erase(textureObject);
750 }
751}
752
753void Context::deleteFramebuffer(GLuint framebuffer)
754{
755 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
756
757 if (framebufferObject != mFramebufferMap.end())
758 {
759 detachFramebuffer(framebuffer);
760
761 delete framebufferObject->second;
762 mFramebufferMap.erase(framebufferObject);
763 }
764}
765
766void Context::deleteRenderbuffer(GLuint renderbuffer)
767{
768 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
769
770 if (renderbufferObject != mRenderbufferMap.end())
771 {
772 detachRenderbuffer(renderbuffer);
773
774 delete renderbufferObject->second;
775 mRenderbufferMap.erase(renderbufferObject);
776 }
777}
778
779void Context::bindArrayBuffer(unsigned int buffer)
780{
781 if (buffer != 0 && !getBuffer(buffer))
782 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000783 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784 }
785
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000786 mState.arrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787}
788
789void Context::bindElementArrayBuffer(unsigned int buffer)
790{
791 if (buffer != 0 && !getBuffer(buffer))
792 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000793 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794 }
795
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796 mState.elementArrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
799void Context::bindTexture2D(GLuint texture)
800{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000801 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000803 mTextureMap[texture] = new Texture2D(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804 }
805
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000806 mState.texture2D = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000808 mState.samplerTexture[SAMPLER_2D][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
811void Context::bindTextureCubeMap(GLuint texture)
812{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000813 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000815 mTextureMap[texture] = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816 }
817
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000818 mState.textureCubeMap = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000820 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
823void Context::bindFramebuffer(GLuint framebuffer)
824{
825 if (!getFramebuffer(framebuffer))
826 {
827 mFramebufferMap[framebuffer] = new Framebuffer();
828 }
829
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000830 mState.framebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
833void Context::bindRenderbuffer(GLuint renderbuffer)
834{
835 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
836 {
837 mRenderbufferMap[renderbuffer] = new Renderbuffer();
838 }
839
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000840 mState.renderbuffer = renderbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841}
842
843void Context::useProgram(GLuint program)
844{
845 Program *programObject = getCurrentProgram();
846
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000847 GLuint priorProgram = mState.currentProgram;
848 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850 if (programObject && programObject->isFlaggedForDeletion())
851 {
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000852 deleteProgram(priorProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
856void Context::setFramebufferZero(Framebuffer *buffer)
857{
858 delete mFramebufferMap[0];
859 mFramebufferMap[0] = buffer;
860}
861
862void Context::setColorbufferZero(Colorbuffer *buffer)
863{
864 delete mColorbufferZero;
865 mColorbufferZero = buffer;
866}
867
868void Context::setDepthbufferZero(Depthbuffer *buffer)
869{
870 delete mDepthbufferZero;
871 mDepthbufferZero = buffer;
872}
873
874void Context::setStencilbufferZero(Stencilbuffer *buffer)
875{
876 delete mStencilbufferZero;
877 mStencilbufferZero = buffer;
878}
879
880void Context::setRenderbuffer(Renderbuffer *buffer)
881{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000882 delete mRenderbufferMap[mState.renderbuffer];
883 mRenderbufferMap[mState.renderbuffer] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
886Buffer *Context::getBuffer(unsigned int handle)
887{
888 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000889
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890 if (buffer == mBufferMap.end())
891 {
892 return NULL;
893 }
894 else
895 {
896 return buffer->second;
897 }
898}
899
900Shader *Context::getShader(unsigned int handle)
901{
902 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000903
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904 if (shader == mShaderMap.end())
905 {
906 return NULL;
907 }
908 else
909 {
910 return shader->second;
911 }
912}
913
914Program *Context::getProgram(unsigned int handle)
915{
916 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918 if (program == mProgramMap.end())
919 {
920 return NULL;
921 }
922 else
923 {
924 return program->second;
925 }
926}
927
928Texture *Context::getTexture(unsigned int handle)
929{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000930 if (handle == 0) return NULL;
931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934 if (texture == mTextureMap.end())
935 {
936 return NULL;
937 }
938 else
939 {
940 return texture->second;
941 }
942}
943
944Framebuffer *Context::getFramebuffer(unsigned int handle)
945{
946 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000947
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948 if (framebuffer == mFramebufferMap.end())
949 {
950 return NULL;
951 }
952 else
953 {
954 return framebuffer->second;
955 }
956}
957
958Renderbuffer *Context::getRenderbuffer(unsigned int handle)
959{
960 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962 if (renderbuffer == mRenderbufferMap.end())
963 {
964 return NULL;
965 }
966 else
967 {
968 return renderbuffer->second;
969 }
970}
971
972Colorbuffer *Context::getColorbuffer(GLuint handle)
973{
974 if (handle != 0)
975 {
976 Renderbuffer *renderbuffer = getRenderbuffer(handle);
977
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000978 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979 {
980 return static_cast<Colorbuffer*>(renderbuffer);
981 }
982 }
983 else // Special case: 0 refers to different initial render targets based on the attachment type
984 {
985 return mColorbufferZero;
986 }
987
988 return NULL;
989}
990
991Depthbuffer *Context::getDepthbuffer(GLuint handle)
992{
993 if (handle != 0)
994 {
995 Renderbuffer *renderbuffer = getRenderbuffer(handle);
996
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000997 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998 {
999 return static_cast<Depthbuffer*>(renderbuffer);
1000 }
1001 }
1002 else // Special case: 0 refers to different initial render targets based on the attachment type
1003 {
1004 return mDepthbufferZero;
1005 }
1006
1007 return NULL;
1008}
1009
1010Stencilbuffer *Context::getStencilbuffer(GLuint handle)
1011{
1012 if (handle != 0)
1013 {
1014 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1015
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001016 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017 {
1018 return static_cast<Stencilbuffer*>(renderbuffer);
1019 }
1020 }
1021 else
1022 {
1023 return mStencilbufferZero;
1024 }
1025
1026 return NULL;
1027}
1028
1029Buffer *Context::getArrayBuffer()
1030{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001031 return getBuffer(mState.arrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032}
1033
1034Buffer *Context::getElementArrayBuffer()
1035{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001036 return getBuffer(mState.elementArrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037}
1038
1039Program *Context::getCurrentProgram()
1040{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001041 return getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042}
1043
1044Texture2D *Context::getTexture2D()
1045{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001046 if (mState.texture2D == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047 {
1048 return mTexture2DZero;
1049 }
1050
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001051 return (Texture2D*)getTexture(mState.texture2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052}
1053
1054TextureCubeMap *Context::getTextureCubeMap()
1055{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001056 if (mState.textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057 {
1058 return mTextureCubeMapZero;
1059 }
1060
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001061 return (TextureCubeMap*)getTexture(mState.textureCubeMap);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062}
1063
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001064Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001066 GLuint texid = mState.samplerTexture[type][sampler];
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001067
1068 if (texid == 0)
1069 {
1070 switch (type)
1071 {
1072 default: UNREACHABLE();
1073 case SAMPLER_2D: return mTexture2DZero;
1074 case SAMPLER_CUBE: return mTextureCubeMapZero;
1075 }
1076 }
1077
1078 return getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081Framebuffer *Context::getFramebuffer()
1082{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001083 return getFramebuffer(mState.framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001086bool Context::getBooleanv(GLenum pname, GLboolean *params)
1087{
1088 switch (pname)
1089 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001090 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1091 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1092 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001093 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001094 params[0] = mState.colorMaskRed;
1095 params[1] = mState.colorMaskGreen;
1096 params[2] = mState.colorMaskBlue;
1097 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001098 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001099 case GL_CULL_FACE: *params = mState.cullFace;
1100 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1101 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1102 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1103 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1104 case GL_STENCIL_TEST: *params = mState.stencilTest;
1105 case GL_DEPTH_TEST: *params = mState.depthTest;
1106 case GL_BLEND: *params = mState.blend;
1107 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001108 default:
1109 return false;
1110 }
1111
1112 return true;
1113}
1114
1115bool Context::getFloatv(GLenum pname, GLfloat *params)
1116{
1117 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1118 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1119 // GetIntegerv as its native query function. As it would require conversion in any
1120 // case, this should make no difference to the calling application.
1121 switch (pname)
1122 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001123 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1124 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1125 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1126 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1127 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 case GL_ALIASED_LINE_WIDTH_RANGE:
1129 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1130 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1131 break;
1132 case GL_ALIASED_POINT_SIZE_RANGE:
1133 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1134 params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX;
1135 break;
1136 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001137 params[0] = mState.zNear;
1138 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001139 break;
1140 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001141 params[0] = mState.colorClearValue.red;
1142 params[1] = mState.colorClearValue.green;
1143 params[2] = mState.colorClearValue.blue;
1144 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001145 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001146 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001147 params[0] = mState.blendColor.red;
1148 params[1] = mState.blendColor.green;
1149 params[2] = mState.blendColor.blue;
1150 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001151 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 default:
1153 return false;
1154 }
1155
1156 return true;
1157}
1158
1159bool Context::getIntegerv(GLenum pname, GLint *params)
1160{
1161 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1162 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1163 // GetIntegerv as its native query function. As it would require conversion in any
1164 // case, this should make no difference to the calling application. You may find it in
1165 // Context::getFloatv.
1166 switch (pname)
1167 {
1168 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1169 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1170 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1171 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1172 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1173 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1174 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1175 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
1176 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1177 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1178 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1179 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001180 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer; break;
1181 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer; break;
1182 case GL_FRAMEBUFFER_BINDING: *params = mState.framebuffer; break;
1183 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer; break;
1184 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1185 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1186 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1187 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1188 case GL_ACTIVE_TEXTURE: *params = mState.activeSampler; break;
1189 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1190 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1191 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1192 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1193 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1194 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1195 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1196 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1197 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1198 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1199 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1200 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1201 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1202 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1203 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1204 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1205 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1206 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1207 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1208 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1209 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1210 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001211 case GL_SUBPIXEL_BITS: *params = 4; break;
1212 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1213 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
1214 case GL_SAMPLE_BUFFERS: *params = 0; break;
1215 case GL_SAMPLES: *params = 0; break;
1216 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1217 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1218 case GL_MAX_VIEWPORT_DIMS:
1219 {
1220 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1221 params[0] = maxDimension;
1222 params[1] = maxDimension;
1223 }
1224 break;
1225 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001226 params[0] = mState.viewportX;
1227 params[1] = mState.viewportY;
1228 params[2] = mState.viewportWidth;
1229 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001230 break;
1231 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001232 params[0] = mState.scissorX;
1233 params[1] = mState.scissorY;
1234 params[2] = mState.scissorWidth;
1235 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001236 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001237 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1238 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001239 case GL_RED_BITS:
1240 case GL_GREEN_BITS:
1241 case GL_BLUE_BITS:
1242 case GL_ALPHA_BITS:
1243 {
1244 gl::Framebuffer *framebuffer = getFramebuffer();
1245 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1246
1247 if (colorbuffer)
1248 {
1249 switch (pname)
1250 {
1251 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1252 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1253 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1254 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1255 }
1256 }
1257 else
1258 {
1259 *params = 0;
1260 }
1261 }
1262 break;
1263 case GL_DEPTH_BITS:
1264 {
1265 gl::Framebuffer *framebuffer = getFramebuffer();
1266 gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer();
1267
1268 if (depthbuffer)
1269 {
1270 *params = depthbuffer->getDepthSize();
1271 }
1272 else
1273 {
1274 *params = 0;
1275 }
1276 }
1277 break;
1278 case GL_STENCIL_BITS:
1279 {
1280 gl::Framebuffer *framebuffer = getFramebuffer();
1281 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1282
1283 if (stencilbuffer)
1284 {
1285 *params = stencilbuffer->getStencilSize();
1286 }
1287 else
1288 {
1289 *params = 0;
1290 }
1291 }
1292 break;
1293 case GL_TEXTURE_BINDING_2D:
1294 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001295 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296 {
1297 error(GL_INVALID_OPERATION);
1298 return false;
1299 }
1300
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001301 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001302 }
1303 break;
1304 case GL_TEXTURE_BINDING_CUBE_MAP:
1305 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001306 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001307 {
1308 error(GL_INVALID_OPERATION);
1309 return false;
1310 }
1311
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001312 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001313 }
1314 break;
1315 default:
1316 return false;
1317 }
1318
1319 return true;
1320}
1321
1322bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1323{
1324 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1325 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1326 // to the fact that it is stored internally as a float, and so would require conversion
1327 // if returned from Context::getIntegerv. Since this conversion is already implemented
1328 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1329 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1330 // application.
1331 switch (pname)
1332 {
1333 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1334 case GL_SHADER_BINARY_FORMATS:
1335 {
1336 *type = GL_INT;
1337 *numParams = 0;
1338 }
1339 break;
1340 case GL_MAX_VERTEX_ATTRIBS:
1341 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1342 case GL_MAX_VARYING_VECTORS:
1343 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1344 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1345 case GL_MAX_TEXTURE_IMAGE_UNITS:
1346 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1347 case GL_MAX_RENDERBUFFER_SIZE:
1348 case GL_NUM_SHADER_BINARY_FORMATS:
1349 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1350 case GL_ARRAY_BUFFER_BINDING:
1351 case GL_FRAMEBUFFER_BINDING:
1352 case GL_RENDERBUFFER_BINDING:
1353 case GL_CURRENT_PROGRAM:
1354 case GL_PACK_ALIGNMENT:
1355 case GL_UNPACK_ALIGNMENT:
1356 case GL_GENERATE_MIPMAP_HINT:
1357 case GL_RED_BITS:
1358 case GL_GREEN_BITS:
1359 case GL_BLUE_BITS:
1360 case GL_ALPHA_BITS:
1361 case GL_DEPTH_BITS:
1362 case GL_STENCIL_BITS:
1363 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1364 case GL_CULL_FACE_MODE:
1365 case GL_FRONT_FACE:
1366 case GL_ACTIVE_TEXTURE:
1367 case GL_STENCIL_FUNC:
1368 case GL_STENCIL_VALUE_MASK:
1369 case GL_STENCIL_REF:
1370 case GL_STENCIL_FAIL:
1371 case GL_STENCIL_PASS_DEPTH_FAIL:
1372 case GL_STENCIL_PASS_DEPTH_PASS:
1373 case GL_STENCIL_BACK_FUNC:
1374 case GL_STENCIL_BACK_VALUE_MASK:
1375 case GL_STENCIL_BACK_REF:
1376 case GL_STENCIL_BACK_FAIL:
1377 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1378 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1379 case GL_DEPTH_FUNC:
1380 case GL_BLEND_SRC_RGB:
1381 case GL_BLEND_SRC_ALPHA:
1382 case GL_BLEND_DST_RGB:
1383 case GL_BLEND_DST_ALPHA:
1384 case GL_BLEND_EQUATION_RGB:
1385 case GL_BLEND_EQUATION_ALPHA:
1386 case GL_STENCIL_WRITEMASK:
1387 case GL_STENCIL_BACK_WRITEMASK:
1388 case GL_STENCIL_CLEAR_VALUE:
1389 case GL_SUBPIXEL_BITS:
1390 case GL_MAX_TEXTURE_SIZE:
1391 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1392 case GL_SAMPLE_BUFFERS:
1393 case GL_SAMPLES:
1394 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1395 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1396 case GL_TEXTURE_BINDING_2D:
1397 case GL_TEXTURE_BINDING_CUBE_MAP:
1398 {
1399 *type = GL_INT;
1400 *numParams = 1;
1401 }
1402 break;
1403 case GL_MAX_VIEWPORT_DIMS:
1404 {
1405 *type = GL_INT;
1406 *numParams = 2;
1407 }
1408 break;
1409 case GL_VIEWPORT:
1410 case GL_SCISSOR_BOX:
1411 {
1412 *type = GL_INT;
1413 *numParams = 4;
1414 }
1415 break;
1416 case GL_SHADER_COMPILER:
1417 case GL_SAMPLE_COVERAGE_INVERT:
1418 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001419 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1420 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1421 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1422 case GL_SAMPLE_COVERAGE:
1423 case GL_SCISSOR_TEST:
1424 case GL_STENCIL_TEST:
1425 case GL_DEPTH_TEST:
1426 case GL_BLEND:
1427 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001428 {
1429 *type = GL_BOOL;
1430 *numParams = 1;
1431 }
1432 break;
1433 case GL_COLOR_WRITEMASK:
1434 {
1435 *type = GL_BOOL;
1436 *numParams = 4;
1437 }
1438 break;
1439 case GL_POLYGON_OFFSET_FACTOR:
1440 case GL_POLYGON_OFFSET_UNITS:
1441 case GL_SAMPLE_COVERAGE_VALUE:
1442 case GL_DEPTH_CLEAR_VALUE:
1443 case GL_LINE_WIDTH:
1444 {
1445 *type = GL_FLOAT;
1446 *numParams = 1;
1447 }
1448 break;
1449 case GL_ALIASED_LINE_WIDTH_RANGE:
1450 case GL_ALIASED_POINT_SIZE_RANGE:
1451 case GL_DEPTH_RANGE:
1452 {
1453 *type = GL_FLOAT;
1454 *numParams = 2;
1455 }
1456 break;
1457 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001458 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 {
1460 *type = GL_FLOAT;
1461 *numParams = 4;
1462 }
1463 break;
1464 default:
1465 return false;
1466 }
1467
1468 return true;
1469}
1470
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001471// Applies the render target surface, depth stencil surface, viewport rectangle and
1472// scissor rectangle to the Direct3D 9 device
1473bool Context::applyRenderTarget(bool ignoreViewport)
1474{
1475 IDirect3DDevice9 *device = getDevice();
1476 Framebuffer *framebufferObject = getFramebuffer();
1477
1478 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1479 {
1480 return false;
1481 }
1482
1483 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1484 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1485
1486 device->SetRenderTarget(0, renderTarget);
1487 device->SetDepthStencilSurface(depthStencil);
1488
1489 D3DVIEWPORT9 viewport;
1490 D3DSURFACE_DESC desc;
1491 renderTarget->GetDesc(&desc);
1492
1493 if (ignoreViewport)
1494 {
1495 viewport.X = 0;
1496 viewport.Y = 0;
1497 viewport.Width = desc.Width;
1498 viewport.Height = desc.Height;
1499 viewport.MinZ = 0.0f;
1500 viewport.MaxZ = 1.0f;
1501 }
1502 else
1503 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001504 viewport.X = std::max(mState.viewportX, 0);
1505 viewport.Y = std::max(mState.viewportY, 0);
1506 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1507 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1508 viewport.MinZ = clamp01(mState.zNear);
1509 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 }
1511
1512 device->SetViewport(&viewport);
1513
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001514 if (mState.scissorTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001516 RECT rect = {mState.scissorX,
1517 mState.scissorY,
1518 mState.scissorX + mState.scissorWidth,
1519 mState.scissorY + mState.scissorHeight};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520
1521 device->SetScissorRect(&rect);
1522 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1523 }
1524 else
1525 {
1526 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1527 }
1528
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001529 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 Program *programObject = getCurrentProgram();
1532
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001533 GLint halfPixelSize = programObject->getUniformLocation("dx_HalfPixelSize");
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001534 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001535 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001536
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001537 GLint window = programObject->getUniformLocation("dx_Window");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001538 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1539 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1540 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001541 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1542
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001543 GLint depth = programObject->getUniformLocation("dx_Depth");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001544 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001545 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1546
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001547 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001548 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001549
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001550 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001551 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001552
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001553 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001554 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001555 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556 }
1557
1558 return true;
1559}
1560
1561// 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 +00001562void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563{
1564 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001565 Program *programObject = getCurrentProgram();
1566
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001567 GLint frontCCW = programObject->getUniformLocation("dx_FrontCCW");
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001568 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001569 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001571 GLint pointsOrLines = programObject->getUniformLocation("dx_PointsOrLines");
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001572 GLint alwaysFront = !isTriangleMode(drawMode);
1573 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1574
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001575 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001577 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001578 }
1579 else
1580 {
1581 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1582 }
1583
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001584 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 {
1586 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001587 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 }
1589 else
1590 {
1591 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
1592 }
1593
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001594 if (mState.blend)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595 {
1596 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1597
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001598 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1599 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001601 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 }
1603 else
1604 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001605 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1606 unorm<8>(mState.blendColor.alpha),
1607 unorm<8>(mState.blendColor.alpha),
1608 unorm<8>(mState.blendColor.alpha)));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609 }
1610
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001611 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1612 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1613 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001615 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1616 mState.destBlendRGB != mState.destBlendAlpha ||
1617 mState.blendEquationRGB != mState.blendEquationAlpha)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 {
1619 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1620
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001621 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1622 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1623 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624
1625 }
1626 else
1627 {
1628 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1629 }
1630 }
1631 else
1632 {
1633 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1634 }
1635
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001636 if (mState.stencilTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001637 {
1638 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1639 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1640
1641 // FIXME: Unsupported by D3D9
1642 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1643 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1644 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001645 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1646 mState.stencilRef != mState.stencilBackRef ||
1647 mState.stencilMask != mState.stencilBackMask)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001648 {
1649 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1650 return error(GL_INVALID_OPERATION);
1651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001653 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1654 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1655 es2dx::ConvertComparison(mState.stencilFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001657 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, mState.stencilRef); // FIXME: Clamp to range
1658 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001660 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1661 es2dx::ConvertStencilOp(mState.stencilFail));
1662 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1663 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1664 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1665 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001667 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1668 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1669 es2dx::ConvertComparison(mState.stencilBackFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001671 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, mState.stencilBackRef); // FIXME: Clamp to range
1672 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001674 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1675 es2dx::ConvertStencilOp(mState.stencilBackFail));
1676 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1677 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1678 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1679 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 }
1681 else
1682 {
1683 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1684 }
1685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001686 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1687 mState.colorMaskBlue, mState.colorMaskAlpha));
1688 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001690 if (mState.polygonOffsetFill)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 {
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001692 gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer();
1693 if (depthbuffer)
1694 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1696 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001697 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1698 }
1699 }
1700 else
1701 {
1702 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1703 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 }
1705
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001706 if (mConfig->mMultiSample != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001708 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001709 {
1710 FIXME("Sample alpha to coverage is unimplemented.");
1711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001713 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001714 {
1715 FIXME("Sample coverage is unimplemented.");
1716 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 }
1718
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001719 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720}
1721
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001722// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001723void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001725 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001727 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001729 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 }
1731 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001732}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001734// The indices parameter to glDrawElements can have two interpretations:
1735// - as a pointer into client memory
1736// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
1737// Handle these cases here and return a pointer to the index data.
1738const Index *Context::adjustIndexPointer(const void *indices)
1739{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001740 if (mState.elementArrayBuffer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001741 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001742 Buffer *buffer = getBuffer(mState.elementArrayBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001743 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
1744 }
1745 else
1746 {
1747 return static_cast<const Index*>(indices);
1748 }
1749}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001751void Context::applyVertexBuffer(GLint first, GLsizei count)
1752{
1753 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1754
1755 mVertexDataManager->preRenderValidate(first, count, translated);
1756
1757 lookupAttributeMapping(translated);
1758
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001759 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001760}
1761
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001762void Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001763{
1764 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1765
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001766 mVertexDataManager->preRenderValidate(indexInfo, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001767
1768 lookupAttributeMapping(translated);
1769
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001770 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001771}
1772
1773// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001774TranslatedIndexData Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001775{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001776 TranslatedIndexData indexInfo = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(mState.elementArrayBuffer), indices);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001777 mBufferBackEnd->setupIndicesPreDraw(indexInfo);
1778 return indexInfo;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779}
1780
1781// Applies the shaders and shader constants to the Direct3D 9 device
1782void Context::applyShaders()
1783{
1784 IDirect3DDevice9 *device = getDevice();
1785 Program *programObject = getCurrentProgram();
1786 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1787 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1788
1789 device->SetVertexShader(vertexShader);
1790 device->SetPixelShader(pixelShader);
1791
1792 programObject->applyUniforms();
1793}
1794
1795// Applies the textures and sampler states to the Direct3D 9 device
1796void Context::applyTextures()
1797{
1798 IDirect3DDevice9 *device = getDevice();
1799 Program *programObject = getCurrentProgram();
1800
1801 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1802 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001803 int textureUnit = programObject->getSamplerMapping(sampler);
1804 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001806 SamplerType textureType = programObject->getSamplerType(sampler);
1807
1808 Texture *texture = getSamplerTexture(textureUnit, textureType);
1809
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001810 if (texture->isComplete())
1811 {
1812 GLenum wrapS = texture->getWrapS();
1813 GLenum wrapT = texture->getWrapT();
1814 GLenum minFilter = texture->getMinFilter();
1815 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001817 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1818 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001820 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1821 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1822 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1823 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1824 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001826 device->SetTexture(sampler, texture->getTexture());
1827 }
1828 else
1829 {
1830 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001833 else
1834 {
1835 device->SetTexture(sampler, NULL);
1836 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 }
1838}
1839
1840void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1841{
1842 Framebuffer *framebuffer = getFramebuffer();
1843 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1844 IDirect3DDevice9 *device = getDevice();
1845
1846 D3DSURFACE_DESC desc;
1847 renderTarget->GetDesc(&desc);
1848
1849 IDirect3DSurface9 *systemSurface;
1850 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1851
1852 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1853 {
1854 return error(GL_OUT_OF_MEMORY);
1855 }
1856
1857 ASSERT(SUCCEEDED(result));
1858
1859 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1860 {
1861 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1862 }
1863
1864 result = device->GetRenderTargetData(renderTarget, systemSurface);
1865
1866 if (result == D3DERR_DRIVERINTERNALERROR)
1867 {
1868 systemSurface->Release();
1869
1870 return error(GL_OUT_OF_MEMORY);
1871 }
1872
1873 if (FAILED(result))
1874 {
1875 UNREACHABLE();
1876 systemSurface->Release();
1877
1878 return; // No sensible error to generate
1879 }
1880
1881 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001882 RECT rect = {std::max(x, 0),
1883 std::max(y, 0),
1884 std::min(x + width, (int)desc.Width),
1885 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886
1887 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1888
1889 if (FAILED(result))
1890 {
1891 UNREACHABLE();
1892 systemSurface->Release();
1893
1894 return; // No sensible error to generate
1895 }
1896
1897 unsigned char *source = (unsigned char*)lock.pBits;
1898 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001899 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001901 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00001902
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 for (int j = 0; j < rect.bottom - rect.top; j++)
1904 {
1905 for (int i = 0; i < rect.right - rect.left; i++)
1906 {
1907 float r;
1908 float g;
1909 float b;
1910 float a;
1911
1912 switch (desc.Format)
1913 {
1914 case D3DFMT_R5G6B5:
1915 {
1916 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1917
1918 a = 1.0f;
1919 b = (rgb & 0x001F) * (1.0f / 0x001F);
1920 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1921 r = (rgb & 0xF800) * (1.0f / 0xF800);
1922 }
1923 break;
1924 case D3DFMT_X1R5G5B5:
1925 {
1926 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1927
1928 a = 1.0f;
1929 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1930 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1931 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1932 }
1933 break;
1934 case D3DFMT_A1R5G5B5:
1935 {
1936 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1937
1938 a = (argb & 0x8000) ? 1.0f : 0.0f;
1939 b = (argb & 0x001F) * (1.0f / 0x001F);
1940 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1941 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1942 }
1943 break;
1944 case D3DFMT_A8R8G8B8:
1945 {
1946 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1947
1948 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1949 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1950 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1951 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1952 }
1953 break;
1954 case D3DFMT_X8R8G8B8:
1955 {
1956 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1957
1958 a = 1.0f;
1959 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1960 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1961 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1962 }
1963 break;
1964 case D3DFMT_A2R10G10B10:
1965 {
1966 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1967
1968 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1969 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1970 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1971 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1972 }
1973 break;
1974 default:
1975 UNIMPLEMENTED(); // FIXME
1976 UNREACHABLE();
1977 }
1978
1979 switch (format)
1980 {
1981 case GL_RGBA:
1982 switch (type)
1983 {
1984 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00001985 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
1986 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
1987 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
1988 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 break;
1990 default: UNREACHABLE();
1991 }
1992 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001993 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 switch (type)
1995 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001996 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00001997 dest16[i + j * outputPitch / sizeof(unsigned short)] =
1998 ((unsigned short)(31 * b + 0.5f) << 0) |
1999 ((unsigned short)(63 * g + 0.5f) << 5) |
2000 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 break;
2002 default: UNREACHABLE();
2003 }
2004 break;
2005 default: UNREACHABLE();
2006 }
2007 }
2008 }
2009
2010 systemSurface->UnlockRect();
2011
2012 systemSurface->Release();
2013}
2014
2015void Context::clear(GLbitfield mask)
2016{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002017 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 IDirect3DDevice9 *device = getDevice();
2019 DWORD flags = 0;
2020
2021 if (mask & GL_COLOR_BUFFER_BIT)
2022 {
2023 mask &= ~GL_COLOR_BUFFER_BIT;
2024 flags |= D3DCLEAR_TARGET;
2025 }
2026
2027 if (mask & GL_DEPTH_BUFFER_BIT)
2028 {
2029 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002030 if (mState.depthMask)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 {
2032 flags |= D3DCLEAR_ZBUFFER;
2033 }
2034 }
2035
2036 Framebuffer *framebufferObject = getFramebuffer();
2037 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
2038
2039 GLuint stencilUnmasked = 0x0;
2040
2041 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
2042 {
2043 D3DSURFACE_DESC desc;
2044 depthStencil->GetDesc(&desc);
2045
2046 mask &= ~GL_STENCIL_BUFFER_BIT;
2047 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2048 stencilUnmasked = (0x1 << stencilSize) - 1;
2049
2050 if (stencilUnmasked != 0x0)
2051 {
2052 flags |= D3DCLEAR_STENCIL;
2053 }
2054 }
2055
2056 if (mask != 0)
2057 {
2058 return error(GL_INVALID_VALUE);
2059 }
2060
2061 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
2062
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002063 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2064 unorm<8>(mState.colorClearValue.red),
2065 unorm<8>(mState.colorClearValue.green),
2066 unorm<8>(mState.colorClearValue.blue));
2067 float depth = clamp01(mState.depthClearValue);
2068 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
2070 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2071
2072 D3DSURFACE_DESC desc;
2073 renderTarget->GetDesc(&desc);
2074
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002075 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076
2077 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002078 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002080 !(mState.colorMaskRed && mState.colorMaskGreen &&
2081 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082
2083 if (needMaskedColorClear || needMaskedStencilClear)
2084 {
2085 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2086 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2087 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2088 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2089 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2090 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2091 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2092 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2093
2094 if (flags & D3DCLEAR_TARGET)
2095 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002096 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2097 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2098 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2099 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
2101 else
2102 {
2103 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2104 }
2105
2106 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2107 {
2108 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2109 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2110 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2111 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002112 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002113 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2115 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
2116 }
2117 else
2118 {
2119 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2120 }
2121
2122 device->SetPixelShader(NULL);
2123 device->SetVertexShader(NULL);
2124 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2125
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002126 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 {
2128 float x, y, z, w;
2129 D3DCOLOR diffuse;
2130 };
2131
2132 Vertex quad[4];
2133 quad[0].x = 0.0f;
2134 quad[0].y = (float)desc.Height;
2135 quad[0].z = 0.0f;
2136 quad[0].w = 1.0f;
2137 quad[0].diffuse = color;
2138
2139 quad[1].x = (float)desc.Width;
2140 quad[1].y = (float)desc.Height;
2141 quad[1].z = 0.0f;
2142 quad[1].w = 1.0f;
2143 quad[1].diffuse = color;
2144
2145 quad[2].x = 0.0f;
2146 quad[2].y = 0.0f;
2147 quad[2].z = 0.0f;
2148 quad[2].w = 1.0f;
2149 quad[2].diffuse = color;
2150
2151 quad[3].x = (float)desc.Width;
2152 quad[3].y = 0.0f;
2153 quad[3].z = 0.0f;
2154 quad[3].w = 1.0f;
2155 quad[3].diffuse = color;
2156
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002157 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159
2160 if (flags & D3DCLEAR_ZBUFFER)
2161 {
2162 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2163 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2164 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2165 }
2166 }
2167 else
2168 {
2169 device->Clear(0, NULL, flags, color, depth, stencil);
2170 }
2171}
2172
2173void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2174{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002175 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
2177 return error(GL_INVALID_OPERATION);
2178 }
2179
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002180 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 IDirect3DDevice9 *device = getDevice();
2182 D3DPRIMITIVETYPE primitiveType;
2183 int primitiveCount;
2184
2185 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2186 return error(GL_INVALID_ENUM);
2187
2188 if (primitiveCount <= 0)
2189 {
2190 return;
2191 }
2192
2193 if (!applyRenderTarget(false))
2194 {
2195 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2196 }
2197
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002198 applyState(mode);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002199 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200 applyShaders();
2201 applyTextures();
2202
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002203 if (!getCurrentProgram()->validateSamplers())
2204 {
2205 return error(GL_INVALID_OPERATION);
2206 }
2207
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002208 if (!cullSkipsDraw(mode))
2209 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002210 display->startScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00002211 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002212 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213}
2214
2215void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2216{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002217 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 {
2219 return error(GL_INVALID_OPERATION);
2220 }
2221
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002222 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 {
2224 return error(GL_INVALID_OPERATION);
2225 }
2226
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002227 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002228 IDirect3DDevice9 *device = getDevice();
2229 D3DPRIMITIVETYPE primitiveType;
2230 int primitiveCount;
2231
2232 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2233 return error(GL_INVALID_ENUM);
2234
2235 if (primitiveCount <= 0)
2236 {
2237 return;
2238 }
2239
2240 if (!applyRenderTarget(false))
2241 {
2242 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2243 }
2244
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002245 applyState(mode);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00002246 TranslatedIndexData indexInfo = applyIndexBuffer(indices, count, mode, type);
2247 applyVertexBuffer(indexInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 applyShaders();
2249 applyTextures();
2250
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002251 if (!getCurrentProgram()->validateSamplers())
2252 {
2253 return error(GL_INVALID_OPERATION);
2254 }
2255
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002256 if (!cullSkipsDraw(mode))
2257 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002258 display->startScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00002259 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/sizeof(Index), primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002260 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261}
2262
2263void Context::finish()
2264{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002265 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 IDirect3DDevice9 *device = getDevice();
2267 IDirect3DQuery9 *occlusionQuery = NULL;
2268
2269 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2270
2271 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2272 {
2273 return error(GL_OUT_OF_MEMORY);
2274 }
2275
2276 ASSERT(SUCCEEDED(result));
2277
2278 if (occlusionQuery)
2279 {
2280 occlusionQuery->Issue(D3DISSUE_BEGIN);
2281
2282 // Render something outside the render target
2283 device->SetPixelShader(NULL);
2284 device->SetVertexShader(NULL);
2285 device->SetFVF(D3DFVF_XYZRHW);
2286 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002287 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
2290 occlusionQuery->Issue(D3DISSUE_END);
2291
2292 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2293 {
2294 // Keep polling, but allow other threads to do something useful first
2295 Sleep(0);
2296 }
2297
2298 occlusionQuery->Release();
2299 }
2300}
2301
2302void Context::flush()
2303{
2304 IDirect3DDevice9 *device = getDevice();
2305 IDirect3DQuery9 *eventQuery = NULL;
2306
2307 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2308
2309 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2310 {
2311 return error(GL_OUT_OF_MEMORY);
2312 }
2313
2314 ASSERT(SUCCEEDED(result));
2315
2316 if (eventQuery)
2317 {
2318 eventQuery->Issue(D3DISSUE_END);
2319
2320 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2321 {
2322 // Keep polling, but allow other threads to do something useful first
2323 Sleep(0);
2324 }
2325
2326 eventQuery->Release();
2327 }
2328}
2329
2330void Context::recordInvalidEnum()
2331{
2332 mInvalidEnum = true;
2333}
2334
2335void Context::recordInvalidValue()
2336{
2337 mInvalidValue = true;
2338}
2339
2340void Context::recordInvalidOperation()
2341{
2342 mInvalidOperation = true;
2343}
2344
2345void Context::recordOutOfMemory()
2346{
2347 mOutOfMemory = true;
2348}
2349
2350void Context::recordInvalidFramebufferOperation()
2351{
2352 mInvalidFramebufferOperation = true;
2353}
2354
2355// Get one of the recorded errors and clear its flag, if any.
2356// [OpenGL ES 2.0.24] section 2.5 page 13.
2357GLenum Context::getError()
2358{
2359 if (mInvalidEnum)
2360 {
2361 mInvalidEnum = false;
2362
2363 return GL_INVALID_ENUM;
2364 }
2365
2366 if (mInvalidValue)
2367 {
2368 mInvalidValue = false;
2369
2370 return GL_INVALID_VALUE;
2371 }
2372
2373 if (mInvalidOperation)
2374 {
2375 mInvalidOperation = false;
2376
2377 return GL_INVALID_OPERATION;
2378 }
2379
2380 if (mOutOfMemory)
2381 {
2382 mOutOfMemory = false;
2383
2384 return GL_OUT_OF_MEMORY;
2385 }
2386
2387 if (mInvalidFramebufferOperation)
2388 {
2389 mInvalidFramebufferOperation = false;
2390
2391 return GL_INVALID_FRAMEBUFFER_OPERATION;
2392 }
2393
2394 return GL_NO_ERROR;
2395}
2396
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002397const char *Context::getPixelShaderProfile()
2398{
2399 return mPsProfile;
2400}
2401
2402const char *Context::getVertexShaderProfile()
2403{
2404 return mVsProfile;
2405}
2406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407void Context::detachBuffer(GLuint buffer)
2408{
2409 // [OpenGL ES 2.0.24] section 2.9 page 22:
2410 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2411 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2412
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002413 if (mState.arrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002415 mState.arrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
2417
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002418 if (mState.elementArrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002420 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422
2423 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2424 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002425 if (mState.vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002427 mState.vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 }
2429 }
2430}
2431
2432void Context::detachTexture(GLuint texture)
2433{
2434 // [OpenGL ES 2.0.24] section 3.8 page 84:
2435 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2436 // rebound to texture object zero
2437
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002438 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002440 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002442 if (mState.samplerTexture[type][sampler] == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002443 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002444 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 }
2447 }
2448
2449 // [OpenGL ES 2.0.24] section 4.4 page 112:
2450 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2451 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2452 // image was attached in the currently bound framebuffer.
2453
2454 Framebuffer *framebuffer = getFramebuffer();
2455
2456 if (framebuffer)
2457 {
2458 framebuffer->detachTexture(texture);
2459 }
2460}
2461
2462void Context::detachFramebuffer(GLuint framebuffer)
2463{
2464 // [OpenGL ES 2.0.24] section 4.4 page 107:
2465 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2466 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2467
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002468 if (mState.framebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 {
2470 bindFramebuffer(0);
2471 }
2472}
2473
2474void Context::detachRenderbuffer(GLuint renderbuffer)
2475{
2476 // [OpenGL ES 2.0.24] section 4.4 page 109:
2477 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2478 // had been executed with the target RENDERBUFFER and name of zero.
2479
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002480 if (mState.renderbuffer == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 {
2482 bindRenderbuffer(0);
2483 }
2484
2485 // [OpenGL ES 2.0.24] section 4.4 page 111:
2486 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2487 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2488 // point to which this image was attached in the currently bound framebuffer.
2489
2490 Framebuffer *framebuffer = getFramebuffer();
2491
2492 if (framebuffer)
2493 {
2494 framebuffer->detachRenderbuffer(renderbuffer);
2495 }
2496}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002497
2498Texture *Context::getIncompleteTexture(SamplerType type)
2499{
2500 Texture *t = mIncompleteTextures[type];
2501
2502 if (t == NULL)
2503 {
2504 static const GLubyte color[] = { 0, 0, 0, 255 };
2505
2506 switch (type)
2507 {
2508 default:
2509 UNREACHABLE();
2510 // default falls through to SAMPLER_2D
2511
2512 case SAMPLER_2D:
2513 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002514 Texture2D *incomplete2d = new Texture2D(this);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002515 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002516 t = incomplete2d;
2517 }
2518 break;
2519
2520 case SAMPLER_CUBE:
2521 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002522 TextureCubeMap *incompleteCube = new TextureCubeMap(this);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002523
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002524 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2525 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2526 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2527 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2528 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2529 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002530
2531 t = incompleteCube;
2532 }
2533 break;
2534 }
2535
2536 mIncompleteTextures[type] = t;
2537 }
2538
2539 return t;
2540}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002541
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002542bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002543{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002544 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002545}
2546
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002547bool Context::isTriangleMode(GLenum drawMode)
2548{
2549 switch (drawMode)
2550 {
2551 case GL_TRIANGLES:
2552 case GL_TRIANGLE_FAN:
2553 case GL_TRIANGLE_STRIP:
2554 return true;
2555 case GL_POINTS:
2556 case GL_LINES:
2557 case GL_LINE_LOOP:
2558 case GL_LINE_STRIP:
2559 return false;
2560 default: UNREACHABLE();
2561 }
2562
2563 return false;
2564}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002565
2566void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2567{
2568 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2569
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002570 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2571 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2572 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2573 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002574
2575 mVertexDataManager->dirtyCurrentValues();
2576}
2577
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578}
2579
2580extern "C"
2581{
2582gl::Context *glCreateContext(const egl::Config *config)
2583{
2584 return new gl::Context(config);
2585}
2586
2587void glDestroyContext(gl::Context *context)
2588{
2589 delete context;
2590
2591 if (context == gl::getContext())
2592 {
2593 gl::makeCurrent(NULL, NULL, NULL);
2594 }
2595}
2596
2597void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2598{
2599 gl::makeCurrent(context, display, surface);
2600}
2601
2602gl::Context *glGetCurrentContext()
2603{
2604 return gl::getContext();
2605}
2606}