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 29 matching lines...) Expand all Loading... |
40 #include "command_buffer/common/cross/rpc_imc.h" | 40 #include "command_buffer/common/cross/rpc_imc.h" |
41 #include "command_buffer/client/cross/cmd_buffer_helper.h" | 41 #include "command_buffer/client/cross/cmd_buffer_helper.h" |
42 #include "command_buffer/client/cross/buffer_sync_proxy.h" | 42 #include "command_buffer/client/cross/buffer_sync_proxy.h" |
43 #include "third_party/vectormath/files/vectormathlibrary/include/vectormath/scal
ar/cpp/vectormath_aos.h" // NOLINT | 43 #include "third_party/vectormath/files/vectormathlibrary/include/vectormath/scal
ar/cpp/vectormath_aos.h" // NOLINT |
44 | 44 |
45 namespace o3d { | 45 namespace o3d { |
46 namespace command_buffer { | 46 namespace command_buffer { |
47 | 47 |
48 namespace math = Vectormath::Aos; | 48 namespace math = Vectormath::Aos; |
49 | 49 |
50 // Adds a Clear command into the command buffer. | |
51 // Parameters: | |
52 // cmd_buffer: the command buffer helper. | |
53 // buffers: a bitfield of which buffers to clear (a combination of | |
54 // GAPIInterface::COLOR, GAPIInterface::DEPTH and GAPIInterface::STENCIL). | |
55 // color: the color buffer clear value. | |
56 // depth: the depth buffer clear value. | |
57 // stencil: the stencil buffer clear value. | |
58 void ClearCmd(CommandBufferHelper *cmd_buffer, | |
59 const unsigned int buffers, | |
60 const RGBA &color, | |
61 float depth, | |
62 unsigned int stencil) { | |
63 CommandBufferEntry args[7]; | |
64 args[0].value_uint32 = buffers; | |
65 args[1].value_float = color.red; | |
66 args[2].value_float = color.green; | |
67 args[3].value_float = color.blue; | |
68 args[4].value_float = color.alpha; | |
69 args[5].value_float = depth; | |
70 args[6].value_uint32 = stencil; | |
71 cmd_buffer->AddCommand(command_buffer::CLEAR, 7, args); | |
72 } | |
73 | |
74 // Adds a SetViewport command into the buffer. | 50 // Adds a SetViewport command into the buffer. |
75 // Parameters: | 51 // Parameters: |
76 // cmd_buffer: the command buffer helper. | 52 // cmd_buffer: the command buffer helper. |
77 // x, y, width, height: the dimensions of the Viewport. | 53 // x, y, width, height: the dimensions of the Viewport. |
78 // z_near, z_far: the near and far clip plane distances. | 54 // z_near, z_far: the near and far clip plane distances. |
79 void SetViewportCmd(CommandBufferHelper *cmd_buffer, | 55 void SetViewportCmd(CommandBufferHelper *cmd_buffer, |
80 unsigned int x, | 56 unsigned int x, |
81 unsigned int y, | 57 unsigned int y, |
82 unsigned int width, | 58 unsigned int width, |
83 unsigned int height, | 59 unsigned int height, |
84 float z_near, | 60 float z_near, |
85 float z_far) { | 61 float z_far) { |
86 CommandBufferEntry args[6]; | 62 cmd_buffer->SetViewport(x, y, width, height, z_near, z_far); |
87 args[0].value_uint32 = x; | |
88 args[1].value_uint32 = y; | |
89 args[2].value_uint32 = width; | |
90 args[3].value_uint32 = height; | |
91 args[4].value_float = z_near; | |
92 args[5].value_float = z_far; | |
93 cmd_buffer->AddCommand(command_buffer::SET_VIEWPORT, 6, args); | |
94 } | 63 } |
95 | 64 |
96 // Copy a data buffer to args, for IMMEDIATE commands. Returns the number of | 65 // Copy a data buffer to args, for IMMEDIATE commands. Returns the number of |
97 // args used. | 66 // args used. |
98 unsigned int CopyToArgs(CommandBufferEntry *args, | 67 unsigned int CopyToArgs(CommandBufferEntry *args, |
99 const void *data, | 68 const void *data, |
100 size_t size) { | 69 size_t size) { |
101 memcpy(args, data, size); | 70 memcpy(args, data, size); |
102 const unsigned int arg_size = sizeof(args[0]); | 71 const unsigned int arg_size = sizeof(args[0]); |
103 return static_cast<unsigned int>((size + arg_size - 1) / arg_size); | 72 return static_cast<unsigned int>((size + arg_size - 1) / arg_size); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 RPCShmHandle shm = CreateShm(kShmSize); | 105 RPCShmHandle shm = CreateShm(kShmSize); |
137 void *shm_address = MapShm(shm, kShmSize); | 106 void *shm_address = MapShm(shm, kShmSize); |
138 unsigned int shm_id = proxy.RegisterSharedMemory(shm, kShmSize); | 107 unsigned int shm_id = proxy.RegisterSharedMemory(shm, kShmSize); |
139 | 108 |
140 { | 109 { |
141 CommandBufferHelper cmd_buffer(&proxy); | 110 CommandBufferHelper cmd_buffer(&proxy); |
142 cmd_buffer.Init(500); | 111 cmd_buffer.Init(500); |
143 | 112 |
144 // Clear the buffers. | 113 // Clear the buffers. |
145 RGBA color = {0.2f, 0.2f, 0.2f, 1.f}; | 114 RGBA color = {0.2f, 0.2f, 0.2f, 1.f}; |
146 ClearCmd(&cmd_buffer, GAPIInterface::COLOR | GAPIInterface::DEPTH, color, | 115 cmd_buffer.Clear(GAPIInterface::COLOR | GAPIInterface::DEPTH, |
147 1.f, 0); | 116 color.red, color.green, color.blue, color.alpha, |
| 117 1.f, 0); |
148 | 118 |
149 const ResourceID vertex_buffer_id = 1; | 119 const ResourceID vertex_buffer_id = 1; |
150 const ResourceID vertex_struct_id = 1; | 120 const ResourceID vertex_struct_id = 1; |
151 | 121 |
152 // AddCommand copies the args, so it is safe to re-use args across various | 122 static const CustomVertex vertices[4] = { |
153 // calls. | |
154 // 20 is the largest command we use (SET_PARAM_DATA_IMMEDIATE for matrices). | |
155 CommandBufferEntry args[20]; | |
156 | |
157 CustomVertex vertices[4] = { | |
158 {-.5f, -.5f, 0.f, 1.f, 0, 0}, | 123 {-.5f, -.5f, 0.f, 1.f, 0, 0}, |
159 {.5f, -.5f, 0.f, 1.f, 1, 0}, | 124 {.5f, -.5f, 0.f, 1.f, 1, 0}, |
160 {-.5f, .5f, 0.f, 1.f, 0, 1}, | 125 {-.5f, .5f, 0.f, 1.f, 0, 1}, |
161 {.5f, .5f, 0.f, 1.f, 1, 1}, | 126 {.5f, .5f, 0.f, 1.f, 1, 1}, |
162 }; | 127 }; |
163 args[0].value_uint32 = vertex_buffer_id; | 128 cmd_buffer.CreateVertexBuffer(vertex_buffer_id, sizeof(vertices), 0); |
164 args[1].value_uint32 = sizeof(vertices); // size | |
165 args[2].value_uint32 = 0; // flags | |
166 cmd_buffer.AddCommand(command_buffer::CREATE_VERTEX_BUFFER, 3, args); | |
167 | 129 |
168 memcpy(shm_address, vertices, sizeof(vertices)); | 130 memcpy(shm_address, vertices, sizeof(vertices)); |
169 args[0].value_uint32 = vertex_buffer_id; | 131 cmd_buffer.SetVertexBufferData( |
170 args[1].value_uint32 = 0; // offset in VB | 132 vertex_buffer_id, 0, sizeof(vertices), shm_id, 0); |
171 args[2].value_uint32 = sizeof(vertices); // size | |
172 args[3].value_uint32 = shm_id; // shm | |
173 args[4].value_uint32 = 0; // offset in shm | |
174 cmd_buffer.AddCommand(command_buffer::SET_VERTEX_BUFFER_DATA, 5, args); | |
175 unsigned int token = cmd_buffer.InsertToken(); | 133 unsigned int token = cmd_buffer.InsertToken(); |
176 | 134 |
177 args[0].value_uint32 = vertex_struct_id; | 135 cmd_buffer.CreateVertexStruct(vertex_struct_id, 2); |
178 args[1].value_uint32 = 2; // input count | |
179 cmd_buffer.AddCommand(command_buffer::CREATE_VERTEX_STRUCT, 2, args); | |
180 | 136 |
181 // Set POSITION input stream | 137 // Set POSITION input stream |
182 args[0].value_uint32 = vertex_struct_id; | 138 cmd_buffer.SetVertexInput(vertex_struct_id, 0, vertex_buffer_id, 0, |
183 args[1].value_uint32 = 0; // input | 139 vertex_struct::POSITION, 0, |
184 args[2].value_uint32 = vertex_buffer_id; // buffer | 140 vertex_struct::FLOAT4, sizeof(CustomVertex)); |
185 args[3].value_uint32 = 0; // offset | |
186 args[4].value_uint32 = | |
187 set_vertex_input_cmd::Stride::MakeValue(sizeof(CustomVertex)) | | |
188 set_vertex_input_cmd::Type::MakeValue(vertex_struct::FLOAT4) | | |
189 set_vertex_input_cmd::Semantic::MakeValue(vertex_struct::POSITION) | | |
190 set_vertex_input_cmd::SemanticIndex::MakeValue(0); | |
191 cmd_buffer.AddCommand(command_buffer::SET_VERTEX_INPUT, 5, args); | |
192 | 141 |
193 // Set TEXCOORD0 input stream | 142 // Set TEXCOORD0 input stream |
194 args[1].value_uint32 = 1; // input | 143 cmd_buffer.SetVertexInput(vertex_struct_id, 1, vertex_buffer_id, 16, |
195 args[3].value_uint32 = 16; // offset | 144 vertex_struct::TEX_COORD, 0, |
196 args[4].value_uint32 = | 145 vertex_struct::FLOAT2, sizeof(CustomVertex)); |
197 set_vertex_input_cmd::Stride::MakeValue(sizeof(CustomVertex)) | | |
198 set_vertex_input_cmd::Type::MakeValue(vertex_struct::FLOAT2) | | |
199 set_vertex_input_cmd::Semantic::MakeValue(vertex_struct::TEX_COORD) | | |
200 set_vertex_input_cmd::SemanticIndex::MakeValue(0); | |
201 cmd_buffer.AddCommand(command_buffer::SET_VERTEX_INPUT, 5, args); | |
202 | 146 |
203 // wait for previous transfer to be executed, so that we can re-use the | 147 // wait for previous transfer to be executed, so that we can re-use the |
204 // transfer shared memory buffer. | 148 // transfer shared memory buffer. |
205 cmd_buffer.WaitForToken(token); | 149 cmd_buffer.WaitForToken(token); |
206 memcpy(shm_address, effect_data, sizeof(effect_data)); | 150 memcpy(shm_address, effect_data, sizeof(effect_data)); |
207 const ResourceID effect_id = 1; | 151 const ResourceID effect_id = 1; |
208 args[0].value_uint32 = effect_id; | 152 cmd_buffer.CreateEffect(effect_id, sizeof(effect_data), shm_id, 0); |
209 args[1].value_uint32 = sizeof(effect_data); // size | |
210 args[2].value_uint32 = shm_id; // shm | |
211 args[3].value_uint32 = 0; // offset in shm | |
212 cmd_buffer.AddCommand(command_buffer::CREATE_EFFECT, 4, args); | |
213 token = cmd_buffer.InsertToken(); | 153 token = cmd_buffer.InsertToken(); |
214 | 154 |
215 // Create a 4x4 2D texture. | 155 // Create a 4x4 2D texture. |
216 const ResourceID texture_id = 1; | 156 const ResourceID texture_id = 1; |
217 args[0].value_uint32 = texture_id; | 157 // TODO(gman): the 0 should be a 1?!?! |
218 args[1].value_uint32 = | 158 cmd_buffer.CreateTexture2d(texture_id, 4, 4, 0, texture::ARGB8, 0); |
219 create_texture_2d_cmd::Width::MakeValue(4) | | |
220 create_texture_2d_cmd::Height::MakeValue(4); | |
221 args[2].value_uint32 = | |
222 create_texture_2d_cmd::Levels::MakeValue(0) | | |
223 create_texture_2d_cmd::Format::MakeValue(texture::ARGB8) | | |
224 create_texture_2d_cmd::Flags::MakeValue(0); | |
225 cmd_buffer.AddCommand(command_buffer::CREATE_TEXTURE_2D, 3, args); | |
226 | 159 |
227 unsigned int texels[4] = { | 160 static const unsigned int texels[4] = { |
228 0xff0000ff, | 161 0xff0000ff, |
229 0xffff00ff, | 162 0xffff00ff, |
230 0xff00ffff, | 163 0xff00ffff, |
231 0xffffffff, | 164 0xffffffff, |
232 }; | 165 }; |
233 // wait for previous transfer to be executed, so that we can re-use the | 166 // wait for previous transfer to be executed, so that we can re-use the |
234 // transfer shared memory buffer. | 167 // transfer shared memory buffer. |
235 cmd_buffer.WaitForToken(token); | 168 cmd_buffer.WaitForToken(token); |
236 memcpy(shm_address, texels, sizeof(texels)); | 169 memcpy(shm_address, texels, sizeof(texels)); |
237 // Creates a 4x4 texture by uploading 2x2 data in each quadrant. | 170 // Creates a 4x4 texture by uploading 2x2 data in each quadrant. |
238 for (unsigned int x = 0; x < 2; ++x) | 171 for (unsigned int x = 0; x < 2; ++x) |
239 for (unsigned int y = 0; y < 2; ++y) { | 172 for (unsigned int y = 0; y < 2; ++y) { |
240 args[0].value_uint32 = texture_id; | 173 cmd_buffer.SetTextureData(texture_id, x * 2, y * 2, 0, 2, 2, 1, 0, 0, |
241 args[1].value_uint32 = | 174 sizeof(texels[0]) * 2, // row_pitch |
242 set_texture_data_cmd::X::MakeValue(x*2) | | 175 0, // slice_pitch |
243 set_texture_data_cmd::Y::MakeValue(y*2); | 176 sizeof(texels), // size |
244 args[2].value_uint32 = | 177 shm_id, |
245 set_texture_data_cmd::Width::MakeValue(2) | | 178 0); |
246 set_texture_data_cmd::Height::MakeValue(2); | |
247 args[3].value_uint32 = | |
248 set_texture_data_cmd::Z::MakeValue(0) | | |
249 set_texture_data_cmd::Depth::MakeValue(1); | |
250 args[4].value_uint32 = set_texture_data_cmd::Level::MakeValue(0); | |
251 args[5].value_uint32 = sizeof(texels[0]) * 2; // row_pitch | |
252 args[6].value_uint32 = 0; // slice_pitch | |
253 args[7].value_uint32 = sizeof(texels); // size | |
254 args[8].value_uint32 = shm_id; | |
255 args[9].value_uint32 = 0; | |
256 cmd_buffer.AddCommand(command_buffer::SET_TEXTURE_DATA, 10, args); | |
257 } | 179 } |
258 token = cmd_buffer.InsertToken(); | 180 token = cmd_buffer.InsertToken(); |
259 | 181 |
260 const ResourceID sampler_id = 1; | 182 const ResourceID sampler_id = 1; |
261 args[0].value_uint32 = sampler_id; | 183 cmd_buffer.CreateSampler(sampler_id); |
262 cmd_buffer.AddCommand(command_buffer::CREATE_SAMPLER, 1, args); | 184 cmd_buffer.SetSamplerTexture(sampler_id, texture_id); |
263 | 185 cmd_buffer.SetSamplerStates(sampler_id, |
264 args[0].value_uint32 = sampler_id; | 186 sampler::CLAMP_TO_EDGE, |
265 args[1].value_uint32 = texture_id; | 187 sampler::CLAMP_TO_EDGE, |
266 cmd_buffer.AddCommand(command_buffer::SET_SAMPLER_TEXTURE, 2, args); | 188 sampler::CLAMP_TO_EDGE, |
267 | 189 sampler::POINT, |
268 args[0].value_uint32 = sampler_id; | 190 sampler::POINT, |
269 args[1].value_uint32 = | 191 sampler::NONE, |
270 set_sampler_states::AddressingU::MakeValue(sampler::CLAMP_TO_EDGE) | | 192 1); |
271 set_sampler_states::AddressingV::MakeValue(sampler::CLAMP_TO_EDGE) | | |
272 set_sampler_states::AddressingW::MakeValue(sampler::CLAMP_TO_EDGE) | | |
273 set_sampler_states::MagFilter::MakeValue(sampler::POINT) | | |
274 set_sampler_states::MinFilter::MakeValue(sampler::POINT) | | |
275 set_sampler_states::MipFilter::MakeValue(sampler::NONE) | | |
276 set_sampler_states::MaxAnisotropy::MakeValue(1); | |
277 cmd_buffer.AddCommand(command_buffer::SET_SAMPLER_STATES, 2, args); | |
278 | 193 |
279 // Create a parameter for the sampler. | 194 // Create a parameter for the sampler. |
280 const ResourceID sampler_param_id = 1; | 195 const ResourceID sampler_param_id = 1; |
281 { | 196 { |
282 const char param_name[] = "s0"; | 197 static const char param_name[] = "s0"; |
283 args[0].value_uint32 = sampler_param_id; | 198 cmd_buffer.CreateParamByNameImmediate(sampler_param_id, effect_id, |
284 args[1].value_uint32 = effect_id; | 199 sizeof(param_name), param_name); |
285 args[2].value_uint32 = sizeof(param_name); | |
286 unsigned int arg_count = CopyToArgs(args + 3, param_name, | |
287 sizeof(param_name)); | |
288 cmd_buffer.AddCommand(command_buffer::CREATE_PARAM_BY_NAME_IMMEDIATE, | |
289 3 + arg_count, args); | |
290 } | 200 } |
291 | 201 |
292 const ResourceID matrix_param_id = 2; | 202 const ResourceID matrix_param_id = 2; |
293 { | 203 { |
294 const char param_name[] = "worldViewProj"; | 204 static const char param_name[] = "worldViewProj"; |
295 args[0].value_uint32 = matrix_param_id; | 205 cmd_buffer.CreateParamByNameImmediate(matrix_param_id, effect_id, |
296 args[1].value_uint32 = effect_id; | 206 sizeof(param_name), param_name); |
297 args[2].value_uint32 = sizeof(param_name); | |
298 unsigned int arg_count = CopyToArgs(args + 3, param_name, | |
299 sizeof(param_name)); | |
300 cmd_buffer.AddCommand(command_buffer::CREATE_PARAM_BY_NAME_IMMEDIATE, | |
301 3 + arg_count, args); | |
302 } | 207 } |
303 | 208 |
304 float t = 0.f; | 209 float t = 0.f; |
305 while (true) { | 210 while (true) { |
306 t = fmodf(t + .01f, 1.f); | 211 t = fmodf(t + .01f, 1.f); |
307 math::Matrix4 m = | 212 math::Matrix4 m = |
308 math::Matrix4::translation(math::Vector3(0.f, 0.f, .5f)); | 213 math::Matrix4::translation(math::Vector3(0.f, 0.f, .5f)); |
309 m *= math::Matrix4::rotationY(t * 2 * 3.1415926f); | 214 m *= math::Matrix4::rotationY(t * 2 * 3.1415926f); |
310 cmd_buffer.AddCommand(command_buffer::BEGIN_FRAME, 0 , NULL); | 215 cmd_buffer.BeginFrame(); |
311 // Clear the background with an animated color (black to red). | 216 // Clear the background with an animated color (black to red). |
312 ClearCmd(&cmd_buffer, GAPIInterface::COLOR | GAPIInterface::DEPTH, color, | 217 cmd_buffer.Clear(GAPIInterface::COLOR | GAPIInterface::DEPTH, |
313 1.f, 0); | 218 color.red, color.green, color.blue, color.alpha, |
| 219 1.f, 0); |
314 | 220 |
315 args[0].value_uint32 = vertex_struct_id; | 221 cmd_buffer.SetVertexStruct(vertex_struct_id); |
316 cmd_buffer.AddCommand(command_buffer::SET_VERTEX_STRUCT, 1, args); | 222 cmd_buffer.SetEffect(effect_id); |
| 223 cmd_buffer.SetParamDataImmediate( |
| 224 sampler_param_id, sizeof(uint32), &sampler_id); |
| 225 cmd_buffer.SetParamDataImmediate( |
| 226 matrix_param_id, sizeof(m), &m); |
| 227 cmd_buffer.Draw(GAPIInterface::TRIANGLE_STRIPS, 0, 2); |
317 | 228 |
318 args[0].value_uint32 = effect_id; | 229 cmd_buffer.EndFrame(); |
319 cmd_buffer.AddCommand(command_buffer::SET_EFFECT, 1, args); | |
320 | |
321 args[0].value_uint32 = sampler_param_id; | |
322 args[1].value_uint32 = sizeof(Uint32); // NOLINT | |
323 args[2].value_uint32 = sampler_id; | |
324 cmd_buffer.AddCommand(command_buffer::SET_PARAM_DATA_IMMEDIATE, 3, args); | |
325 | |
326 args[0].value_uint32 = matrix_param_id; | |
327 args[1].value_uint32 = sizeof(m); | |
328 unsigned int arg_count = CopyToArgs(args + 2, &m, sizeof(m)); | |
329 cmd_buffer.AddCommand(command_buffer::SET_PARAM_DATA_IMMEDIATE, | |
330 2 + arg_count, args); | |
331 | |
332 args[0].value_uint32 = GAPIInterface::TRIANGLE_STRIPS; | |
333 args[1].value_uint32 = 0; // first | |
334 args[2].value_uint32 = 2; // primitive count | |
335 cmd_buffer.AddCommand(command_buffer::DRAW, 3, args); | |
336 | |
337 cmd_buffer.AddCommand(command_buffer::END_FRAME, 0 , NULL); | |
338 cmd_buffer.Flush(); | 230 cmd_buffer.Flush(); |
339 } | 231 } |
340 | 232 |
341 cmd_buffer.Finish(); | 233 cmd_buffer.Finish(); |
342 } | 234 } |
343 | 235 |
344 proxy.CloseConnection(); | 236 proxy.CloseConnection(); |
345 proxy.UnregisterSharedMemory(shm_id); | 237 proxy.UnregisterSharedMemory(shm_id); |
346 DestroyShm(shm); | 238 DestroyShm(shm); |
347 | 239 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 int main(int argc, char **argv) { | 290 int main(int argc, char **argv) { |
399 nacl::HtpHandle htp_handle = InitConnection(argc, argv); | 291 nacl::HtpHandle htp_handle = InitConnection(argc, argv); |
400 if (htp_handle == nacl::kInvalidHtpHandle) { | 292 if (htp_handle == nacl::kInvalidHtpHandle) { |
401 return 1; | 293 return 1; |
402 } | 294 } |
403 | 295 |
404 o3d::command_buffer::BigTestClient(htp_handle); | 296 o3d::command_buffer::BigTestClient(htp_handle); |
405 CloseConnection(htp_handle); | 297 CloseConnection(htp_handle); |
406 return 0; | 298 return 0; |
407 } | 299 } |
OLD | NEW |