summaryrefslogtreecommitdiffstats
path: root/o3d/command_buffer/client/cross/cmd_buffer_helper.h
blob: b00c8fc6fd288b5979de2bd6427021ae8db8a892 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
 * 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 command buffer helper class.

#ifndef O3D_COMMAND_BUFFER_CLIENT_CROSS_CMD_BUFFER_HELPER_H_
#define O3D_COMMAND_BUFFER_CLIENT_CROSS_CMD_BUFFER_HELPER_H_

#include "command_buffer/common/cross/logging.h"
#include "command_buffer/common/cross/buffer_sync_api.h"
#include "command_buffer/common/cross/cmd_buffer_format.h"

namespace o3d {
namespace command_buffer {

// Command buffer helper class. This class simplifies ring buffer management:
// it will allocate the buffer, give it to the buffer interface, and let the
// user add commands to it, while taking care of the synchronization (put and
// get). It also provides a way to ensure commands have been executed, through
// the token mechanism:
//
// helper.AddCommand(...);
// helper.AddCommand(...);
// unsigned int token = helper.InsertToken();
// helper.AddCommand(...);
// helper.AddCommand(...);
// [...]
//
// helper.WaitForToken(token);  // this doesn't return until the first two
//                              // commands have been executed.
class CommandBufferHelper {
 public:
  // Constructs a CommandBufferHelper object. The helper needs to be
  // initialized by calling Init() before use.
  // Parameters:
  //   interface: the buffer interface the helper sends commands to.
  explicit CommandBufferHelper(BufferSyncInterface *interface);
  ~CommandBufferHelper();

  // Initializes the command buffer by allocating shared memory.
  // Parameters:
  //   entry_count: the number of entries in the buffer. Note that commands
  //     sent through the buffer must use at most entry_count-2 arguments
  //     (entry_count-1 size).
  // Returns:
  //   true if successful.
  bool Init(unsigned int entry_count);

  // Flushes the commands, setting the put pointer to let the buffer interface
  // know that new commands have been added.
  void Flush() {
    interface_->Put(put_);
  }

  // Waits until all the commands have been executed.
  void Finish();

  // Waits until a given number of available entries are available.
  // Parameters:
  //   count: number of entries needed. This value must be at most
  //     the size of the buffer minus one.
  void WaitForAvailableEntries(unsigned int count);

  // Adds a command data to the command buffer. This may wait until sufficient
  // space is available.
  // Parameters:
  //   entries: The command entries to add.
  //   count: The number of entries.
  void AddCommandData(const CommandBufferEntry* entries, unsigned int count) {
    WaitForAvailableEntries(count);
    for (; count > 0; --count) {
      entries_[put_++] = *entries++;
    }
    DCHECK_LE(put_, entry_count_);
    if (put_ == entry_count_) put_ = 0;
  }

  // A typed version of AddCommandData.
  template <typename T>
  void AddTypedCmdData(const T& cmd) {
    AddCommandData(reinterpret_cast<const CommandBufferEntry*>(&cmd),
                   ComputeNumEntries(sizeof(cmd)));
  }

  // Adds a command to the command buffer. This may wait until sufficient space
  // is available.
  // Parameters:
  //   command: the command index.
  //   arg_count: the number of arguments for the command.
  //   args: the arguments for the command (these are copied before the
  //     function returns).
  void AddCommand(unsigned int command,
                  unsigned int arg_count,
                  const CommandBufferEntry *args) {
    CommandHeader header;
    header.size = arg_count + 1;
    header.command = command;
    WaitForAvailableEntries(header.size);
    entries_[put_++].value_header = header;
    for (unsigned int i = 0; i < arg_count; ++i) {
      entries_[put_++] = args[i];
    }
    DCHECK_LE(put_, entry_count_);
    if (put_ == entry_count_) put_ = 0;
  }

  // Inserts a new token into the command buffer. This token either has a value
  // different from previously inserted tokens, or ensures that previously
  // inserted tokens with that value have already passed through the command
  // stream.
  // Returns:
  //   the value of the new token.
  unsigned int InsertToken();

  // Waits until the token of a particular value has passed through the command
  // stream (i.e. commands inserted before that token have been executed).
  // NOTE: This will call Flush if it needs to block.
  // Parameters:
  //   the value of the token to wait for.
  void WaitForToken(unsigned int token);

