00001
00002
00003
00004
00005
00006
00013 #ifndef TK11_BUFFER_HPP
00014 #define TK11_BUFFER_HPP
00015
00016 #include "d3d.hpp"
00017
00018 #include <boost/noncopyable.hpp>
00019
00020 namespace tk11 {
00021
00023
00037 ID3D11Buffer_ptr create_buffer(ID3D11Device_ptr device, const D3D11_BUFFER_DESC& desc, const D3D11_SUBRESOURCE_DATA& init_data);
00038
00040
00053 ID3D11Buffer_ptr create_buffer(ID3D11Device_ptr device, const D3D11_BUFFER_DESC& desc);
00054
00056
00071 template<typename T>
00072 ID3D11Buffer_ptr create_typed_buffer(ID3D11Device_ptr device, const T* data,
00073 std::size_t size, UINT bind_flags, D3D11_USAGE usage = D3D11_USAGE_DEFAULT)
00074 {
00075
00076 D3D11_BUFFER_DESC desc;
00077 desc.Usage = usage;
00078 desc.ByteWidth = static_cast<UINT>(sizeof(T) * size);
00079 desc.BindFlags = bind_flags;
00080 desc.CPUAccessFlags = 0;
00081 desc.MiscFlags = 0;
00082
00083
00084 D3D11_SUBRESOURCE_DATA init_data;
00085 init_data.pSysMem = data;
00086 init_data.SysMemPitch = 0;
00087 init_data.SysMemSlicePitch = 0;
00088
00089 return create_buffer(device, desc, init_data);
00090 }
00091
00093
00107 template<typename Vertex>
00108 ID3D11Buffer_ptr create_vertex_buffer(ID3D11Device_ptr device, const Vertex* data, std::size_t size, D3D11_USAGE usage = D3D11_USAGE_DEFAULT) {
00109 return create_typed_buffer(device, data, size, D3D11_BIND_VERTEX_BUFFER, usage);
00110 }
00111
00113
00127 template<typename Index>
00128 ID3D11Buffer_ptr create_index_buffer(ID3D11Device_ptr device,
00129 const Index* data, std::size_t size,
00130 D3D11_USAGE usage = D3D11_USAGE_DEFAULT)
00131 {
00132 return create_typed_buffer(device, data, size, D3D11_BIND_INDEX_BUFFER, usage);
00133 }
00134
00136
00151 ID3D11Buffer_ptr create_constant_buffer(ID3D11Device_ptr device,
00152 std::size_t size, D3D11_USAGE usage = D3D11_USAGE_DYNAMIC,
00153 UINT cpu_access_flags = D3D11_CPU_ACCESS_WRITE);
00154
00156
00175 ID3D11Buffer_ptr create_constant_buffer(ID3D11Device_ptr device,
00176 const void* data, std::size_t size,
00177 D3D11_USAGE usage = D3D11_USAGE_IMMUTABLE,
00178 UINT cpu_access_flags = 0);
00179
00181
00196 template<typename T>
00197 ID3D11Buffer_ptr create_typed_constant_buffer(ID3D11Device_ptr device,
00198 D3D11_USAGE usage = D3D11_USAGE_DYNAMIC,
00199 UINT cpu_access_flags = D3D11_CPU_ACCESS_WRITE)
00200 {
00201 return create_constant_buffer(device, sizeof(T), usage, cpu_access_flags);
00202 }
00203
00205
00224 template<typename T>
00225 ID3D11Buffer_ptr create_typed_constant_buffer(ID3D11Device_ptr device,
00226 const T& data, D3D11_USAGE usage = D3D11_USAGE_IMMUTABLE,
00227 UINT cpu_access_flags = 0)
00228 {
00229 return create_constant_buffer(device, &data, sizeof(T), usage, cpu_access_flags);
00230 }
00231
00232
00234
00242 class Mapped_Subresource
00243 : public D3D11_MAPPED_SUBRESOURCE
00244 , public boost::noncopyable
00245 {
00246 public:
00248
00257 Mapped_Subresource(ID3D11DeviceContext_ptr context,
00258 ID3D11Resource_ptr resource, UINT subresource = 0,
00259 D3D11_MAP type = D3D11_MAP_WRITE_DISCARD);
00260
00262 ~Mapped_Subresource();
00263
00265
00271 void map(D3D11_MAP type = D3D11_MAP_WRITE_DISCARD);
00272
00274 void unmap();
00275
00276
00277 #if 0
00278
00279 enum Non_Blocking { non_blocking };
00280
00282
00291 Mapped_Subresource(Non_Blocking tag, ID3D11DeviceContext_ptr context,
00292 ID3D11Resource_ptr resource, UINT subresource);
00293
00295
00303 bool try_map(D3D11_MAP type = D3D11_MAP_WRITE_DISCARD);
00304 #endif
00305
00306 private:
00308 ID3D11DeviceContext_ptr context;
00309
00311 ID3D11Resource_ptr resource;
00312
00314 UINT subresource;
00315
00317 bool mapped;
00318 };
00319
00320
00322
00333 void update_constant_buffer(ID3D11DeviceContext_ptr context,
00334 ID3D11Buffer_ptr buffer, const void* data, std::size_t size);
00335
00337
00347 template<typename T>
00348 void typed_update_constant_buffer(ID3D11DeviceContext_ptr context, ID3D11Buffer_ptr buffer, const T& data) {
00349
00350 Mapped_Subresource mapped_subresource(context, buffer);
00351
00352
00353 *reinterpret_cast<T*>(mapped_subresource.pData) = data;
00354 }
00355
00356 }
00357
00358 #endif