summaryrefslogtreecommitdiffstats
path: root/o3d/command_buffer/service/win/d3d9/gapi_d3d9.cc
blob: df9cb46586bf545eb3fde9fa9735aca5eb0f94c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright 2009, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


// This file contains the implementation of the GAPID3D9 class.

#include "command_buffer/service/win/d3d9/gapi_d3d9.h"

namespace o3d {
namespace command_buffer {

GAPID3D9::GAPID3D9()
    : d3d_module_(NULL),
      d3dx_module_(NULL),
      d3d_(NULL),
      d3d_device_(NULL),
      hwnd_(NULL),
      current_vertex_struct_(0),
      validate_streams_(true),
      max_vertices_(0),
      current_effect_id_(0),
      validate_effect_(true),
      current_effect_(NULL),
      vertex_buffers_(),
      index_buffers_(),
      vertex_structs_(),
      back_buffer_surface_(NULL),
      back_buffer_depth_surface_(NULL),
      current_surface_id_(kInvalidResource),
      current_depth_surface_id_(kInvalidResource),
      direct3d_create9_(NULL),
      get_shader_constant_table_(NULL),
      create_effect_(NULL),
      get_shader_input_semantics_(NULL) {}

GAPID3D9::~GAPID3D9() {}

// Initializes a D3D interface and device, and sets basic states.
bool GAPID3D9::Initialize() {
  if (!FindDirect3DFunctions()) {
    Destroy();
    return false;
  }

  d3d_ = Direct3DCreate(D3D_SDK_VERSION);
  if (NULL == d3d_) {
    LOG(ERROR) << "Failed to create the initial D3D9 Interface";
    Destroy();
    return false;
  }
  d3d_device_ = NULL;

  D3DDISPLAYMODE d3ddm;
  d3d_->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
  // NOTE: make sure the backbuffer matches this format, as it is
  // currently currently assumed to be 32-bit 8X8R8G8B

  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp, sizeof(d3dpp));
  d3dpp.Windowed               = TRUE;
  d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat       = d3ddm.Format;
  d3dpp.EnableAutoDepthStencil = TRUE;
  d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_ONE;  // wait for vsync
  // Note: SwapEffect=DISCARD is req. for multisample to function
  // Note: AutoDepthStencilFormat is 16-bit (not the usual 8-bit)

  // query multisampling
  const int kNumTypesToCheck = 4;
  D3DMULTISAMPLE_TYPE multisample_types[] = { D3DMULTISAMPLE_5_SAMPLES,
                                              D3DMULTISAMPLE_4_SAMPLES,
                                              D3DMULTISAMPLE_2_SAMPLES,
                                              D3DMULTISAMPLE_NONE };
  DWORD multisample_quality = 0;
  for (int i = 0; i < kNumTypesToCheck; ++i) {
    // check back-buffer for multisampling at level "i";
    // back buffer = 32-bit XRGB (i.e. no alpha)
    if (SUCCEEDED(d3d_->CheckDeviceMultiSampleType(
                      D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      D3DFMT_X8R8G8B8,
                      true,  // result is windowed
                      multisample_types[i],
                      &multisample_quality))) {
      // back buffer succeeded, now check depth-buffer
      // depth buffer = 24-bit, stencil = 8-bit
      // NOTE: 8-bit not 16-bit like the D3DPRESENT_PARAMETERS
      if (SUCCEEDED(d3d_->CheckDeviceMultiSampleType(
                        D3DADAPTER_DEFAULT,
                        D3DDEVTYPE_HAL,
                        D3DFMT_D24S8,
                        true,  // result is windowed
                        multisample_types[i],
                        &multisample_quality))) {
        d3dpp.MultiSampleType = multisample_types[i];
        d3dpp.MultiSampleQuality = multisample_quality - 1;
        break;
      }
    }
  }
  // D3DCREATE_FPU_PRESERVE is there because Firefox 3 relies on specific FPU
  // flags for its UI rendering. Apparently Firefox 2 does not, though we don't
  // currently propagate that info.
  // TODO: check if FPU_PRESERVE has a significant perf hit, in which
  // case find out if we can disable it for Firefox 2/other browsers, and/or if
  // it makes sense to switch FPU flags before/after every DX call.
  DWORD flags = D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE;
  if (!SUCCEEDED(d3d_->CreateDevice(D3DADAPTER_DEFAULT,
                                    D3DDEVTYPE_HAL,
                                    hwnd_,
                                    flags,
                                    &d3dpp,
                                    &d3d_device_))) {
    LOG(ERROR) << "Failed to create the D3D Device";
    Destroy();
    return false;
  }
  // initialise the d3d graphics state.
  HR(d3d_device_->SetRenderState(D3DRS_LIGHTING, FALSE));
  HR(d3d_device_->SetRenderState(D3DRS_ZENABLE, TRUE));
  HR(d3d_device_->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE));
  return true;
}

