diff options
Diffstat (limited to 'o3d/command_buffer/samples/bubble/bubble_module.cc')
-rw-r--r-- | o3d/command_buffer/samples/bubble/bubble_module.cc | 590 |
1 files changed, 218 insertions, 372 deletions
diff --git a/o3d/command_buffer/samples/bubble/bubble_module.cc b/o3d/command_buffer/samples/bubble/bubble_module.cc index 5d5a2f7..cec8109 100644 --- a/o3d/command_buffer/samples/bubble/bubble_module.cc +++ b/o3d/command_buffer/samples/bubble/bubble_module.cc @@ -34,23 +34,24 @@ // plug-in. #include <math.h> -#include <stdint.h> -#ifdef __native_demo__ +#ifdef __native_client__ #include <sys/nacl_syscalls.h> #else -#include "native_client/src/shared/imc/nacl_imc.h" +#include "third_party/native_client/googleclient/native_client/src/trusted/desc/nrd_all_modules.h" #endif #include "command_buffer/common/cross/gapi_interface.h" #include "command_buffer/common/cross/rpc_imc.h" +#include "command_buffer/client/cross/fenced_allocator.h" #include "command_buffer/client/cross/cmd_buffer_helper.h" #include "command_buffer/client/cross/buffer_sync_proxy.h" -#include "command_buffer/client/cross/fenced_allocator.h" #include "command_buffer/samples/bubble/utils.h" #include "command_buffer/samples/bubble/iridescence_texture.h" #include "command_buffer/samples/bubble/perlin_noise.h" #include "third_party/vectormath/files/vectormathlibrary/include/vectormath/scalar/cpp/vectormath_aos.h" // NOLINT -#include "native_client/src/shared/npruntime/nacl_npapi.h" +#if OS_WIN + typedef unsigned __int64 uint64_t; +#endif // Cube map data is hard-coded in cubemap.cc as a byte array. // Format is 64x64xBRGA, D3D face ordering (+X, -X, +Y, -Y, +Z, -Z). @@ -67,7 +68,7 @@ const unsigned int kCubeMapFaceSize = kCubeMapWidth * kCubeMapHeight * 4; HELPER->Finish(); \ BufferSyncInterface::ParseError error = \ HELPER->interface()->GetParseError(); \ - if (error != BufferSyncInterface::kParseNoError) { \ + if (error != BufferSyncInterface::kParseNoError) { \ printf("CMD error %d at %s:%d\n", error, __FILE__, __LINE__); \ } \ } while (false) @@ -100,40 +101,6 @@ namespace command_buffer { namespace math = Vectormath::Aos; -// Adds a Clear command into the command buffer. -// Parameters: -// cmd_buffer: the command buffer helper. -// buffers: a bitfield of which buffers to clear (a combination of -// GAPIInterface::COLOR, GAPIInterface::DEPTH and GAPIInterface::STENCIL). -// color: the color buffer clear value. -// depth: the depth buffer clear value. -// stencil: the stencil buffer clear value. -void ClearCmd(CommandBufferHelper *cmd_buffer, - const unsigned int buffers, - const RGBA &color, - float depth, - unsigned int stencil) { - cmd_buffer->Clear(buffers, color.red, color.green, color.blue, color.alpha, - depth, stencil); - CHECK_ERROR(cmd_buffer); -} - -// Adds a SetViewport command into the buffer. -// Parameters: -// cmd_buffer: the command buffer helper. -// x, y, width, height: the dimensions of the Viewport. -// z_near, z_far: the near and far clip plane distances. -void SetViewportCmd(CommandBufferHelper *cmd_buffer, - unsigned int x, - unsigned int y, - unsigned int width, - unsigned int height, - float z_near, - float z_far) { - cmd_buffer->SetViewport(x, y, width, height, z_near, z_far); - CHECK_ERROR(cmd_buffer); -} - // Copy a data buffer to args, for IMMEDIATE commands. Returns the number of // args used. unsigned int CopyToArgs(CommandBufferEntry *args, @@ -294,14 +261,6 @@ void MakeNoiseTexture(unsigned int width, } } -// Gets current time in microseconds. -uint64_t GetTimeUsec() { - return 0ULL; -// struct timeval tv; -// gettimeofday(&tv, NULL); -// return tv.tv_sec * 1000000ULL + tv.tv_usec; -} - class BubbleDemo { public: BubbleDemo(); @@ -310,6 +269,8 @@ class BubbleDemo { // Creates the socket pair for the connection. nacl::HtpHandle CreateSockets(); + // Sets a handle. + void SetHandle(int index, nacl::HtpHandle handle); // Initializes the demo once connected. void Initialize(); // Finalizes the demo. @@ -375,7 +336,6 @@ class BubbleDemo { unsigned char *noise_texture_; unsigned char *iridescence_texture_; unsigned int seed_; - uint64_t start_time_; float time_; std::vector<Bubble> bubbles_; }; @@ -406,13 +366,18 @@ BubbleDemo::BubbleDemo() indices_(NULL), noise_texture_(NULL), iridescence_texture_(NULL), - seed_(GetTimeUsec()), - start_time_(0), + seed_(0x12345678u), // pick some non-zero seed. time_(0.f) { handle_pair_[0] = nacl::kInvalidHtpHandle; handle_pair_[1] = nacl::kInvalidHtpHandle; } +void BubbleDemo::SetHandle(int index, nacl::HtpHandle handle) { + DCHECK_GE(index, 0); + DCHECK_LT(index, arraysize(handle_pair_)); + handle_pair_[index] = handle; +} + nacl::HtpHandle BubbleDemo::CreateSockets() { nacl::Handle socket_pair[2]; if (nacl::SocketPair(socket_pair) < 0) { @@ -466,356 +431,260 @@ void BubbleDemo::Initialize() { // Clear the buffers. RGBA color = {0.f, 0.f, 0.f, 1.f}; - ClearCmd(helper_.get(), GAPIInterface::COLOR | GAPIInterface::DEPTH, color, - 1.f, 0); + helper_->Clear(command_buffer::kColor | command_buffer::kDepth, + color.red, color.green, color.blue, color.alpha, + 1.f, 0); // Create geometry arrays and structures - args[0].value_uint32 = vertex_buffer_id_; - args[1].value_uint32 = kVertexBufferSize; // size - args[2].value_uint32 = 0; // flags - helper_->AddCommand(CreateVertexBuffer, 3, args); + helper_->CreateVertexBuffer(vertex_buffer_id_, kVertexBufferSize, + vertex_buffer::kNone); CHECK_ERROR(helper_); - args[0].value_uint32 = vertex_buffer_id_; - args[1].value_uint32 = 0; // offset in VB - args[2].value_uint32 = kVertexBufferSize; // size - args[3].value_uint32 = shm_id_; // shm - args[4].value_uint32 = allocator_->GetOffset(vertices_); // offset in shm - helper_->AddCommand(SetVertexBufferData, 5, args); + helper_->SetVertexBufferData(vertex_buffer_id_, 0, kVertexBufferSize, + shm_id_, allocator_->GetOffset(vertices_)); CHECK_ERROR(helper_); - args[0].value_uint32 = index_buffer_id_; - args[1].value_uint32 = kIndexBufferSize; // size - args[2].value_uint32 = index_buffer::INDEX_32BIT; // flags - helper_->AddCommand(CreateIndexBuffer, 3, args); + helper_->CreateIndexBuffer(index_buffer_id_, kIndexBufferSize, + index_buffer::kIndex32Bit); CHECK_ERROR(helper_); - args[0].value_uint32 = index_buffer_id_; - args[1].value_uint32 = 0; // offset in IB - args[2].value_uint32 = kIndexBufferSize; // size - args[3].value_uint32 = shm_id_; // shm - args[4].value_uint32 = allocator_->GetOffset(indices_); // offset in shm - helper_->AddCommand(SetIndexBufferData, 5, args); + helper_->SetIndexBufferData(index_buffer_id_, 0, kIndexBufferSize, + shm_id_, allocator_->GetOffset(indices_)); CHECK_ERROR(helper_); - args[0].value_uint32 = vertex_struct_id_; - args[1].value_uint32 = 3; // input count - helper_->AddCommand(CreateVertexStruct, 2, args); + helper_->CreateVertexStruct(vertex_struct_id_, 3); CHECK_ERROR(helper_); // Set POSITION input stream - args[0].value_uint32 = vertex_struct_id_; - args[1].value_uint32 = 0; // input - args[2].value_uint32 = vertex_buffer_id_; // buffer - args[3].value_uint32 = 0; // offset - args[4].value_uint32 = - set_vertex_input_cmd::Stride::MakeValue(sizeof(CustomVertex)) | - set_vertex_input_cmd::Type::MakeValue(vertex_struct::FLOAT3) | - set_vertex_input_cmd::Semantic::MakeValue(vertex_struct::POSITION) | - set_vertex_input_cmd::SemanticIndex::MakeValue(0); - helper_->AddCommand(SetVertexInput, 5, args); + helper_->SetVertexInput(vertex_struct_id_, 0, + vertex_buffer_id_, offsetof(CustomVertex, x), + vertex_struct::kPosition, 0, + vertex_struct::kFloat3, sizeof(CustomVertex)); CHECK_ERROR(helper_); // Set NORMAL input stream - args[1].value_uint32 = 1; // input - args[3].value_uint32 = 12; // offset - args[4].value_uint32 = - set_vertex_input_cmd::Stride::MakeValue(sizeof(CustomVertex)) | - set_vertex_input_cmd::Type::MakeValue(vertex_struct::FLOAT3) | - set_vertex_input_cmd::Semantic::MakeValue(vertex_struct::NORMAL) | - set_vertex_input_cmd::SemanticIndex::MakeValue(0); - helper_->AddCommand(SetVertexInput, 5, args); + helper_->SetVertexInput(vertex_struct_id_, 1, + vertex_buffer_id_, offsetof(CustomVertex, nx), + vertex_struct::kNormal, 0, + vertex_struct::kFloat3, sizeof(CustomVertex)); CHECK_ERROR(helper_); // Set TEXCOORD0 input stream - args[1].value_uint32 = 2; // input - args[3].value_uint32 = 24; // offset - args[4].value_uint32 = - set_vertex_input_cmd::Stride::MakeValue(sizeof(CustomVertex)) | - set_vertex_input_cmd::Type::MakeValue(vertex_struct::FLOAT2) | - set_vertex_input_cmd::Semantic::MakeValue(vertex_struct::TEX_COORD) | - set_vertex_input_cmd::SemanticIndex::MakeValue(0); - helper_->AddCommand(SetVertexInput, 5, args); + helper_->SetVertexInput(vertex_struct_id_, 2, + vertex_buffer_id_, offsetof(CustomVertex, u), + vertex_struct::kTexCoord, 0, + vertex_struct::kFloat3, sizeof(CustomVertex)); CHECK_ERROR(helper_); // Create a 2D texture. - args[0].value_uint32 = noise_texture_id_; - args[1].value_uint32 = - create_texture_2d_cmd::Width::MakeValue(kTexWidth) | - create_texture_2d_cmd::Height::MakeValue(kTexHeight); - args[2].value_uint32 = - create_texture_2d_cmd::Levels::MakeValue(0) | - create_texture_2d_cmd::Format::MakeValue(texture::ARGB8) | - create_texture_2d_cmd::Flags::MakeValue(0); - helper_->AddCommand(CreateTexture2d, 3, args); + helper_->CreateTexture2d(noise_texture_id_, + kTexWidth, kTexHeight, + 1, texture::kARGB8, false); CHECK_ERROR(helper_); - args[0].value_uint32 = noise_texture_id_; - args[1].value_uint32 = - set_texture_data_cmd::X::MakeValue(0) | - set_texture_data_cmd::Y::MakeValue(0); - args[2].value_uint32 = - set_texture_data_cmd::Width::MakeValue(kTexWidth) | - set_texture_data_cmd::Height::MakeValue(kTexHeight); - args[3].value_uint32 = - set_texture_data_cmd::Z::MakeValue(0) | - set_texture_data_cmd::Depth::MakeValue(1); - args[4].value_uint32 = set_texture_data_cmd::Level::MakeValue(0); - args[5].value_uint32 = kTexWidth * 4; // row_pitch - args[6].value_uint32 = 0; // slice_pitch - args[7].value_uint32 = kTexSize; // size - args[8].value_uint32 = shm_id_; - args[9].value_uint32 = allocator_->GetOffset(noise_texture_); - helper_->AddCommand(SetTextureData, 10, args); + helper_->SetTextureData(noise_texture_id_, + 0, 0, 0, + kTexWidth, kTexHeight, 1, + 0, texture::kFaceNone, + kTexWidth * 4, // row_pitch + 0, // slice_pitch + kTexSize, // size + shm_id_, + allocator_->GetOffset(noise_texture_)); CHECK_ERROR(helper_); - args[0].value_uint32 = noise_sampler_id_; - helper_->AddCommand(CreateSampler, 1, args); + helper_->CreateSampler(noise_sampler_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = noise_sampler_id_; - args[1].value_uint32 = noise_texture_id_; - helper_->AddCommand(SetSamplerTexture, 2, args); + helper_->SetSamplerTexture(noise_sampler_id_, noise_texture_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = noise_sampler_id_; - args[1].value_uint32 = - set_sampler_states::AddressingU::MakeValue(sampler::WRAP) | - set_sampler_states::AddressingV::MakeValue(sampler::WRAP) | - set_sampler_states::AddressingW::MakeValue(sampler::WRAP) | - set_sampler_states::MagFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MinFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MipFilter::MakeValue(sampler::NONE) | - set_sampler_states::MaxAnisotropy::MakeValue(1); - helper_->AddCommand(SetSamplerStates, 2, args); + helper_->SetSamplerStates(noise_sampler_id_, + sampler::kWrap, + sampler::kWrap, + sampler::kWrap, + sampler::kLinear, + sampler::kLinear, + sampler::kNone, + 1); CHECK_ERROR(helper_); // Create a 2D texture. - args[0].value_uint32 = iridescence_texture_id_; - args[1].value_uint32 = - create_texture_2d_cmd::Width::MakeValue(kTexWidth) | - create_texture_2d_cmd::Height::MakeValue(kTexHeight); - args[2].value_uint32 = - create_texture_2d_cmd::Levels::MakeValue(0) | - create_texture_2d_cmd::Format::MakeValue(texture::ARGB8) | - create_texture_2d_cmd::Flags::MakeValue(0); - helper_->AddCommand(CreateTexture2d, 3, args); + helper_->CreateTexture2d(iridescence_texture_id_, + kTexWidth, kTexHeight, + 1, texture::kARGB8, false); CHECK_ERROR(helper_); - args[0].value_uint32 = iridescence_texture_id_; - args[1].value_uint32 = - set_texture_data_cmd::X::MakeValue(0) | - set_texture_data_cmd::Y::MakeValue(0); - args[2].value_uint32 = - set_texture_data_cmd::Width::MakeValue(kTexWidth) | - set_texture_data_cmd::Height::MakeValue(kTexHeight); - args[3].value_uint32 = - set_texture_data_cmd::Z::MakeValue(0) | - set_texture_data_cmd::Depth::MakeValue(1); - args[4].value_uint32 = set_texture_data_cmd::Level::MakeValue(0); - args[5].value_uint32 = kTexWidth * 4; // row_pitch - args[6].value_uint32 = 0; // slice_pitch - args[7].value_uint32 = kTexSize; // size - args[8].value_uint32 = shm_id_; - args[9].value_uint32 = allocator_->GetOffset(iridescence_texture_); - helper_->AddCommand(SetTextureData, 10, args); + helper_->SetTextureData(iridescence_texture_id_, + 0, 0, 0, + kTexWidth, kTexHeight, 1, + 0, texture::kFaceNone, + kTexWidth * 4, // row_pitch + 0, // slice_pitch + kTexSize, // size + shm_id_, + allocator_->GetOffset(iridescence_texture_)); CHECK_ERROR(helper_); - args[0].value_uint32 = iridescence_sampler_id_; - helper_->AddCommand(CreateSampler, 1, args); + helper_->CreateSampler(iridescence_sampler_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = iridescence_sampler_id_; - args[1].value_uint32 = iridescence_texture_id_; - helper_->AddCommand(SetSamplerTexture, 2, args); + helper_->SetSamplerTexture(iridescence_sampler_id_, iridescence_texture_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = iridescence_sampler_id_; - args[1].value_uint32 = - set_sampler_states::AddressingU::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::AddressingV::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::AddressingW::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::MagFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MinFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MipFilter::MakeValue(sampler::NONE) | - set_sampler_states::MaxAnisotropy::MakeValue(1); - helper_->AddCommand(SetSamplerStates, 2, args); + helper_->SetSamplerStates(iridescence_sampler_id_, + sampler::kClampToEdge, + sampler::kClampToEdge, + sampler::kClampToEdge, + sampler::kLinear, + sampler::kLinear, + sampler::kNone, + 1); CHECK_ERROR(helper_); // Create a Cubemap texture. - args[0].value_uint32 = cubemap_id_; - args[1].value_uint32 = - create_texture_cube_cmd::Side::MakeValue(kCubeMapWidth); - args[2].value_uint32 = - create_texture_cube_cmd::Levels::MakeValue(0) | - create_texture_cube_cmd::Format::MakeValue(texture::ARGB8) | - create_texture_cube_cmd::Flags::MakeValue(0); - helper_->AddCommand(CreateTextureCube, 3, args); + helper_->CreateTextureCube(cubemap_id_, + kCubeMapWidth, + 1, texture::kARGB8, false); CHECK_ERROR(helper_); for (unsigned int face = 0; face < 6; ++face) { void *data = allocator_->Alloc(kCubeMapFaceSize); memcpy(data, g_cubemap_data + face*kCubeMapFaceSize, kCubeMapFaceSize); - args[0].value_uint32 = cubemap_id_; - args[1].value_uint32 = - set_texture_data_cmd::X::MakeValue(0) | - set_texture_data_cmd::Y::MakeValue(0); - args[2].value_uint32 = - set_texture_data_cmd::Width::MakeValue(kCubeMapWidth) | - set_texture_data_cmd::Height::MakeValue(kCubeMapHeight); - args[3].value_uint32 = - set_texture_data_cmd::Z::MakeValue(0) | - set_texture_data_cmd::Depth::MakeValue(1); - args[4].value_uint32 = - set_texture_data_cmd::Level::MakeValue(0) | - set_texture_data_cmd::Face::MakeValue(face); - args[5].value_uint32 = kCubeMapWidth * 4; // row_pitch - args[6].value_uint32 = 0; // slice_pitch - args[7].value_uint32 = kCubeMapFaceSize; // size - args[8].value_uint32 = shm_id_; - args[9].value_uint32 = allocator_->GetOffset(data); - helper_->AddCommand(SetTextureData, 10, args); + helper_->SetTextureData(cubemap_id_, + 0, 0, 0, + kCubeMapWidth, kCubeMapHeight, 1, + 0, static_cast<texture::Face>(face), + kCubeMapWidth * 4, // row_pitch + 0, // slice_pitch + kCubeMapFaceSize, // size + shm_id_, + allocator_->GetOffset(data)); CHECK_ERROR(helper_); allocator_->FreePendingToken(data, helper_->InsertToken()); } - args[0].value_uint32 = cubemap_sampler_id_; - helper_->AddCommand(CreateSampler, 1, args); + helper_->CreateSampler(cubemap_sampler_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = cubemap_sampler_id_; - args[1].value_uint32 = cubemap_id_; - helper_->AddCommand(SetSamplerTexture, 2, args); + helper_->SetSamplerTexture(cubemap_sampler_id_, cubemap_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = cubemap_sampler_id_; - args[1].value_uint32 = - set_sampler_states::AddressingU::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::AddressingV::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::AddressingW::MakeValue(sampler::CLAMP_TO_EDGE) | - set_sampler_states::MagFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MinFilter::MakeValue(sampler::LINEAR) | - set_sampler_states::MipFilter::MakeValue(sampler::NONE) | - set_sampler_states::MaxAnisotropy::MakeValue(1); - helper_->AddCommand(SetSamplerStates, 2, args); + helper_->SetSamplerStates(cubemap_sampler_id_, + sampler::kClampToEdge, + sampler::kClampToEdge, + sampler::kClampToEdge, + sampler::kLinear, + sampler::kLinear, + sampler::kNone, + 1); CHECK_ERROR(helper_); // Create the effect, and parameters. + + // TODO(gman): expand the size param of the command buffer because it would + // be nice if we could just do this + // + // helper_->CreateEffectImmediate(effect_id_, + // sizeof(effect_data), effect_data); + // + // instead of all this alloc, memcpy, free stuff. Currently we can't because a + // command's size is only 8 bits and so the most we can pass in in an + // immediate command is 1024 bytes. + void *data = allocator_->Alloc(sizeof(effect_data)); memcpy(data, effect_data, sizeof(effect_data)); - args[0].value_uint32 = effect_id_; - args[1].value_uint32 = sizeof(effect_data); // size - args[2].value_uint32 = shm_id_; // shm - args[3].value_uint32 = allocator_->GetOffset(data); // offset in shm - helper_->AddCommand(CreateEffect, 4, args); + helper_->CreateEffect(effect_id_, sizeof(effect_data), + shm_id_, allocator_->GetOffset(data)); CHECK_ERROR(helper_); allocator_->FreePendingToken(data, helper_->InsertToken()); { const char param_name[] = "noise_sampler"; - args[0].value_uint32 = noise_sampler_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(noise_sampler_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "iridescence_sampler"; - args[0].value_uint32 = iridescence_sampler_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(iridescence_sampler_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "env_sampler"; - args[0].value_uint32 = cubemap_sampler_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(cubemap_sampler_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "worldViewProj"; - args[0].value_uint32 = mvp_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(mvp_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "world"; - args[0].value_uint32 = world_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(world_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "worldIT"; - args[0].value_uint32 = worldIT_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(worldIT_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "eye"; - args[0].value_uint32 = eye_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(eye_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } { const char param_name[] = "thickness_params"; - args[0].value_uint32 = thickness_param_id_; - args[1].value_uint32 = effect_id_; - args[2].value_uint32 = sizeof(param_name); - unsigned int arg_count = CopyToArgs(args + 3, param_name, - sizeof(param_name)); - helper_->AddCommand(CreateParamByNameImmediate, 3 + arg_count, args); + helper_->CreateParamByNameImmediate(thickness_param_id_, + effect_id_, + sizeof(param_name), + param_name); CHECK_ERROR(helper_); } // Bind textures to the parameters - args[0].value_uint32 = noise_sampler_param_id_; - args[1].value_uint32 = sizeof(Uint32); // NOLINT - args[2].value_uint32 = noise_sampler_id_; - helper_->AddCommand(SetParamDataImmediate, 3, args); + helper_->SetParamDataImmediate(noise_sampler_param_id_, + sizeof(noise_sampler_id_), + &noise_sampler_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = iridescence_sampler_param_id_; - args[1].value_uint32 = sizeof(Uint32); // NOLINT - args[2].value_uint32 = iridescence_sampler_id_; - helper_->AddCommand(SetParamDataImmediate, 3, args); + helper_->SetParamDataImmediate(iridescence_sampler_param_id_, + sizeof(iridescence_sampler_id_), + &iridescence_sampler_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = cubemap_sampler_param_id_; - args[1].value_uint32 = sizeof(Uint32); // NOLINT - args[2].value_uint32 = cubemap_sampler_id_; - helper_->AddCommand(SetParamDataImmediate, 3, args); + helper_->SetParamDataImmediate(cubemap_sampler_param_id_, + sizeof(cubemap_sampler_id_), + &cubemap_sampler_id_); CHECK_ERROR(helper_); // Create our random bubbles. @@ -830,15 +699,14 @@ void BubbleDemo::Initialize() { bubble.scale = Randf(.5, 2.f, &seed_); float max_thickness = Randf(.3f, .5f, &seed_); float min_thickness = Randf(.3f, max_thickness, &seed_); - // thickness = base * e^(-y*falloff) for y in [-scale..scale]. + // thickness = base * e^(-y * falloff) for y in [-scale..scale]. bubble.thickness_falloff = - logf(max_thickness/min_thickness)/(2 * bubble.scale); + logf(max_thickness / min_thickness) / (2 * bubble.scale); bubble.base_thickness = max_thickness * expf(-bubble.scale * bubble.thickness_falloff); bubble.noise_ratio = Randf(.2f, .5f, &seed_); bubbles_.push_back(bubble); } - start_time_ = GetTimeUsec(); } void BubbleDemo::Finalize() { @@ -879,109 +747,77 @@ void BubbleDemo::DrawBubble(const math::Matrix4& view, math::Matrix4 modelIT = math::inverse(math::transpose(model)); math::Matrix4 mvp = proj * view * model; - // AddCommand copies the args, so it is safe to re-use args across various - // calls. - // 20 is the largest command we use (SetParamDataImmediate for matrices). - CommandBufferEntry args[20]; - - args[0].value_uint32 = vertex_struct_id_; - helper_->AddCommand(SetVertexStruct, 1, args); + helper_->SetVertexStruct(vertex_struct_id_); CHECK_ERROR(helper_); - args[0].value_uint32 = mvp_param_id_; - args[1].value_uint32 = sizeof(mvp); - unsigned int arg_count = CopyToArgs(args + 2, &mvp, sizeof(mvp)); - helper_->AddCommand(SetParamDataImmediate, 2 + arg_count, args); + helper_->SetParamDataImmediate(mvp_param_id_, sizeof(mvp), &mvp); CHECK_ERROR(helper_); - args[0].value_uint32 = world_param_id_; - args[1].value_uint32 = sizeof(model); - arg_count = CopyToArgs(args + 2, &model, sizeof(model)); - helper_->AddCommand(SetParamDataImmediate, 2 + arg_count, args); + helper_->SetParamDataImmediate(world_param_id_, sizeof(model), &model); CHECK_ERROR(helper_); - args[0].value_uint32 = worldIT_param_id_; - args[1].value_uint32 = sizeof(modelIT); - arg_count = CopyToArgs(args + 2, &modelIT, sizeof(modelIT)); - helper_->AddCommand(SetParamDataImmediate, 2 + arg_count, args); + helper_->SetParamDataImmediate(worldIT_param_id_, sizeof(modelIT), &modelIT); CHECK_ERROR(helper_); - args[0].value_uint32 = eye_param_id_; - args[1].value_uint32 = sizeof(eye); - arg_count = CopyToArgs(args + 2, &eye, sizeof(eye)); - helper_->AddCommand(SetParamDataImmediate, 2 + arg_count, args); + helper_->SetParamDataImmediate(eye_param_id_, sizeof(eye), &eye); CHECK_ERROR(helper_); - args[0].value_uint32 = - set_blending::ColorSrcFunc::MakeValue(GAPIInterface::BLEND_FUNC_ONE) | - set_blending::ColorDstFunc::MakeValue( - GAPIInterface::BLEND_FUNC_SRC_ALPHA) | - set_blending::ColorEq::MakeValue(GAPIInterface::BLEND_EQ_ADD) | - set_blending::SeparateAlpha::MakeValue(0) | - set_blending::Enable::MakeValue(1); - helper_->AddCommand(SetBlending, 1, args); + helper_->SetBlending(command_buffer::kBlendFuncOne, + command_buffer::kBlendFuncSrcAlpha, + command_buffer::kBlendEqAdd, + command_buffer::kBlendFuncOne, + command_buffer::kBlendFuncOne, + command_buffer::kBlendEqAdd, + false, + true); CHECK_ERROR(helper_); - args[0].value_uint32 = effect_id_; - helper_->AddCommand(SetEffect, 1, args); + helper_->SetEffect(effect_id_); CHECK_ERROR(helper_); // Draw back faces first. - args[0].value_uint32 = - set_polygon_raster::FillMode::MakeValue( - GAPIInterface::POLYGON_MODE_FILL) | - set_polygon_raster::CullMode::MakeValue(GAPIInterface::CULL_CCW); - helper_->AddCommand(SetPolygonRaster, 1, args); + helper_->SetPolygonRaster(command_buffer::kPolygonModeFill, + command_buffer::kCullCCW); CHECK_ERROR(helper_); - args[0].value_uint32 = thickness_param_id_; - args[1].value_uint32 = 4*sizeof(float); // NOLINT - args[2].value_float = bubble.thickness_falloff; - args[3].value_float = bubble.base_thickness; - args[4].value_float = bubble.noise_ratio; - args[5].value_float = .5f; // back face attenuation - helper_->AddCommand(SetParamDataImmediate, 6, args); + float temp[4] = { + bubble.thickness_falloff, + bubble.base_thickness, + bubble.noise_ratio, + .5f, // back face attenuation + }; + helper_->SetParamDataImmediate(thickness_param_id_, sizeof(temp), temp); CHECK_ERROR(helper_); - args[0].value_uint32 = GAPIInterface::TRIANGLES; - args[1].value_uint32 = index_buffer_id_; - args[2].value_uint32 = 0; // first - args[3].value_uint32 = kIndexCount/3; // primitive count - args[4].value_uint32 = 0; // min index - args[5].value_uint32 = kVertexCount-1; // max index - helper_->AddCommand(DrawIndexed, 6, args); + helper_->DrawIndexed(command_buffer::kTriangles, + index_buffer_id_, + 0, // first + kIndexCount / 3, // primitive count + 0, // min index + kVertexCount - 1); // max index CHECK_ERROR(helper_); // Then front faces. - args[0].value_uint32 = - set_polygon_raster::FillMode::MakeValue( - GAPIInterface::POLYGON_MODE_FILL) | - set_polygon_raster::CullMode::MakeValue(GAPIInterface::CULL_CW); - helper_->AddCommand(SetPolygonRaster, 1, args); + helper_->SetPolygonRaster(command_buffer::kPolygonModeFill, + command_buffer::kCullCW); CHECK_ERROR(helper_); - args[0].value_uint32 = thickness_param_id_; - args[1].value_uint32 = 4*sizeof(float); // NOLINT - args[2].value_float = bubble.thickness_falloff; - args[3].value_float = bubble.base_thickness; - args[4].value_float = bubble.noise_ratio; - args[5].value_float = 1.f; - helper_->AddCommand(SetParamDataImmediate, 6, args); + temp[3] = 1.f; // back face attenuation + helper_->SetParamDataImmediate(thickness_param_id_, sizeof(temp), temp); CHECK_ERROR(helper_); - args[0].value_uint32 = GAPIInterface::TRIANGLES; - args[1].value_uint32 = index_buffer_id_; - args[2].value_uint32 = 0; // first - args[3].value_uint32 = kIndexCount/3; // primitive count - args[4].value_uint32 = 0; // min index - args[5].value_uint32 = kVertexCount-1; // max index - helper_->AddCommand(DrawIndexed, 6, args); + helper_->DrawIndexed(command_buffer::kTriangles, + index_buffer_id_, + 0, // first + kIndexCount / 3, // primitive count + 0, // min index + kVertexCount - 1); // max index CHECK_ERROR(helper_); } void BubbleDemo::Render() { - uint64_t time_usec = GetTimeUsec() - start_time_; - time_ = time_usec * 1.e-6f; + time_ = time_ + .01f; + // Camera path float r = 20.f; float theta = time_ / 4.f; @@ -993,11 +829,12 @@ void BubbleDemo::Render() { math::CreatePerspectiveMatrix(kPi / 4.f, 1.f, .1f, 10000.f); math::Matrix4 view = math::Matrix4::lookAt(eye, target, up); - helper_->AddCommand(BeginFrame, 0 , NULL); + helper_->BeginFrame(); CHECK_ERROR(helper_); RGBA color = {0.2f, 0.2f, 0.2f, 1.f}; - ClearCmd(helper_.get(), GAPIInterface::COLOR | GAPIInterface::DEPTH, color, - 1.f, 0); + helper_->Clear(command_buffer::kColor | command_buffer::kDepth, + color.red, color.green, color.blue, color.alpha, + 1.f, 0); // Sort bubbles back-to-front. std::sort(bubbles_.begin(), bubbles_.end(), BubbleSorter(view)); @@ -1007,9 +844,10 @@ void BubbleDemo::Render() { DrawBubble(view, proj, bubble, time_ * 2.f * kPi * bubble.rotation_speed); } - helper_->AddCommand(EndFrame, 0 , NULL); + helper_->EndFrame(); CHECK_ERROR(helper_); helper_->Flush(); + helper_->Finish(); } } // namespace command_buffer @@ -1298,8 +1136,16 @@ int main(int argc, char **argv) { return 1; } - o3d::command_buffer::BubbleDemo(htp_handle); + o3d::command_buffer::BubbleDemo demo; + demo.SetHandle(0, htp_handle); + demo.Initialize(); + for (;;) { + // TODO(gman): Add a way out of this loop. + demo.Render(); + } + demo.Finalize(); CloseConnection(htp_handle); return 0; } + |