  // Returns the buffer interface used to send synchronous commands.
  BufferSyncInterface *interface() { return interface_; }

  // Waits for a certain amount of space to be available. Returns address
  // of space.
  CommandBufferEntry* GetSpace(uint32 entries);

  // Typed version of GetSpace. Gets enough room for the given type and returns
  // a reference to it.
  template <typename T>
  T& GetCmdSpace() {
    COMPILE_ASSERT(T::kArgFlags == cmd::kFixed, Cmd_kArgFlags_not_kFixed);
    uint32 space_needed = ComputeNumEntries(sizeof(T));
    void* data = GetSpace(space_needed);
    return *reinterpret_cast<T*>(data);
  }

  // Typed version of GetSpace for immediate commands.
  template <typename T>
  T& GetImmediateCmdSpace(size_t space) {
    COMPILE_ASSERT(T::kArgFlags == cmd::kAtLeastN, Cmd_kArgFlags_not_kAtLeastN);
    uint32 space_needed = ComputeNumEntries(sizeof(T) + space);
    void* data = GetSpace(space_needed);
    return *reinterpret_cast<T*>(data);
  }

  // ------------------ Individual commands ----------------------

  void Noop(uint32 skip_count) {
    cmd::Noop& cmd = GetImmediateCmdSpace<cmd::Noop>(
        skip_count * sizeof(CommandBufferEntry));
    cmd.Init(skip_count);
  }


  void SetToken(uint32 token) {
    cmd::SetToken& cmd = GetCmdSpace<cmd::SetToken>();
    cmd.Init(token);
  }


  void BeginFrame() {
    cmd::BeginFrame& cmd = GetCmdSpace<cmd::BeginFrame>();
    cmd.Init();
  }


  void EndFrame() {
    cmd::EndFrame& cmd = GetCmdSpace<cmd::EndFrame>();
    cmd.Init();
  }

  void Clear(
      uint32 buffers,
      float red, float green, float blue, float alpha,
      float depth, uint32 stencil) {
    cmd::Clear& cmd = GetCmdSpace<cmd::Clear>();
    cmd.Init(buffers, red, green, blue, alpha, depth, stencil);
  }

  void SetViewport(
      uint32 left,
      uint32 top,
      uint32 width,
      uint32 height,
      float z_min,
      float z_max) {
    cmd::SetViewport& cmd = GetCmdSpace<cmd::SetViewport>();
    cmd.Init(left, top, width, height, z_min, z_max);
  }

  void CreateVertexBuffer(
      ResourceId vertex_buffer_id, uint32 size, vertex_buffer::Flags flags) {
    cmd::CreateVertexBuffer& cmd = GetCmdSpace<cmd::CreateVertexBuffer>();
    cmd.Init(vertex_buffer_id, size, flags);
  }

  void DestroyVertexBuffer(ResourceId vertex_buffer_id) {
    cmd::DestroyVertexBuffer& cmd = GetCmdSpace<cmd::DestroyVertexBuffer>();
    cmd.Init(vertex_buffer_id);
  }

  void SetVertexBufferDataImmediate(
      ResourceId vertex_buffer_id, uint32 offset,
      const void* data, uint32 size) {
    cmd::SetVertexBufferDataImmediate& cmd =
        GetImmediateCmdSpace<cmd::SetVertexBufferDataImmediate>(size);
    cmd.Init(vertex_buffer_id, offset, data, size);
  }

  void SetVertexBufferData(
      ResourceId vertex_buffer_id, uint32 offset, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::SetVertexBufferData& cmd =
        GetCmdSpace<cmd::SetVertexBufferData>();
    cmd.Init(vertex_buffer_id, offset, size,
             shared_memory_id, shared_memory_offset);
  }

  void GetVertexBufferData(
      ResourceId vertex_buffer_id, uint32 offset, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetVertexBufferData& cmd =
        GetCmdSpace<cmd::GetVertexBufferData>();
    cmd.Init(vertex_buffer_id, offset, size,
             shared_memory_id, shared_memory_offset);
  }

  void CreateIndexBuffer(
      ResourceId index_buffer_id, uint32 size, index_buffer::Flags flags) {
    cmd::CreateIndexBuffer& cmd =
        GetCmdSpace<cmd::CreateIndexBuffer>();
    cmd.Init(index_buffer_id, size, flags);
  }