// Deletes the D3D9 Device and releases the D3D interface.
void GAPID3D9::Destroy() {
  vertex_buffers_.DestroyAllResources();
  index_buffers_.DestroyAllResources();
  vertex_structs_.DestroyAllResources();
  effects_.DestroyAllResources();
  effect_params_.DestroyAllResources();
  textures_.DestroyAllResources();
  samplers_.DestroyAllResources();
  render_surfaces_.DestroyAllResources();
  depth_surfaces_.DestroyAllResources();
  if (d3d_device_) {
    d3d_device_->Release();
    d3d_device_ = NULL;
  }
  if (d3d_) {
    d3d_->Release();
    d3d_ = NULL;
  }
  if (d3dx_module_) {
    FreeLibrary(d3dx_module_);
    d3dx_module_ = NULL;
    get_shader_constant_table_ = NULL;
    create_effect_ = NULL;
    get_shader_input_semantics_ = NULL;
  }
  if (d3d_module_) {
    FreeLibrary(d3d_module_);
    d3d_module_ = NULL;
    direct3d_create9_ = NULL;
  }
}

// Begins the frame.
void GAPID3D9::BeginFrame() {
  HR(d3d_device_->GetRenderTarget(0, &back_buffer_surface_));
  HR(d3d_device_->GetDepthStencilSurface(&back_buffer_depth_surface_));
  HR(d3d_device_->BeginScene());
}

// Ends the frame, presenting the back buffer.
void GAPID3D9::EndFrame() {
  DirtyEffect();
  HR(d3d_device_->EndScene());
  HR(d3d_device_->Present(NULL, NULL, NULL, NULL));

  // Release the back-buffer references.
  back_buffer_surface_ = NULL;
  back_buffer_depth_surface_ = NULL;
}

// Clears the selected buffers.
void GAPID3D9::Clear(unsigned int buffers,
                     const RGBA &color,
                     float depth,
                     unsigned int stencil) {
  DWORD flags = (buffers & kColor ? D3DCLEAR_TARGET : 0) |
                (buffers & kDepth ? D3DCLEAR_ZBUFFER : 0) |
                (buffers & kStencil ? D3DCLEAR_STENCIL : 0);
  HR(d3d_device_->Clear(0,
                        NULL,
                        flags,
                        D3DCOLOR_COLORVALUE(color.red,
                                            color.green,
                                            color.blue,
                                            color.alpha),
                        depth,
                        stencil));
}

// Sets the viewport.
void GAPID3D9::SetViewport(unsigned int x,
                           unsigned int y,
                           unsigned int width,
                           unsigned int height,
                           float z_min,
                           float z_max) {
  D3DVIEWPORT9 viewport = {x, y, width, height, z_min, z_max};
  HR(d3d_device_->SetViewport(&viewport));
}

// Converts an unsigned int RGBA color into an unsigned int ARGB (DirectX)
// color.
static unsigned int RGBAToARGB(unsigned int rgba) {
  return (rgba >> 8) | (rgba << 24);
}

// Sets the current VertexStruct. Just keep track of the ID.
BufferSyncInterface::ParseError GAPID3D9::SetVertexStruct(ResourceId id) {
  current_vertex_struct_ = id;
  validate_streams_ = true;
  return BufferSyncInterface::kParseNoError;
}

