OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 num_rows = (num_rows + 3) / 4; | 113 num_rows = (num_rows + 3) / 4; |
114 } | 114 } |
115 | 115 |
116 for (unsigned yy = 0; yy < num_rows; ++yy) { | 116 for (unsigned yy = 0; yy < num_rows; ++yy) { |
117 memcpy(dst, src, bytes_per_row); | 117 memcpy(dst, src, bytes_per_row); |
118 src += src_pitch; | 118 src += src_pitch; |
119 dst += dst_pitch; | 119 dst += dst_pitch; |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 // Sends the SET_TEXTURE_DATA command after formatting the args properly. | 123 // Sends the SetTextureData command after formatting the args properly. |
124 void SetTextureData(RendererCB *renderer, | 124 void SetTextureData(RendererCB *renderer, |
125 ResourceID texture_id, | 125 ResourceID texture_id, |
126 unsigned int x, | 126 unsigned int x, |
127 unsigned int y, | 127 unsigned int y, |
128 unsigned int mip_width, | 128 unsigned int mip_width, |
129 unsigned int mip_height, | 129 unsigned int mip_height, |
130 unsigned int z, | 130 unsigned int z, |
131 unsigned int depth, | 131 unsigned int depth, |
132 unsigned int level, | 132 unsigned int level, |
133 TextureCUBE::CubeFace face, | 133 TextureCUBE::CubeFace face, |
134 int pitch, | 134 int pitch, |
135 size_t mip_size, | 135 size_t mip_size, |
136 unsigned char* mip_data) { | 136 unsigned char* mip_data) { |
137 FencedAllocatorWrapper *allocator = renderer->allocator(); | 137 FencedAllocatorWrapper *allocator = renderer->allocator(); |
138 CommandBufferHelper *helper = renderer->helper(); | 138 CommandBufferHelper *helper = renderer->helper(); |
139 | 139 helper->SetTextureData( |
140 CommandBufferEntry args[10]; | 140 texture_id, |
141 args[0].value_uint32 = texture_id; | 141 x, y, z, |
142 args[1].value_uint32 = | 142 mip_width, mip_height, depth, |
143 set_texture_data_cmd::X::MakeValue(x) | | 143 level, face, |
144 set_texture_data_cmd::Y::MakeValue(y); | 144 pitch, |
145 args[2].value_uint32 = | 145 0, // slice_pitch |
146 set_texture_data_cmd::Width::MakeValue(mip_width) | | 146 mip_size, |
147 set_texture_data_cmd::Height::MakeValue(mip_height); | 147 renderer->transfer_shm_id(), |
148 args[3].value_uint32 = | 148 allocator->GetOffset(mip_data)); |
149 set_texture_data_cmd::Z::MakeValue(z) | | |
150 set_texture_data_cmd::Depth::MakeValue(depth); | |
151 args[4].value_uint32 = | |
152 set_texture_data_cmd::Level::MakeValue(level) | | |
153 set_texture_data_cmd::Face::MakeValue(face); | |
154 args[5].value_uint32 = pitch; | |
155 args[6].value_uint32 = 0; // slice_pitch | |
156 args[7].value_uint32 = mip_size; | |
157 args[8].value_uint32 = renderer->transfer_shm_id(); | |
158 args[9].value_uint32 = allocator->GetOffset(mip_data); | |
159 helper->AddCommand(command_buffer::SET_TEXTURE_DATA, 10, args); | |
160 allocator->FreePendingToken(mip_data, helper->InsertToken()); | 149 allocator->FreePendingToken(mip_data, helper->InsertToken()); |
161 } | 150 } |
162 // Updates a command buffer texture resource from a bitmap, rescaling if | 151 // Updates a command buffer texture resource from a bitmap, rescaling if |
163 // necessary. | 152 // necessary. |
164 void UpdateResourceFromBitmap(RendererCB *renderer, | 153 void UpdateResourceFromBitmap(RendererCB *renderer, |
165 ResourceID texture_id, | 154 ResourceID texture_id, |
166 unsigned int level, | 155 unsigned int level, |
167 TextureCUBE::CubeFace face, | 156 TextureCUBE::CubeFace face, |
168 const Bitmap &bitmap) { | 157 const Bitmap &bitmap) { |
169 DCHECK(bitmap.image_data()); | 158 DCHECK(bitmap.image_data()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 CommandBufferHelper *helper = renderer->helper(); | 195 CommandBufferHelper *helper = renderer->helper(); |
207 unsigned int mip_width = std::max(1U, bitmap.width() >> level); | 196 unsigned int mip_width = std::max(1U, bitmap.width() >> level); |
208 unsigned int mip_height = std::max(1U, bitmap.height() >> level); | 197 unsigned int mip_height = std::max(1U, bitmap.height() >> level); |
209 size_t mip_size = | 198 size_t mip_size = |
210 image::ComputeBufferSize(mip_width, mip_height, bitmap.format()); | 199 image::ComputeBufferSize(mip_width, mip_height, bitmap.format()); |
211 unsigned char *buffer = allocator->AllocTyped<unsigned char>(mip_size); | 200 unsigned char *buffer = allocator->AllocTyped<unsigned char>(mip_size); |
212 DCHECK(buffer); | 201 DCHECK(buffer); |
213 | 202 |
214 size_t pitch = image::ComputeBufferSize(mip_width, 1, bitmap.format()); | 203 size_t pitch = image::ComputeBufferSize(mip_width, 1, bitmap.format()); |
215 | 204 |
216 CommandBufferEntry args[10]; | 205 helper->GetTextureData( |
217 args[0].value_uint32 = texture_id; | 206 texture_id, |
218 args[1].value_uint32 = | 207 0, |
219 get_texture_data_cmd::X::MakeValue(0) | | 208 0, |
220 get_texture_data_cmd::Y::MakeValue(0); | 209 0, |
221 args[2].value_uint32 = | 210 mip_width, |
222 get_texture_data_cmd::Width::MakeValue(mip_width) | | 211 mip_height, |
223 get_texture_data_cmd::Height::MakeValue(mip_height); | 212 1, |
224 args[3].value_uint32 = | 213 level, |
225 get_texture_data_cmd::Z::MakeValue(0) | | 214 face, |
226 get_texture_data_cmd::Depth::MakeValue(1); | 215 pitch, |
227 args[4].value_uint32 = | 216 0, |
228 get_texture_data_cmd::Level::MakeValue(level) | | 217 mip_size, |
229 get_texture_data_cmd::Face::MakeValue(face); | 218 renderer->transfer_shm_id(), |
230 args[5].value_uint32 = pitch; | 219 allocator->GetOffset(buffer)); |
231 args[6].value_uint32 = 0; // slice_pitch | |
232 args[7].value_uint32 = mip_size; | |
233 args[8].value_uint32 = renderer->transfer_shm_id(); | |
234 args[9].value_uint32 = allocator->GetOffset(buffer); | |
235 helper->AddCommand(command_buffer::GET_TEXTURE_DATA, 10, args); | |
236 helper->Finish(); | 220 helper->Finish(); |
237 memcpy(bitmap.GetMipData(level), buffer, mip_size); | 221 memcpy(bitmap.GetMipData(level), buffer, mip_size); |
238 allocator->Free(buffer); | 222 allocator->Free(buffer); |
239 } | 223 } |
240 | 224 |
241 } // anonymous namespace | 225 } // anonymous namespace |
242 | 226 |
243 static const unsigned int kMaxTextureSize = 2048; | 227 static const unsigned int kMaxTextureSize = 2048; |
244 | 228 |
245 // Texture2DCB ----------------------------------------------------------------- | 229 // Texture2DCB ----------------------------------------------------------------- |
(...skipping 19 matching lines...) Expand all Loading... |
265 service_locator->GetService<Renderer>())), | 249 service_locator->GetService<Renderer>())), |
266 resource_id_(resource_id), | 250 resource_id_(resource_id), |
267 backing_bitmap_(Bitmap::Ref(new Bitmap(service_locator))), | 251 backing_bitmap_(Bitmap::Ref(new Bitmap(service_locator))), |
268 has_levels_(0), | 252 has_levels_(0), |
269 locked_levels_(0) { | 253 locked_levels_(0) { |
270 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); | 254 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); |
271 } | 255 } |
272 | 256 |
273 Texture2DCB::~Texture2DCB() { | 257 Texture2DCB::~Texture2DCB() { |
274 if (resource_id_ != command_buffer::kInvalidResource) { | 258 if (resource_id_ != command_buffer::kInvalidResource) { |
275 CommandBufferEntry args[1]; | 259 renderer_->helper()->DestroyTexture(resource_id_); |
276 args[0].value_uint32 = resource_id_; | |
277 renderer_->helper()->AddCommand(command_buffer::DESTROY_TEXTURE, 1, args); | |
278 } | 260 } |
279 } | 261 } |
280 | 262 |
281 // Creates a new texture object from scratch. | 263 // Creates a new texture object from scratch. |
282 Texture2DCB* Texture2DCB::Create(ServiceLocator* service_locator, | 264 Texture2DCB* Texture2DCB::Create(ServiceLocator* service_locator, |
283 Texture::Format format, | 265 Texture::Format format, |
284 int levels, | 266 int levels, |
285 int width, | 267 int width, |
286 int height, | 268 int height, |
287 bool enable_render_surfaces) { | 269 bool enable_render_surfaces) { |
288 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); | 270 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); |
289 RendererCB *renderer = static_cast<RendererCB *>( | 271 RendererCB *renderer = static_cast<RendererCB *>( |
290 service_locator->GetService<Renderer>()); | 272 service_locator->GetService<Renderer>()); |
291 texture::Format cb_format = CBFormatFromO3DFormat(format); | 273 texture::Format cb_format = CBFormatFromO3DFormat(format); |
292 if (cb_format == texture::NUM_FORMATS) { | 274 if (cb_format == texture::NUM_FORMATS) { |
293 O3D_ERROR(service_locator) | 275 O3D_ERROR(service_locator) |
294 << "Unsupported format in Texture2DCB::Create."; | 276 << "Unsupported format in Texture2DCB::Create."; |
295 return NULL; | 277 return NULL; |
296 } | 278 } |
297 if (width > kMaxTextureSize || height > kMaxTextureSize) { | 279 if (width > kMaxTextureSize || height > kMaxTextureSize) { |
298 O3D_ERROR(service_locator) << "Texture dimensions (" << width | 280 O3D_ERROR(service_locator) << "Texture dimensions (" << width |
299 << ", " << height << ") too big."; | 281 << ", " << height << ") too big."; |
300 return NULL; | 282 return NULL; |
301 } | 283 } |
302 | 284 |
303 ResourceID texture_id = renderer->texture_ids().AllocateID(); | 285 ResourceID texture_id = renderer->texture_ids().AllocateID(); |
304 CommandBufferEntry args[3]; | 286 renderer->helper()->CreateTexture2d( |
305 args[0].value_uint32 = texture_id; | 287 texture_id, |
306 args[1].value_uint32 = | 288 width, height, |
307 create_texture_2d_cmd::Width::MakeValue(width) | | 289 levels, cb_format, enable_render_surfaces); |
308 create_texture_2d_cmd::Height::MakeValue(height); | |
309 args[2].value_uint32 = | |
310 create_texture_2d_cmd::Levels::MakeValue(levels) | | |
311 create_texture_2d_cmd::Format::MakeValue(cb_format) | | |
312 create_texture_2d_cmd::Flags::MakeValue(enable_render_surfaces); | |
313 renderer->helper()->AddCommand(command_buffer::CREATE_TEXTURE_2D, 3, args); | |
314 | 290 |
315 Texture2DCB *texture = new Texture2DCB(service_locator, texture_id, | 291 Texture2DCB *texture = new Texture2DCB(service_locator, texture_id, |
316 format, levels, width, height, | 292 format, levels, width, height, |
317 enable_render_surfaces); | 293 enable_render_surfaces); |
318 | 294 |
319 return texture; | 295 return texture; |
320 } | 296 } |
321 | 297 |
322 void Texture2DCB::SetRect(int level, | 298 void Texture2DCB::SetRect(int level, |
323 unsigned dst_left, | 299 unsigned dst_left, |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 resource_id_(resource_id) { | 465 resource_id_(resource_id) { |
490 for (int ii = 0; ii < static_cast<int>(NUMBER_OF_FACES); ++ii) { | 466 for (int ii = 0; ii < static_cast<int>(NUMBER_OF_FACES); ++ii) { |
491 backing_bitmaps_[ii] = Bitmap::Ref(new Bitmap(service_locator)); | 467 backing_bitmaps_[ii] = Bitmap::Ref(new Bitmap(service_locator)); |
492 has_levels_[ii] = 0; | 468 has_levels_[ii] = 0; |
493 locked_levels_[ii] = 0; | 469 locked_levels_[ii] = 0; |
494 } | 470 } |
495 } | 471 } |
496 | 472 |
497 TextureCUBECB::~TextureCUBECB() { | 473 TextureCUBECB::~TextureCUBECB() { |
498 if (resource_id_ != command_buffer::kInvalidResource) { | 474 if (resource_id_ != command_buffer::kInvalidResource) { |
499 CommandBufferEntry args[1]; | 475 renderer_->helper()->DestroyTexture(resource_id_); |
500 args[0].value_uint32 = resource_id_; | |
501 renderer_->helper()->AddCommand(command_buffer::DESTROY_TEXTURE, 1, args); | |
502 } | 476 } |
503 } | 477 } |
504 | 478 |
505 // Create a new Cube texture from scratch. | 479 // Create a new Cube texture from scratch. |
506 TextureCUBECB* TextureCUBECB::Create(ServiceLocator* service_locator, | 480 TextureCUBECB* TextureCUBECB::Create(ServiceLocator* service_locator, |
507 Texture::Format format, | 481 Texture::Format format, |
508 int levels, | 482 int levels, |
509 int edge_length, | 483 int edge_length, |
510 bool enable_render_surfaces) { | 484 bool enable_render_surfaces) { |
511 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); | 485 DCHECK_NE(format, Texture::UNKNOWN_FORMAT); |
512 RendererCB *renderer = static_cast<RendererCB *>( | 486 RendererCB *renderer = static_cast<RendererCB *>( |
513 service_locator->GetService<Renderer>()); | 487 service_locator->GetService<Renderer>()); |
514 texture::Format cb_format = CBFormatFromO3DFormat(format); | 488 texture::Format cb_format = CBFormatFromO3DFormat(format); |
515 if (cb_format == texture::NUM_FORMATS) { | 489 if (cb_format == texture::NUM_FORMATS) { |
516 O3D_ERROR(service_locator) | 490 O3D_ERROR(service_locator) |
517 << "Unsupported format in Texture2DCB::Create."; | 491 << "Unsupported format in Texture2DCB::Create."; |
518 return NULL; | 492 return NULL; |
519 } | 493 } |
520 if (edge_length > kMaxTextureSize) { | 494 if (edge_length > kMaxTextureSize) { |
521 O3D_ERROR(service_locator) << "Texture dimensions (" << edge_length | 495 O3D_ERROR(service_locator) << "Texture dimensions (" << edge_length |
522 << ", " << edge_length << ") too big."; | 496 << ", " << edge_length << ") too big."; |
523 return NULL; | 497 return NULL; |
524 } | 498 } |
525 | 499 |
526 ResourceID texture_id = renderer->texture_ids().AllocateID(); | 500 ResourceID texture_id = renderer->texture_ids().AllocateID(); |
527 CommandBufferEntry args[3]; | 501 renderer->helper()->CreateTextureCube( |
528 args[0].value_uint32 = texture_id; | 502 texture_id, |
529 args[1].value_uint32 = create_texture_cube_cmd::Side::MakeValue(edge_length); | 503 edge_length, |
530 args[2].value_uint32 = | 504 levels, cb_format, enable_render_surfaces); |
531 create_texture_cube_cmd::Levels::MakeValue(levels) | | |
532 create_texture_cube_cmd::Format::MakeValue(cb_format) | | |
533 create_texture_cube_cmd::Flags::MakeValue(enable_render_surfaces); | |
534 renderer->helper()->AddCommand(command_buffer::CREATE_TEXTURE_CUBE, 3, args); | |
535 | 505 |
536 TextureCUBECB* texture = | 506 TextureCUBECB* texture = |
537 new TextureCUBECB(service_locator, texture_id, format, levels, | 507 new TextureCUBECB(service_locator, texture_id, format, levels, |
538 edge_length, enable_render_surfaces); | 508 edge_length, enable_render_surfaces); |
539 | 509 |
540 return texture; | 510 return texture; |
541 } | 511 } |
542 | 512 |
543 void TextureCUBECB::SetRect(TextureCUBE::CubeFace face, | 513 void TextureCUBECB::SetRect(TextureCUBE::CubeFace face, |
544 int level, | 514 int level, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 face, | 668 face, |
699 this, | 669 this, |
700 renderer_)); | 670 renderer_)); |
701 } | 671 } |
702 | 672 |
703 const Texture::RGBASwizzleIndices& TextureCUBECB::GetABGR32FSwizzleIndices() { | 673 const Texture::RGBASwizzleIndices& TextureCUBECB::GetABGR32FSwizzleIndices() { |
704 return g_cb_abgr32f_swizzle_indices; | 674 return g_cb_abgr32f_swizzle_indices; |
705 } | 675 } |
706 | 676 |
707 } // namespace o3d | 677 } // namespace o3d |
OLD | NEW |