  void DestroyIndexBuffer(ResourceId index_buffer_id) {
    cmd::DestroyIndexBuffer& cmd = GetCmdSpace<cmd::DestroyIndexBuffer>();
    cmd.Init(index_buffer_id);
  }

  void SetIndexBufferDataImmediate(
      ResourceId index_buffer_id, uint32 offset,
      const void* data, uint32 size) {
    cmd::SetIndexBufferDataImmediate& cmd =
        GetImmediateCmdSpace<cmd::SetIndexBufferDataImmediate>(size);
    cmd.Init(index_buffer_id, offset, data, size);
  }

  void SetIndexBufferData(
      ResourceId index_buffer_id, uint32 offset, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::SetIndexBufferData& cmd = GetCmdSpace<cmd::SetIndexBufferData>();
    cmd.Init(index_buffer_id, offset, size,
             shared_memory_id, shared_memory_offset);
  }

  void GetIndexBufferData(
      ResourceId index_buffer_id, uint32 offset, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetIndexBufferData& cmd = GetCmdSpace<cmd::GetIndexBufferData>();
    cmd.Init(index_buffer_id, offset, size,
             shared_memory_id, shared_memory_offset);
  }

  void CreateVertexStruct(ResourceId vertex_struct_id, uint32 input_count) {
    cmd::CreateVertexStruct& cmd = GetCmdSpace<cmd::CreateVertexStruct>();
    cmd.Init(vertex_struct_id, input_count);
  }

  void DestroyVertexStruct(ResourceId vertex_struct_id) {
    cmd::DestroyVertexStruct& cmd = GetCmdSpace<cmd::DestroyVertexStruct>();
    cmd.Init(vertex_struct_id);
  }

  void SetVertexInput(
      ResourceId vertex_struct_id,
      uint32 input_index,
      ResourceId vertex_buffer_id,
      uint32 offset,
      vertex_struct::Semantic semantic,
      uint32 semantic_index,
      vertex_struct::Type type,
      uint32 stride) {
    cmd::SetVertexInput& cmd = GetCmdSpace<cmd::SetVertexInput>();
    cmd.Init(
        vertex_struct_id,
        input_index,
        vertex_buffer_id,
        offset,
        semantic,
        semantic_index,
        type,
        stride);
  }

  void SetVertexStruct(ResourceId vertex_struct_id) {
    cmd::SetVertexStruct& cmd = GetCmdSpace<cmd::SetVertexStruct>();
    cmd.Init(vertex_struct_id);
  }

  void Draw(PrimitiveType primitive_type, uint32 first, uint32 count) {
    cmd::Draw& cmd = GetCmdSpace<cmd::Draw>();
    cmd.Init(primitive_type, first, count);
  }

  void DrawIndexed(
      PrimitiveType primitive_type,
      ResourceId index_buffer_id,
      uint32 first,
      uint32 count,
      uint32 min_index,
      uint32 max_index) {
    cmd::DrawIndexed& cmd = GetCmdSpace<cmd::DrawIndexed>();
    cmd.Init(
        primitive_type,
        index_buffer_id,
        first,
        count,
        min_index,
        max_index);
  }

  void CreateEffect(
      ResourceId effect_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::CreateEffect& cmd = GetCmdSpace<cmd::CreateEffect>();
    cmd.Init(effect_id, size, shared_memory_id, shared_memory_offset);
  }

  void CreateEffectImmediate(
      ResourceId effect_id, uint32 size, const void* data) {
    cmd::CreateEffectImmediate& cmd =
        GetImmediateCmdSpace<cmd::CreateEffectImmediate>(size);
    cmd.Init(effect_id, size, data);
  }

  void DestroyEffect(ResourceId effect_id) {
    cmd::DestroyEffect& cmd = GetCmdSpace<cmd::DestroyEffect>();
    cmd.Init(effect_id);
  }

  void SetEffect(ResourceId effect_id) {
    cmd::SetEffect& cmd = GetCmdSpace<cmd::SetEffect>();
    cmd.Init(effect_id);
  }

  void GetParamCount(
      ResourceId effect_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetParamCount& cmd = GetCmdSpace<cmd::GetParamCount>();
    cmd.Init(effect_id, size, shared_memory_id, shared_memory_offset);
  }

  void CreateParam(ResourceId param_id, ResourceId effect_id, uint32 index) {
    cmd::CreateParam& cmd = GetCmdSpace<cmd::CreateParam>();
    cmd.Init(param_id, effect_id, index);
  }

