• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
8 
9 #include "libANGLE/renderer/d3d/d3d9/Renderer9.h"
10 
11 #include <EGL/eglext.h>
12 #include <sstream>
13 
14 #include "common/utilities.h"
15 #include "libANGLE/Buffer.h"
16 #include "libANGLE/Context.h"
17 #include "libANGLE/Display.h"
18 #include "libANGLE/Framebuffer.h"
19 #include "libANGLE/FramebufferAttachment.h"
20 #include "libANGLE/Program.h"
21 #include "libANGLE/Renderbuffer.h"
22 #include "libANGLE/State.h"
23 #include "libANGLE/Surface.h"
24 #include "libANGLE/Texture.h"
25 #include "libANGLE/angletypes.h"
26 #include "libANGLE/features.h"
27 #include "libANGLE/formatutils.h"
28 #include "libANGLE/renderer/d3d/CompilerD3D.h"
29 #include "libANGLE/renderer/d3d/DisplayD3D.h"
30 #include "libANGLE/renderer/d3d/FramebufferD3D.h"
31 #include "libANGLE/renderer/d3d/IndexDataManager.h"
32 #include "libANGLE/renderer/d3d/ProgramD3D.h"
33 #include "libANGLE/renderer/d3d/ProgramExecutableD3D.h"
34 #include "libANGLE/renderer/d3d/RenderbufferD3D.h"
35 #include "libANGLE/renderer/d3d/ShaderD3D.h"
36 #include "libANGLE/renderer/d3d/SurfaceD3D.h"
37 #include "libANGLE/renderer/d3d/TextureD3D.h"
38 #include "libANGLE/renderer/d3d/d3d9/Blit9.h"
39 #include "libANGLE/renderer/d3d/d3d9/Buffer9.h"
40 #include "libANGLE/renderer/d3d/d3d9/Context9.h"
41 #include "libANGLE/renderer/d3d/d3d9/Device9.h"
42 #include "libANGLE/renderer/d3d/d3d9/Fence9.h"
43 #include "libANGLE/renderer/d3d/d3d9/Framebuffer9.h"
44 #include "libANGLE/renderer/d3d/d3d9/Image9.h"
45 #include "libANGLE/renderer/d3d/d3d9/IndexBuffer9.h"
46 #include "libANGLE/renderer/d3d/d3d9/NativeWindow9.h"
47 #include "libANGLE/renderer/d3d/d3d9/Query9.h"
48 #include "libANGLE/renderer/d3d/d3d9/RenderTarget9.h"
49 #include "libANGLE/renderer/d3d/d3d9/ShaderExecutable9.h"
50 #include "libANGLE/renderer/d3d/d3d9/SwapChain9.h"
51 #include "libANGLE/renderer/d3d/d3d9/TextureStorage9.h"
52 #include "libANGLE/renderer/d3d/d3d9/VertexArray9.h"
53 #include "libANGLE/renderer/d3d/d3d9/VertexBuffer9.h"
54 #include "libANGLE/renderer/d3d/d3d9/formatutils9.h"
55 #include "libANGLE/renderer/d3d/d3d9/renderer9_utils.h"
56 #include "libANGLE/renderer/d3d/driver_utils_d3d.h"
57 #include "libANGLE/renderer/driver_utils.h"
58 #include "libANGLE/trace.h"
59 
60 #if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
61 #    define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
62 #endif
63 
64 // Enable ANGLE_SUPPORT_SHADER_MODEL_2 if you wish devices with only shader model 2.
65 // Such a device would not be conformant.
66 #ifndef ANGLE_SUPPORT_SHADER_MODEL_2
67 #    define ANGLE_SUPPORT_SHADER_MODEL_2 0
68 #endif
69 
70 namespace rx
71 {
72 
73 namespace
74 {
75 enum
76 {
77     MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
78     MAX_PIXEL_CONSTANT_VECTORS_SM2   = 32,
79     MAX_PIXEL_CONSTANT_VECTORS_SM3   = 224,
80     MAX_VARYING_VECTORS_SM2          = 8,
81     MAX_VARYING_VECTORS_SM3          = 10,
82 
83     MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
84 };
85 
86 template <typename T>
DrawPoints(IDirect3DDevice9 * device,GLsizei count,const void * indices,int minIndex)87 static void DrawPoints(IDirect3DDevice9 *device, GLsizei count, const void *indices, int minIndex)
88 {
89     for (int i = 0; i < count; i++)
90     {
91         unsigned int indexValue =
92             static_cast<unsigned int>(static_cast<const T *>(indices)[i]) - minIndex;
93         device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
94     }
95 }
96 
97 // A hard limit on buffer size. This works around a problem in the NVIDIA drivers where buffer sizes
98 // close to MAX_UINT would give undefined results. The limit of MAX_UINT/2 should be generous enough
99 // for almost any demanding application.
100 constexpr UINT kMaximumBufferSizeHardLimit = std::numeric_limits<UINT>::max() >> 1;
101 }  // anonymous namespace
102 
Renderer9(egl::Display * display)103 Renderer9::Renderer9(egl::Display *display) : RendererD3D(display), mStateManager(this)
104 {
105     mD3d9Module = nullptr;
106 
107     mD3d9         = nullptr;
108     mD3d9Ex       = nullptr;
109     mDevice       = nullptr;
110     mDeviceEx     = nullptr;
111     mDeviceWindow = nullptr;
112     mBlit         = nullptr;
113 
114     mAdapter = D3DADAPTER_DEFAULT;
115 
116     const egl::AttributeMap &attributes = display->getAttributeMap();
117     EGLint requestedDeviceType          = static_cast<EGLint>(attributes.get(
118         EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE));
119     switch (requestedDeviceType)
120     {
121         case EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE:
122             mDeviceType = D3DDEVTYPE_HAL;
123             break;
124 
125         case EGL_PLATFORM_ANGLE_DEVICE_TYPE_D3D_REFERENCE_ANGLE:
126             mDeviceType = D3DDEVTYPE_REF;
127             break;
128 
129         case EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE:
130             mDeviceType = D3DDEVTYPE_NULLREF;
131             break;
132 
133         default:
134             UNREACHABLE();
135     }
136 
137     mMaskedClearSavedState = nullptr;
138 
139     mVertexDataManager = nullptr;
140     mIndexDataManager  = nullptr;
141     mLineLoopIB        = nullptr;
142     mCountingIB        = nullptr;
143 
144     mMaxNullColorbufferLRU = 0;
145     for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
146     {
147         mNullRenderTargetCache[i].lruCount     = 0;
148         mNullRenderTargetCache[i].width        = 0;
149         mNullRenderTargetCache[i].height       = 0;
150         mNullRenderTargetCache[i].renderTarget = nullptr;
151     }
152 
153     mAppliedVertexShader  = nullptr;
154     mAppliedPixelShader   = nullptr;
155     mAppliedProgramSerial = 0;
156 
157     gl::InitializeDebugAnnotations(&mAnnotator);
158 }
159 
setGlobalDebugAnnotator()160 void Renderer9::setGlobalDebugAnnotator()
161 {
162     gl::InitializeDebugAnnotations(&mAnnotator);
163 }
164 
~Renderer9()165 Renderer9::~Renderer9()
166 {
167     if (mDevice)
168     {
169         // If the device is lost, reset it first to prevent leaving the driver in an unstable state
170         if (testDeviceLost())
171         {
172             resetDevice();
173         }
174     }
175 
176     release();
177 }
178 
release()179 void Renderer9::release()
180 {
181     gl::UninitializeDebugAnnotations();
182 
183     mTranslatedAttribCache.clear();
184 
185     releaseDeviceResources();
186 
187     SafeRelease(mDevice);
188     SafeRelease(mDeviceEx);
189     SafeRelease(mD3d9);
190     SafeRelease(mD3d9Ex);
191 
192     mCompiler.release();
193 
194     if (mDeviceWindow)
195     {
196         DestroyWindow(mDeviceWindow);
197         mDeviceWindow = nullptr;
198     }
199 
200     mD3d9Module = nullptr;
201 }
202 
initialize()203 egl::Error Renderer9::initialize()
204 {
205     ANGLE_TRACE_EVENT0("gpu.angle", "GetModuleHandle_d3d9");
206     mD3d9Module = ::LoadLibrary(TEXT("d3d9.dll"));
207 
208     if (mD3d9Module == nullptr)
209     {
210         return egl::Error(EGL_NOT_INITIALIZED, D3D9_INIT_MISSING_DEP, "No D3D9 module found.");
211     }
212 
213     typedef HRESULT(WINAPI * Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex **);
214     Direct3DCreate9ExFunc Direct3DCreate9ExPtr =
215         reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
216 
217     // Use Direct3D9Ex if available. Among other things, this version is less
218     // inclined to report a lost context, for example when the user switches
219     // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are
220     // available.
221     if (static_cast<bool>(ANGLE_D3D9EX) && Direct3DCreate9ExPtr &&
222         SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
223     {
224         ANGLE_TRACE_EVENT0("gpu.angle", "D3d9Ex_QueryInterface");
225         ASSERT(mD3d9Ex);
226         mD3d9Ex->QueryInterface(__uuidof(IDirect3D9), reinterpret_cast<void **>(&mD3d9));
227         ASSERT(mD3d9);
228     }
229     else
230     {
231         ANGLE_TRACE_EVENT0("gpu.angle", "Direct3DCreate9");
232         mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
233     }
234 
235     if (!mD3d9)
236     {
237         return egl::Error(EGL_NOT_INITIALIZED, D3D9_INIT_MISSING_DEP,
238                           "Could not create D3D9 device.");
239     }
240 
241     if (mDisplay->getNativeDisplayId() != nullptr)
242     {
243         //  UNIMPLEMENTED();   // FIXME: Determine which adapter index the device context
244         //  corresponds to
245     }
246 
247     HRESULT result;
248 
249     // Give up on getting device caps after about one second.
250     {
251         ANGLE_TRACE_EVENT0("gpu.angle", "GetDeviceCaps");
252         for (int i = 0; i < 10; ++i)
253         {
254             result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
255             if (SUCCEEDED(result))
256             {
257                 break;
258             }
259             else if (result == D3DERR_NOTAVAILABLE)
260             {
261                 Sleep(100);  // Give the driver some time to initialize/recover
262             }
263             else if (FAILED(result))  // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY,
264                                       // D3DERR_INVALIDDEVICE, or another error we can't recover
265                                       // from
266             {
267                 std::ostringstream err;
268                 err << "Failed to get device caps, " << gl::FmtHR(result);
269                 return egl::Error(EGL_NOT_INITIALIZED, D3D9_INIT_OTHER_ERROR, err.str());
270             }
271         }
272     }
273 
274 #if ANGLE_SUPPORT_SHADER_MODEL_2
275     size_t minShaderModel = 2;
276 #else
277     size_t minShaderModel = 3;
278 #endif
279 
280     if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(minShaderModel, 0))
281     {
282         std::ostringstream err;
283         err << "Renderer does not support PS " << minShaderModel << ".0, aborting!";
284         return egl::Error(EGL_NOT_INITIALIZED, D3D9_INIT_UNSUPPORTED_VERSION, err.str());
285     }
286 
287     // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture
288     // to a render target texture is not supported. This is required by
289     // Texture2D::ensureRenderTarget.
290     if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
291     {
292         return egl::Error(EGL_NOT_INITIALIZED, D3D9_INIT_UNSUPPORTED_STRETCHRECT,
293                           "Renderer does not support StretctRect from textures.");
294     }
295 
296     {
297         ANGLE_TRACE_EVENT0("gpu.angle", "GetAdapterIdentifier");
298         mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
299     }
300 
301     static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
302     static const TCHAR className[]  = TEXT("STATIC");
303 
304     {
305         ANGLE_TRACE_EVENT0("gpu.angle", "CreateWindowEx");
306         mDeviceWindow =
307             CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1,
308                            1, HWND_MESSAGE, nullptr, GetModuleHandle(nullptr), nullptr);
309     }
310 
311     D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
312     DWORD behaviorFlags =
313         D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES | D3DCREATE_MULTITHREADED;
314 
315     {
316         ANGLE_TRACE_EVENT0("gpu.angle", "D3d9_CreateDevice");
317         result = mD3d9->CreateDevice(
318             mAdapter, mDeviceType, mDeviceWindow,
319             behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
320             &presentParameters, &mDevice);
321 
322         if (FAILED(result))
323         {
324             ERR() << "CreateDevice1 failed: (" << gl::FmtHR(result) << ")";
325         }
326     }
327     if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
328     {
329         std::ostringstream err;
330         err << "CreateDevice failed: device lost or out of memory (" << gl::FmtHR(result) << ")";
331         return egl::Error(EGL_BAD_ALLOC, D3D9_INIT_OUT_OF_MEMORY, err.str());
332     }
333 
334     if (FAILED(result))
335     {
336         ANGLE_TRACE_EVENT0("gpu.angle", "D3d9_CreateDevice2");
337         result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow,
338                                      behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING,
339                                      &presentParameters, &mDevice);
340 
341         if (FAILED(result))
342         {
343             ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY ||
344                    result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
345             std::ostringstream err;
346             err << "CreateDevice2 failed: device lost, not available, or of out of memory ("
347                 << gl::FmtHR(result) << ")";
348             return egl::Error(EGL_BAD_ALLOC, D3D9_INIT_OUT_OF_MEMORY, err.str());
349         }
350     }
351 
352     if (mD3d9Ex)
353     {
354         ANGLE_TRACE_EVENT0("gpu.angle", "mDevice_QueryInterface");
355         result = mDevice->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void **)&mDeviceEx);
356         ASSERT(SUCCEEDED(result));
357     }
358 
359     {
360         ANGLE_TRACE_EVENT0("gpu.angle", "ShaderCache initialize");
361         mVertexShaderCache.initialize(mDevice);
362         mPixelShaderCache.initialize(mDevice);
363     }
364 
365     D3DDISPLAYMODE currentDisplayMode;
366     mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
367 
368     // Check vertex texture support
369     // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
370     // We test this using D3D9 by checking support for the R16F format.
371     mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
372                             SUCCEEDED(mD3d9->CheckDeviceFormat(
373                                 mAdapter, mDeviceType, currentDisplayMode.Format,
374                                 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
375 
376     ANGLE_TRY(initializeDevice());
377 
378     return egl::NoError();
379 }
380 
381 // do any one-time device initialization
382 // NOTE: this is also needed after a device lost/reset
383 // to reset the scene status and ensure the default states are reset.
initializeDevice()384 egl::Error Renderer9::initializeDevice()
385 {
386     // Permanent non-default states
387     mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
388     mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
389 
390     if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
391     {
392         mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD &)mDeviceCaps.MaxPointSize);
393     }
394     else
395     {
396         mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000);  // 1.0f
397     }
398 
399     const gl::Caps &rendererCaps = getNativeCaps();
400 
401     mCurVertexSamplerStates.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Vertex]);
402     mCurPixelSamplerStates.resize(
403         rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]);
404 
405     mCurVertexTextures.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Vertex]);
406     mCurPixelTextures.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]);
407 
408     markAllStateDirty();
409 
410     mSceneStarted = false;
411 
412     ASSERT(!mBlit);
413     mBlit = new Blit9(this);
414 
415     ASSERT(!mVertexDataManager && !mIndexDataManager);
416     mIndexDataManager = new IndexDataManager(this);
417 
418     mTranslatedAttribCache.resize(getNativeCaps().maxVertexAttributes);
419 
420     mStateManager.initialize();
421 
422     return egl::NoError();
423 }
424 
getDefaultPresentParameters()425 D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
426 {
427     D3DPRESENT_PARAMETERS presentParameters = {};
428 
429     // The default swap chain is never actually used. Surface will create a new swap chain with the
430     // proper parameters.
431     presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
432     presentParameters.BackBufferCount        = 1;
433     presentParameters.BackBufferFormat       = D3DFMT_UNKNOWN;
434     presentParameters.BackBufferWidth        = 1;
435     presentParameters.BackBufferHeight       = 1;
436     presentParameters.EnableAutoDepthStencil = FALSE;
437     presentParameters.Flags                  = 0;
438     presentParameters.hDeviceWindow          = mDeviceWindow;
439     presentParameters.MultiSampleQuality     = 0;
440     presentParameters.MultiSampleType        = D3DMULTISAMPLE_NONE;
441     presentParameters.PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
442     presentParameters.SwapEffect             = D3DSWAPEFFECT_DISCARD;
443     presentParameters.Windowed               = TRUE;
444 
445     return presentParameters;
446 }
447 
generateConfigs()448 egl::ConfigSet Renderer9::generateConfigs()
449 {
450     static const GLenum colorBufferFormats[] = {
451         GL_BGR5_A1_ANGLEX,
452         GL_BGRA8_EXT,
453         GL_RGB565,
454 
455     };
456 
457     static const GLenum depthStencilBufferFormats[] = {
458         GL_NONE,
459         GL_DEPTH_COMPONENT32_OES,
460         GL_DEPTH24_STENCIL8_OES,
461         GL_DEPTH_COMPONENT24_OES,
462         GL_DEPTH_COMPONENT16,
463     };
464 
465     const gl::Caps &rendererCaps                  = getNativeCaps();
466     const gl::TextureCapsMap &rendererTextureCaps = getNativeTextureCaps();
467 
468     D3DDISPLAYMODE currentDisplayMode;
469     mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
470 
471     // Determine the min and max swap intervals
472     int minSwapInterval = 4;
473     int maxSwapInterval = 0;
474 
475     if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
476     {
477         minSwapInterval = std::min(minSwapInterval, 0);
478         maxSwapInterval = std::max(maxSwapInterval, 0);
479     }
480     if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
481     {
482         minSwapInterval = std::min(minSwapInterval, 1);
483         maxSwapInterval = std::max(maxSwapInterval, 1);
484     }
485     if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
486     {
487         minSwapInterval = std::min(minSwapInterval, 2);
488         maxSwapInterval = std::max(maxSwapInterval, 2);
489     }
490     if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
491     {
492         minSwapInterval = std::min(minSwapInterval, 3);
493         maxSwapInterval = std::max(maxSwapInterval, 3);
494     }
495     if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
496     {
497         minSwapInterval = std::min(minSwapInterval, 4);
498         maxSwapInterval = std::max(maxSwapInterval, 4);
499     }
500 
501     egl::ConfigSet configs;
502     for (size_t formatIndex = 0; formatIndex < ArraySize(colorBufferFormats); formatIndex++)
503     {
504         GLenum colorBufferInternalFormat = colorBufferFormats[formatIndex];
505         const gl::TextureCaps &colorBufferFormatCaps =
506             rendererTextureCaps.get(colorBufferInternalFormat);
507         if (colorBufferFormatCaps.renderbuffer)
508         {
509             ASSERT(colorBufferFormatCaps.textureAttachment);
510             for (size_t depthStencilIndex = 0;
511                  depthStencilIndex < ArraySize(depthStencilBufferFormats); depthStencilIndex++)
512             {
513                 GLenum depthStencilBufferInternalFormat =
514                     depthStencilBufferFormats[depthStencilIndex];
515                 const gl::TextureCaps &depthStencilBufferFormatCaps =
516                     rendererTextureCaps.get(depthStencilBufferInternalFormat);
517                 if (depthStencilBufferFormatCaps.renderbuffer ||
518                     depthStencilBufferInternalFormat == GL_NONE)
519                 {
520                     ASSERT(depthStencilBufferFormatCaps.textureAttachment ||
521                            depthStencilBufferInternalFormat == GL_NONE);
522                     const gl::InternalFormat &colorBufferFormatInfo =
523                         gl::GetSizedInternalFormatInfo(colorBufferInternalFormat);
524                     const gl::InternalFormat &depthStencilBufferFormatInfo =
525                         gl::GetSizedInternalFormatInfo(depthStencilBufferInternalFormat);
526                     const d3d9::TextureFormat &d3d9ColorBufferFormatInfo =
527                         d3d9::GetTextureFormatInfo(colorBufferInternalFormat);
528 
529                     egl::Config config;
530                     config.renderTargetFormat = colorBufferInternalFormat;
531                     config.depthStencilFormat = depthStencilBufferInternalFormat;
532                     config.bufferSize         = colorBufferFormatInfo.getEGLConfigBufferSize();
533                     config.redSize            = colorBufferFormatInfo.redBits;
534                     config.greenSize          = colorBufferFormatInfo.greenBits;
535                     config.blueSize           = colorBufferFormatInfo.blueBits;
536                     config.luminanceSize      = colorBufferFormatInfo.luminanceBits;
537                     config.alphaSize          = colorBufferFormatInfo.alphaBits;
538                     config.alphaMaskSize      = 0;
539                     config.bindToTextureRGB   = (colorBufferFormatInfo.format == GL_RGB);
540                     config.bindToTextureRGBA  = (colorBufferFormatInfo.format == GL_RGBA ||
541                                                 colorBufferFormatInfo.format == GL_BGRA_EXT);
542                     config.colorBufferType    = EGL_RGB_BUFFER;
543                     // Mark as slow if blits to the back-buffer won't be straight forward
544                     config.configCaveat =
545                         (currentDisplayMode.Format == d3d9ColorBufferFormatInfo.renderFormat)
546                             ? EGL_NONE
547                             : EGL_SLOW_CONFIG;
548                     config.configID          = static_cast<EGLint>(configs.size() + 1);
549                     config.conformant        = EGL_OPENGL_ES2_BIT;
550                     config.depthSize         = depthStencilBufferFormatInfo.depthBits;
551                     config.level             = 0;
552                     config.matchNativePixmap = EGL_NONE;
553                     config.maxPBufferWidth   = rendererCaps.max2DTextureSize;
554                     config.maxPBufferHeight  = rendererCaps.max2DTextureSize;
555                     config.maxPBufferPixels =
556                         rendererCaps.max2DTextureSize * rendererCaps.max2DTextureSize;
557                     config.maxSwapInterval  = maxSwapInterval;
558                     config.minSwapInterval  = minSwapInterval;
559                     config.nativeRenderable = EGL_FALSE;
560                     config.nativeVisualID   = 0;
561                     config.nativeVisualType = EGL_NONE;
562                     config.renderableType   = EGL_OPENGL_ES2_BIT;
563                     config.sampleBuffers    = 0;  // FIXME: enumerate multi-sampling
564                     config.samples          = 0;
565                     config.stencilSize      = depthStencilBufferFormatInfo.stencilBits;
566                     config.surfaceType =
567                         EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
568                     config.transparentType       = EGL_NONE;
569                     config.transparentRedValue   = 0;
570                     config.transparentGreenValue = 0;
571                     config.transparentBlueValue  = 0;
572                     config.colorComponentType    = gl_egl::GLComponentTypeToEGLColorComponentType(
573                         colorBufferFormatInfo.componentType);
574 
575                     configs.add(config);
576                 }
577             }
578         }
579     }
580 
581     ASSERT(configs.size() > 0);
582     return configs;
583 }
584 
generateDisplayExtensions(egl::DisplayExtensions * outExtensions) const585 void Renderer9::generateDisplayExtensions(egl::DisplayExtensions *outExtensions) const
586 {
587     outExtensions->createContextRobustness = true;
588 
589     if (getShareHandleSupport())
590     {
591         outExtensions->d3dShareHandleClientBuffer     = true;
592         outExtensions->surfaceD3DTexture2DShareHandle = true;
593     }
594     outExtensions->d3dTextureClientBuffer = true;
595 
596     outExtensions->querySurfacePointer = true;
597     outExtensions->windowFixedSize     = true;
598     outExtensions->postSubBuffer       = true;
599 
600     outExtensions->image               = true;
601     outExtensions->imageBase           = true;
602     outExtensions->glTexture2DImage    = true;
603     outExtensions->glRenderbufferImage = true;
604 
605     outExtensions->noConfigContext = true;
606 
607     // Contexts are virtualized so textures and semaphores can be shared globally
608     outExtensions->displayTextureShareGroup   = true;
609     outExtensions->displaySemaphoreShareGroup = true;
610 
611     // D3D9 can be used without an output surface
612     outExtensions->surfacelessContext = true;
613 
614     outExtensions->robustResourceInitializationANGLE = true;
615 }
616 
startScene()617 void Renderer9::startScene()
618 {
619     if (!mSceneStarted)
620     {
621         long result = mDevice->BeginScene();
622         if (SUCCEEDED(result))
623         {
624             // This is defensive checking against the device being
625             // lost at unexpected times.
626             mSceneStarted = true;
627         }
628     }
629 }
630 
endScene()631 void Renderer9::endScene()
632 {
633     if (mSceneStarted)
634     {
635         // EndScene can fail if the device was lost, for example due
636         // to a TDR during a draw call.
637         mDevice->EndScene();
638         mSceneStarted = false;
639     }
640 }
641 
flush(const gl::Context * context)642 angle::Result Renderer9::flush(const gl::Context *context)
643 {
644     IDirect3DQuery9 *query = nullptr;
645     ANGLE_TRY(allocateEventQuery(context, &query));
646 
647     Context9 *context9 = GetImplAs<Context9>(context);
648 
649     HRESULT result = query->Issue(D3DISSUE_END);
650     ANGLE_TRY_HR(context9, result, "Failed to issue event query");
651 
652     // Grab the query data once
653     result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
654     freeEventQuery(query);
655     ANGLE_TRY_HR(context9, result, "Failed to get event query data");
656 
657     return angle::Result::Continue;
658 }
659 
finish(const gl::Context * context)660 angle::Result Renderer9::finish(const gl::Context *context)
661 {
662     IDirect3DQuery9 *query = nullptr;
663     ANGLE_TRY(allocateEventQuery(context, &query));
664 
665     Context9 *context9 = GetImplAs<Context9>(context);
666 
667     HRESULT result = query->Issue(D3DISSUE_END);
668     ANGLE_TRY_HR(context9, result, "Failed to issue event query");
669 
670     // Grab the query data once
671     result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
672     if (FAILED(result))
673     {
674         freeEventQuery(query);
675     }
676     ANGLE_TRY_HR(context9, result, "Failed to get event query data");
677 
678     // Loop until the query completes
679     unsigned int attempt = 0;
680     while (result == S_FALSE)
681     {
682         // Keep polling, but allow other threads to do something useful first
683         std::this_thread::yield();
684 
685         result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
686         attempt++;
687 
688         if (result == S_FALSE)
689         {
690             // explicitly check for device loss
691             // some drivers seem to return S_FALSE even if the device is lost
692             // instead of D3DERR_DEVICELOST like they should
693             bool checkDeviceLost = (attempt % kPollingD3DDeviceLostCheckFrequency) == 0;
694             if (checkDeviceLost && testDeviceLost())
695             {
696                 result = D3DERR_DEVICELOST;
697             }
698         }
699 
700         if (FAILED(result))
701         {
702             freeEventQuery(query);
703         }
704         ANGLE_TRY_HR(context9, result, "Failed to get event query data");
705     }
706 
707     freeEventQuery(query);
708 
709     return angle::Result::Continue;
710 }
711 
isValidNativeWindow(EGLNativeWindowType window) const712 bool Renderer9::isValidNativeWindow(EGLNativeWindowType window) const
713 {
714     return NativeWindow9::IsValidNativeWindow(window);
715 }
716 
createNativeWindow(EGLNativeWindowType window,const egl::Config *,const egl::AttributeMap &) const717 NativeWindowD3D *Renderer9::createNativeWindow(EGLNativeWindowType window,
718                                                const egl::Config *,
719                                                const egl::AttributeMap &) const
720 {
721     return new NativeWindow9(window);
722 }
723 
createSwapChain(NativeWindowD3D * nativeWindow,HANDLE shareHandle,IUnknown * d3dTexture,GLenum backBufferFormat,GLenum depthBufferFormat,EGLint orientation,EGLint samples)724 SwapChainD3D *Renderer9::createSwapChain(NativeWindowD3D *nativeWindow,
725                                          HANDLE shareHandle,
726                                          IUnknown *d3dTexture,
727                                          GLenum backBufferFormat,
728                                          GLenum depthBufferFormat,
729                                          EGLint orientation,
730                                          EGLint samples)
731 {
732     return new SwapChain9(this, GetAs<NativeWindow9>(nativeWindow), shareHandle, d3dTexture,
733                           backBufferFormat, depthBufferFormat, orientation);
734 }
735 
getD3DTextureInfo(const egl::Config * configuration,IUnknown * d3dTexture,const egl::AttributeMap & attribs,EGLint * width,EGLint * height,GLsizei * samples,gl::Format * glFormat,const angle::Format ** angleFormat,UINT * arraySlice) const736 egl::Error Renderer9::getD3DTextureInfo(const egl::Config *configuration,
737                                         IUnknown *d3dTexture,
738                                         const egl::AttributeMap &attribs,
739                                         EGLint *width,
740                                         EGLint *height,
741                                         GLsizei *samples,
742                                         gl::Format *glFormat,
743                                         const angle::Format **angleFormat,
744                                         UINT *arraySlice) const
745 {
746     IDirect3DTexture9 *texture = nullptr;
747     if (FAILED(d3dTexture->QueryInterface(&texture)))
748     {
749         return egl::Error(EGL_BAD_PARAMETER, "Client buffer is not a IDirect3DTexture9");
750     }
751 
752     IDirect3DDevice9 *textureDevice = nullptr;
753     texture->GetDevice(&textureDevice);
754     if (textureDevice != mDevice)
755     {
756         SafeRelease(texture);
757         return egl::Error(EGL_BAD_PARAMETER, "Texture's device does not match.");
758     }
759     SafeRelease(textureDevice);
760 
761     D3DSURFACE_DESC desc;
762     texture->GetLevelDesc(0, &desc);
763     SafeRelease(texture);
764 
765     if (width)
766     {
767         *width = static_cast<EGLint>(desc.Width);
768     }
769     if (height)
770     {
771         *height = static_cast<EGLint>(desc.Height);
772     }
773 
774     // GetSamplesCount() returns 0 when multisampling isn't used.
775     GLsizei sampleCount = d3d9_gl::GetSamplesCount(desc.MultiSampleType);
776     if ((configuration && configuration->samples > 1) || sampleCount != 0)
777     {
778         return egl::Error(EGL_BAD_PARAMETER,
779                           "Multisampling not supported for client buffer texture");
780     }
781     if (samples)
782     {
783         *samples = static_cast<EGLint>(sampleCount);
784     }
785 
786     // From table egl.restrictions in EGL_ANGLE_d3d_texture_client_buffer.
787     switch (desc.Format)
788     {
789         case D3DFMT_R8G8B8:
790         case D3DFMT_A8R8G8B8:
791         case D3DFMT_A16B16G16R16F:
792         case D3DFMT_A32B32G32R32F:
793             break;
794 
795         default:
796             std::ostringstream err;
797             err << "Unknown client buffer texture format: " << desc.Format;
798             return egl::Error(EGL_BAD_PARAMETER, err.str());
799     }
800 
801     const auto &d3dFormatInfo = d3d9::GetD3DFormatInfo(desc.Format);
802     ASSERT(d3dFormatInfo.info().id != angle::FormatID::NONE);
803 
804     if (glFormat)
805     {
806         *glFormat = gl::Format(d3dFormatInfo.info().glInternalFormat);
807     }
808 
809     if (angleFormat)
810     {
811 
812         *angleFormat = &d3dFormatInfo.info();
813     }
814 
815     if (arraySlice)
816     {
817         *arraySlice = 0;
818     }
819 
820     return egl::NoError();
821 }
822 
validateShareHandle(const egl::Config * config,HANDLE shareHandle,const egl::AttributeMap & attribs) const823 egl::Error Renderer9::validateShareHandle(const egl::Config *config,
824                                           HANDLE shareHandle,
825                                           const egl::AttributeMap &attribs) const
826 {
827     if (shareHandle == nullptr)
828     {
829         return egl::Error(EGL_BAD_PARAMETER, "NULL share handle.");
830     }
831 
832     EGLint width  = attribs.getAsInt(EGL_WIDTH, 0);
833     EGLint height = attribs.getAsInt(EGL_HEIGHT, 0);
834     ASSERT(width != 0 && height != 0);
835 
836     const d3d9::TextureFormat &backBufferd3dFormatInfo =
837         d3d9::GetTextureFormatInfo(config->renderTargetFormat);
838 
839     IDirect3DTexture9 *texture = nullptr;
840     HRESULT result             = mDevice->CreateTexture(width, height, 1, D3DUSAGE_RENDERTARGET,
841                                                         backBufferd3dFormatInfo.texFormat, D3DPOOL_DEFAULT,
842                                                         &texture, &shareHandle);
843     if (FAILED(result))
844     {
845         std::ostringstream err;
846         err << "Failed to open share handle, " << gl::FmtHR(result);
847         return egl::Error(EGL_BAD_PARAMETER, err.str());
848     }
849 
850     DWORD levelCount = texture->GetLevelCount();
851 
852     D3DSURFACE_DESC desc;
853     texture->GetLevelDesc(0, &desc);
854     SafeRelease(texture);
855 
856     if (levelCount != 1 || desc.Width != static_cast<UINT>(width) ||
857         desc.Height != static_cast<UINT>(height) ||
858         desc.Format != backBufferd3dFormatInfo.texFormat)
859     {
860         return egl::Error(EGL_BAD_PARAMETER, "Invalid texture parameters in share handle texture.");
861     }
862 
863     return egl::NoError();
864 }
865 
createContext(const gl::State & state,gl::ErrorSet * errorSet)866 ContextImpl *Renderer9::createContext(const gl::State &state, gl::ErrorSet *errorSet)
867 {
868     return new Context9(state, errorSet, this);
869 }
870 
getD3DDevice()871 void *Renderer9::getD3DDevice()
872 {
873     return mDevice;
874 }
875 
allocateEventQuery(const gl::Context * context,IDirect3DQuery9 ** outQuery)876 angle::Result Renderer9::allocateEventQuery(const gl::Context *context, IDirect3DQuery9 **outQuery)
877 {
878     if (mEventQueryPool.empty())
879     {
880         HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, outQuery);
881         ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to allocate event query");
882     }
883     else
884     {
885         *outQuery = mEventQueryPool.back();
886         mEventQueryPool.pop_back();
887     }
888 
889     return angle::Result::Continue;
890 }
891 
freeEventQuery(IDirect3DQuery9 * query)892 void Renderer9::freeEventQuery(IDirect3DQuery9 *query)
893 {
894     if (mEventQueryPool.size() > 1000)
895     {
896         SafeRelease(query);
897     }
898     else
899     {
900         mEventQueryPool.push_back(query);
901     }
902 }
903 
createVertexShader(d3d::Context * context,const DWORD * function,size_t length,IDirect3DVertexShader9 ** outShader)904 angle::Result Renderer9::createVertexShader(d3d::Context *context,
905                                             const DWORD *function,
906                                             size_t length,
907                                             IDirect3DVertexShader9 **outShader)
908 {
909     return mVertexShaderCache.create(context, function, length, outShader);
910 }
911 
createPixelShader(d3d::Context * context,const DWORD * function,size_t length,IDirect3DPixelShader9 ** outShader)912 angle::Result Renderer9::createPixelShader(d3d::Context *context,
913                                            const DWORD *function,
914                                            size_t length,
915                                            IDirect3DPixelShader9 **outShader)
916 {
917     return mPixelShaderCache.create(context, function, length, outShader);
918 }
919 
createVertexBuffer(UINT Length,DWORD Usage,IDirect3DVertexBuffer9 ** ppVertexBuffer)920 HRESULT Renderer9::createVertexBuffer(UINT Length,
921                                       DWORD Usage,
922                                       IDirect3DVertexBuffer9 **ppVertexBuffer)
923 {
924     // Force buffers to be limited to a fixed max size.
925     if (Length > kMaximumBufferSizeHardLimit)
926     {
927         return E_OUTOFMEMORY;
928     }
929 
930     D3DPOOL Pool = getBufferPool(Usage);
931     return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, nullptr);
932 }
933 
createVertexBuffer()934 VertexBuffer *Renderer9::createVertexBuffer()
935 {
936     return new VertexBuffer9(this);
937 }
938 
createIndexBuffer(UINT Length,DWORD Usage,D3DFORMAT Format,IDirect3DIndexBuffer9 ** ppIndexBuffer)939 HRESULT Renderer9::createIndexBuffer(UINT Length,
940                                      DWORD Usage,
941                                      D3DFORMAT Format,
942                                      IDirect3DIndexBuffer9 **ppIndexBuffer)
943 {
944     // Force buffers to be limited to a fixed max size.
945     if (Length > kMaximumBufferSizeHardLimit)
946     {
947         return E_OUTOFMEMORY;
948     }
949 
950     D3DPOOL Pool = getBufferPool(Usage);
951     return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, nullptr);
952 }
953 
createIndexBuffer()954 IndexBuffer *Renderer9::createIndexBuffer()
955 {
956     return new IndexBuffer9(this);
957 }
958 
createStreamProducerD3DTexture(egl::Stream::ConsumerType consumerType,const egl::AttributeMap & attribs)959 StreamProducerImpl *Renderer9::createStreamProducerD3DTexture(
960     egl::Stream::ConsumerType consumerType,
961     const egl::AttributeMap &attribs)
962 {
963     // Streams are not supported under D3D9
964     UNREACHABLE();
965     return nullptr;
966 }
967 
supportsFastCopyBufferToTexture(GLenum internalFormat) const968 bool Renderer9::supportsFastCopyBufferToTexture(GLenum internalFormat) const
969 {
970     // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
971     return false;
972 }
973 
fastCopyBufferToTexture(const gl::Context * context,const gl::PixelUnpackState & unpack,gl::Buffer * unpackBuffer,unsigned int offset,RenderTargetD3D * destRenderTarget,GLenum destinationFormat,GLenum sourcePixelsType,const gl::Box & destArea)974 angle::Result Renderer9::fastCopyBufferToTexture(const gl::Context *context,
975                                                  const gl::PixelUnpackState &unpack,
976                                                  gl::Buffer *unpackBuffer,
977                                                  unsigned int offset,
978                                                  RenderTargetD3D *destRenderTarget,
979                                                  GLenum destinationFormat,
980                                                  GLenum sourcePixelsType,
981                                                  const gl::Box &destArea)
982 {
983     // Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
984     ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
985     return angle::Result::Stop;
986 }
987 
setSamplerState(const gl::Context * context,gl::ShaderType type,int index,gl::Texture * texture,const gl::SamplerState & samplerState)988 angle::Result Renderer9::setSamplerState(const gl::Context *context,
989                                          gl::ShaderType type,
990                                          int index,
991                                          gl::Texture *texture,
992                                          const gl::SamplerState &samplerState)
993 {
994     CurSamplerState &appliedSampler = (type == gl::ShaderType::Fragment)
995                                           ? mCurPixelSamplerStates[index]
996                                           : mCurVertexSamplerStates[index];
997 
998     // Make sure to add the level offset for our tiny compressed texture workaround
999     TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
1000 
1001     TextureStorage *storage = nullptr;
1002     ANGLE_TRY(textureD3D->getNativeTexture(context, &storage));
1003 
1004     // Storage should exist, texture should be complete
1005     ASSERT(storage);
1006 
1007     DWORD baseLevel = texture->getBaseLevel() + storage->getTopLevel();
1008 
1009     if (appliedSampler.forceSet || appliedSampler.baseLevel != baseLevel ||
1010         memcmp(&samplerState, &appliedSampler, sizeof(gl::SamplerState)) != 0)
1011     {
1012         int d3dSamplerOffset = (type == gl::ShaderType::Fragment) ? 0 : D3DVERTEXTEXTURESAMPLER0;
1013         int d3dSampler       = index + d3dSamplerOffset;
1014 
1015         mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU,
1016                                  gl_d3d9::ConvertTextureWrap(samplerState.getWrapS()));
1017         mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV,
1018                                  gl_d3d9::ConvertTextureWrap(samplerState.getWrapT()));
1019 
1020         mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER,
1021                                  gl_d3d9::ConvertMagFilter(samplerState.getMagFilter(),
1022                                                            samplerState.getMaxAnisotropy()));
1023 
1024         D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1025         float lodBias;
1026         gl_d3d9::ConvertMinFilter(samplerState.getMinFilter(), &d3dMinFilter, &d3dMipFilter,
1027                                   &lodBias, samplerState.getMaxAnisotropy(), baseLevel);
1028         mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
1029         mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
1030         mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, baseLevel);
1031         mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPMAPLODBIAS, static_cast<DWORD>(lodBias));
1032         if (getNativeExtensions().textureFilterAnisotropicEXT)
1033         {
1034             DWORD maxAnisotropy = std::min(mDeviceCaps.MaxAnisotropy,
1035                                            static_cast<DWORD>(samplerState.getMaxAnisotropy()));
1036             mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, maxAnisotropy);
1037         }
1038 
1039         const gl::InternalFormat &info =
1040             gl::GetSizedInternalFormatInfo(textureD3D->getBaseLevelInternalFormat());
1041 
1042         mDevice->SetSamplerState(d3dSampler, D3DSAMP_SRGBTEXTURE, info.colorEncoding == GL_SRGB);
1043 
1044         if (samplerState.usesBorderColor())
1045         {
1046             angle::ColorGeneric borderColor = texture->getBorderColor();
1047             ASSERT(borderColor.type == angle::ColorGeneric::Type::Float);
1048 
1049             // Enforce opaque alpha for opaque formats, excluding DXT1 RGBA as it has no bits info.
1050             if (info.alphaBits == 0 && info.componentCount < 4)
1051             {
1052                 borderColor.colorF.alpha = 1.0f;
1053             }
1054 
1055             if (info.isLUMA())
1056             {
1057                 if (info.luminanceBits == 0)
1058                 {
1059                     borderColor.colorF.red = 0.0f;
1060                 }
1061                 // Older Intel drivers use RGBA border color when sampling from D3DFMT_A8L8.
1062                 // However, some recent Intel drivers sample alpha from green border channel
1063                 // when using this format. Assume the old behavior because newer GPUs should
1064                 // use D3D11 anyway.
1065                 borderColor.colorF.green = borderColor.colorF.red;
1066                 borderColor.colorF.blue  = borderColor.colorF.red;
1067             }
1068 
1069             D3DCOLOR d3dBorderColor;
1070             if (info.colorEncoding == GL_SRGB && getFeatures().borderColorSrgb.enabled)
1071             {
1072                 d3dBorderColor =
1073                     D3DCOLOR_RGBA(gl::linearToSRGB(gl::clamp01(borderColor.colorF.red)),
1074                                   gl::linearToSRGB(gl::clamp01(borderColor.colorF.green)),
1075                                   gl::linearToSRGB(gl::clamp01(borderColor.colorF.blue)),
1076                                   gl::unorm<8>(borderColor.colorF.alpha));
1077             }
1078             else
1079             {
1080                 d3dBorderColor = gl_d3d9::ConvertColor(borderColor.colorF);
1081             }
1082 
1083             mDevice->SetSamplerState(d3dSampler, D3DSAMP_BORDERCOLOR, d3dBorderColor);
1084         }
1085     }
1086 
1087     appliedSampler.forceSet     = false;
1088     appliedSampler.samplerState = samplerState;
1089     appliedSampler.baseLevel    = baseLevel;
1090 
1091     return angle::Result::Continue;
1092 }
1093 
setTexture(const gl::Context * context,gl::ShaderType type,int index,gl::Texture * texture)1094 angle::Result Renderer9::setTexture(const gl::Context *context,
1095                                     gl::ShaderType type,
1096                                     int index,
1097                                     gl::Texture *texture)
1098 {
1099     int d3dSamplerOffset = (type == gl::ShaderType::Fragment) ? 0 : D3DVERTEXTEXTURESAMPLER0;
1100     int d3dSampler       = index + d3dSamplerOffset;
1101     IDirect3DBaseTexture9 *d3dTexture = nullptr;
1102     bool forceSetTexture              = false;
1103 
1104     std::vector<uintptr_t> &appliedTextures =
1105         (type == gl::ShaderType::Fragment) ? mCurPixelTextures : mCurVertexTextures;
1106 
1107     if (texture)
1108     {
1109         TextureD3D *textureImpl = GetImplAs<TextureD3D>(texture);
1110 
1111         TextureStorage *texStorage = nullptr;
1112         ANGLE_TRY(textureImpl->getNativeTexture(context, &texStorage));
1113 
1114         // Texture should be complete and have a storage
1115         ASSERT(texStorage);
1116 
1117         TextureStorage9 *storage9 = GetAs<TextureStorage9>(texStorage);
1118         ANGLE_TRY(storage9->getBaseTexture(context, &d3dTexture));
1119 
1120         // If we get NULL back from getBaseTexture here, something went wrong
1121         // in the texture class and we're unexpectedly missing the d3d texture
1122         ASSERT(d3dTexture != nullptr);
1123 
1124         forceSetTexture = textureImpl->hasDirtyImages();
1125         textureImpl->resetDirty();
1126     }
1127 
1128     if (forceSetTexture || appliedTextures[index] != reinterpret_cast<uintptr_t>(d3dTexture))
1129     {
1130         mDevice->SetTexture(d3dSampler, d3dTexture);
1131     }
1132 
1133     appliedTextures[index] = reinterpret_cast<uintptr_t>(d3dTexture);
1134 
1135     return angle::Result::Continue;
1136 }
1137 
updateState(const gl::Context * context,gl::PrimitiveMode drawMode)1138 angle::Result Renderer9::updateState(const gl::Context *context, gl::PrimitiveMode drawMode)
1139 {
1140     const auto &glState = context->getState();
1141 
1142     // Applies the render target surface, depth stencil surface, viewport rectangle and
1143     // scissor rectangle to the renderer
1144     gl::Framebuffer *framebuffer = glState.getDrawFramebuffer();
1145     ASSERT(framebuffer && !framebuffer->hasAnyDirtyBit());
1146 
1147     Framebuffer9 *framebuffer9 = GetImplAs<Framebuffer9>(framebuffer);
1148 
1149     ANGLE_TRY(applyRenderTarget(context, framebuffer9->getCachedColorRenderTargets()[0],
1150                                 framebuffer9->getCachedDepthStencilRenderTarget()));
1151 
1152     // Setting viewport state
1153     setViewport(glState.getViewport(), glState.getNearPlane(), glState.getFarPlane(), drawMode,
1154                 glState.getRasterizerState().frontFace, false);
1155 
1156     // Setting scissors state
1157     setScissorRectangle(glState.getScissor(), glState.isScissorTestEnabled());
1158 
1159     // Setting blend, depth stencil, and rasterizer states
1160     // Since framebuffer->getSamples will return the original samples which may be different with
1161     // the sample counts that we set in render target view, here we use renderTarget->getSamples to
1162     // get the actual samples.
1163     GLsizei samples                                       = 0;
1164     const gl::FramebufferAttachment *firstColorAttachment = framebuffer->getFirstColorAttachment();
1165     if (firstColorAttachment)
1166     {
1167         ASSERT(firstColorAttachment->isAttached());
1168         RenderTarget9 *renderTarget = nullptr;
1169         ANGLE_TRY(firstColorAttachment->getRenderTarget(context, firstColorAttachment->getSamples(),
1170                                                         &renderTarget));
1171         samples = renderTarget->getSamples();
1172 
1173         mDevice->SetRenderState(D3DRS_SRGBWRITEENABLE,
1174                                 renderTarget->getInternalFormat() == GL_SRGB8_ALPHA8);
1175     }
1176     gl::RasterizerState rasterizer = glState.getRasterizerState();
1177     rasterizer.pointDrawMode       = (drawMode == gl::PrimitiveMode::Points);
1178     rasterizer.multiSample         = (samples != 0);
1179 
1180     ANGLE_TRY(setBlendDepthRasterStates(context, drawMode));
1181 
1182     mStateManager.resetDirtyBits();
1183 
1184     return angle::Result::Continue;
1185 }
1186 
setScissorRectangle(const gl::Rectangle & scissor,bool enabled)1187 void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
1188 {
1189     mStateManager.setScissorState(scissor, enabled);
1190 }
1191 
setBlendDepthRasterStates(const gl::Context * context,gl::PrimitiveMode drawMode)1192 angle::Result Renderer9::setBlendDepthRasterStates(const gl::Context *context,
1193                                                    gl::PrimitiveMode drawMode)
1194 {
1195     const auto &glState              = context->getState();
1196     gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
1197     ASSERT(!drawFramebuffer->hasAnyDirtyBit());
1198     // Since framebuffer->getSamples will return the original samples which may be different with
1199     // the sample counts that we set in render target view, here we use renderTarget->getSamples to
1200     // get the actual samples.
1201     GLsizei samples = 0;
1202     const gl::FramebufferAttachment *firstColorAttachment =
1203         drawFramebuffer->getFirstColorAttachment();
1204     if (firstColorAttachment)
1205     {
1206         ASSERT(firstColorAttachment->isAttached());
1207         RenderTarget9 *renderTarget = nullptr;
1208         ANGLE_TRY(firstColorAttachment->getRenderTarget(context, firstColorAttachment->getSamples(),
1209                                                         &renderTarget));
1210         samples = renderTarget->getSamples();
1211     }
1212     gl::RasterizerState rasterizer = glState.getRasterizerState();
1213     rasterizer.pointDrawMode       = (drawMode == gl::PrimitiveMode::Points);
1214     rasterizer.multiSample         = (samples != 0);
1215 
1216     unsigned int mask = GetBlendSampleMask(glState, samples);
1217     mStateManager.setBlendDepthRasterStates(glState, mask);
1218     return angle::Result::Continue;
1219 }
1220 
setViewport(const gl::Rectangle & viewport,float zNear,float zFar,gl::PrimitiveMode drawMode,GLenum frontFace,bool ignoreViewport)1221 void Renderer9::setViewport(const gl::Rectangle &viewport,
1222                             float zNear,
1223                             float zFar,
1224                             gl::PrimitiveMode drawMode,
1225                             GLenum frontFace,
1226                             bool ignoreViewport)
1227 {
1228     mStateManager.setViewportState(viewport, zNear, zFar, drawMode, frontFace, ignoreViewport);
1229 }
1230 
applyPrimitiveType(gl::PrimitiveMode mode,GLsizei count,bool usesPointSize)1231 bool Renderer9::applyPrimitiveType(gl::PrimitiveMode mode, GLsizei count, bool usesPointSize)
1232 {
1233     switch (mode)
1234     {
1235         case gl::PrimitiveMode::Points:
1236             mPrimitiveType  = D3DPT_POINTLIST;
1237             mPrimitiveCount = count;
1238             break;
1239         case gl::PrimitiveMode::Lines:
1240             mPrimitiveType  = D3DPT_LINELIST;
1241             mPrimitiveCount = count / 2;
1242             break;
1243         case gl::PrimitiveMode::LineLoop:
1244             mPrimitiveType = D3DPT_LINESTRIP;
1245             mPrimitiveCount =
1246                 count - 1;  // D3D doesn't support line loops, so we draw the last line separately
1247             break;
1248         case gl::PrimitiveMode::LineStrip:
1249             mPrimitiveType  = D3DPT_LINESTRIP;
1250             mPrimitiveCount = count - 1;
1251             break;
1252         case gl::PrimitiveMode::Triangles:
1253             mPrimitiveType  = D3DPT_TRIANGLELIST;
1254             mPrimitiveCount = count / 3;
1255             break;
1256         case gl::PrimitiveMode::TriangleStrip:
1257             mPrimitiveType  = D3DPT_TRIANGLESTRIP;
1258             mPrimitiveCount = count - 2;
1259             break;
1260         case gl::PrimitiveMode::TriangleFan:
1261             mPrimitiveType  = D3DPT_TRIANGLEFAN;
1262             mPrimitiveCount = count - 2;
1263             break;
1264         default:
1265             UNREACHABLE();
1266             return false;
1267     }
1268 
1269     return mPrimitiveCount > 0;
1270 }
1271 
getNullColorRenderTarget(const gl::Context * context,const RenderTarget9 * depthRenderTarget,const RenderTarget9 ** outColorRenderTarget)1272 angle::Result Renderer9::getNullColorRenderTarget(const gl::Context *context,
1273                                                   const RenderTarget9 *depthRenderTarget,
1274                                                   const RenderTarget9 **outColorRenderTarget)
1275 {
1276     ASSERT(depthRenderTarget);
1277 
1278     const gl::Extents &size = depthRenderTarget->getExtents();
1279 
1280     // search cached nullcolorbuffers
1281     for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1282     {
1283         if (mNullRenderTargetCache[i].renderTarget != nullptr &&
1284             mNullRenderTargetCache[i].width == size.width &&
1285             mNullRenderTargetCache[i].height == size.height)
1286         {
1287             mNullRenderTargetCache[i].lruCount = ++mMaxNullColorbufferLRU;
1288             *outColorRenderTarget              = mNullRenderTargetCache[i].renderTarget;
1289             return angle::Result::Continue;
1290         }
1291     }
1292 
1293     RenderTargetD3D *nullRenderTarget = nullptr;
1294     ANGLE_TRY(createRenderTarget(context, size.width, size.height, GL_NONE, 0, &nullRenderTarget));
1295 
1296     // add nullbuffer to the cache
1297     NullRenderTargetCacheEntry *oldest = &mNullRenderTargetCache[0];
1298     for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1299     {
1300         if (mNullRenderTargetCache[i].lruCount < oldest->lruCount)
1301         {
1302             oldest = &mNullRenderTargetCache[i];
1303         }
1304     }
1305 
1306     SafeDelete(oldest->renderTarget);
1307     oldest->renderTarget = GetAs<RenderTarget9>(nullRenderTarget);
1308     oldest->lruCount     = ++mMaxNullColorbufferLRU;
1309     oldest->width        = size.width;
1310     oldest->height       = size.height;
1311 
1312     *outColorRenderTarget = oldest->renderTarget;
1313     return angle::Result::Continue;
1314 }
1315 
applyRenderTarget(const gl::Context * context,const RenderTarget9 * colorRenderTargetIn,const RenderTarget9 * depthStencilRenderTarget)1316 angle::Result Renderer9::applyRenderTarget(const gl::Context *context,
1317                                            const RenderTarget9 *colorRenderTargetIn,
1318                                            const RenderTarget9 *depthStencilRenderTarget)
1319 {
1320     // if there is no color attachment we must synthesize a NULL colorattachment
1321     // to keep the D3D runtime happy.  This should only be possible if depth texturing.
1322     const RenderTarget9 *colorRenderTarget = colorRenderTargetIn;
1323     if (colorRenderTarget == nullptr)
1324     {
1325         ANGLE_TRY(getNullColorRenderTarget(context, depthStencilRenderTarget, &colorRenderTarget));
1326     }
1327     ASSERT(colorRenderTarget != nullptr);
1328 
1329     size_t renderTargetWidth  = 0;
1330     size_t renderTargetHeight = 0;
1331 
1332     bool renderTargetChanged        = false;
1333     unsigned int renderTargetSerial = colorRenderTarget->getSerial();
1334     if (renderTargetSerial != mAppliedRenderTargetSerial)
1335     {
1336         // Apply the render target on the device
1337         IDirect3DSurface9 *renderTargetSurface = colorRenderTarget->getSurface();
1338         ASSERT(renderTargetSurface);
1339 
1340         mDevice->SetRenderTarget(0, renderTargetSurface);
1341         SafeRelease(renderTargetSurface);
1342 
1343         renderTargetWidth  = colorRenderTarget->getWidth();
1344         renderTargetHeight = colorRenderTarget->getHeight();
1345 
1346         mAppliedRenderTargetSerial = renderTargetSerial;
1347         renderTargetChanged        = true;
1348     }
1349 
1350     unsigned int depthStencilSerial = 0;
1351     if (depthStencilRenderTarget != nullptr)
1352     {
1353         depthStencilSerial = depthStencilRenderTarget->getSerial();
1354     }
1355 
1356     if (depthStencilSerial != mAppliedDepthStencilSerial || !mDepthStencilInitialized)
1357     {
1358         unsigned int depthSize   = 0;
1359         unsigned int stencilSize = 0;
1360 
1361         // Apply the depth stencil on the device
1362         if (depthStencilRenderTarget)
1363         {
1364             IDirect3DSurface9 *depthStencilSurface = depthStencilRenderTarget->getSurface();
1365             ASSERT(depthStencilSurface);
1366 
1367             mDevice->SetDepthStencilSurface(depthStencilSurface);
1368             SafeRelease(depthStencilSurface);
1369 
1370             const gl::InternalFormat &format =
1371                 gl::GetSizedInternalFormatInfo(depthStencilRenderTarget->getInternalFormat());
1372 
1373             depthSize   = format.depthBits;
1374             stencilSize = format.stencilBits;
1375         }
1376         else
1377         {
1378             mDevice->SetDepthStencilSurface(nullptr);
1379         }
1380 
1381         mStateManager.updateDepthSizeIfChanged(mDepthStencilInitialized, depthSize);
1382         mStateManager.updateStencilSizeIfChanged(mDepthStencilInitialized, stencilSize);
1383 
1384         mAppliedDepthStencilSerial = depthStencilSerial;
1385         mDepthStencilInitialized   = true;
1386     }
1387 
1388     if (renderTargetChanged || !mRenderTargetDescInitialized)
1389     {
1390         mStateManager.forceSetBlendState();
1391         mStateManager.forceSetScissorState();
1392         mStateManager.setRenderTargetBounds(renderTargetWidth, renderTargetHeight);
1393         mRenderTargetDescInitialized = true;
1394     }
1395 
1396     return angle::Result::Continue;
1397 }
1398 
applyVertexBuffer(const gl::Context * context,gl::PrimitiveMode mode,GLint first,GLsizei count,GLsizei instances,TranslatedIndexData *)1399 angle::Result Renderer9::applyVertexBuffer(const gl::Context *context,
1400                                            gl::PrimitiveMode mode,
1401                                            GLint first,
1402                                            GLsizei count,
1403                                            GLsizei instances,
1404                                            TranslatedIndexData * /*indexInfo*/)
1405 {
1406     const gl::State &state = context->getState();
1407     ANGLE_TRY(mVertexDataManager->prepareVertexData(context, first, count, &mTranslatedAttribCache,
1408                                                     instances));
1409 
1410     return mVertexDeclarationCache.applyDeclaration(context, mDevice, mTranslatedAttribCache,
1411                                                     state.getProgramExecutable(), first, instances,
1412                                                     &mRepeatDraw);
1413 }
1414 
1415 // Applies the indices and element array bindings to the Direct3D 9 device
applyIndexBuffer(const gl::Context * context,const void * indices,GLsizei count,gl::PrimitiveMode mode,gl::DrawElementsType type,TranslatedIndexData * indexInfo)1416 angle::Result Renderer9::applyIndexBuffer(const gl::Context *context,
1417                                           const void *indices,
1418                                           GLsizei count,
1419                                           gl::PrimitiveMode mode,
1420                                           gl::DrawElementsType type,
1421                                           TranslatedIndexData *indexInfo)
1422 {
1423     gl::VertexArray *vao           = context->getState().getVertexArray();
1424     gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1425 
1426     gl::DrawElementsType dstType = gl::DrawElementsType::InvalidEnum;
1427     ANGLE_TRY(GetIndexTranslationDestType(context, count, type, indices, false, &dstType));
1428 
1429     ANGLE_TRY(mIndexDataManager->prepareIndexData(context, type, dstType, count, elementArrayBuffer,
1430                                                   indices, indexInfo));
1431 
1432     // Directly binding the storage buffer is not supported for d3d9
1433     ASSERT(indexInfo->storage == nullptr);
1434 
1435     if (indexInfo->serial != mAppliedIBSerial)
1436     {
1437         IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(indexInfo->indexBuffer);
1438 
1439         mDevice->SetIndices(indexBuffer->getBuffer());
1440         mAppliedIBSerial = indexInfo->serial;
1441     }
1442 
1443     return angle::Result::Continue;
1444 }
1445 
drawArraysImpl(const gl::Context * context,gl::PrimitiveMode mode,GLint startVertex,GLsizei count,GLsizei instances)1446 angle::Result Renderer9::drawArraysImpl(const gl::Context *context,
1447                                         gl::PrimitiveMode mode,
1448                                         GLint startVertex,
1449                                         GLsizei count,
1450                                         GLsizei instances)
1451 {
1452     ASSERT(!context->getState().isTransformFeedbackActiveUnpaused());
1453 
1454     startScene();
1455 
1456     if (mode == gl::PrimitiveMode::LineLoop)
1457     {
1458         return drawLineLoop(context, count, gl::DrawElementsType::InvalidEnum, nullptr, 0, nullptr);
1459     }
1460 
1461     if (instances > 0)
1462     {
1463         StaticIndexBufferInterface *countingIB = nullptr;
1464         ANGLE_TRY(getCountingIB(context, count, &countingIB));
1465 
1466         if (mAppliedIBSerial != countingIB->getSerial())
1467         {
1468             IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(countingIB->getIndexBuffer());
1469 
1470             mDevice->SetIndices(indexBuffer->getBuffer());
1471             mAppliedIBSerial = countingIB->getSerial();
1472         }
1473 
1474         for (int i = 0; i < mRepeatDraw; i++)
1475         {
1476             mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1477         }
1478 
1479         return angle::Result::Continue;
1480     }
1481 
1482     // Regular case
1483     mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1484     return angle::Result::Continue;
1485 }
1486 
drawElementsImpl(const gl::Context * context,gl::PrimitiveMode mode,GLsizei count,gl::DrawElementsType type,const void * indices,GLsizei instances)1487 angle::Result Renderer9::drawElementsImpl(const gl::Context *context,
1488                                           gl::PrimitiveMode mode,
1489                                           GLsizei count,
1490                                           gl::DrawElementsType type,
1491                                           const void *indices,
1492                                           GLsizei instances)
1493 {
1494     TranslatedIndexData indexInfo;
1495 
1496     ANGLE_TRY(applyIndexBuffer(context, indices, count, mode, type, &indexInfo));
1497 
1498     gl::IndexRange indexRange;
1499     ANGLE_TRY(context->getState().getVertexArray()->getIndexRange(context, type, count, indices,
1500                                                                   &indexRange));
1501 
1502     size_t vertexCount = indexRange.vertexCount();
1503     ANGLE_TRY(applyVertexBuffer(context, mode, static_cast<GLsizei>(indexRange.start),
1504                                 static_cast<GLsizei>(vertexCount), instances, &indexInfo));
1505 
1506     startScene();
1507 
1508     int minIndex = static_cast<int>(indexRange.start);
1509 
1510     gl::VertexArray *vao           = context->getState().getVertexArray();
1511     gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1512 
1513     if (mode == gl::PrimitiveMode::Points)
1514     {
1515         return drawIndexedPoints(context, count, type, indices, minIndex, elementArrayBuffer);
1516     }
1517 
1518     if (mode == gl::PrimitiveMode::LineLoop)
1519     {
1520         return drawLineLoop(context, count, type, indices, minIndex, elementArrayBuffer);
1521     }
1522 
1523     for (int i = 0; i < mRepeatDraw; i++)
1524     {
1525         mDevice->DrawIndexedPrimitive(mPrimitiveType, -minIndex, minIndex,
1526                                       static_cast<UINT>(vertexCount), indexInfo.startIndex,
1527                                       mPrimitiveCount);
1528     }
1529     return angle::Result::Continue;
1530 }
1531 
drawLineLoop(const gl::Context * context,GLsizei count,gl::DrawElementsType type,const void * indices,int minIndex,gl::Buffer * elementArrayBuffer)1532 angle::Result Renderer9::drawLineLoop(const gl::Context *context,
1533                                       GLsizei count,
1534                                       gl::DrawElementsType type,
1535                                       const void *indices,
1536                                       int minIndex,
1537                                       gl::Buffer *elementArrayBuffer)
1538 {
1539     // Get the raw indices for an indexed draw
1540     if (type != gl::DrawElementsType::InvalidEnum && elementArrayBuffer)
1541     {
1542         BufferD3D *storage        = GetImplAs<BufferD3D>(elementArrayBuffer);
1543         intptr_t offset           = reinterpret_cast<intptr_t>(indices);
1544         const uint8_t *bufferData = nullptr;
1545         ANGLE_TRY(storage->getData(context, &bufferData));
1546         indices = bufferData + offset;
1547     }
1548 
1549     unsigned int startIndex = 0;
1550     Context9 *context9      = GetImplAs<Context9>(context);
1551 
1552     if (getNativeExtensions().elementIndexUintOES)
1553     {
1554         if (!mLineLoopIB)
1555         {
1556             mLineLoopIB = new StreamingIndexBufferInterface(this);
1557             ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, INITIAL_INDEX_BUFFER_SIZE,
1558                                                       gl::DrawElementsType::UnsignedInt));
1559         }
1560 
1561         // Checked by Renderer9::applyPrimitiveType
1562         ASSERT(count >= 0);
1563 
1564         ANGLE_CHECK(context9,
1565                     static_cast<unsigned int>(count) + 1 <=
1566                         (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)),
1567                     "Failed to create a 32-bit looping index buffer for "
1568                     "GL_LINE_LOOP, too many indices required.",
1569                     GL_OUT_OF_MEMORY);
1570 
1571         const unsigned int spaceNeeded =
1572             (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
1573         ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, spaceNeeded,
1574                                                   gl::DrawElementsType::UnsignedInt));
1575 
1576         void *mappedMemory  = nullptr;
1577         unsigned int offset = 0;
1578         ANGLE_TRY(mLineLoopIB->mapBuffer(context, spaceNeeded, &mappedMemory, &offset));
1579 
1580         startIndex         = static_cast<unsigned int>(offset) / 4;
1581         unsigned int *data = static_cast<unsigned int *>(mappedMemory);
1582 
1583         switch (type)
1584         {
1585             case gl::DrawElementsType::InvalidEnum:  // Non-indexed draw
1586                 for (int i = 0; i < count; i++)
1587                 {
1588                     data[i] = i;
1589                 }
1590                 data[count] = 0;
1591                 break;
1592             case gl::DrawElementsType::UnsignedByte:
1593                 for (int i = 0; i < count; i++)
1594                 {
1595                     data[i] = static_cast<const GLubyte *>(indices)[i];
1596                 }
1597                 data[count] = static_cast<const GLubyte *>(indices)[0];
1598                 break;
1599             case gl::DrawElementsType::UnsignedShort:
1600                 for (int i = 0; i < count; i++)
1601                 {
1602                     data[i] = static_cast<const GLushort *>(indices)[i];
1603                 }
1604                 data[count] = static_cast<const GLushort *>(indices)[0];
1605                 break;
1606             case gl::DrawElementsType::UnsignedInt:
1607                 for (int i = 0; i < count; i++)
1608                 {
1609                     data[i] = static_cast<const GLuint *>(indices)[i];
1610                 }
1611                 data[count] = static_cast<const GLuint *>(indices)[0];
1612                 break;
1613             default:
1614                 UNREACHABLE();
1615         }
1616 
1617         ANGLE_TRY(mLineLoopIB->unmapBuffer(context));
1618     }
1619     else
1620     {
1621         if (!mLineLoopIB)
1622         {
1623             mLineLoopIB = new StreamingIndexBufferInterface(this);
1624             ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, INITIAL_INDEX_BUFFER_SIZE,
1625                                                       gl::DrawElementsType::UnsignedShort));
1626         }
1627 
1628         // Checked by Renderer9::applyPrimitiveType
1629         ASSERT(count >= 0);
1630 
1631         ANGLE_CHECK(context9,
1632                     static_cast<unsigned int>(count) + 1 <=
1633                         (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)),
1634                     "Failed to create a 16-bit looping index buffer for "
1635                     "GL_LINE_LOOP, too many indices required.",
1636                     GL_OUT_OF_MEMORY);
1637 
1638         const unsigned int spaceNeeded =
1639             (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
1640         ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, spaceNeeded,
1641                                                   gl::DrawElementsType::UnsignedShort));
1642 
1643         void *mappedMemory = nullptr;
1644         unsigned int offset;
1645         ANGLE_TRY(mLineLoopIB->mapBuffer(context, spaceNeeded, &mappedMemory, &offset));
1646 
1647         startIndex           = static_cast<unsigned int>(offset) / 2;
1648         unsigned short *data = static_cast<unsigned short *>(mappedMemory);
1649 
1650         switch (type)
1651         {
1652             case gl::DrawElementsType::InvalidEnum:  // Non-indexed draw
1653                 for (int i = 0; i < count; i++)
1654                 {
1655                     data[i] = static_cast<unsigned short>(i);
1656                 }
1657                 data[count] = 0;
1658                 break;
1659             case gl::DrawElementsType::UnsignedByte:
1660                 for (int i = 0; i < count; i++)
1661                 {
1662                     data[i] = static_cast<const GLubyte *>(indices)[i];
1663                 }
1664                 data[count] = static_cast<const GLubyte *>(indices)[0];
1665                 break;
1666             case gl::DrawElementsType::UnsignedShort:
1667                 for (int i = 0; i < count; i++)
1668                 {
1669                     data[i] = static_cast<const GLushort *>(indices)[i];
1670                 }
1671                 data[count] = static_cast<const GLushort *>(indices)[0];
1672                 break;
1673             case gl::DrawElementsType::UnsignedInt:
1674                 for (int i = 0; i < count; i++)
1675                 {
1676                     data[i] = static_cast<unsigned short>(static_cast<const GLuint *>(indices)[i]);
1677                 }
1678                 data[count] = static_cast<unsigned short>(static_cast<const GLuint *>(indices)[0]);
1679                 break;
1680             default:
1681                 UNREACHABLE();
1682         }
1683 
1684         ANGLE_TRY(mLineLoopIB->unmapBuffer(context));
1685     }
1686 
1687     if (mAppliedIBSerial != mLineLoopIB->getSerial())
1688     {
1689         IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(mLineLoopIB->getIndexBuffer());
1690 
1691         mDevice->SetIndices(indexBuffer->getBuffer());
1692         mAppliedIBSerial = mLineLoopIB->getSerial();
1693     }
1694 
1695     mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
1696 
1697     return angle::Result::Continue;
1698 }
1699 
drawIndexedPoints(const gl::Context * context,GLsizei count,gl::DrawElementsType type,const void * indices,int minIndex,gl::Buffer * elementArrayBuffer)1700 angle::Result Renderer9::drawIndexedPoints(const gl::Context *context,
1701                                            GLsizei count,
1702                                            gl::DrawElementsType type,
1703                                            const void *indices,
1704                                            int minIndex,
1705                                            gl::Buffer *elementArrayBuffer)
1706 {
1707     // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1708     // for each individual point. This call is not expected to happen often.
1709 
1710     if (elementArrayBuffer)
1711     {
1712         BufferD3D *storage = GetImplAs<BufferD3D>(elementArrayBuffer);
1713         intptr_t offset    = reinterpret_cast<intptr_t>(indices);
1714 
1715         const uint8_t *bufferData = nullptr;
1716         ANGLE_TRY(storage->getData(context, &bufferData));
1717         indices = bufferData + offset;
1718     }
1719 
1720     switch (type)
1721     {
1722         case gl::DrawElementsType::UnsignedByte:
1723             DrawPoints<GLubyte>(mDevice, count, indices, minIndex);
1724             return angle::Result::Continue;
1725         case gl::DrawElementsType::UnsignedShort:
1726             DrawPoints<GLushort>(mDevice, count, indices, minIndex);
1727             return angle::Result::Continue;
1728         case gl::DrawElementsType::UnsignedInt:
1729             DrawPoints<GLuint>(mDevice, count, indices, minIndex);
1730             return angle::Result::Continue;
1731         default:
1732             ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
1733     }
1734 }
1735 
getCountingIB(const gl::Context * context,size_t count,StaticIndexBufferInterface ** outIB)1736 angle::Result Renderer9::getCountingIB(const gl::Context *context,
1737                                        size_t count,
1738                                        StaticIndexBufferInterface **outIB)
1739 {
1740     // Update the counting index buffer if it is not large enough or has not been created yet.
1741     if (count <= 65536)  // 16-bit indices
1742     {
1743         const unsigned int spaceNeeded = static_cast<unsigned int>(count) * sizeof(unsigned short);
1744 
1745         if (!mCountingIB || mCountingIB->getBufferSize() < spaceNeeded)
1746         {
1747             SafeDelete(mCountingIB);
1748             mCountingIB = new StaticIndexBufferInterface(this);
1749             ANGLE_TRY(mCountingIB->reserveBufferSpace(context, spaceNeeded,
1750                                                       gl::DrawElementsType::UnsignedShort));
1751 
1752             void *mappedMemory = nullptr;
1753             ANGLE_TRY(mCountingIB->mapBuffer(context, spaceNeeded, &mappedMemory, nullptr));
1754 
1755             unsigned short *data = static_cast<unsigned short *>(mappedMemory);
1756             for (size_t i = 0; i < count; i++)
1757             {
1758                 data[i] = static_cast<unsigned short>(i);
1759             }
1760 
1761             ANGLE_TRY(mCountingIB->unmapBuffer(context));
1762         }
1763     }
1764     else if (getNativeExtensions().elementIndexUintOES)
1765     {
1766         const unsigned int spaceNeeded = static_cast<unsigned int>(count) * sizeof(unsigned int);
1767 
1768         if (!mCountingIB || mCountingIB->getBufferSize() < spaceNeeded)
1769         {
1770             SafeDelete(mCountingIB);
1771             mCountingIB = new StaticIndexBufferInterface(this);
1772             ANGLE_TRY(mCountingIB->reserveBufferSpace(context, spaceNeeded,
1773                                                       gl::DrawElementsType::UnsignedInt));
1774 
1775             void *mappedMemory = nullptr;
1776             ANGLE_TRY(mCountingIB->mapBuffer(context, spaceNeeded, &mappedMemory, nullptr));
1777 
1778             unsigned int *data = static_cast<unsigned int *>(mappedMemory);
1779             for (unsigned int i = 0; i < count; i++)
1780             {
1781                 data[i] = i;
1782             }
1783 
1784             ANGLE_TRY(mCountingIB->unmapBuffer(context));
1785         }
1786     }
1787     else
1788     {
1789         ANGLE_TRY_HR(GetImplAs<Context9>(context), E_OUTOFMEMORY,
1790                      "Could not create a counting index buffer for glDrawArraysInstanced.");
1791     }
1792 
1793     *outIB = mCountingIB;
1794     return angle::Result::Continue;
1795 }
1796 
applyShaders(const gl::Context * context,gl::PrimitiveMode drawMode)1797 angle::Result Renderer9::applyShaders(const gl::Context *context, gl::PrimitiveMode drawMode)
1798 {
1799     const gl::State &state = context->getState();
1800     Context9 *context9     = GetImplAs<Context9>(context);
1801     RendererD3D *renderer  = context9->getRenderer();
1802 
1803     // This method is called single-threaded.
1804     ANGLE_TRY(ensureHLSLCompilerInitialized(context9));
1805 
1806     ProgramExecutableD3D *executableD3D =
1807         GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
1808     VertexArray9 *vao = GetImplAs<VertexArray9>(state.getVertexArray());
1809     executableD3D->updateCachedInputLayout(renderer, vao->getCurrentStateSerial(), state);
1810 
1811     ShaderExecutableD3D *vertexExe = nullptr;
1812     ANGLE_TRY(executableD3D->getVertexExecutableForCachedInputLayout(context9, renderer, &vertexExe,
1813                                                                      nullptr));
1814 
1815     const gl::Framebuffer *drawFramebuffer = state.getDrawFramebuffer();
1816     executableD3D->updateCachedOutputLayout(context, drawFramebuffer);
1817 
1818     ShaderExecutableD3D *pixelExe = nullptr;
1819     ANGLE_TRY(executableD3D->getPixelExecutableForCachedOutputLayout(context9, renderer, &pixelExe,
1820                                                                      nullptr));
1821 
1822     IDirect3DVertexShader9 *vertexShader =
1823         (vertexExe ? GetAs<ShaderExecutable9>(vertexExe)->getVertexShader() : nullptr);
1824     IDirect3DPixelShader9 *pixelShader =
1825         (pixelExe ? GetAs<ShaderExecutable9>(pixelExe)->getPixelShader() : nullptr);
1826 
1827     if (vertexShader != mAppliedVertexShader)
1828     {
1829         mDevice->SetVertexShader(vertexShader);
1830         mAppliedVertexShader = vertexShader;
1831     }
1832 
1833     if (pixelShader != mAppliedPixelShader)
1834     {
1835         mDevice->SetPixelShader(pixelShader);
1836         mAppliedPixelShader = pixelShader;
1837     }
1838 
1839     // D3D9 has a quirk where creating multiple shaders with the same content
1840     // can return the same shader pointer. Because GL programs store different data
1841     // per-program, checking the program serial guarantees we upload fresh
1842     // uniform data even if our shader pointers are the same.
1843     // https://code.google.com/p/angleproject/issues/detail?id=661
1844     unsigned int programSerial = executableD3D->getSerial();
1845     if (programSerial != mAppliedProgramSerial)
1846     {
1847         executableD3D->dirtyAllUniforms();
1848         mStateManager.forceSetDXUniformsState();
1849         mAppliedProgramSerial = programSerial;
1850     }
1851 
1852     applyUniforms(executableD3D);
1853 
1854     // Driver uniforms
1855     mStateManager.setShaderConstants();
1856 
1857     return angle::Result::Continue;
1858 }
1859 
applyUniforms(ProgramExecutableD3D * executableD3D)1860 void Renderer9::applyUniforms(ProgramExecutableD3D *executableD3D)
1861 {
1862     // Skip updates if we're not dirty. Note that D3D9 cannot have compute or geometry.
1863     if (!executableD3D->anyShaderUniformsDirty())
1864     {
1865         return;
1866     }
1867 
1868     const auto &uniformArray = executableD3D->getD3DUniforms();
1869 
1870     for (const D3DUniform *targetUniform : uniformArray)
1871     {
1872         // Built-in uniforms must be skipped.
1873         if (!targetUniform->isReferencedByShader(gl::ShaderType::Vertex) &&
1874             !targetUniform->isReferencedByShader(gl::ShaderType::Fragment))
1875             continue;
1876 
1877         const GLfloat *f = reinterpret_cast<const GLfloat *>(targetUniform->firstNonNullData());
1878         const GLint *i   = reinterpret_cast<const GLint *>(targetUniform->firstNonNullData());
1879 
1880         switch (targetUniform->typeInfo.type)
1881         {
1882             case GL_SAMPLER_2D:
1883             case GL_SAMPLER_CUBE:
1884             case GL_SAMPLER_EXTERNAL_OES:
1885             case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
1886                 break;
1887             case GL_BOOL:
1888             case GL_BOOL_VEC2:
1889             case GL_BOOL_VEC3:
1890             case GL_BOOL_VEC4:
1891                 applyUniformnbv(targetUniform, i);
1892                 break;
1893             case GL_FLOAT:
1894             case GL_FLOAT_VEC2:
1895             case GL_FLOAT_VEC3:
1896             case GL_FLOAT_VEC4:
1897             case GL_FLOAT_MAT2:
1898             case GL_FLOAT_MAT3:
1899             case GL_FLOAT_MAT4:
1900                 applyUniformnfv(targetUniform, f);
1901                 break;
1902             case GL_INT:
1903             case GL_INT_VEC2:
1904             case GL_INT_VEC3:
1905             case GL_INT_VEC4:
1906                 applyUniformniv(targetUniform, i);
1907                 break;
1908             default:
1909                 UNREACHABLE();
1910         }
1911     }
1912 
1913     executableD3D->markUniformsClean();
1914 }
1915 
applyUniformnfv(const D3DUniform * targetUniform,const GLfloat * v)1916 void Renderer9::applyUniformnfv(const D3DUniform *targetUniform, const GLfloat *v)
1917 {
1918     if (targetUniform->isReferencedByShader(gl::ShaderType::Fragment))
1919     {
1920         mDevice->SetPixelShaderConstantF(
1921             targetUniform->mShaderRegisterIndexes[gl::ShaderType::Fragment], v,
1922             targetUniform->registerCount);
1923     }
1924 
1925     if (targetUniform->isReferencedByShader(gl::ShaderType::Vertex))
1926     {
1927         mDevice->SetVertexShaderConstantF(
1928             targetUniform->mShaderRegisterIndexes[gl::ShaderType::Vertex], v,
1929             targetUniform->registerCount);
1930     }
1931 }
1932 
applyUniformniv(const D3DUniform * targetUniform,const GLint * v)1933 void Renderer9::applyUniformniv(const D3DUniform *targetUniform, const GLint *v)
1934 {
1935     ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1936     GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
1937 
1938     for (unsigned int i = 0; i < targetUniform->registerCount; i++)
1939     {
1940         vector[i][0] = (GLfloat)v[4 * i + 0];
1941         vector[i][1] = (GLfloat)v[4 * i + 1];
1942         vector[i][2] = (GLfloat)v[4 * i + 2];
1943         vector[i][3] = (GLfloat)v[4 * i + 3];
1944     }
1945 
1946     applyUniformnfv(targetUniform, (GLfloat *)vector);
1947 }
1948 
applyUniformnbv(const D3DUniform * targetUniform,const GLint * v)1949 void Renderer9::applyUniformnbv(const D3DUniform *targetUniform, const GLint *v)
1950 {
1951     ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1952     GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
1953 
1954     for (unsigned int i = 0; i < targetUniform->registerCount; i++)
1955     {
1956         vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1957         vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1958         vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1959         vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
1960     }
1961 
1962     applyUniformnfv(targetUniform, (GLfloat *)vector);
1963 }
1964 
clear(const ClearParameters & clearParams,const RenderTarget9 * colorRenderTarget,const RenderTarget9 * depthStencilRenderTarget)1965 void Renderer9::clear(const ClearParameters &clearParams,
1966                       const RenderTarget9 *colorRenderTarget,
1967                       const RenderTarget9 *depthStencilRenderTarget)
1968 {
1969     // Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
1970     ASSERT(clearParams.colorType == GL_FLOAT);
1971 
1972     // Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
1973     bool clearColor = clearParams.clearColor[0];
1974     for (unsigned int i = 0; i < clearParams.clearColor.size(); i++)
1975     {
1976         ASSERT(clearParams.clearColor[i] == clearColor);
1977     }
1978 
1979     float depth   = gl::clamp01(clearParams.depthValue);
1980     DWORD stencil = clearParams.stencilValue & 0x000000FF;
1981 
1982     unsigned int stencilUnmasked = 0x0;
1983     if (clearParams.clearStencil && depthStencilRenderTarget)
1984     {
1985         const gl::InternalFormat &depthStencilFormat =
1986             gl::GetSizedInternalFormatInfo(depthStencilRenderTarget->getInternalFormat());
1987         if (depthStencilFormat.stencilBits > 0)
1988         {
1989             const d3d9::D3DFormat &d3dFormatInfo =
1990                 d3d9::GetD3DFormatInfo(depthStencilRenderTarget->getD3DFormat());
1991             stencilUnmasked = (0x1 << d3dFormatInfo.stencilBits) - 1;
1992         }
1993     }
1994 
1995     const bool needMaskedStencilClear =
1996         clearParams.clearStencil &&
1997         (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1998 
1999     bool needMaskedColorClear = false;
2000     D3DCOLOR color            = D3DCOLOR_ARGB(255, 0, 0, 0);
2001     if (clearColor)
2002     {
2003         ASSERT(colorRenderTarget != nullptr);
2004 
2005         const gl::InternalFormat &formatInfo =
2006             gl::GetSizedInternalFormatInfo(colorRenderTarget->getInternalFormat());
2007         const d3d9::D3DFormat &d3dFormatInfo =
2008             d3d9::GetD3DFormatInfo(colorRenderTarget->getD3DFormat());
2009 
2010         color =
2011             D3DCOLOR_ARGB(gl::unorm<8>((formatInfo.alphaBits == 0 && d3dFormatInfo.alphaBits > 0)
2012                                            ? 1.0f
2013                                            : clearParams.colorF.alpha),
2014                           gl::unorm<8>((formatInfo.redBits == 0 && d3dFormatInfo.redBits > 0)
2015                                            ? 0.0f
2016                                            : clearParams.colorF.red),
2017                           gl::unorm<8>((formatInfo.greenBits == 0 && d3dFormatInfo.greenBits > 0)
2018                                            ? 0.0f
2019                                            : clearParams.colorF.green),
2020                           gl::unorm<8>((formatInfo.blueBits == 0 && d3dFormatInfo.blueBits > 0)
2021                                            ? 0.0f
2022                                            : clearParams.colorF.blue));
2023 
2024         const uint8_t colorMask =
2025             gl::BlendStateExt::ColorMaskStorage::GetValueIndexed(0, clearParams.colorMask);
2026         bool r, g, b, a;
2027         gl::BlendStateExt::UnpackColorMask(colorMask, &r, &g, &b, &a);
2028         if ((formatInfo.redBits > 0 && !r) || (formatInfo.greenBits > 0 && !g) ||
2029             (formatInfo.blueBits > 0 && !b) || (formatInfo.alphaBits > 0 && !a))
2030         {
2031             needMaskedColorClear = true;
2032         }
2033     }
2034 
2035     if (needMaskedColorClear || needMaskedStencilClear)
2036     {
2037         // State which is altered in all paths from this point to the clear call is saved.
2038         // State which is altered in only some paths will be flagged dirty in the case that
2039         //  that path is taken.
2040         HRESULT hr;
2041         if (mMaskedClearSavedState == nullptr)
2042         {
2043             hr = mDevice->BeginStateBlock();
2044             ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2045 
2046             mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2047             mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2048             mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2049             mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2050             mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2051             mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2052             mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2053             mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2054             mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2055             mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2056             mDevice->SetPixelShader(nullptr);
2057             mDevice->SetVertexShader(nullptr);
2058             mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2059             mDevice->SetStreamSource(0, nullptr, 0, 0);
2060             mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2061             mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2062             mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2063             mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2064             mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2065             mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2066             mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2067 
2068             for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2069             {
2070                 mDevice->SetStreamSourceFreq(i, 1);
2071             }
2072 
2073             hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2074             ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2075         }
2076 
2077         ASSERT(mMaskedClearSavedState != nullptr);
2078 
2079         if (mMaskedClearSavedState != nullptr)
2080         {
2081             hr = mMaskedClearSavedState->Capture();
2082             ASSERT(SUCCEEDED(hr));
2083         }
2084 
2085         mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2086         mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2087         mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2088         mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2089         mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2090         mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2091         mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2092         mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2093 
2094         if (clearColor)
2095         {
2096             // clearParams.colorMask follows the same packing scheme as
2097             // D3DCOLORWRITEENABLE_RED/GREEN/BLUE/ALPHA
2098             mDevice->SetRenderState(
2099                 D3DRS_COLORWRITEENABLE,
2100                 gl::BlendStateExt::ColorMaskStorage::GetValueIndexed(0, clearParams.colorMask));
2101         }
2102         else
2103         {
2104             mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2105         }
2106 
2107         if (stencilUnmasked != 0x0 && clearParams.clearStencil)
2108         {
2109             mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2110             mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2111             mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2112             mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2113             mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
2114             mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2115             mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2116             mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
2117         }
2118         else
2119         {
2120             mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2121         }
2122 
2123         mDevice->SetPixelShader(nullptr);
2124         mDevice->SetVertexShader(nullptr);
2125         mDevice->SetFVF(D3DFVF_XYZRHW);
2126         mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2127         mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2128         mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2129         mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2130         mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2131         mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2132         mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2133 
2134         for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2135         {
2136             mDevice->SetStreamSourceFreq(i, 1);
2137         }
2138 
2139         int renderTargetWidth  = mStateManager.getRenderTargetWidth();
2140         int renderTargetHeight = mStateManager.getRenderTargetHeight();
2141 
2142         float quad[4][4];  // A quadrilateral covering the target, aligned to match the edges
2143         quad[0][0] = -0.5f;
2144         quad[0][1] = renderTargetHeight - 0.5f;
2145         quad[0][2] = 0.0f;
2146         quad[0][3] = 1.0f;
2147 
2148         quad[1][0] = renderTargetWidth - 0.5f;
2149         quad[1][1] = renderTargetHeight - 0.5f;
2150         quad[1][2] = 0.0f;
2151         quad[1][3] = 1.0f;
2152 
2153         quad[2][0] = -0.5f;
2154         quad[2][1] = -0.5f;
2155         quad[2][2] = 0.0f;
2156         quad[2][3] = 1.0f;
2157 
2158         quad[3][0] = renderTargetWidth - 0.5f;
2159         quad[3][1] = -0.5f;
2160         quad[3][2] = 0.0f;
2161         quad[3][3] = 1.0f;
2162 
2163         startScene();
2164         mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2165 
2166         if (clearParams.clearDepth)
2167         {
2168             mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2169             mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2170             mDevice->Clear(0, nullptr, D3DCLEAR_ZBUFFER, color, depth, stencil);
2171         }
2172 
2173         if (mMaskedClearSavedState != nullptr)
2174         {
2175             mMaskedClearSavedState->Apply();
2176         }
2177     }
2178     else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
2179     {
2180         DWORD dxClearFlags = 0;
2181         if (clearColor)
2182         {
2183             dxClearFlags |= D3DCLEAR_TARGET;
2184         }
2185         if (clearParams.clearDepth)
2186         {
2187             dxClearFlags |= D3DCLEAR_ZBUFFER;
2188         }
2189         if (clearParams.clearStencil)
2190         {
2191             dxClearFlags |= D3DCLEAR_STENCIL;
2192         }
2193 
2194         mDevice->Clear(0, nullptr, dxClearFlags, color, depth, stencil);
2195     }
2196 }
2197 
markAllStateDirty()2198 void Renderer9::markAllStateDirty()
2199 {
2200     mAppliedRenderTargetSerial   = 0;
2201     mAppliedDepthStencilSerial   = 0;
2202     mDepthStencilInitialized     = false;
2203     mRenderTargetDescInitialized = false;
2204 
2205     mStateManager.forceSetRasterState();
2206     mStateManager.forceSetDepthStencilState();
2207     mStateManager.forceSetBlendState();
2208     mStateManager.forceSetScissorState();
2209     mStateManager.forceSetViewportState();
2210 
2211     ASSERT(mCurVertexSamplerStates.size() == mCurVertexTextures.size());
2212     for (unsigned int i = 0; i < mCurVertexTextures.size(); i++)
2213     {
2214         mCurVertexSamplerStates[i].forceSet = true;
2215         mCurVertexTextures[i]               = angle::DirtyPointer;
2216     }
2217 
2218     ASSERT(mCurPixelSamplerStates.size() == mCurPixelTextures.size());
2219     for (unsigned int i = 0; i < mCurPixelSamplerStates.size(); i++)
2220     {
2221         mCurPixelSamplerStates[i].forceSet = true;
2222         mCurPixelTextures[i]               = angle::DirtyPointer;
2223     }
2224 
2225     mAppliedIBSerial      = 0;
2226     mAppliedVertexShader  = nullptr;
2227     mAppliedPixelShader   = nullptr;
2228     mAppliedProgramSerial = 0;
2229     mStateManager.forceSetDXUniformsState();
2230 
2231     mVertexDeclarationCache.markStateDirty();
2232 }
2233 
releaseDeviceResources()2234 void Renderer9::releaseDeviceResources()
2235 {
2236     for (size_t i = 0; i < mEventQueryPool.size(); i++)
2237     {
2238         SafeRelease(mEventQueryPool[i]);
2239     }
2240     mEventQueryPool.clear();
2241 
2242     SafeRelease(mMaskedClearSavedState);
2243 
2244     mVertexShaderCache.clear();
2245     mPixelShaderCache.clear();
2246 
2247     SafeDelete(mBlit);
2248     SafeDelete(mVertexDataManager);
2249     SafeDelete(mIndexDataManager);
2250     SafeDelete(mLineLoopIB);
2251     SafeDelete(mCountingIB);
2252 
2253     for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2254     {
2255         SafeDelete(mNullRenderTargetCache[i].renderTarget);
2256     }
2257 }
2258 
2259 // set notify to true to broadcast a message to all contexts of the device loss
testDeviceLost()2260 bool Renderer9::testDeviceLost()
2261 {
2262     HRESULT status = getDeviceStatusCode();
2263     return FAILED(status);
2264 }
2265 
getDeviceStatusCode()2266 HRESULT Renderer9::getDeviceStatusCode()
2267 {
2268     HRESULT status = D3D_OK;
2269 
2270     if (mDeviceEx)
2271     {
2272         status = mDeviceEx->CheckDeviceState(nullptr);
2273     }
2274     else if (mDevice)
2275     {
2276         status = mDevice->TestCooperativeLevel();
2277     }
2278 
2279     return status;
2280 }
2281 
testDeviceResettable()2282 bool Renderer9::testDeviceResettable()
2283 {
2284     // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
2285     // DEVICEREMOVED indicates the device has been stopped and must be recreated
2286     switch (getDeviceStatusCode())
2287     {
2288         case D3DERR_DEVICENOTRESET:
2289         case D3DERR_DEVICEHUNG:
2290             return true;
2291         case D3DERR_DEVICELOST:
2292             return (mDeviceEx != nullptr);
2293         case D3DERR_DEVICEREMOVED:
2294             ASSERT(mDeviceEx != nullptr);
2295             return isRemovedDeviceResettable();
2296         default:
2297             return false;
2298     }
2299 }
2300 
resetDevice()2301 bool Renderer9::resetDevice()
2302 {
2303     releaseDeviceResources();
2304 
2305     D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2306 
2307     HRESULT result     = D3D_OK;
2308     bool lost          = testDeviceLost();
2309     bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED);
2310 
2311     // Device Removed is a feature which is only present with D3D9Ex
2312     ASSERT(mDeviceEx != nullptr || !removedDevice);
2313 
2314     for (int attempts = 3; lost && attempts > 0; attempts--)
2315     {
2316         if (removedDevice)
2317         {
2318             // Device removed, which may trigger on driver reinstallation,
2319             // may cause a longer wait other reset attempts before the
2320             // system is ready to handle creating a new device.
2321             Sleep(800);
2322             lost = !resetRemovedDevice();
2323         }
2324         else if (mDeviceEx)
2325         {
2326             Sleep(500);  // Give the graphics driver some CPU time
2327             result = mDeviceEx->ResetEx(&presentParameters, nullptr);
2328             lost   = testDeviceLost();
2329         }
2330         else
2331         {
2332             result = mDevice->TestCooperativeLevel();
2333             while (result == D3DERR_DEVICELOST)
2334             {
2335                 Sleep(100);  // Give the graphics driver some CPU time
2336                 result = mDevice->TestCooperativeLevel();
2337             }
2338 
2339             if (result == D3DERR_DEVICENOTRESET)
2340             {
2341                 result = mDevice->Reset(&presentParameters);
2342             }
2343             lost = testDeviceLost();
2344         }
2345     }
2346 
2347     if (FAILED(result))
2348     {
2349         ERR() << "Reset/ResetEx failed multiple times, " << gl::FmtHR(result);
2350         return false;
2351     }
2352 
2353     if (removedDevice && lost)
2354     {
2355         ERR() << "Device lost reset failed multiple times";
2356         return false;
2357     }
2358 
2359     // If the device was removed, we already finished re-initialization in resetRemovedDevice
2360     if (!removedDevice)
2361     {
2362         // reset device defaults
2363         if (initializeDevice().isError())
2364         {
2365             return false;
2366         }
2367     }
2368 
2369     return true;
2370 }
2371 
isRemovedDeviceResettable() const2372 bool Renderer9::isRemovedDeviceResettable() const
2373 {
2374     bool success = false;
2375 
2376 #if ANGLE_D3D9EX
2377     IDirect3D9Ex *d3d9Ex = nullptr;
2378     typedef HRESULT(WINAPI * Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex **);
2379     Direct3DCreate9ExFunc Direct3DCreate9ExPtr =
2380         reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
2381 
2382     if (Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &d3d9Ex)))
2383     {
2384         D3DCAPS9 deviceCaps;
2385         HRESULT result = d3d9Ex->GetDeviceCaps(mAdapter, mDeviceType, &deviceCaps);
2386         success        = SUCCEEDED(result);
2387     }
2388 
2389     SafeRelease(d3d9Ex);
2390 #else
2391     UNREACHABLE();
2392 #endif
2393 
2394     return success;
2395 }
2396 
resetRemovedDevice()2397 bool Renderer9::resetRemovedDevice()
2398 {
2399     // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx:
2400     // The hardware adapter has been removed. Application must destroy the device, do enumeration of
2401     // adapters and create another Direct3D device. If application continues rendering without
2402     // calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only.
2403     release();
2404     return !initialize().isError();
2405 }
2406 
getVendorId() const2407 VendorID Renderer9::getVendorId() const
2408 {
2409     return static_cast<VendorID>(mAdapterIdentifier.VendorId);
2410 }
2411 
getRendererDescription() const2412 std::string Renderer9::getRendererDescription() const
2413 {
2414     std::ostringstream rendererString;
2415 
2416     rendererString << mAdapterIdentifier.Description;
2417     if (getShareHandleSupport())
2418     {
2419         rendererString << " Direct3D9Ex";
2420     }
2421     else
2422     {
2423         rendererString << " Direct3D9";
2424     }
2425 
2426     rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_"
2427                    << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2428     rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_"
2429                    << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2430 
2431     return rendererString.str();
2432 }
2433 
getAdapterIdentifier() const2434 DeviceIdentifier Renderer9::getAdapterIdentifier() const
2435 {
2436     DeviceIdentifier deviceIdentifier = {};
2437     deviceIdentifier.VendorId         = static_cast<UINT>(mAdapterIdentifier.VendorId);
2438     deviceIdentifier.DeviceId         = static_cast<UINT>(mAdapterIdentifier.DeviceId);
2439     deviceIdentifier.SubSysId         = static_cast<UINT>(mAdapterIdentifier.SubSysId);
2440     deviceIdentifier.Revision         = static_cast<UINT>(mAdapterIdentifier.Revision);
2441     deviceIdentifier.FeatureLevel     = 0;
2442 
2443     return deviceIdentifier;
2444 }
2445 
getReservedVertexUniformVectors() const2446 unsigned int Renderer9::getReservedVertexUniformVectors() const
2447 {
2448     return d3d9_gl::GetReservedVertexUniformVectors();
2449 }
2450 
getReservedFragmentUniformVectors() const2451 unsigned int Renderer9::getReservedFragmentUniformVectors() const
2452 {
2453     return d3d9_gl::GetReservedFragmentUniformVectors();
2454 }
2455 
getShareHandleSupport() const2456 bool Renderer9::getShareHandleSupport() const
2457 {
2458     // PIX doesn't seem to support using share handles, so disable them.
2459     return (mD3d9Ex != nullptr) && !gl::DebugAnnotationsActive(/*context=*/nullptr);
2460 }
2461 
getMajorShaderModel() const2462 int Renderer9::getMajorShaderModel() const
2463 {
2464     return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
2465 }
2466 
getMinorShaderModel() const2467 int Renderer9::getMinorShaderModel() const
2468 {
2469     return D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2470 }
2471 
getShaderModelSuffix() const2472 std::string Renderer9::getShaderModelSuffix() const
2473 {
2474     return "";
2475 }
2476 
getCapsDeclTypes() const2477 DWORD Renderer9::getCapsDeclTypes() const
2478 {
2479     return mDeviceCaps.DeclTypes;
2480 }
2481 
getBufferPool(DWORD usage) const2482 D3DPOOL Renderer9::getBufferPool(DWORD usage) const
2483 {
2484     if (mD3d9Ex != nullptr)
2485     {
2486         return D3DPOOL_DEFAULT;
2487     }
2488     else
2489     {
2490         if (!(usage & D3DUSAGE_DYNAMIC))
2491         {
2492             return D3DPOOL_MANAGED;
2493         }
2494     }
2495 
2496     return D3DPOOL_DEFAULT;
2497 }
2498 
copyImage2D(const gl::Context * context,const gl::Framebuffer * framebuffer,const gl::Rectangle & sourceRect,GLenum destFormat,const gl::Offset & destOffset,TextureStorage * storage,GLint level)2499 angle::Result Renderer9::copyImage2D(const gl::Context *context,
2500                                      const gl::Framebuffer *framebuffer,
2501                                      const gl::Rectangle &sourceRect,
2502                                      GLenum destFormat,
2503                                      const gl::Offset &destOffset,
2504                                      TextureStorage *storage,
2505                                      GLint level)
2506 {
2507     RECT rect;
2508     rect.left   = sourceRect.x;
2509     rect.top    = sourceRect.y;
2510     rect.right  = sourceRect.x + sourceRect.width;
2511     rect.bottom = sourceRect.y + sourceRect.height;
2512 
2513     return mBlit->copy2D(context, framebuffer, rect, destFormat, destOffset, storage, level);
2514 }
2515 
copyImageCube(const gl::Context * context,const gl::Framebuffer * framebuffer,const gl::Rectangle & sourceRect,GLenum destFormat,const gl::Offset & destOffset,TextureStorage * storage,gl::TextureTarget target,GLint level)2516 angle::Result Renderer9::copyImageCube(const gl::Context *context,
2517                                        const gl::Framebuffer *framebuffer,
2518                                        const gl::Rectangle &sourceRect,
2519                                        GLenum destFormat,
2520                                        const gl::Offset &destOffset,
2521                                        TextureStorage *storage,
2522                                        gl::TextureTarget target,
2523                                        GLint level)
2524 {
2525     RECT rect;
2526     rect.left   = sourceRect.x;
2527     rect.top    = sourceRect.y;
2528     rect.right  = sourceRect.x + sourceRect.width;
2529     rect.bottom = sourceRect.y + sourceRect.height;
2530 
2531     return mBlit->copyCube(context, framebuffer, rect, destFormat, destOffset, storage, target,
2532                            level);
2533 }
2534 
copyImage3D(const gl::Context * context,const gl::Framebuffer * framebuffer,const gl::Rectangle & sourceRect,GLenum destFormat,const gl::Offset & destOffset,TextureStorage * storage,GLint level)2535 angle::Result Renderer9::copyImage3D(const gl::Context *context,
2536                                      const gl::Framebuffer *framebuffer,
2537                                      const gl::Rectangle &sourceRect,
2538                                      GLenum destFormat,
2539                                      const gl::Offset &destOffset,
2540                                      TextureStorage *storage,
2541                                      GLint level)
2542 {
2543     // 3D textures are not available in the D3D9 backend.
2544     ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
2545     return angle::Result::Stop;
2546 }
2547 
copyImage2DArray(const gl::Context * context,const gl::Framebuffer * framebuffer,const gl::Rectangle & sourceRect,GLenum destFormat,const gl::Offset & destOffset,TextureStorage * storage,GLint level)2548 angle::Result Renderer9::copyImage2DArray(const gl::Context *context,
2549                                           const gl::Framebuffer *framebuffer,
2550                                           const gl::Rectangle &sourceRect,
2551                                           GLenum destFormat,
2552                                           const gl::Offset &destOffset,
2553                                           TextureStorage *storage,
2554                                           GLint level)
2555 {
2556     // 2D array textures are not available in the D3D9 backend.
2557     ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
2558     return angle::Result::Stop;
2559 }
2560 
copyTexture(const gl::Context * context,const gl::Texture * source,GLint sourceLevel,gl::TextureTarget srcTarget,const gl::Box & sourceBox,GLenum destFormat,GLenum destType,const gl::Offset & destOffset,TextureStorage * storage,gl::TextureTarget destTarget,GLint destLevel,bool unpackFlipY,bool unpackPremultiplyAlpha,bool unpackUnmultiplyAlpha)2561 angle::Result Renderer9::copyTexture(const gl::Context *context,
2562                                      const gl::Texture *source,
2563                                      GLint sourceLevel,
2564                                      gl::TextureTarget srcTarget,
2565                                      const gl::Box &sourceBox,
2566                                      GLenum destFormat,
2567                                      GLenum destType,
2568                                      const gl::Offset &destOffset,
2569                                      TextureStorage *storage,
2570                                      gl::TextureTarget destTarget,
2571                                      GLint destLevel,
2572                                      bool unpackFlipY,
2573                                      bool unpackPremultiplyAlpha,
2574                                      bool unpackUnmultiplyAlpha)
2575 {
2576     RECT rect;
2577     rect.left   = sourceBox.x;
2578     rect.top    = sourceBox.y;
2579     rect.right  = sourceBox.x + sourceBox.width;
2580     rect.bottom = sourceBox.y + sourceBox.height;
2581 
2582     return mBlit->copyTexture(context, source, sourceLevel, rect, destFormat, destOffset, storage,
2583                               destTarget, destLevel, unpackFlipY, unpackPremultiplyAlpha,
2584                               unpackUnmultiplyAlpha);
2585 }
2586 
copyCompressedTexture(const gl::Context * context,const gl::Texture * source,GLint sourceLevel,TextureStorage * storage,GLint destLevel)2587 angle::Result Renderer9::copyCompressedTexture(const gl::Context *context,
2588                                                const gl::Texture *source,
2589                                                GLint sourceLevel,
2590                                                TextureStorage *storage,
2591                                                GLint destLevel)
2592 {
2593     ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
2594     return angle::Result::Stop;
2595 }
2596 
createRenderTarget(const gl::Context * context,int width,int height,GLenum format,GLsizei samples,RenderTargetD3D ** outRT)2597 angle::Result Renderer9::createRenderTarget(const gl::Context *context,
2598                                             int width,
2599                                             int height,
2600                                             GLenum format,
2601                                             GLsizei samples,
2602                                             RenderTargetD3D **outRT)
2603 {
2604     const d3d9::TextureFormat &d3d9FormatInfo = d3d9::GetTextureFormatInfo(format);
2605 
2606     const gl::TextureCaps &textureCaps = getNativeTextureCaps().get(format);
2607     GLuint supportedSamples            = textureCaps.getNearestSamples(samples);
2608 
2609     IDirect3DTexture9 *texture      = nullptr;
2610     IDirect3DSurface9 *renderTarget = nullptr;
2611     if (width > 0 && height > 0)
2612     {
2613         bool requiresInitialization = false;
2614         HRESULT result              = D3DERR_INVALIDCALL;
2615 
2616         const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(format);
2617         if (formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
2618         {
2619             result = mDevice->CreateDepthStencilSurface(
2620                 width, height, d3d9FormatInfo.renderFormat,
2621                 gl_d3d9::GetMultisampleType(supportedSamples), 0, FALSE, &renderTarget, nullptr);
2622         }
2623         else
2624         {
2625             requiresInitialization = (d3d9FormatInfo.dataInitializerFunction != nullptr);
2626             if (supportedSamples > 0)
2627             {
2628                 result = mDevice->CreateRenderTarget(width, height, d3d9FormatInfo.renderFormat,
2629                                                      gl_d3d9::GetMultisampleType(supportedSamples),
2630                                                      0, FALSE, &renderTarget, nullptr);
2631             }
2632             else
2633             {
2634                 result = mDevice->CreateTexture(
2635                     width, height, 1, D3DUSAGE_RENDERTARGET, d3d9FormatInfo.texFormat,
2636                     getTexturePool(D3DUSAGE_RENDERTARGET), &texture, nullptr);
2637                 if (!FAILED(result))
2638                 {
2639                     result = texture->GetSurfaceLevel(0, &renderTarget);
2640                 }
2641             }
2642         }
2643 
2644         ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to create render target");
2645 
2646         if (requiresInitialization)
2647         {
2648             // This format requires that the data be initialized before the render target can be
2649             // used Unfortunately this requires a Get call on the d3d device but it is far better
2650             // than having to mark the render target as lockable and copy data to the gpu.
2651             IDirect3DSurface9 *prevRenderTarget = nullptr;
2652             mDevice->GetRenderTarget(0, &prevRenderTarget);
2653             mDevice->SetRenderTarget(0, renderTarget);
2654             mDevice->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 0.0f, 0);
2655             mDevice->SetRenderTarget(0, prevRenderTarget);
2656         }
2657     }
2658 
2659     *outRT = new TextureRenderTarget9(texture, 0, renderTarget, format, width, height, 1,
2660                                       supportedSamples);
2661     return angle::Result::Continue;
2662 }
2663 
createRenderTargetCopy(const gl::Context * context,RenderTargetD3D * source,RenderTargetD3D ** outRT)2664 angle::Result Renderer9::createRenderTargetCopy(const gl::Context *context,
2665                                                 RenderTargetD3D *source,
2666                                                 RenderTargetD3D **outRT)
2667 {
2668     ASSERT(source != nullptr);
2669 
2670     RenderTargetD3D *newRT = nullptr;
2671     ANGLE_TRY(createRenderTarget(context, source->getWidth(), source->getHeight(),
2672                                  source->getInternalFormat(), source->getSamples(), &newRT));
2673 
2674     RenderTarget9 *source9 = GetAs<RenderTarget9>(source);
2675     RenderTarget9 *dest9   = GetAs<RenderTarget9>(newRT);
2676 
2677     HRESULT result = mDevice->StretchRect(source9->getSurface(), nullptr, dest9->getSurface(),
2678                                           nullptr, D3DTEXF_NONE);
2679     ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to copy render target");
2680 
2681     *outRT = newRT;
2682     return angle::Result::Continue;
2683 }
2684 
loadExecutable(d3d::Context * context,const uint8_t * function,size_t length,gl::ShaderType type,const std::vector<D3DVarying> & streamOutVaryings,bool separatedOutputBuffers,ShaderExecutableD3D ** outExecutable)2685 angle::Result Renderer9::loadExecutable(d3d::Context *context,
2686                                         const uint8_t *function,
2687                                         size_t length,
2688                                         gl::ShaderType type,
2689                                         const std::vector<D3DVarying> &streamOutVaryings,
2690                                         bool separatedOutputBuffers,
2691                                         ShaderExecutableD3D **outExecutable)
2692 {
2693     // Transform feedback is not supported in ES2 or D3D9
2694     ASSERT(streamOutVaryings.empty());
2695 
2696     switch (type)
2697     {
2698         case gl::ShaderType::Vertex:
2699         {
2700             IDirect3DVertexShader9 *vshader = nullptr;
2701             ANGLE_TRY(createVertexShader(context, (DWORD *)function, length, &vshader));
2702             *outExecutable = new ShaderExecutable9(function, length, vshader);
2703         }
2704         break;
2705         case gl::ShaderType::Fragment:
2706         {
2707             IDirect3DPixelShader9 *pshader = nullptr;
2708             ANGLE_TRY(createPixelShader(context, (DWORD *)function, length, &pshader));
2709             *outExecutable = new ShaderExecutable9(function, length, pshader);
2710         }
2711         break;
2712         default:
2713             ANGLE_HR_UNREACHABLE(context);
2714     }
2715 
2716     return angle::Result::Continue;
2717 }
2718 
compileToExecutable(d3d::Context * context,gl::InfoLog & infoLog,const std::string & shaderHLSL,gl::ShaderType type,const std::vector<D3DVarying> & streamOutVaryings,bool separatedOutputBuffers,const CompilerWorkaroundsD3D & workarounds,ShaderExecutableD3D ** outExectuable)2719 angle::Result Renderer9::compileToExecutable(d3d::Context *context,
2720                                              gl::InfoLog &infoLog,
2721                                              const std::string &shaderHLSL,
2722                                              gl::ShaderType type,
2723                                              const std::vector<D3DVarying> &streamOutVaryings,
2724                                              bool separatedOutputBuffers,
2725                                              const CompilerWorkaroundsD3D &workarounds,
2726                                              ShaderExecutableD3D **outExectuable)
2727 {
2728     // Transform feedback is not supported in ES2 or D3D9
2729     ASSERT(streamOutVaryings.empty());
2730 
2731     std::stringstream profileStream;
2732 
2733     switch (type)
2734     {
2735         case gl::ShaderType::Vertex:
2736             profileStream << "vs";
2737             break;
2738         case gl::ShaderType::Fragment:
2739             profileStream << "ps";
2740             break;
2741         default:
2742             ANGLE_HR_UNREACHABLE(context);
2743     }
2744 
2745     profileStream << "_" << ((getMajorShaderModel() >= 3) ? 3 : 2);
2746     profileStream << "_" << "0";
2747 
2748     std::string profile = profileStream.str();
2749 
2750     UINT flags = ANGLE_COMPILE_OPTIMIZATION_LEVEL;
2751 
2752     if (workarounds.skipOptimization)
2753     {
2754         flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2755     }
2756     else if (workarounds.useMaxOptimization)
2757     {
2758         flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
2759     }
2760 
2761     if (gl::DebugAnnotationsActive(/*context=*/nullptr))
2762     {
2763 #ifndef NDEBUG
2764         flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2765 #endif
2766 
2767         flags |= D3DCOMPILE_DEBUG;
2768     }
2769 
2770     // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders
2771     // when it would otherwise pass with alternative options. Try the default flags first and if
2772     // compilation fails, try some alternatives.
2773     std::vector<CompileConfig> configs;
2774     configs.push_back(CompileConfig(flags, "default"));
2775     configs.push_back(CompileConfig(flags | D3DCOMPILE_AVOID_FLOW_CONTROL, "avoid flow control"));
2776     configs.push_back(CompileConfig(flags | D3DCOMPILE_PREFER_FLOW_CONTROL, "prefer flow control"));
2777 
2778     ID3DBlob *binary = nullptr;
2779     std::string debugInfo;
2780     angle::Result error = mCompiler.compileToBinary(context, infoLog, shaderHLSL, profile, configs,
2781                                                     nullptr, &binary, &debugInfo);
2782     ANGLE_TRY(error);
2783 
2784     // It's possible that binary is NULL if the compiler failed in all configurations.  Set the
2785     // executable to NULL and return GL_NO_ERROR to signify that there was a link error but the
2786     // internal state is still OK.
2787     if (!binary)
2788     {
2789         *outExectuable = nullptr;
2790         return angle::Result::Continue;
2791     }
2792 
2793     error = loadExecutable(context, reinterpret_cast<const uint8_t *>(binary->GetBufferPointer()),
2794                            binary->GetBufferSize(), type, streamOutVaryings, separatedOutputBuffers,
2795                            outExectuable);
2796 
2797     SafeRelease(binary);
2798     ANGLE_TRY(error);
2799 
2800     if (!debugInfo.empty())
2801     {
2802         (*outExectuable)->appendDebugInfo(debugInfo);
2803     }
2804 
2805     return angle::Result::Continue;
2806 }
2807 
ensureHLSLCompilerInitialized(d3d::Context * context)2808 angle::Result Renderer9::ensureHLSLCompilerInitialized(d3d::Context *context)
2809 {
2810     return mCompiler.ensureInitialized(context);
2811 }
2812 
createUniformStorage(size_t storageSize)2813 UniformStorageD3D *Renderer9::createUniformStorage(size_t storageSize)
2814 {
2815     return new UniformStorageD3D(storageSize);
2816 }
2817 
boxFilter(Context9 * context9,IDirect3DSurface9 * source,IDirect3DSurface9 * dest)2818 angle::Result Renderer9::boxFilter(Context9 *context9,
2819                                    IDirect3DSurface9 *source,
2820                                    IDirect3DSurface9 *dest)
2821 {
2822     return mBlit->boxFilter(context9, source, dest);
2823 }
2824 
getTexturePool(DWORD usage) const2825 D3DPOOL Renderer9::getTexturePool(DWORD usage) const
2826 {
2827     if (mD3d9Ex != nullptr)
2828     {
2829         return D3DPOOL_DEFAULT;
2830     }
2831     else
2832     {
2833         if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
2834         {
2835             return D3DPOOL_MANAGED;
2836         }
2837     }
2838 
2839     return D3DPOOL_DEFAULT;
2840 }
2841 
copyToRenderTarget(const gl::Context * context,IDirect3DSurface9 * dest,IDirect3DSurface9 * source,bool fromManaged)2842 angle::Result Renderer9::copyToRenderTarget(const gl::Context *context,
2843                                             IDirect3DSurface9 *dest,
2844                                             IDirect3DSurface9 *source,
2845                                             bool fromManaged)
2846 {
2847     ASSERT(source && dest);
2848 
2849     Context9 *context9 = GetImplAs<Context9>(context);
2850 
2851     HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
2852 
2853     if (fromManaged)
2854     {
2855         D3DSURFACE_DESC desc;
2856         source->GetDesc(&desc);
2857 
2858         IDirect3DSurface9 *surf = 0;
2859         result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2860                                                       D3DPOOL_SYSTEMMEM, &surf, nullptr);
2861 
2862         if (SUCCEEDED(result))
2863         {
2864             ANGLE_TRY(Image9::CopyLockableSurfaces(context9, surf, source));
2865             result = mDevice->UpdateSurface(surf, nullptr, dest, nullptr);
2866             SafeRelease(surf);
2867         }
2868     }
2869     else
2870     {
2871         endScene();
2872         result = mDevice->StretchRect(source, nullptr, dest, nullptr, D3DTEXF_NONE);
2873     }
2874 
2875     ANGLE_TRY_HR(context9, result, "Failed to blit internal texture");
2876     return angle::Result::Continue;
2877 }
2878 
getRendererClass() const2879 RendererClass Renderer9::getRendererClass() const
2880 {
2881     return RENDERER_D3D9;
2882 }
2883 
createImage()2884 ImageD3D *Renderer9::createImage()
2885 {
2886     return new Image9(this);
2887 }
2888 
createExternalImageSibling(const gl::Context * context,EGLenum target,EGLClientBuffer buffer,const egl::AttributeMap & attribs)2889 ExternalImageSiblingImpl *Renderer9::createExternalImageSibling(const gl::Context *context,
2890                                                                 EGLenum target,
2891                                                                 EGLClientBuffer buffer,
2892                                                                 const egl::AttributeMap &attribs)
2893 {
2894     UNREACHABLE();
2895     return nullptr;
2896 }
2897 
generateMipmap(const gl::Context * context,ImageD3D * dest,ImageD3D * src)2898 angle::Result Renderer9::generateMipmap(const gl::Context *context, ImageD3D *dest, ImageD3D *src)
2899 {
2900     Image9 *src9 = GetAs<Image9>(src);
2901     Image9 *dst9 = GetAs<Image9>(dest);
2902     return Image9::GenerateMipmap(GetImplAs<Context9>(context), dst9, src9);
2903 }
2904 
generateMipmapUsingD3D(const gl::Context * context,TextureStorage * storage,const gl::TextureState & textureState)2905 angle::Result Renderer9::generateMipmapUsingD3D(const gl::Context *context,
2906                                                 TextureStorage *storage,
2907                                                 const gl::TextureState &textureState)
2908 {
2909     ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
2910     return angle::Result::Stop;
2911 }
2912 
copyImage(const gl::Context * context,ImageD3D * dest,ImageD3D * source,const gl::Box & sourceBox,const gl::Offset & destOffset,bool unpackFlipY,bool unpackPremultiplyAlpha,bool unpackUnmultiplyAlpha)2913 angle::Result Renderer9::copyImage(const gl::Context *context,
2914                                    ImageD3D *dest,
2915                                    ImageD3D *source,
2916                                    const gl::Box &sourceBox,
2917                                    const gl::Offset &destOffset,
2918                                    bool unpackFlipY,
2919                                    bool unpackPremultiplyAlpha,
2920                                    bool unpackUnmultiplyAlpha)
2921 {
2922     Image9 *dest9 = GetAs<Image9>(dest);
2923     Image9 *src9  = GetAs<Image9>(source);
2924     return Image9::CopyImage(context, dest9, src9, sourceBox.toRect(), destOffset, unpackFlipY,
2925                              unpackPremultiplyAlpha, unpackUnmultiplyAlpha);
2926 }
2927 
createTextureStorage2D(SwapChainD3D * swapChain,const std::string & label)2928 TextureStorage *Renderer9::createTextureStorage2D(SwapChainD3D *swapChain, const std::string &label)
2929 {
2930     SwapChain9 *swapChain9 = GetAs<SwapChain9>(swapChain);
2931     return new TextureStorage9_2D(this, swapChain9, label);
2932 }
2933 
createTextureStorageEGLImage(EGLImageD3D * eglImage,RenderTargetD3D * renderTargetD3D,const std::string & label)2934 TextureStorage *Renderer9::createTextureStorageEGLImage(EGLImageD3D *eglImage,
2935                                                         RenderTargetD3D *renderTargetD3D,
2936                                                         const std::string &label)
2937 {
2938     return new TextureStorage9_EGLImage(this, eglImage, GetAs<RenderTarget9>(renderTargetD3D),
2939                                         label);
2940 }
2941 
createTextureStorageBuffer(const gl::OffsetBindingPointer<gl::Buffer> & buffer,GLenum internalFormat,const std::string & label)2942 TextureStorage *Renderer9::createTextureStorageBuffer(
2943     const gl::OffsetBindingPointer<gl::Buffer> &buffer,
2944     GLenum internalFormat,
2945     const std::string &label)
2946 {
2947     UNREACHABLE();
2948     return nullptr;
2949 }
2950 
createTextureStorageExternal(egl::Stream * stream,const egl::Stream::GLTextureDescription & desc,const std::string & label)2951 TextureStorage *Renderer9::createTextureStorageExternal(
2952     egl::Stream *stream,
2953     const egl::Stream::GLTextureDescription &desc,
2954     const std::string &label)
2955 {
2956     UNIMPLEMENTED();
2957     return nullptr;
2958 }
2959 
createTextureStorage2D(GLenum internalformat,BindFlags bindFlags,GLsizei width,GLsizei height,int levels,const std::string & label,bool hintLevelZeroOnly)2960 TextureStorage *Renderer9::createTextureStorage2D(GLenum internalformat,
2961                                                   BindFlags bindFlags,
2962                                                   GLsizei width,
2963                                                   GLsizei height,
2964                                                   int levels,
2965                                                   const std::string &label,
2966                                                   bool hintLevelZeroOnly)
2967 {
2968     return new TextureStorage9_2D(this, internalformat, bindFlags.renderTarget, width, height,
2969                                   levels, label);
2970 }
2971 
createTextureStorageCube(GLenum internalformat,BindFlags bindFlags,int size,int levels,bool hintLevelZeroOnly,const std::string & label)2972 TextureStorage *Renderer9::createTextureStorageCube(GLenum internalformat,
2973                                                     BindFlags bindFlags,
2974                                                     int size,
2975                                                     int levels,
2976                                                     bool hintLevelZeroOnly,
2977                                                     const std::string &label)
2978 {
2979     return new TextureStorage9_Cube(this, internalformat, bindFlags.renderTarget, size, levels,
2980                                     hintLevelZeroOnly, label);
2981 }
2982 
createTextureStorage3D(GLenum internalformat,BindFlags bindFlags,GLsizei width,GLsizei height,GLsizei depth,int levels,const std::string & label)2983 TextureStorage *Renderer9::createTextureStorage3D(GLenum internalformat,
2984                                                   BindFlags bindFlags,
2985                                                   GLsizei width,
2986                                                   GLsizei height,
2987                                                   GLsizei depth,
2988                                                   int levels,
2989                                                   const std::string &label)
2990 {
2991     // 3D textures are not supported by the D3D9 backend.
2992     UNREACHABLE();
2993 
2994     return nullptr;
2995 }
2996 
createTextureStorage2DArray(GLenum internalformat,BindFlags bindFlags,GLsizei width,GLsizei height,GLsizei depth,int levels,const std::string & label)2997 TextureStorage *Renderer9::createTextureStorage2DArray(GLenum internalformat,
2998                                                        BindFlags bindFlags,
2999                                                        GLsizei width,
3000                                                        GLsizei height,
3001                                                        GLsizei depth,
3002                                                        int levels,
3003                                                        const std::string &label)
3004 {
3005     // 2D array textures are not supported by the D3D9 backend.
3006     UNREACHABLE();
3007 
3008     return nullptr;
3009 }
3010 
createTextureStorage2DMultisample(GLenum internalformat,GLsizei width,GLsizei height,int levels,int samples,bool fixedSampleLocations,const std::string & label)3011 TextureStorage *Renderer9::createTextureStorage2DMultisample(GLenum internalformat,
3012                                                              GLsizei width,
3013                                                              GLsizei height,
3014                                                              int levels,
3015                                                              int samples,
3016                                                              bool fixedSampleLocations,
3017                                                              const std::string &label)
3018 {
3019     // 2D multisampled textures are not supported by the D3D9 backend.
3020     UNREACHABLE();
3021 
3022     return nullptr;
3023 }
3024 
createTextureStorage2DMultisampleArray(GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,int levels,int samples,bool fixedSampleLocations,const std::string & label)3025 TextureStorage *Renderer9::createTextureStorage2DMultisampleArray(GLenum internalformat,
3026                                                                   GLsizei width,
3027                                                                   GLsizei height,
3028                                                                   GLsizei depth,
3029                                                                   int levels,
3030                                                                   int samples,
3031                                                                   bool fixedSampleLocations,
3032                                                                   const std::string &label)
3033 {
3034     // 2D multisampled textures are not supported by the D3D9 backend.
3035     UNREACHABLE();
3036 
3037     return nullptr;
3038 }
3039 
getLUID(LUID * adapterLuid) const3040 bool Renderer9::getLUID(LUID *adapterLuid) const
3041 {
3042     adapterLuid->HighPart = 0;
3043     adapterLuid->LowPart  = 0;
3044 
3045     if (mD3d9Ex)
3046     {
3047         mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3048         return true;
3049     }
3050 
3051     return false;
3052 }
3053 
getVertexConversionType(angle::FormatID vertexFormatID) const3054 VertexConversionType Renderer9::getVertexConversionType(angle::FormatID vertexFormatID) const
3055 {
3056     return d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID).conversionType;
3057 }
3058 
getVertexComponentType(angle::FormatID vertexFormatID) const3059 GLenum Renderer9::getVertexComponentType(angle::FormatID vertexFormatID) const
3060 {
3061     return d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID).componentType;
3062 }
3063 
getVertexSpaceRequired(const gl::Context * context,const gl::VertexAttribute & attrib,const gl::VertexBinding & binding,size_t count,GLsizei instances,GLuint baseInstance,unsigned int * bytesRequiredOut) const3064 angle::Result Renderer9::getVertexSpaceRequired(const gl::Context *context,
3065                                                 const gl::VertexAttribute &attrib,
3066                                                 const gl::VertexBinding &binding,
3067                                                 size_t count,
3068                                                 GLsizei instances,
3069                                                 GLuint baseInstance,
3070                                                 unsigned int *bytesRequiredOut) const
3071 {
3072     if (!attrib.enabled)
3073     {
3074         *bytesRequiredOut = 16u;
3075         return angle::Result::Continue;
3076     }
3077 
3078     angle::FormatID vertexFormatID = gl::GetVertexFormatID(attrib, gl::VertexAttribType::Float);
3079     const d3d9::VertexFormat &d3d9VertexInfo =
3080         d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID);
3081 
3082     unsigned int elementCount  = 0;
3083     const unsigned int divisor = binding.getDivisor();
3084     if (instances == 0 || divisor == 0)
3085     {
3086         elementCount = static_cast<unsigned int>(count);
3087     }
3088     else
3089     {
3090         // Round up to divisor, if possible
3091         elementCount = UnsignedCeilDivide(static_cast<unsigned int>(instances), divisor);
3092     }
3093 
3094     bool check = (d3d9VertexInfo.outputElementSize >
3095                   std::numeric_limits<unsigned int>::max() / elementCount);
3096     ANGLE_CHECK(GetImplAs<Context9>(context), !check,
3097                 "New vertex buffer size would result in an overflow.", GL_OUT_OF_MEMORY);
3098 
3099     *bytesRequiredOut = static_cast<unsigned int>(d3d9VertexInfo.outputElementSize) * elementCount;
3100     return angle::Result::Continue;
3101 }
3102 
generateCaps(gl::Caps * outCaps,gl::TextureCapsMap * outTextureCaps,gl::Extensions * outExtensions,gl::Limitations * outLimitations,ShPixelLocalStorageOptions * outPLSOptions) const3103 void Renderer9::generateCaps(gl::Caps *outCaps,
3104                              gl::TextureCapsMap *outTextureCaps,
3105                              gl::Extensions *outExtensions,
3106                              gl::Limitations *outLimitations,
3107                              ShPixelLocalStorageOptions *outPLSOptions) const
3108 {
3109     d3d9_gl::GenerateCaps(mD3d9, mDevice, mDeviceType, mAdapter, outCaps, outTextureCaps,
3110                           outExtensions, outLimitations);
3111 }
3112 
initializeFeatures(angle::FeaturesD3D * features) const3113 void Renderer9::initializeFeatures(angle::FeaturesD3D *features) const
3114 {
3115     ApplyFeatureOverrides(features, mDisplay->getState().featureOverrides);
3116     if (!mDisplay->getState().featureOverrides.allDisabled)
3117     {
3118         d3d9::InitializeFeatures(features, mAdapterIdentifier.VendorId);
3119     }
3120 }
3121 
initializeFrontendFeatures(angle::FrontendFeatures * features) const3122 void Renderer9::initializeFrontendFeatures(angle::FrontendFeatures *features) const
3123 {
3124     ApplyFeatureOverrides(features, mDisplay->getState().featureOverrides);
3125     if (!mDisplay->getState().featureOverrides.allDisabled)
3126     {
3127         d3d9::InitializeFrontendFeatures(features, mAdapterIdentifier.VendorId);
3128     }
3129 }
3130 
createEGLDevice()3131 DeviceImpl *Renderer9::createEGLDevice()
3132 {
3133     return new Device9(mDevice);
3134 }
3135 
CurSamplerState()3136 Renderer9::CurSamplerState::CurSamplerState()
3137     : forceSet(true), baseLevel(std::numeric_limits<size_t>::max()), samplerState()
3138 {}
3139 
genericDrawElements(const gl::Context * context,gl::PrimitiveMode mode,GLsizei count,gl::DrawElementsType type,const void * indices,GLsizei instances)3140 angle::Result Renderer9::genericDrawElements(const gl::Context *context,
3141                                              gl::PrimitiveMode mode,
3142                                              GLsizei count,
3143                                              gl::DrawElementsType type,
3144                                              const void *indices,
3145                                              GLsizei instances)
3146 {
3147     const gl::State &state = context->getState();
3148     ProgramExecutableD3D *executableD3D =
3149         GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
3150     ASSERT(executableD3D != nullptr);
3151     bool usesPointSize = executableD3D->usesPointSize();
3152 
3153     if (executableD3D->isSamplerMappingDirty())
3154     {
3155         executableD3D->updateSamplerMapping();
3156     }
3157 
3158     if (!applyPrimitiveType(mode, count, usesPointSize))
3159     {
3160         return angle::Result::Continue;
3161     }
3162 
3163     ANGLE_TRY(updateState(context, mode));
3164     ANGLE_TRY(applyTextures(context));
3165     ANGLE_TRY(applyShaders(context, mode));
3166 
3167     if (!skipDraw(state, mode))
3168     {
3169         ANGLE_TRY(drawElementsImpl(context, mode, count, type, indices, instances));
3170     }
3171 
3172     return angle::Result::Continue;
3173 }
3174 
genericDrawArrays(const gl::Context * context,gl::PrimitiveMode mode,GLint first,GLsizei count,GLsizei instances)3175 angle::Result Renderer9::genericDrawArrays(const gl::Context *context,
3176                                            gl::PrimitiveMode mode,
3177                                            GLint first,
3178                                            GLsizei count,
3179                                            GLsizei instances)
3180 {
3181     const gl::State &state = context->getState();
3182     ProgramExecutableD3D *executableD3D =
3183         GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
3184     ASSERT(executableD3D != nullptr);
3185     bool usesPointSize = executableD3D->usesPointSize();
3186 
3187     if (executableD3D->isSamplerMappingDirty())
3188     {
3189         executableD3D->updateSamplerMapping();
3190     }
3191 
3192     if (!applyPrimitiveType(mode, count, usesPointSize))
3193     {
3194         return angle::Result::Continue;
3195     }
3196 
3197     ANGLE_TRY(updateState(context, mode));
3198     ANGLE_TRY(applyVertexBuffer(context, mode, first, count, instances, nullptr));
3199     ANGLE_TRY(applyTextures(context));
3200     ANGLE_TRY(applyShaders(context, mode));
3201 
3202     if (!skipDraw(context->getState(), mode))
3203     {
3204         ANGLE_TRY(drawArraysImpl(context, mode, first, count, instances));
3205     }
3206 
3207     return angle::Result::Continue;
3208 }
3209 
createDefaultFramebuffer(const gl::FramebufferState & state)3210 FramebufferImpl *Renderer9::createDefaultFramebuffer(const gl::FramebufferState &state)
3211 {
3212     return new Framebuffer9(state, this);
3213 }
3214 
getMaxSupportedESVersion() const3215 gl::Version Renderer9::getMaxSupportedESVersion() const
3216 {
3217     return gl::Version(2, 0);
3218 }
3219 
getMaxConformantESVersion() const3220 gl::Version Renderer9::getMaxConformantESVersion() const
3221 {
3222     return gl::Version(2, 0);
3223 }
3224 
clearRenderTarget(const gl::Context * context,RenderTargetD3D * renderTarget,const gl::ColorF & clearColorValue,const float clearDepthValue,const unsigned int clearStencilValue)3225 angle::Result Renderer9::clearRenderTarget(const gl::Context *context,
3226                                            RenderTargetD3D *renderTarget,
3227                                            const gl::ColorF &clearColorValue,
3228                                            const float clearDepthValue,
3229                                            const unsigned int clearStencilValue)
3230 {
3231     D3DCOLOR color =
3232         D3DCOLOR_ARGB(gl::unorm<8>(clearColorValue.alpha), gl::unorm<8>(clearColorValue.red),
3233                       gl::unorm<8>(clearColorValue.green), gl::unorm<8>(clearColorValue.blue));
3234     float depth   = clearDepthValue;
3235     DWORD stencil = clearStencilValue & 0x000000FF;
3236 
3237     unsigned int renderTargetSerial        = renderTarget->getSerial();
3238     RenderTarget9 *renderTarget9           = GetAs<RenderTarget9>(renderTarget);
3239     IDirect3DSurface9 *renderTargetSurface = renderTarget9->getSurface();
3240     ASSERT(renderTargetSurface);
3241 
3242     DWORD dxClearFlags = 0;
3243 
3244     const gl::InternalFormat &internalFormatInfo =
3245         gl::GetSizedInternalFormatInfo(renderTarget->getInternalFormat());
3246     if (internalFormatInfo.depthBits > 0 || internalFormatInfo.stencilBits > 0)
3247     {
3248         dxClearFlags = D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL;
3249         if (mAppliedDepthStencilSerial != renderTargetSerial)
3250         {
3251             mDevice->SetDepthStencilSurface(renderTargetSurface);
3252         }
3253     }
3254     else
3255     {
3256         dxClearFlags = D3DCLEAR_TARGET;
3257         if (mAppliedRenderTargetSerial != renderTargetSerial)
3258         {
3259             mDevice->SetRenderTarget(0, renderTargetSurface);
3260         }
3261     }
3262     SafeRelease(renderTargetSurface);
3263 
3264     D3DVIEWPORT9 viewport;
3265     viewport.X      = 0;
3266     viewport.Y      = 0;
3267     viewport.Width  = renderTarget->getWidth();
3268     viewport.Height = renderTarget->getHeight();
3269     mDevice->SetViewport(&viewport);
3270 
3271     mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
3272 
3273     mDevice->Clear(0, nullptr, dxClearFlags, color, depth, stencil);
3274 
3275     markAllStateDirty();
3276 
3277     return angle::Result::Continue;
3278 }
3279 
canSelectViewInVertexShader() const3280 bool Renderer9::canSelectViewInVertexShader() const
3281 {
3282     return false;
3283 }
3284 
3285 // For each Direct3D sampler of either the pixel or vertex stage,
3286 // looks up the corresponding OpenGL texture image unit and texture type,
3287 // and sets the texture and its addressing/filtering state (or NULL when inactive).
3288 // Sampler mapping needs to be up-to-date on the program object before this is called.
applyTextures(const gl::Context * context,gl::ShaderType shaderType)3289 angle::Result Renderer9::applyTextures(const gl::Context *context, gl::ShaderType shaderType)
3290 {
3291     const auto &glState = context->getState();
3292     const auto &caps    = context->getCaps();
3293     ProgramExecutableD3D *executableD3D =
3294         GetImplAs<ProgramExecutableD3D>(glState.getProgramExecutable());
3295 
3296     ASSERT(!executableD3D->isSamplerMappingDirty());
3297 
3298     // TODO(jmadill): Use the Program's sampler bindings.
3299     const gl::ActiveTexturesCache &activeTextures = glState.getActiveTexturesCache();
3300 
3301     const gl::RangeUI samplerRange = executableD3D->getUsedSamplerRange(shaderType);
3302     for (unsigned int samplerIndex = samplerRange.low(); samplerIndex < samplerRange.high();
3303          samplerIndex++)
3304     {
3305         GLint textureUnit = executableD3D->getSamplerMapping(shaderType, samplerIndex, caps);
3306         ASSERT(textureUnit != -1);
3307         gl::Texture *texture = activeTextures[textureUnit];
3308 
3309         // A nullptr texture indicates incomplete.
3310         if (texture)
3311         {
3312             gl::Sampler *samplerObject = glState.getSampler(textureUnit);
3313 
3314             const gl::SamplerState &samplerState =
3315                 samplerObject ? samplerObject->getSamplerState() : texture->getSamplerState();
3316 
3317             ANGLE_TRY(setSamplerState(context, shaderType, samplerIndex, texture, samplerState));
3318             ANGLE_TRY(setTexture(context, shaderType, samplerIndex, texture));
3319         }
3320         else
3321         {
3322             gl::TextureType textureType =
3323                 executableD3D->getSamplerTextureType(shaderType, samplerIndex);
3324 
3325             // Texture is not sampler complete or it is in use by the framebuffer.  Bind the
3326             // incomplete texture.
3327             gl::Texture *incompleteTexture = nullptr;
3328             ANGLE_TRY(getIncompleteTexture(context, textureType, &incompleteTexture));
3329             ANGLE_TRY(setSamplerState(context, shaderType, samplerIndex, incompleteTexture,
3330                                       incompleteTexture->getSamplerState()));
3331             ANGLE_TRY(setTexture(context, shaderType, samplerIndex, incompleteTexture));
3332         }
3333     }
3334 
3335     // Set all the remaining textures to NULL
3336     int samplerCount = (shaderType == gl::ShaderType::Fragment)
3337                            ? caps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]
3338                            : caps.maxShaderTextureImageUnits[gl::ShaderType::Vertex];
3339 
3340     // TODO(jmadill): faster way?
3341     for (int samplerIndex = samplerRange.high(); samplerIndex < samplerCount; samplerIndex++)
3342     {
3343         ANGLE_TRY(setTexture(context, shaderType, samplerIndex, nullptr));
3344     }
3345 
3346     return angle::Result::Continue;
3347 }
3348 
applyTextures(const gl::Context * context)3349 angle::Result Renderer9::applyTextures(const gl::Context *context)
3350 {
3351     ANGLE_TRY(applyTextures(context, gl::ShaderType::Vertex));
3352     ANGLE_TRY(applyTextures(context, gl::ShaderType::Fragment));
3353     return angle::Result::Continue;
3354 }
3355 
getIncompleteTexture(const gl::Context * context,gl::TextureType type,gl::Texture ** textureOut)3356 angle::Result Renderer9::getIncompleteTexture(const gl::Context *context,
3357                                               gl::TextureType type,
3358                                               gl::Texture **textureOut)
3359 {
3360     return GetImplAs<Context9>(context)->getIncompleteTexture(context, type, textureOut);
3361 }
3362 
ensureVertexDataManagerInitialized(const gl::Context * context)3363 angle::Result Renderer9::ensureVertexDataManagerInitialized(const gl::Context *context)
3364 {
3365     if (!mVertexDataManager)
3366     {
3367         mVertexDataManager = new VertexDataManager(this);
3368         ANGLE_TRY(mVertexDataManager->initialize(context));
3369     }
3370 
3371     return angle::Result::Continue;
3372 }
3373 
getVendorString() const3374 std::string Renderer9::getVendorString() const
3375 {
3376     return GetVendorString(getVendorId());
3377 }
3378 
getVersionString(bool includeFullVersion) const3379 std::string Renderer9::getVersionString(bool includeFullVersion) const
3380 {
3381     std::ostringstream versionString;
3382     std::string driverName(mAdapterIdentifier.Driver);
3383     if (!driverName.empty())
3384     {
3385         versionString << mAdapterIdentifier.Driver;
3386     }
3387     else
3388     {
3389         versionString << "D3D9";
3390     }
3391 
3392     if (includeFullVersion)
3393     {
3394         versionString << " -";
3395         versionString << GetDriverVersionString(mAdapterIdentifier.DriverVersion);
3396     }
3397 
3398     return versionString.str();
3399 }
3400 
CreateRenderer9(egl::Display * display)3401 RendererD3D *CreateRenderer9(egl::Display *display)
3402 {
3403     return new Renderer9(display);
3404 }
3405 
3406 }  // namespace rx
3407