bool GAPID3D9::FindDirect3DFunctions() {
  d3d_module_ = LoadLibrary(TEXT("d3d9.dll"));
  if (NULL == d3d_module_) {
    LOG(ERROR) << "Failed to load d3d9.dll";
    return false;
  }

  direct3d_create9_ = reinterpret_cast<Direct3DCreate9Proc>(
      GetProcAddress(d3d_module_, "Direct3DCreate9"));
  if (NULL == direct3d_create9_) {
    LOG(ERROR) << "Failed to find Direct3DCreate9 in d3d9.dll";
    Destroy();
    return false;
  }

  d3dx_module_ = LoadLibrary(TEXT("d3dx9_36.dll"));
  if (NULL == d3d_module_) {
    LOG(ERROR) << "Failed to load d3dx9_36.dll";
    return false;
  }

  get_shader_constant_table_ = reinterpret_cast<D3DXGetShaderConstantTableProc>(
      GetProcAddress(d3dx_module_, "D3DXGetShaderConstantTable"));
  if (NULL == get_shader_constant_table_) {
    LOG(ERROR) << "Failed to find D3DXGetShaderConstantTable in d3dx9_36.dll";
    Destroy();
    return false;
  }

  create_effect_ = reinterpret_cast<D3DXCreateEffectProc>(
      GetProcAddress(d3dx_module_, "D3DXCreateEffect"));
  if (NULL == create_effect_) {
    LOG(ERROR) << "Failed to find D3DXCreateEffect in d3dx9_36.dll";
    Destroy();
    return false;
  }

  get_shader_input_semantics_ =
      reinterpret_cast<D3DXGetShaderInputSemanticsProc>(
          GetProcAddress(d3dx_module_, "D3DXGetShaderInputSemantics"));
  if (NULL == get_shader_input_semantics_) {
    LOG(ERROR) << "Failed to find D3DXGetShaderInputSemantics in d3dx9_36.dll";
    Destroy();
    return false;
  }

  return true;
}

// Sets in D3D the input streams of the current vertex struct.
bool GAPID3D9::ValidateStreams() {
  DCHECK(validate_streams_);
  VertexStructD3D9 *vertex_struct = vertex_structs_.Get(current_vertex_struct_);
  if (!vertex_struct) {
    LOG(ERROR) << "Drawing with invalid streams.";
    return false;
  }
  max_vertices_ = vertex_struct->SetStreams(this);
  validate_streams_ = false;
  return max_vertices_ > 0;
}

// Converts a GAPID3D9::PrimitiveType to a D3DPRIMITIVETYPE.
static D3DPRIMITIVETYPE D3DPrimitive(
    command_buffer::PrimitiveType primitive_type) {
  switch (primitive_type) {
    case command_buffer::kPoints:
      return D3DPT_POINTLIST;
    case command_buffer::kLines:
      return D3DPT_LINELIST;
    case command_buffer::kLineStrips:
      return D3DPT_LINESTRIP;
    case command_buffer::kTriangles:
      return D3DPT_TRIANGLELIST;
    case command_buffer::kTriangleStrips:
      return D3DPT_TRIANGLESTRIP;
    case command_buffer::kTriangleFans:
      return D3DPT_TRIANGLEFAN;
    default:
      LOG(FATAL) << "Invalid primitive type";
      return D3DPT_POINTLIST;
  }
}

// Draws with the current vertex struct.
BufferSyncInterface::ParseError GAPID3D9::Draw(
    PrimitiveType primitive_type,
    unsigned int first,
    unsigned int count) {
  if (validate_streams_ && !ValidateStreams()) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }
  if (validate_effect_ && !ValidateEffect()) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }
  DCHECK(current_effect_);
  if (!current_effect_->CommitParameters()) {
    return BufferSyncInterface::kParseInvalidArguments;
  }
  if (first + count > max_vertices_) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }
  HR(d3d_device_->DrawPrimitive(D3DPrimitive(primitive_type), first, count));
  return BufferSyncInterface::kParseNoError;
}

// Draws with the current vertex struct.
BufferSyncInterface::ParseError GAPID3D9::DrawIndexed(
    PrimitiveType primitive_type,
    ResourceId index_buffer_id,
    unsigned int first,
    unsigned int count,
    unsigned int min_index,
    unsigned int max_index) {
  IndexBufferD3D9 *index_buffer = index_buffers_.Get(index_buffer_id);
  if (!index_buffer) return BufferSyncInterface::kParseInvalidArguments;
  if (validate_streams_ && !ValidateStreams()) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }
  if (validate_effect_ && !ValidateEffect()) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }
  DCHECK(current_effect_);
  if (!current_effect_->CommitParameters()) {
    return BufferSyncInterface::kParseInvalidArguments;
  }
  if ((min_index >= max_vertices_) || (max_index > max_vertices_)) {
    // TODO: add proper error management
    return BufferSyncInterface::kParseInvalidArguments;
  }

  HR(d3d_device_->SetIndices(index_buffer->d3d_index_buffer()));
  HR(d3d_device_->DrawIndexedPrimitive(D3DPrimitive(primitive_type), 0,
                                       min_index, max_index - min_index + 1,
                                       first, count));
  return BufferSyncInterface::kParseNoError;
}

}  // namespace command_buffer
}  // namespace o3d