  void CreateParamByName(
      ResourceId param_id, ResourceId effect_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::CreateParamByName& cmd = GetCmdSpace<cmd::CreateParamByName>();
    cmd.Init(param_id, effect_id, size, shared_memory_id, shared_memory_offset);
  }

  void CreateParamByNameImmediate(
      ResourceId param_id, ResourceId effect_id,
      uint32 size, const void* data) {
    cmd::CreateParamByNameImmediate& cmd =
        GetImmediateCmdSpace<cmd::CreateParamByNameImmediate>(size);
    cmd.Init(param_id, effect_id, size, data);
  }

  void DestroyParam(ResourceId param_id) {
    cmd::DestroyParam& cmd = GetCmdSpace<cmd::DestroyParam>();
    cmd.Init(param_id);
  }

  void SetParamData(
      ResourceId param_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::SetParamData& cmd = GetCmdSpace<cmd::SetParamData>();
    cmd.Init(param_id, size, shared_memory_id, shared_memory_offset);
  }

  void SetParamDataImmediate(
      ResourceId param_id, uint32 size, const void* data) {
    cmd::SetParamDataImmediate& cmd =
        GetImmediateCmdSpace<cmd::SetParamDataImmediate>(size);
    cmd.Init(param_id, size, data);
  }

  void GetParamDesc(
      ResourceId param_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetParamDesc& cmd = GetCmdSpace<cmd::GetParamDesc>();
    cmd.Init(param_id, size, shared_memory_id, shared_memory_offset);
  }

  void GetStreamCount(
      ResourceId effect_id, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetStreamCount& cmd = GetCmdSpace<cmd::GetStreamCount>();
    cmd.Init(effect_id, size, shared_memory_id, shared_memory_offset);
  }

  void GetStreamDesc(
      ResourceId effect_id, uint32 index, uint32 size,
      uint32 shared_memory_id, uint32 shared_memory_offset) {
    cmd::GetStreamDesc& cmd = GetCmdSpace<cmd::GetStreamDesc>();
    cmd.Init(effect_id, index, size, shared_memory_id, shared_memory_offset);
  }

  void DestroyTexture(ResourceId texture_id) {
    cmd::DestroyTexture& cmd = GetCmdSpace<cmd::DestroyTexture>();
    cmd.Init(texture_id);
  }

  void CreateTexture2d(
      ResourceId texture_id,
      uint32 width, uint32 height,
      uint32 levels, texture::Format format,
      bool enable_render_surfaces) {
    cmd::CreateTexture2d& cmd = GetCmdSpace<cmd::CreateTexture2d>();
    cmd.Init(texture_id,
             width, height, levels, format,
             enable_render_surfaces);
  }

  void CreateTexture3d(
      ResourceId texture_id,
      uint32 width, uint32 height, uint32 depth,
      uint32 levels, texture::Format format,
      bool enable_render_surfaces) {
    cmd::CreateTexture3d& cmd = GetCmdSpace<cmd::CreateTexture3d>();
    cmd.Init(texture_id,
             width, height, depth,
             levels, format,
             enable_render_surfaces);
  }

  void CreateTextureCube(
      ResourceId texture_id,
      uint32 edge_length, uint32 levels, texture::Format format,
      bool enable_render_surfaces) {
    cmd::CreateTextureCube& cmd = GetCmdSpace<cmd::CreateTextureCube>();
    cmd.Init(texture_id,
             edge_length, levels, format,
             enable_render_surfaces);
  }

  void SetTextureData(
      ResourceId texture_id,
      uint32 x,
      uint32 y,
      uint32 z,
      uint32 width,
      uint32 height,
      uint32 depth,
      uint32 level,
      texture::Face face,
      uint32 row_pitch,
      uint32 slice_pitch,
      uint32 size,
      uint32 shared_memory_id,
      uint32 shared_memory_offset) {
    cmd::SetTextureData& cmd = GetCmdSpace<cmd::SetTextureData>();
    cmd.Init(
        texture_id,
        x,
        y,
        z,
        width,
        height,
        depth,
        level,
        face,
        row_pitch,
        slice_pitch,
        size,
        shared_memory_id,
        shared_memory_offset);
  }

