blob: f21f8b6873b4ebdf705ed4c0e8930fbe79bfed2b [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);
40 depthClearValue = 1.0f;
41 stencilClearValue = 0;
42
43 cullFace = false;
44 cullMode = GL_BACK;
45 frontFace = GL_CCW;
46 depthTest = false;
47 depthFunc = GL_LESS;
48 blend = false;
49 sourceBlendRGB = GL_ONE;
50 sourceBlendAlpha = GL_ONE;
51 destBlendRGB = GL_ZERO;
52 destBlendAlpha = GL_ZERO;
53 blendEquationRGB = GL_FUNC_ADD;
54 blendEquationAlpha = GL_FUNC_ADD;
55 blendColor.red = 0;
56 blendColor.green = 0;
57 blendColor.blue = 0;
58 blendColor.alpha = 0;
59 stencilTest = false;
60 stencilFunc = GL_ALWAYS;
61 stencilRef = 0;
62 stencilMask = -1;
63 stencilWritemask = -1;
64 stencilBackFunc = GL_ALWAYS;
65 stencilBackRef = 0;
66 stencilBackMask = - 1;
67 stencilBackWritemask = -1;
68 stencilFail = GL_KEEP;
69 stencilPassDepthFail = GL_KEEP;
70 stencilPassDepthPass = GL_KEEP;
71 stencilBackFail = GL_KEEP;
72 stencilBackPassDepthFail = GL_KEEP;
73 stencilBackPassDepthPass = GL_KEEP;
74 polygonOffsetFill = false;
daniel@transgaming.com777f2672010-04-07 03:25:16 +000075 polygonOffsetFactor = 0.0f;
76 polygonOffsetUnits = 0.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 sampleAlphaToCoverage = false;
78 sampleCoverage = false;
79 sampleCoverageValue = 1.0f;
80 sampleCoverageInvert = GL_FALSE;
81 scissorTest = false;
82 dither = true;
daniel@transgaming.com5949aa12010-03-21 04:31:15 +000083 generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000085 lineWidth = 1.0f;
86
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 viewportX = 0;
88 viewportY = 0;
89 viewportWidth = config->mDisplayMode.Width;
90 viewportHeight = config->mDisplayMode.Height;
91 zNear = 0.0f;
92 zFar = 1.0f;
93
94 scissorX = 0;
95 scissorY = 0;
96 scissorWidth = config->mDisplayMode.Width;
97 scissorHeight = config->mDisplayMode.Height;
98
99 colorMaskRed = true;
100 colorMaskGreen = true;
101 colorMaskBlue = true;
102 colorMaskAlpha = true;
103 depthMask = true;
104
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
118 activeSampler = 0;
119 arrayBuffer = 0;
120 elementArrayBuffer = 0;
121 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 {
130 samplerTexture[type][sampler] = 0;
131 }
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.com4f39fd92010-03-08 20:26:45 +0000139 currentProgram = 0;
140
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141 packAlignment = 4;
142 unpackAlignment = 4;
143
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{
160 currentProgram = 0;
161
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.com159acdf2010-03-21 04:31:24 +0000240 if(!mHasBeenCurrent)
241 {
242 viewportX = 0;
243 viewportY = 0;
244 viewportWidth = surface->getWidth();
245 viewportHeight = surface->getHeight();
246
247 scissorX = 0;
248 scissorY = 0;
249 scissorWidth = surface->getWidth();
250 scissorHeight = surface->getHeight();
251
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{
279 colorClearValue.red = red;
280 colorClearValue.green = green;
281 colorClearValue.blue = blue;
282 colorClearValue.alpha = alpha;
283}
284
285void Context::setClearDepth(float depth)
286{
287 depthClearValue = depth;
288}
289
290void Context::setClearStencil(int stencil)
291{
292 stencilClearValue = stencil;
293}
294
295// Returns an unused buffer name
296GLuint Context::createBuffer()
297{
298 unsigned int handle = 1;
299
300 while (mBufferMap.find(handle) != mBufferMap.end())
301 {
302 handle++;
303 }
304
305 mBufferMap[handle] = NULL;
306
307 return handle;
308}
309
310// Returns an unused shader/program name
311GLuint Context::createShader(GLenum type)
312{
313 unsigned int handle = 1;
314
315 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
316 {
317 handle++;
318 }
319
320 if (type == GL_VERTEX_SHADER)
321 {
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000322 mShaderMap[handle] = new VertexShader(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 }
324 else if (type == GL_FRAGMENT_SHADER)
325 {
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000326 mShaderMap[handle] = new FragmentShader(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327 }
328 else UNREACHABLE();
329
330 return handle;
331}
332
333// Returns an unused program/shader name
334GLuint Context::createProgram()
335{
336 unsigned int handle = 1;
337
338 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
339 {
340 handle++;
341 }
342
343 mProgramMap[handle] = new Program();
344
345 return handle;
346}
347
348// Returns an unused texture name
349GLuint Context::createTexture()
350{
351 unsigned int handle = 1;
352
353 while (mTextureMap.find(handle) != mTextureMap.end())
354 {
355 handle++;
356 }
357
358 mTextureMap[handle] = NULL;
359
360 return handle;
361}
362
363// Returns an unused framebuffer name
364GLuint Context::createFramebuffer()
365{
366 unsigned int handle = 1;
367
368 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
369 {
370 handle++;
371 }
372
373 mFramebufferMap[handle] = NULL;
374
375 return handle;
376}
377
378// Returns an unused renderbuffer name
379GLuint Context::createRenderbuffer()
380{
381 unsigned int handle = 1;
382
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000383 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384 {
385 handle++;
386 }
387
388 mRenderbufferMap[handle] = NULL;
389
390 return handle;
391}
392
393void Context::deleteBuffer(GLuint buffer)
394{
395 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
396
397 if (bufferObject != mBufferMap.end())
398 {
399 detachBuffer(buffer);
400
401 delete bufferObject->second;
402 mBufferMap.erase(bufferObject);
403 }
404}
405
406void Context::deleteShader(GLuint shader)
407{
408 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
409
410 if (shaderObject != mShaderMap.end())
411 {
412 if (!shaderObject->second->isAttached())
413 {
414 delete shaderObject->second;
415 mShaderMap.erase(shaderObject);
416 }
417 else
418 {
419 shaderObject->second->flagForDeletion();
420 }
421 }
422}
423
424void Context::deleteProgram(GLuint program)
425{
426 ProgramMap::iterator programObject = mProgramMap.find(program);
427
428 if (programObject != mProgramMap.end())
429 {
430 if (program != currentProgram)
431 {
432 delete programObject->second;
433 mProgramMap.erase(programObject);
434 }
435 else
436 {
437 programObject->second->flagForDeletion();
438 }
439 }
440}
441
442void Context::deleteTexture(GLuint texture)
443{
444 TextureMap::iterator textureObject = mTextureMap.find(texture);
445
446 if (textureObject != mTextureMap.end())
447 {
448 detachTexture(texture);
449
450 if (texture != 0)
451 {
452 delete textureObject->second;
453 }
454
455 mTextureMap.erase(textureObject);
456 }
457}
458
459void Context::deleteFramebuffer(GLuint framebuffer)
460{
461 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
462
463 if (framebufferObject != mFramebufferMap.end())
464 {
465 detachFramebuffer(framebuffer);
466
467 delete framebufferObject->second;
468 mFramebufferMap.erase(framebufferObject);
469 }
470}
471
472void Context::deleteRenderbuffer(GLuint renderbuffer)
473{
474 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
475
476 if (renderbufferObject != mRenderbufferMap.end())
477 {
478 detachRenderbuffer(renderbuffer);
479
480 delete renderbufferObject->second;
481 mRenderbufferMap.erase(renderbufferObject);
482 }
483}
484
485void Context::bindArrayBuffer(unsigned int buffer)
486{
487 if (buffer != 0 && !getBuffer(buffer))
488 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000489 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 }
491
492 arrayBuffer = buffer;
493}
494
495void Context::bindElementArrayBuffer(unsigned int buffer)
496{
497 if (buffer != 0 && !getBuffer(buffer))
498 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000499 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 }
501
502 elementArrayBuffer = buffer;
503}
504
505void Context::bindTexture2D(GLuint texture)
506{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000507 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000509 mTextureMap[texture] = new Texture2D(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 }
511
512 texture2D = texture;
513
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000514 samplerTexture[SAMPLER_2D][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515}
516
517void Context::bindTextureCubeMap(GLuint texture)
518{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000519 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000521 mTextureMap[texture] = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 }
523
524 textureCubeMap = texture;
525
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000526 samplerTexture[SAMPLER_CUBE][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527}
528
529void Context::bindFramebuffer(GLuint framebuffer)
530{
531 if (!getFramebuffer(framebuffer))
532 {
533 mFramebufferMap[framebuffer] = new Framebuffer();
534 }
535
536 this->framebuffer = framebuffer;
537}
538
539void Context::bindRenderbuffer(GLuint renderbuffer)
540{
541 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
542 {
543 mRenderbufferMap[renderbuffer] = new Renderbuffer();
544 }
545
546 this->renderbuffer = renderbuffer;
547}
548
549void Context::useProgram(GLuint program)
550{
551 Program *programObject = getCurrentProgram();
552
553 if (programObject && programObject->isFlaggedForDeletion())
554 {
555 deleteProgram(currentProgram);
556 }
557
558 currentProgram = program;
559}
560
561void Context::setFramebufferZero(Framebuffer *buffer)
562{
563 delete mFramebufferMap[0];
564 mFramebufferMap[0] = buffer;
565}
566
567void Context::setColorbufferZero(Colorbuffer *buffer)
568{
569 delete mColorbufferZero;
570 mColorbufferZero = buffer;
571}
572
573void Context::setDepthbufferZero(Depthbuffer *buffer)
574{
575 delete mDepthbufferZero;
576 mDepthbufferZero = buffer;
577}
578
579void Context::setStencilbufferZero(Stencilbuffer *buffer)
580{
581 delete mStencilbufferZero;
582 mStencilbufferZero = buffer;
583}
584
585void Context::setRenderbuffer(Renderbuffer *buffer)
586{
587 delete mRenderbufferMap[renderbuffer];
588 mRenderbufferMap[renderbuffer] = buffer;
589}
590
591Buffer *Context::getBuffer(unsigned int handle)
592{
593 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000594
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595 if (buffer == mBufferMap.end())
596 {
597 return NULL;
598 }
599 else
600 {
601 return buffer->second;
602 }
603}
604
605Shader *Context::getShader(unsigned int handle)
606{
607 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000608
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609 if (shader == mShaderMap.end())
610 {
611 return NULL;
612 }
613 else
614 {
615 return shader->second;
616 }
617}
618
619Program *Context::getProgram(unsigned int handle)
620{
621 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000622
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623 if (program == mProgramMap.end())
624 {
625 return NULL;
626 }
627 else
628 {
629 return program->second;
630 }
631}
632
633Texture *Context::getTexture(unsigned int handle)
634{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000635 if (handle == 0) return NULL;
636
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639 if (texture == mTextureMap.end())
640 {
641 return NULL;
642 }
643 else
644 {
645 return texture->second;
646 }
647}
648
649Framebuffer *Context::getFramebuffer(unsigned int handle)
650{
651 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000652
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653 if (framebuffer == mFramebufferMap.end())
654 {
655 return NULL;
656 }
657 else
658 {
659 return framebuffer->second;
660 }
661}
662
663Renderbuffer *Context::getRenderbuffer(unsigned int handle)
664{
665 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667 if (renderbuffer == mRenderbufferMap.end())
668 {
669 return NULL;
670 }
671 else
672 {
673 return renderbuffer->second;
674 }
675}
676
677Colorbuffer *Context::getColorbuffer(GLuint handle)
678{
679 if (handle != 0)
680 {
681 Renderbuffer *renderbuffer = getRenderbuffer(handle);
682
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000683 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 {
685 return static_cast<Colorbuffer*>(renderbuffer);
686 }
687 }
688 else // Special case: 0 refers to different initial render targets based on the attachment type
689 {
690 return mColorbufferZero;
691 }
692
693 return NULL;
694}
695
696Depthbuffer *Context::getDepthbuffer(GLuint handle)
697{
698 if (handle != 0)
699 {
700 Renderbuffer *renderbuffer = getRenderbuffer(handle);
701
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000702 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703 {
704 return static_cast<Depthbuffer*>(renderbuffer);
705 }
706 }
707 else // Special case: 0 refers to different initial render targets based on the attachment type
708 {
709 return mDepthbufferZero;
710 }
711
712 return NULL;
713}
714
715Stencilbuffer *Context::getStencilbuffer(GLuint handle)
716{
717 if (handle != 0)
718 {
719 Renderbuffer *renderbuffer = getRenderbuffer(handle);
720
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000721 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722 {
723 return static_cast<Stencilbuffer*>(renderbuffer);
724 }
725 }
726 else
727 {
728 return mStencilbufferZero;
729 }
730
731 return NULL;
732}
733
734Buffer *Context::getArrayBuffer()
735{
736 return getBuffer(arrayBuffer);
737}
738
739Buffer *Context::getElementArrayBuffer()
740{
741 return getBuffer(elementArrayBuffer);
742}
743
744Program *Context::getCurrentProgram()
745{
746 return getProgram(currentProgram);
747}
748
749Texture2D *Context::getTexture2D()
750{
751 if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target
752 {
753 return mTexture2DZero;
754 }
755
756 return (Texture2D*)getTexture(texture2D);
757}
758
759TextureCubeMap *Context::getTextureCubeMap()
760{
761 if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
762 {
763 return mTextureCubeMapZero;
764 }
765
766 return (TextureCubeMap*)getTexture(textureCubeMap);
767}
768
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000769Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000771 GLuint texid = samplerTexture[type][sampler];
772
773 if (texid == 0)
774 {
775 switch (type)
776 {
777 default: UNREACHABLE();
778 case SAMPLER_2D: return mTexture2DZero;
779 case SAMPLER_CUBE: return mTextureCubeMapZero;
780 }
781 }
782
783 return getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
786Framebuffer *Context::getFramebuffer()
787{
788 return getFramebuffer(framebuffer);
789}
790
daniel@transgaming.com777f2672010-04-07 03:25:16 +0000791bool Context::getBooleanv(GLenum pname, GLboolean *params)
792{
793 switch (pname)
794 {
795 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
796 case GL_SAMPLE_COVERAGE_INVERT: *params = sampleCoverageInvert; break;
797 case GL_DEPTH_WRITEMASK: *params = depthMask; break;
798 case GL_COLOR_WRITEMASK:
799 params[0] = colorMaskRed;
800 params[1] = colorMaskGreen;
801 params[2] = colorMaskBlue;
802 params[3] = colorMaskAlpha;
803 break;
daniel@transgaming.com79f66772010-04-13 03:26:09 +0000804 case GL_CULL_FACE: *params = cullFace;
805 case GL_POLYGON_OFFSET_FILL: *params = polygonOffsetFill;
806 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = sampleAlphaToCoverage;
807 case GL_SAMPLE_COVERAGE: *params = sampleCoverage;
808 case GL_SCISSOR_TEST: *params = scissorTest;
809 case GL_STENCIL_TEST: *params = stencilTest;
810 case GL_DEPTH_TEST: *params = depthTest;
811 case GL_BLEND: *params = blend;
812 case GL_DITHER: *params = dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +0000813 default:
814 return false;
815 }
816
817 return true;
818}
819
820bool Context::getFloatv(GLenum pname, GLfloat *params)
821{
822 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
823 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
824 // GetIntegerv as its native query function. As it would require conversion in any
825 // case, this should make no difference to the calling application.
826 switch (pname)
827 {
828 case GL_LINE_WIDTH: *params = lineWidth; break;
829 case GL_SAMPLE_COVERAGE_VALUE: *params = sampleCoverageValue; break;
830 case GL_DEPTH_CLEAR_VALUE: *params = depthClearValue; break;
831 case GL_POLYGON_OFFSET_FACTOR: *params = polygonOffsetFactor; break;
832 case GL_POLYGON_OFFSET_UNITS: *params = polygonOffsetUnits; break;
833 case GL_ALIASED_LINE_WIDTH_RANGE:
834 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
835 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
836 break;
837 case GL_ALIASED_POINT_SIZE_RANGE:
838 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
839 params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX;
840 break;
841 case GL_DEPTH_RANGE:
842 params[0] = zNear;
843 params[1] = zFar;
844 break;
845 case GL_COLOR_CLEAR_VALUE:
846 params[0] = colorClearValue.red;
847 params[1] = colorClearValue.green;
848 params[2] = colorClearValue.blue;
849 params[3] = colorClearValue.alpha;
850 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +0000851 case GL_BLEND_COLOR:
852 params[0] = blendColor.red;
853 params[1] = blendColor.green;
854 params[2] = blendColor.blue;
855 params[3] = blendColor.alpha;
856 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +0000857 default:
858 return false;
859 }
860
861 return true;
862}
863
864bool Context::getIntegerv(GLenum pname, GLint *params)
865{
866 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
867 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
868 // GetIntegerv as its native query function. As it would require conversion in any
869 // case, this should make no difference to the calling application. You may find it in
870 // Context::getFloatv.
871 switch (pname)
872 {
873 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
874 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
875 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
876 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
877 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
878 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
879 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
880 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
881 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
882 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
883 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
884 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
885 case GL_ARRAY_BUFFER_BINDING: *params = arrayBuffer; break;
886 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = elementArrayBuffer; break;
887 case GL_FRAMEBUFFER_BINDING: *params = framebuffer; break;
888 case GL_RENDERBUFFER_BINDING: *params = renderbuffer; break;
889 case GL_CURRENT_PROGRAM: *params = currentProgram; break;
890 case GL_PACK_ALIGNMENT: *params = packAlignment; break;
891 case GL_UNPACK_ALIGNMENT: *params = unpackAlignment; break;
892 case GL_GENERATE_MIPMAP_HINT: *params = generateMipmapHint; break;
893 case GL_ACTIVE_TEXTURE: *params = activeSampler; break;
894 case GL_STENCIL_FUNC: *params = stencilFunc; break;
895 case GL_STENCIL_REF: *params = stencilRef; break;
896 case GL_STENCIL_VALUE_MASK: *params = stencilMask; break;
897 case GL_STENCIL_BACK_FUNC: *params = stencilBackFunc; break;
898 case GL_STENCIL_BACK_REF: *params = stencilBackRef; break;
899 case GL_STENCIL_BACK_VALUE_MASK: *params = stencilBackMask; break;
900 case GL_STENCIL_FAIL: *params = stencilFail; break;
901 case GL_STENCIL_PASS_DEPTH_FAIL: *params = stencilPassDepthFail; break;
902 case GL_STENCIL_PASS_DEPTH_PASS: *params = stencilPassDepthPass; break;
903 case GL_STENCIL_BACK_FAIL: *params = stencilBackFail; break;
904 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = stencilBackPassDepthFail; break;
905 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = stencilBackPassDepthPass; break;
906 case GL_DEPTH_FUNC: *params = depthFunc; break;
907 case GL_BLEND_SRC_RGB: *params = sourceBlendRGB; break;
908 case GL_BLEND_SRC_ALPHA: *params = sourceBlendAlpha; break;
909 case GL_BLEND_DST_RGB: *params = destBlendRGB; break;
910 case GL_BLEND_DST_ALPHA: *params = destBlendAlpha; break;
911 case GL_BLEND_EQUATION_RGB: *params = blendEquationRGB; break;
912 case GL_BLEND_EQUATION_ALPHA: *params = blendEquationAlpha; break;
913 case GL_STENCIL_WRITEMASK: *params = stencilWritemask; break;
914 case GL_STENCIL_BACK_WRITEMASK: *params = stencilBackWritemask; break;
915 case GL_STENCIL_CLEAR_VALUE: *params = stencilClearValue; break;
916 case GL_SUBPIXEL_BITS: *params = 4; break;
917 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
918 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
919 case GL_SAMPLE_BUFFERS: *params = 0; break;
920 case GL_SAMPLES: *params = 0; break;
921 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
922 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
923 case GL_MAX_VIEWPORT_DIMS:
924 {
925 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
926 params[0] = maxDimension;
927 params[1] = maxDimension;
928 }
929 break;
930 case GL_VIEWPORT:
931 params[0] = viewportX;
932 params[1] = viewportY;
933 params[2] = viewportWidth;
934 params[3] = viewportHeight;
935 break;
936 case GL_SCISSOR_BOX:
937 params[0] = scissorX;
938 params[1] = scissorY;
939 params[2] = scissorWidth;
940 params[3] = scissorHeight;
941 break;
942 case GL_CULL_FACE_MODE: *params = cullMode; break;
943 case GL_FRONT_FACE: *params = frontFace; break;
944 case GL_RED_BITS:
945 case GL_GREEN_BITS:
946 case GL_BLUE_BITS:
947 case GL_ALPHA_BITS:
948 {
949 gl::Framebuffer *framebuffer = getFramebuffer();
950 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
951
952 if (colorbuffer)
953 {
954 switch (pname)
955 {
956 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
957 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
958 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
959 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
960 }
961 }
962 else
963 {
964 *params = 0;
965 }
966 }
967 break;
968 case GL_DEPTH_BITS:
969 {
970 gl::Framebuffer *framebuffer = getFramebuffer();
971 gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer();
972
973 if (depthbuffer)
974 {
975 *params = depthbuffer->getDepthSize();
976 }
977 else
978 {
979 *params = 0;
980 }
981 }
982 break;
983 case GL_STENCIL_BITS:
984 {
985 gl::Framebuffer *framebuffer = getFramebuffer();
986 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
987
988 if (stencilbuffer)
989 {
990 *params = stencilbuffer->getStencilSize();
991 }
992 else
993 {
994 *params = 0;
995 }
996 }
997 break;
998 case GL_TEXTURE_BINDING_2D:
999 {
1000 if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
1001 {
1002 error(GL_INVALID_OPERATION);
1003 return false;
1004 }
1005
1006 *params = samplerTexture[SAMPLER_2D][activeSampler];
1007 }
1008 break;
1009 case GL_TEXTURE_BINDING_CUBE_MAP:
1010 {
1011 if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
1012 {
1013 error(GL_INVALID_OPERATION);
1014 return false;
1015 }
1016
1017 *params = samplerTexture[SAMPLER_CUBE][activeSampler];
1018 }
1019 break;
1020 default:
1021 return false;
1022 }
1023
1024 return true;
1025}
1026
1027bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1028{
1029 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1030 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1031 // to the fact that it is stored internally as a float, and so would require conversion
1032 // if returned from Context::getIntegerv. Since this conversion is already implemented
1033 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1034 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1035 // application.
1036 switch (pname)
1037 {
1038 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1039 case GL_SHADER_BINARY_FORMATS:
1040 {
1041 *type = GL_INT;
1042 *numParams = 0;
1043 }
1044 break;
1045 case GL_MAX_VERTEX_ATTRIBS:
1046 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1047 case GL_MAX_VARYING_VECTORS:
1048 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1049 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1050 case GL_MAX_TEXTURE_IMAGE_UNITS:
1051 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1052 case GL_MAX_RENDERBUFFER_SIZE:
1053 case GL_NUM_SHADER_BINARY_FORMATS:
1054 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1055 case GL_ARRAY_BUFFER_BINDING:
1056 case GL_FRAMEBUFFER_BINDING:
1057 case GL_RENDERBUFFER_BINDING:
1058 case GL_CURRENT_PROGRAM:
1059 case GL_PACK_ALIGNMENT:
1060 case GL_UNPACK_ALIGNMENT:
1061 case GL_GENERATE_MIPMAP_HINT:
1062 case GL_RED_BITS:
1063 case GL_GREEN_BITS:
1064 case GL_BLUE_BITS:
1065 case GL_ALPHA_BITS:
1066 case GL_DEPTH_BITS:
1067 case GL_STENCIL_BITS:
1068 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1069 case GL_CULL_FACE_MODE:
1070 case GL_FRONT_FACE:
1071 case GL_ACTIVE_TEXTURE:
1072 case GL_STENCIL_FUNC:
1073 case GL_STENCIL_VALUE_MASK:
1074 case GL_STENCIL_REF:
1075 case GL_STENCIL_FAIL:
1076 case GL_STENCIL_PASS_DEPTH_FAIL:
1077 case GL_STENCIL_PASS_DEPTH_PASS:
1078 case GL_STENCIL_BACK_FUNC:
1079 case GL_STENCIL_BACK_VALUE_MASK:
1080 case GL_STENCIL_BACK_REF:
1081 case GL_STENCIL_BACK_FAIL:
1082 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1083 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1084 case GL_DEPTH_FUNC:
1085 case GL_BLEND_SRC_RGB:
1086 case GL_BLEND_SRC_ALPHA:
1087 case GL_BLEND_DST_RGB:
1088 case GL_BLEND_DST_ALPHA:
1089 case GL_BLEND_EQUATION_RGB:
1090 case GL_BLEND_EQUATION_ALPHA:
1091 case GL_STENCIL_WRITEMASK:
1092 case GL_STENCIL_BACK_WRITEMASK:
1093 case GL_STENCIL_CLEAR_VALUE:
1094 case GL_SUBPIXEL_BITS:
1095 case GL_MAX_TEXTURE_SIZE:
1096 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1097 case GL_SAMPLE_BUFFERS:
1098 case GL_SAMPLES:
1099 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1100 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1101 case GL_TEXTURE_BINDING_2D:
1102 case GL_TEXTURE_BINDING_CUBE_MAP:
1103 {
1104 *type = GL_INT;
1105 *numParams = 1;
1106 }
1107 break;
1108 case GL_MAX_VIEWPORT_DIMS:
1109 {
1110 *type = GL_INT;
1111 *numParams = 2;
1112 }
1113 break;
1114 case GL_VIEWPORT:
1115 case GL_SCISSOR_BOX:
1116 {
1117 *type = GL_INT;
1118 *numParams = 4;
1119 }
1120 break;
1121 case GL_SHADER_COMPILER:
1122 case GL_SAMPLE_COVERAGE_INVERT:
1123 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001124 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1125 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1126 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1127 case GL_SAMPLE_COVERAGE:
1128 case GL_SCISSOR_TEST:
1129 case GL_STENCIL_TEST:
1130 case GL_DEPTH_TEST:
1131 case GL_BLEND:
1132 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001133 {
1134 *type = GL_BOOL;
1135 *numParams = 1;
1136 }
1137 break;
1138 case GL_COLOR_WRITEMASK:
1139 {
1140 *type = GL_BOOL;
1141 *numParams = 4;
1142 }
1143 break;
1144 case GL_POLYGON_OFFSET_FACTOR:
1145 case GL_POLYGON_OFFSET_UNITS:
1146 case GL_SAMPLE_COVERAGE_VALUE:
1147 case GL_DEPTH_CLEAR_VALUE:
1148 case GL_LINE_WIDTH:
1149 {
1150 *type = GL_FLOAT;
1151 *numParams = 1;
1152 }
1153 break;
1154 case GL_ALIASED_LINE_WIDTH_RANGE:
1155 case GL_ALIASED_POINT_SIZE_RANGE:
1156 case GL_DEPTH_RANGE:
1157 {
1158 *type = GL_FLOAT;
1159 *numParams = 2;
1160 }
1161 break;
1162 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001163 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001164 {
1165 *type = GL_FLOAT;
1166 *numParams = 4;
1167 }
1168 break;
1169 default:
1170 return false;
1171 }
1172
1173 return true;
1174}
1175
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176// Applies the render target surface, depth stencil surface, viewport rectangle and
1177// scissor rectangle to the Direct3D 9 device
1178bool Context::applyRenderTarget(bool ignoreViewport)
1179{
1180 IDirect3DDevice9 *device = getDevice();
1181 Framebuffer *framebufferObject = getFramebuffer();
1182
1183 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1184 {
1185 return false;
1186 }
1187
1188 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1189 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1190
1191 device->SetRenderTarget(0, renderTarget);
1192 device->SetDepthStencilSurface(depthStencil);
1193
1194 D3DVIEWPORT9 viewport;
1195 D3DSURFACE_DESC desc;
1196 renderTarget->GetDesc(&desc);
1197
1198 if (ignoreViewport)
1199 {
1200 viewport.X = 0;
1201 viewport.Y = 0;
1202 viewport.Width = desc.Width;
1203 viewport.Height = desc.Height;
1204 viewport.MinZ = 0.0f;
1205 viewport.MaxZ = 1.0f;
1206 }
1207 else
1208 {
daniel@transgaming.com16973022010-03-11 19:22:19 +00001209 viewport.X = std::max(viewportX, 0);
1210 viewport.Y = std::max(viewportY, 0);
1211 viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X);
1212 viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001213 viewport.MinZ = clamp01(zNear);
1214 viewport.MaxZ = clamp01(zFar);
1215 }
1216
1217 device->SetViewport(&viewport);
1218
1219 if (scissorTest)
1220 {
1221 RECT rect = {scissorX,
1222 scissorY,
1223 scissorX + scissorWidth,
1224 scissorY + scissorHeight};
1225
1226 device->SetScissorRect(&rect);
1227 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1228 }
1229 else
1230 {
1231 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1232 }
1233
1234 if (currentProgram)
1235 {
1236 D3DSURFACE_DESC description;
1237 renderTarget->GetDesc(&description);
1238 Program *programObject = getCurrentProgram();
1239
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001240 GLint halfPixelSize = programObject->getUniformLocation("dx_HalfPixelSize");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001241 GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height};
1242 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001243
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001244 GLint window = programObject->getUniformLocation("dx_Window");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001245 GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f};
1246 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1247
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001248 GLint depth = programObject->getUniformLocation("dx_Depth");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001249 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
1250 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1251
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001252 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001253 programObject->setUniform1fv(near, 1, &zNear);
1254
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001255 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001256 programObject->setUniform1fv(far, 1, &zFar);
1257
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001258 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001259 GLfloat zDiff = zFar - zNear;
1260 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261 }
1262
1263 return true;
1264}
1265
1266// 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 +00001267void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268{
1269 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001270 Program *programObject = getCurrentProgram();
1271
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001272 GLint frontCCW = programObject->getUniformLocation("dx_FrontCCW");
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001273 GLint ccw = (frontFace == GL_CCW);
1274 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00001276 GLint pointsOrLines = programObject->getUniformLocation("dx_PointsOrLines");
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001277 GLint alwaysFront = !isTriangleMode(drawMode);
1278 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280 if (cullFace)
1281 {
1282 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace));
1283 }
1284 else
1285 {
1286 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1287 }
1288
1289 if (depthTest)
1290 {
1291 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1292 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc));
1293 }
1294 else
1295 {
1296 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
1297 }
1298
1299 if (blend)
1300 {
1301 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1302
1303 if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1304 destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1305 {
1306 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor));
1307 }
1308 else
1309 {
1310 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha),
1311 unorm<8>(blendColor.alpha),
1312 unorm<8>(blendColor.alpha),
1313 unorm<8>(blendColor.alpha)));
1314 }
1315
1316 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB));
1317 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB));
1318 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB));
1319
1320 if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha)
1321 {
1322 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1323
1324 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha));
1325 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha));
1326 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha));
1327
1328 }
1329 else
1330 {
1331 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1332 }
1333 }
1334 else
1335 {
1336 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1337 }
1338
1339 if (stencilTest)
1340 {
1341 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1342 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1343
1344 // FIXME: Unsupported by D3D9
1345 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1346 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1347 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001348 if(stencilWritemask != stencilBackWritemask || stencilRef != stencilBackRef || stencilMask != stencilBackMask)
1349 {
1350 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1351 return error(GL_INVALID_OPERATION);
1352 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001353
1354 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask);
1355 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc));
1356
1357 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range
1358 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask);
1359
1360 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail));
1361 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail));
1362 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass));
1363
1364 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask);
1365 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc));
1366
1367 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range
1368 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask);
1369
1370 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail));
1371 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail));
1372 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass));
1373 }
1374 else
1375 {
1376 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1377 }
1378
1379 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha));
1380 device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE);
1381
1382 if (polygonOffsetFill)
1383 {
1384 UNIMPLEMENTED(); // FIXME
1385 }
1386
1387 if (sampleAlphaToCoverage)
1388 {
1389 UNIMPLEMENTED(); // FIXME
1390 }
1391
1392 if (sampleCoverage)
1393 {
1394 UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one
1395 }
1396
1397 device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE);
1398}
1399
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001400// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001401void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001403 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001405 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001406 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001407 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408 }
1409 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001410}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001412// The indices parameter to glDrawElements can have two interpretations:
1413// - as a pointer into client memory
1414// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
1415// Handle these cases here and return a pointer to the index data.
1416const Index *Context::adjustIndexPointer(const void *indices)
1417{
1418 if (elementArrayBuffer)
1419 {
1420 Buffer *buffer = getBuffer(elementArrayBuffer);
1421 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
1422 }
1423 else
1424 {
1425 return static_cast<const Index*>(indices);
1426 }
1427}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001428
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001429void Context::applyVertexBuffer(GLint first, GLsizei count)
1430{
1431 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1432
1433 mVertexDataManager->preRenderValidate(first, count, translated);
1434
1435 lookupAttributeMapping(translated);
1436
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001437 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001438}
1439
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001440void Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001441{
1442 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1443
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001444 mVertexDataManager->preRenderValidate(indexInfo, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001445
1446 lookupAttributeMapping(translated);
1447
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001448 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001449}
1450
1451// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001452TranslatedIndexData Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001453{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001454 TranslatedIndexData indexInfo = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(elementArrayBuffer), indices);
1455 mBufferBackEnd->setupIndicesPreDraw(indexInfo);
1456 return indexInfo;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457}
1458
1459// Applies the shaders and shader constants to the Direct3D 9 device
1460void Context::applyShaders()
1461{
1462 IDirect3DDevice9 *device = getDevice();
1463 Program *programObject = getCurrentProgram();
1464 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1465 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1466
1467 device->SetVertexShader(vertexShader);
1468 device->SetPixelShader(pixelShader);
1469
1470 programObject->applyUniforms();
1471}
1472
1473// Applies the textures and sampler states to the Direct3D 9 device
1474void Context::applyTextures()
1475{
1476 IDirect3DDevice9 *device = getDevice();
1477 Program *programObject = getCurrentProgram();
1478
1479 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1480 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001481 int textureUnit = programObject->getSamplerMapping(sampler);
1482 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001484 SamplerType textureType = programObject->getSamplerType(sampler);
1485
1486 Texture *texture = getSamplerTexture(textureUnit, textureType);
1487
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001488 if (texture->isComplete())
1489 {
1490 GLenum wrapS = texture->getWrapS();
1491 GLenum wrapT = texture->getWrapT();
1492 GLenum minFilter = texture->getMinFilter();
1493 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001495 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1496 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001498 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1499 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1500 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1501 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1502 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001504 device->SetTexture(sampler, texture->getTexture());
1505 }
1506 else
1507 {
1508 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001511 else
1512 {
1513 device->SetTexture(sampler, NULL);
1514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515 }
1516}
1517
1518void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1519{
1520 Framebuffer *framebuffer = getFramebuffer();
1521 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1522 IDirect3DDevice9 *device = getDevice();
1523
1524 D3DSURFACE_DESC desc;
1525 renderTarget->GetDesc(&desc);
1526
1527 IDirect3DSurface9 *systemSurface;
1528 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1529
1530 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1531 {
1532 return error(GL_OUT_OF_MEMORY);
1533 }
1534
1535 ASSERT(SUCCEEDED(result));
1536
1537 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1538 {
1539 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1540 }
1541
1542 result = device->GetRenderTargetData(renderTarget, systemSurface);
1543
1544 if (result == D3DERR_DRIVERINTERNALERROR)
1545 {
1546 systemSurface->Release();
1547
1548 return error(GL_OUT_OF_MEMORY);
1549 }
1550
1551 if (FAILED(result))
1552 {
1553 UNREACHABLE();
1554 systemSurface->Release();
1555
1556 return; // No sensible error to generate
1557 }
1558
1559 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001560 RECT rect = {std::max(x, 0),
1561 std::max(y, 0),
1562 std::min(x + width, (int)desc.Width),
1563 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001564
1565 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1566
1567 if (FAILED(result))
1568 {
1569 UNREACHABLE();
1570 systemSurface->Release();
1571
1572 return; // No sensible error to generate
1573 }
1574
1575 unsigned char *source = (unsigned char*)lock.pBits;
1576 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001577 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001578
1579 for (int j = 0; j < rect.bottom - rect.top; j++)
1580 {
1581 for (int i = 0; i < rect.right - rect.left; i++)
1582 {
1583 float r;
1584 float g;
1585 float b;
1586 float a;
1587
1588 switch (desc.Format)
1589 {
1590 case D3DFMT_R5G6B5:
1591 {
1592 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1593
1594 a = 1.0f;
1595 b = (rgb & 0x001F) * (1.0f / 0x001F);
1596 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1597 r = (rgb & 0xF800) * (1.0f / 0xF800);
1598 }
1599 break;
1600 case D3DFMT_X1R5G5B5:
1601 {
1602 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1603
1604 a = 1.0f;
1605 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1606 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1607 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1608 }
1609 break;
1610 case D3DFMT_A1R5G5B5:
1611 {
1612 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1613
1614 a = (argb & 0x8000) ? 1.0f : 0.0f;
1615 b = (argb & 0x001F) * (1.0f / 0x001F);
1616 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1617 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1618 }
1619 break;
1620 case D3DFMT_A8R8G8B8:
1621 {
1622 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1623
1624 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1625 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1626 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1627 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1628 }
1629 break;
1630 case D3DFMT_X8R8G8B8:
1631 {
1632 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1633
1634 a = 1.0f;
1635 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1636 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1637 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1638 }
1639 break;
1640 case D3DFMT_A2R10G10B10:
1641 {
1642 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1643
1644 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1645 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1646 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1647 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1648 }
1649 break;
1650 default:
1651 UNIMPLEMENTED(); // FIXME
1652 UNREACHABLE();
1653 }
1654
1655 switch (format)
1656 {
1657 case GL_RGBA:
1658 switch (type)
1659 {
1660 case GL_UNSIGNED_BYTE:
1661 dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f);
1662 dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f);
1663 dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f);
1664 dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f);
1665 break;
1666 default: UNREACHABLE();
1667 }
1668 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001669 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 switch (type)
1671 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001672 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
1673 dest16[i + j * width] = ((unsigned short)(31 * b + 0.5f) << 0) |
1674 ((unsigned short)(63 * g + 0.5f) << 5) |
1675 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 break;
1677 default: UNREACHABLE();
1678 }
1679 break;
1680 default: UNREACHABLE();
1681 }
1682 }
1683 }
1684
1685 systemSurface->UnlockRect();
1686
1687 systemSurface->Release();
1688}
1689
1690void Context::clear(GLbitfield mask)
1691{
1692 IDirect3DDevice9 *device = getDevice();
1693 DWORD flags = 0;
1694
1695 if (mask & GL_COLOR_BUFFER_BIT)
1696 {
1697 mask &= ~GL_COLOR_BUFFER_BIT;
1698 flags |= D3DCLEAR_TARGET;
1699 }
1700
1701 if (mask & GL_DEPTH_BUFFER_BIT)
1702 {
1703 mask &= ~GL_DEPTH_BUFFER_BIT;
1704 if (depthMask)
1705 {
1706 flags |= D3DCLEAR_ZBUFFER;
1707 }
1708 }
1709
1710 Framebuffer *framebufferObject = getFramebuffer();
1711 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1712
1713 GLuint stencilUnmasked = 0x0;
1714
1715 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
1716 {
1717 D3DSURFACE_DESC desc;
1718 depthStencil->GetDesc(&desc);
1719
1720 mask &= ~GL_STENCIL_BUFFER_BIT;
1721 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
1722 stencilUnmasked = (0x1 << stencilSize) - 1;
1723
1724 if (stencilUnmasked != 0x0)
1725 {
1726 flags |= D3DCLEAR_STENCIL;
1727 }
1728 }
1729
1730 if (mask != 0)
1731 {
1732 return error(GL_INVALID_VALUE);
1733 }
1734
1735 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
1736
1737 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue));
1738 float depth = clamp01(depthClearValue);
1739 int stencil = stencilClearValue & 0x000000FF;
1740
1741 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1742
1743 D3DSURFACE_DESC desc;
1744 renderTarget->GetDesc(&desc);
1745
1746 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha;
1747
1748 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
1749 (stencilWritemask & stencilUnmasked) != stencilUnmasked;
1750 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001751 !(colorMaskRed && colorMaskGreen &&
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 colorMaskBlue && alphaUnmasked);
1753
1754 if (needMaskedColorClear || needMaskedStencilClear)
1755 {
1756 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1757 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1758 device->SetRenderState(D3DRS_ZENABLE, FALSE);
1759 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1760 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1761 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1762 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1763 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1764
1765 if (flags & D3DCLEAR_TARGET)
1766 {
1767 device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
1768 (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
1769 (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
1770 (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
1771 }
1772 else
1773 {
1774 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1775 }
1776
1777 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
1778 {
1779 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1780 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1781 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1782 device->SetRenderState(D3DRS_STENCILREF, stencil);
1783 device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001784 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1786 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1787 }
1788 else
1789 {
1790 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1791 }
1792
1793 device->SetPixelShader(NULL);
1794 device->SetVertexShader(NULL);
1795 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1796
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001797 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798 {
1799 float x, y, z, w;
1800 D3DCOLOR diffuse;
1801 };
1802
1803 Vertex quad[4];
1804 quad[0].x = 0.0f;
1805 quad[0].y = (float)desc.Height;
1806 quad[0].z = 0.0f;
1807 quad[0].w = 1.0f;
1808 quad[0].diffuse = color;
1809
1810 quad[1].x = (float)desc.Width;
1811 quad[1].y = (float)desc.Height;
1812 quad[1].z = 0.0f;
1813 quad[1].w = 1.0f;
1814 quad[1].diffuse = color;
1815
1816 quad[2].x = 0.0f;
1817 quad[2].y = 0.0f;
1818 quad[2].z = 0.0f;
1819 quad[2].w = 1.0f;
1820 quad[2].diffuse = color;
1821
1822 quad[3].x = (float)desc.Width;
1823 quad[3].y = 0.0f;
1824 quad[3].z = 0.0f;
1825 quad[3].w = 1.0f;
1826 quad[3].diffuse = color;
1827
1828 device->BeginScene();
1829 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
1830 device->EndScene();
1831
1832 if (flags & D3DCLEAR_ZBUFFER)
1833 {
1834 device->SetRenderState(D3DRS_ZENABLE, TRUE);
1835 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1836 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1837 }
1838 }
1839 else
1840 {
1841 device->Clear(0, NULL, flags, color, depth, stencil);
1842 }
1843}
1844
1845void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
1846{
1847 if (!currentProgram)
1848 {
1849 return error(GL_INVALID_OPERATION);
1850 }
1851
1852 IDirect3DDevice9 *device = getDevice();
1853 D3DPRIMITIVETYPE primitiveType;
1854 int primitiveCount;
1855
1856 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1857 return error(GL_INVALID_ENUM);
1858
1859 if (primitiveCount <= 0)
1860 {
1861 return;
1862 }
1863
1864 if (!applyRenderTarget(false))
1865 {
1866 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1867 }
1868
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001869 applyState(mode);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001870 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 applyShaders();
1872 applyTextures();
1873
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001874 if (!cullSkipsDraw(mode))
1875 {
1876 device->BeginScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001877 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001878 device->EndScene();
1879 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880}
1881
1882void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
1883{
1884 if (!currentProgram)
1885 {
1886 return error(GL_INVALID_OPERATION);
1887 }
1888
1889 if (!indices && !elementArrayBuffer)
1890 {
1891 return error(GL_INVALID_OPERATION);
1892 }
1893
1894 IDirect3DDevice9 *device = getDevice();
1895 D3DPRIMITIVETYPE primitiveType;
1896 int primitiveCount;
1897
1898 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1899 return error(GL_INVALID_ENUM);
1900
1901 if (primitiveCount <= 0)
1902 {
1903 return;
1904 }
1905
1906 if (!applyRenderTarget(false))
1907 {
1908 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1909 }
1910
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001911 applyState(mode);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001912 TranslatedIndexData indexInfo = applyIndexBuffer(indices, count, mode, type);
1913 applyVertexBuffer(indexInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914 applyShaders();
1915 applyTextures();
1916
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001917 if (!cullSkipsDraw(mode))
1918 {
1919 device->BeginScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001920 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 +00001921 device->EndScene();
1922 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923}
1924
1925void Context::finish()
1926{
1927 IDirect3DDevice9 *device = getDevice();
1928 IDirect3DQuery9 *occlusionQuery = NULL;
1929
1930 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
1931
1932 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1933 {
1934 return error(GL_OUT_OF_MEMORY);
1935 }
1936
1937 ASSERT(SUCCEEDED(result));
1938
1939 if (occlusionQuery)
1940 {
1941 occlusionQuery->Issue(D3DISSUE_BEGIN);
1942
1943 // Render something outside the render target
1944 device->SetPixelShader(NULL);
1945 device->SetVertexShader(NULL);
1946 device->SetFVF(D3DFVF_XYZRHW);
1947 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
1948 device->BeginScene();
1949 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
1950 device->EndScene();
1951
1952 occlusionQuery->Issue(D3DISSUE_END);
1953
1954 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1955 {
1956 // Keep polling, but allow other threads to do something useful first
1957 Sleep(0);
1958 }
1959
1960 occlusionQuery->Release();
1961 }
1962}
1963
1964void Context::flush()
1965{
1966 IDirect3DDevice9 *device = getDevice();
1967 IDirect3DQuery9 *eventQuery = NULL;
1968
1969 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
1970
1971 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1972 {
1973 return error(GL_OUT_OF_MEMORY);
1974 }
1975
1976 ASSERT(SUCCEEDED(result));
1977
1978 if (eventQuery)
1979 {
1980 eventQuery->Issue(D3DISSUE_END);
1981
1982 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1983 {
1984 // Keep polling, but allow other threads to do something useful first
1985 Sleep(0);
1986 }
1987
1988 eventQuery->Release();
1989 }
1990}
1991
1992void Context::recordInvalidEnum()
1993{
1994 mInvalidEnum = true;
1995}
1996
1997void Context::recordInvalidValue()
1998{
1999 mInvalidValue = true;
2000}
2001
2002void Context::recordInvalidOperation()
2003{
2004 mInvalidOperation = true;
2005}
2006
2007void Context::recordOutOfMemory()
2008{
2009 mOutOfMemory = true;
2010}
2011
2012void Context::recordInvalidFramebufferOperation()
2013{
2014 mInvalidFramebufferOperation = true;
2015}
2016
2017// Get one of the recorded errors and clear its flag, if any.
2018// [OpenGL ES 2.0.24] section 2.5 page 13.
2019GLenum Context::getError()
2020{
2021 if (mInvalidEnum)
2022 {
2023 mInvalidEnum = false;
2024
2025 return GL_INVALID_ENUM;
2026 }
2027
2028 if (mInvalidValue)
2029 {
2030 mInvalidValue = false;
2031
2032 return GL_INVALID_VALUE;
2033 }
2034
2035 if (mInvalidOperation)
2036 {
2037 mInvalidOperation = false;
2038
2039 return GL_INVALID_OPERATION;
2040 }
2041
2042 if (mOutOfMemory)
2043 {
2044 mOutOfMemory = false;
2045
2046 return GL_OUT_OF_MEMORY;
2047 }
2048
2049 if (mInvalidFramebufferOperation)
2050 {
2051 mInvalidFramebufferOperation = false;
2052
2053 return GL_INVALID_FRAMEBUFFER_OPERATION;
2054 }
2055
2056 return GL_NO_ERROR;
2057}
2058
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002059const char *Context::getPixelShaderProfile()
2060{
2061 return mPsProfile;
2062}
2063
2064const char *Context::getVertexShaderProfile()
2065{
2066 return mVsProfile;
2067}
2068
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069void Context::detachBuffer(GLuint buffer)
2070{
2071 // [OpenGL ES 2.0.24] section 2.9 page 22:
2072 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2073 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2074
2075 if (arrayBuffer == buffer)
2076 {
2077 arrayBuffer = 0;
2078 }
2079
2080 if (elementArrayBuffer == buffer)
2081 {
2082 elementArrayBuffer = 0;
2083 }
2084
2085 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2086 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002087 if (vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002089 vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091 }
2092}
2093
2094void Context::detachTexture(GLuint texture)
2095{
2096 // [OpenGL ES 2.0.24] section 3.8 page 84:
2097 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2098 // rebound to texture object zero
2099
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002100 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002102 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002104 if (samplerTexture[type][sampler] == texture)
2105 {
2106 samplerTexture[type][sampler] = 0;
2107 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 }
2109 }
2110
2111 // [OpenGL ES 2.0.24] section 4.4 page 112:
2112 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2113 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2114 // image was attached in the currently bound framebuffer.
2115
2116 Framebuffer *framebuffer = getFramebuffer();
2117
2118 if (framebuffer)
2119 {
2120 framebuffer->detachTexture(texture);
2121 }
2122}
2123
2124void Context::detachFramebuffer(GLuint framebuffer)
2125{
2126 // [OpenGL ES 2.0.24] section 4.4 page 107:
2127 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2128 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2129
2130 if (this->framebuffer == framebuffer)
2131 {
2132 bindFramebuffer(0);
2133 }
2134}
2135
2136void Context::detachRenderbuffer(GLuint renderbuffer)
2137{
2138 // [OpenGL ES 2.0.24] section 4.4 page 109:
2139 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2140 // had been executed with the target RENDERBUFFER and name of zero.
2141
2142 if (this->renderbuffer == renderbuffer)
2143 {
2144 bindRenderbuffer(0);
2145 }
2146
2147 // [OpenGL ES 2.0.24] section 4.4 page 111:
2148 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2149 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2150 // point to which this image was attached in the currently bound framebuffer.
2151
2152 Framebuffer *framebuffer = getFramebuffer();
2153
2154 if (framebuffer)
2155 {
2156 framebuffer->detachRenderbuffer(renderbuffer);
2157 }
2158}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002159
2160Texture *Context::getIncompleteTexture(SamplerType type)
2161{
2162 Texture *t = mIncompleteTextures[type];
2163
2164 if (t == NULL)
2165 {
2166 static const GLubyte color[] = { 0, 0, 0, 255 };
2167
2168 switch (type)
2169 {
2170 default:
2171 UNREACHABLE();
2172 // default falls through to SAMPLER_2D
2173
2174 case SAMPLER_2D:
2175 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002176 Texture2D *incomplete2d = new Texture2D(this);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002177 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002178 t = incomplete2d;
2179 }
2180 break;
2181
2182 case SAMPLER_CUBE:
2183 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002184 TextureCubeMap *incompleteCube = new TextureCubeMap(this);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002185
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002186 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2187 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2188 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2189 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2190 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2191 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002192
2193 t = incompleteCube;
2194 }
2195 break;
2196 }
2197
2198 mIncompleteTextures[type] = t;
2199 }
2200
2201 return t;
2202}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002203
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002204bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002205{
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002206 return cullFace && cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002207}
2208
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002209bool Context::isTriangleMode(GLenum drawMode)
2210{
2211 switch (drawMode)
2212 {
2213 case GL_TRIANGLES:
2214 case GL_TRIANGLE_FAN:
2215 case GL_TRIANGLE_STRIP:
2216 return true;
2217 case GL_POINTS:
2218 case GL_LINES:
2219 case GL_LINE_LOOP:
2220 case GL_LINE_STRIP:
2221 return false;
2222 default: UNREACHABLE();
2223 }
2224
2225 return false;
2226}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002227
2228void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2229{
2230 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2231
2232 vertexAttribute[index].mCurrentValue[0] = values[0];
2233 vertexAttribute[index].mCurrentValue[1] = values[1];
2234 vertexAttribute[index].mCurrentValue[2] = values[2];
2235 vertexAttribute[index].mCurrentValue[3] = values[3];
2236
2237 mVertexDataManager->dirtyCurrentValues();
2238}
2239
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240}
2241
2242extern "C"
2243{
2244gl::Context *glCreateContext(const egl::Config *config)
2245{
2246 return new gl::Context(config);
2247}
2248
2249void glDestroyContext(gl::Context *context)
2250{
2251 delete context;
2252
2253 if (context == gl::getContext())
2254 {
2255 gl::makeCurrent(NULL, NULL, NULL);
2256 }
2257}
2258
2259void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2260{
2261 gl::makeCurrent(context, display, surface);
2262}
2263
2264gl::Context *glGetCurrentContext()
2265{
2266 return gl::getContext();
2267}
2268}