summaryrefslogtreecommitdiffstats
path: root/o3d/command_buffer/service/cross/resource.h
blob: dd2fa5234e5d4129745d6374e242f18a02b7cf9c (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
/*
 * 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 definition for resource classes and the resource map.

#ifndef O3D_COMMAND_BUFFER_SERVICE_CROSS_RESOURCE_H_
#define O3D_COMMAND_BUFFER_SERVICE_CROSS_RESOURCE_H_

#include <vector>
#include "base/scoped_ptr.h"
#include "core/cross/types.h"
#include "command_buffer/common/cross/resource.h"

namespace o3d {
namespace command_buffer {

// Base class for resources, just providing a common Destroy function.
class Resource {
 public:
  Resource() {}
  virtual ~Resource() {}
 private:
  DISALLOW_COPY_AND_ASSIGN(Resource);
};

// VertexBuffer class, representing a vertex buffer resource.
class VertexBuffer: public Resource {
 public:
  VertexBuffer(unsigned int size, unsigned int flags)
      : size_(size),
        flags_(flags) {}
  virtual ~VertexBuffer() {}

  // Returns the vertex buffer flags.
  unsigned int flags() const { return flags_; }
  // Sets the vertex buffer flags.
  void set_flags(unsigned int flags) { flags_ = flags; }
  // Returns the vertex buffer size.
  unsigned int size() const { return size_; }
  // Sets the vertex buffer size.
  void set_size(unsigned int size) { size_ = size; }
 protected:
  unsigned int size_;
  unsigned int flags_;
 private:
  DISALLOW_COPY_AND_ASSIGN(VertexBuffer);
};

// IndexBuffer class, representing an index buffer resource.
class IndexBuffer: public Resource {
 public:
  IndexBuffer(unsigned int size, unsigned int flags)
      : size_(size),
        flags_(flags) {}
  virtual ~IndexBuffer() {}

  // Returns the index buffer flags.
  unsigned int flags() const { return flags_; }
  // Sets the index buffer flags.
  void set_flags(unsigned int flags) { flags_ = flags; }
  // Returns the index buffer size.
  unsigned int size() const { return size_; }
  // Sets the index buffer size.
  void set_size(unsigned int size) { size_ = size; }
 protected:
  unsigned int size_;
  unsigned int flags_;
 private:
  DISALLOW_COPY_AND_ASSIGN(IndexBuffer);
};

// VertexStruct class, representing a vertex struct resource.
class VertexStruct: public Resource {
 public:
  // The representation of an input data stream.
  struct Element {
    ResourceID vertex_buffer;
    unsigned int offset;
    unsigned int stride;
    vertex_struct::Type type;
    vertex_struct::Semantic semantic;
    unsigned int semantic_index;
  };

  explicit VertexStruct(unsigned int count)
      : count_(count),
        elements_(new Element[count]) {
    memset(elements_.get(), 0, count * sizeof(Element));  // NOLINT
  }

  // Returns the number of inputs in this struct.
  unsigned int count() const { return count_; }
  // Returns an element by index.
  Element &GetElement(unsigned int i) {
    DCHECK_GT(count_, i);
    return elements_[i];
  }
 protected:
  unsigned int count_;
  scoped_array<Element> elements_;
 private:
  DISALLOW_COPY_AND_ASSIGN(VertexStruct);
};

// Effect class, representing an effect.
class Effect: public Resource {
 public:
  Effect() {}
 private:
  DISALLOW_COPY_AND_ASSIGN(Effect);
};

// EffectParam class, representing an effect parameter.
class EffectParam: public Resource {
 public:
  explicit EffectParam(effect_param::DataType data_type)
      : data_type_(data_type) {
  }

  // Gets the data type of this parameter.
  effect_param::DataType data_type() const { return data_type_; }
 private:
  effect_param::DataType data_type_;
  DISALLOW_COPY_AND_ASSIGN(EffectParam);
};

// Texture class, representing a texture resource.
class Texture: public Resource {
 public:
  Texture(texture::Type type,
          unsigned int levels,
          texture::Format format,
          bool enable_render_surfaces,
          unsigned int flags)
      : type_(type),
        levels_(levels),
        format_(format),
        render_surfaces_enabled_(enable_render_surfaces),
        flags_(flags) {}
  virtual ~Texture() {}

  // Returns the type of the texture.
  texture::Type type() const { return type_; }
  // Returns the texture flags.
  unsigned int flags() const { return flags_; }
  // Returns the texture format.
  texture::Format format() const { return format_; }
  // Returns whether the texture supports render surfaces
  bool render_surfaces_enabled() const { return render_surfaces_enabled_; }
  // Returns the number of mipmap levels in the texture.
  unsigned int levels() const { return levels_; }
 private:
  texture::Type type_;
  unsigned int levels_;
  texture::Format format_;
  bool render_surfaces_enabled_;
  unsigned int flags_;
  DISALLOW_COPY_AND_ASSIGN(Texture);
};

// RenderSurface class, representing a render surface/target
class RenderSurface: public Resource {
 public:
  RenderSurface() {}
 private:
  DISALLOW_COPY_AND_ASSIGN(RenderSurface);
};

// RenderSurface class, representing a render surface/target
class RenderDepthStencilSurface: public Resource {
 public:
  RenderDepthStencilSurface() {}
 private:
  DISALLOW_COPY_AND_ASSIGN(RenderDepthStencilSurface);
};


// Texture class, representing a sampler resource.
class Sampler: public Resource {
 public:
  Sampler() {}
 private:
  DISALLOW_COPY_AND_ASSIGN(Sampler);
};

// Base of ResourceMap. Contains most of the implementation of ResourceMap, to
// avoid template bloat.
class ResourceMapBase {
 public:
  ResourceMapBase() : resources_() {}
  ~ResourceMapBase() {}

  // Assigns a resource to a resource ID. Assigning a resource to an ID that
  // already has an existing resource will destroy that existing resource. The
  // map takes ownership of the resource.
  void Assign(ResourceID id, Resource* resource);
  // Destroys a resource.
  bool Destroy(ResourceID id);
  // Destroy all resources.
  void DestroyAllResources();
  // Gets a resource by ID.
  Resource *Get(ResourceID id) {
    return (id < resources_.size()) ? resources_[id] : NULL;
  }
 private:
  typedef std::vector<Resource *> Container;
  Container resources_;
};

// Resource Map class, allowing resource ID <-> Resource association. This is a
// dense map, optimized for retrieval (O(1)).
template<class T> class ResourceMap {
 public:
  ResourceMap() : container_() {}
  ~ResourceMap() {}

  // Assigns a resource to a resource ID. Assigning a resource to an ID that
  // already has an existing resource will destroy that existing resource. The
  // map takes ownership of the resource.
  void Assign(ResourceID id, T* resource) {
    container_.Assign(id, resource);
  }
  // Destroys a resource.
  bool Destroy(ResourceID id) {
    return container_.Destroy(id);
  }
  // Destroy all resources.
  void DestroyAllResources() {
    return container_.DestroyAllResources();
  }
  // Gets a resource by ID.
  T *Get(ResourceID id) {
    return down_cast<T*>(container_.Get(id));
  }
 private:
  ResourceMapBase container_;
};

}  // namespace command_buffer
}  // namespace o3d

#endif  // O3D_COMMAND_BUFFER_SERVICE_CROSS_RESOURCE_H_