  void SetTextureDataImmediate(
      ResourceId texture_id,
      uint32 x,
      uint32 y,
      uint32 z,
      uint32 width,
      uint32 height,
      uint32 depth,
      uint32 level,
      texture::Face face,
      uint32 row_pitch,
      uint32 slice_pitch,
      uint32 size,
      const void* data) {
    cmd::SetTextureDataImmediate& cmd =
        GetImmediateCmdSpace<cmd::SetTextureDataImmediate>(size);
    cmd.Init(
        texture_id,
        x,
        y,
        z,
        width,
        height,
        depth,
        level,
        face,
        row_pitch,
        slice_pitch,
        size,
        data);
  }

  void GetTextureData(
      ResourceId texture_id,
      uint32 x,
      uint32 y,
      uint32 z,
      uint32 width,
      uint32 height,
      uint32 depth,
      uint32 level,
      texture::Face face,
      uint32 row_pitch,
      uint32 slice_pitch,
      uint32 size,
      uint32 shared_memory_id,
      uint32 shared_memory_offset) {
    cmd::GetTextureData& cmd = GetCmdSpace<cmd::GetTextureData>();
    cmd.Init(
        texture_id,
        x,
        y,
        z,
        width,
        height,
        depth,
        level,
        face,
        row_pitch,
        slice_pitch,
        size,
        shared_memory_id,
        shared_memory_offset);
  }

  void CreateSampler(ResourceId sampler_id) {
    cmd::CreateSampler& cmd = GetCmdSpace<cmd::CreateSampler>();
    cmd.Init(sampler_id);
  }

  void DestroySampler(ResourceId sampler_id) {
    cmd::DestroySampler& cmd = GetCmdSpace<cmd::DestroySampler>();
    cmd.Init(sampler_id);
  }

  void SetSamplerStates(
      ResourceId sampler_id,
      sampler::AddressingMode address_u_value,
      sampler::AddressingMode address_v_value,
      sampler::AddressingMode address_w_value,
      sampler::FilteringMode mag_filter_value,
      sampler::FilteringMode min_filter_value,
      sampler::FilteringMode mip_filter_value,
      uint8 max_anisotropy) {
    cmd::SetSamplerStates& cmd = GetCmdSpace<cmd::SetSamplerStates>();
    cmd.Init(
        sampler_id,
        address_u_value,
        address_v_value,
        address_w_value,
        mag_filter_value,
        min_filter_value,
        mip_filter_value,
        max_anisotropy);
  }

  void SetSamplerBorderColor(
      ResourceId sampler_id,
      float red, float green, float blue, float alpha) {
    cmd::SetSamplerBorderColor& cmd =
        GetCmdSpace<cmd::SetSamplerBorderColor>();
    cmd.Init(sampler_id, red, green, blue, alpha);
  }

  void SetSamplerTexture(ResourceId sampler_id, ResourceId texture_id) {
    cmd::SetSamplerTexture& cmd = GetCmdSpace<cmd::SetSamplerTexture>();
    cmd.Init(sampler_id, texture_id);
  }

  void SetScissor(
      uint32 x,
      uint32 y,
      uint32 width,
      uint32 height,
      bool enable) {
    cmd::SetScissor& cmd = GetCmdSpace<cmd::SetScissor>();
    cmd.Init(
        x,
        y,
        width,
        height,
        enable);
  }

  void SetPolygonOffset(float slope_factor, float units) {
    cmd::SetPolygonOffset& cmd = GetCmdSpace<cmd::SetPolygonOffset>();
    cmd.Init(slope_factor, units);
  }

  void SetPointLineRaster(
      bool line_smooth_enable, bool point_sprite_enable, float point_size) {
    cmd::SetPointLineRaster& cmd = GetCmdSpace<cmd::SetPointLineRaster>();
    cmd.Init(line_smooth_enable, point_sprite_enable, point_size);
  }

  void SetPolygonRaster(PolygonMode fill_mode, FaceCullMode cull_mode) {
    cmd::SetPolygonRaster& cmd = GetCmdSpace<cmd::SetPolygonRaster>();
    cmd.Init(fill_mode, cull_mode);
  }

  void SetAlphaTest(Comparison func, bool enable, float value) {
    cmd::SetAlphaTest& cmd = GetCmdSpace<cmd::SetAlphaTest>();
    cmd.Init(func, enable, value);
  }

  void SetDepthTest(Comparison func, bool write_enable, bool enable) {
    cmd::SetDepthTest& cmd = GetCmdSpace<cmd::SetDepthTest>();
    cmd.Init(func, write_enable, enable);
  }

  void SetStencilTest(
      uint8 write_mask,
      uint8 compare_mask,
      uint8 reference_value,
      bool separate_ccw,
      bool enable,
      Comparison cw_func,
      StencilOp cw_pass_op,
      StencilOp cw_fail_op,
      StencilOp cw_z_fail_op,
      Comparison ccw_func,
      StencilOp ccw_pass_op,
      StencilOp ccw_fail_op,
      StencilOp ccw_z_fail_op) {
    cmd::SetStencilTest& cmd = GetCmdSpace<cmd::SetStencilTest>();
    cmd.Init(
        write_mask,
        compare_mask,
        reference_value,
        separate_ccw,
        enable,
        cw_func,
        cw_pass_op,
        cw_fail_op,
        cw_z_fail_op,
        ccw_func,
        ccw_pass_op,
        ccw_fail_op,
        ccw_z_fail_op);
  }

  void SetColorWrite(uint8 mask, bool dither_enable) {
    cmd::SetColorWrite& cmd = GetCmdSpace<cmd::SetColorWrite>();
    cmd.Init(mask, dither_enable);
  }

  void SetBlending(
      BlendFunc color_src_func,
      BlendFunc color_dst_func,
      BlendEq color_eq,
      BlendFunc alpha_src_func,
      BlendFunc alpha_dst_func,
      BlendEq alpha_eq,
      bool separate_alpha,
      bool enable) {
    cmd::SetBlending& cmd = GetCmdSpace<cmd::SetBlending>();
    cmd.Init(
        color_src_func,
        color_dst_func,
        color_eq,
        alpha_src_func,
        alpha_dst_func,
        alpha_eq,
        separate_alpha,
        enable);
  }

  void SetBlendingColor(float red, float green, float blue, float alpha) {
    cmd::SetBlendingColor& cmd = GetCmdSpace<cmd::SetBlendingColor>();
    cmd.Init(red, green, blue, alpha);
  }

  void CreateRenderSurface(
      ResourceId render_surface_id, ResourceId texture_id,
      uint32 width, uint32 height,
      uint32 level, uint32 side) {
    cmd::CreateRenderSurface& cmd = GetCmdSpace<cmd::CreateRenderSurface>();
    cmd.Init(render_surface_id, texture_id, width, height, level, side);
  }

  void DestroyRenderSurface(ResourceId render_surface_id) {
    cmd::DestroyRenderSurface& cmd =
        GetCmdSpace<cmd::DestroyRenderSurface>();
    cmd.Init(render_surface_id);
  }

  void CreateDepthSurface(
      ResourceId depth_surface_id, uint32 width, uint32 height) {
    cmd::CreateDepthSurface& cmd = GetCmdSpace<cmd::CreateDepthSurface>();
    cmd.Init(depth_surface_id, width, height);
  }

  void DestroyDepthSurface(ResourceId depth_surface_id) {
    cmd::DestroyDepthSurface& cmd = GetCmdSpace<cmd::DestroyDepthSurface>();
    cmd.Init(depth_surface_id);
  }

  void SetRenderSurface(
      ResourceId render_surface_id, ResourceId depth_surface_id) {
    cmd::SetRenderSurface& cmd = GetCmdSpace<cmd::SetRenderSurface>();
    cmd.Init(render_surface_id, depth_surface_id);
  }

  void SetBackSurfaces() {
    cmd::SetBackSurfaces& cmd = GetCmdSpace<cmd::SetBackSurfaces>();
    cmd.Init();
  }

 private:
  // Waits until get changes, updating the value of get_.
  void WaitForGetChange();

  // Returns the number of available entries (they may not be contiguous).
  unsigned int AvailableEntries() {
    return static_cast<unsigned int>(
        (get_ - put_ - 1 + entry_count_) % entry_count_);
  }

  BufferSyncInterface *interface_;
  CommandBufferEntry *entries_;
  unsigned int entry_count_;
  unsigned int token_;
  unsigned int last_token_read_;
  RPCShmHandle shm_handle_;
  unsigned int shm_id_;
  CommandBufferOffset get_;
  CommandBufferOffset put_;

  friend class CommandBufferHelperTest;
};

}  // namespace command_buffer
}  // namespace o3d

#endif  // O3D_COMMAND_BUFFER_CLIENT_CROSS_CMD_BUFFER_HELPER_H_