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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 bool EffectHelper::CreateEffectParameters(ResourceID effect_id, | 46 bool EffectHelper::CreateEffectParameters(ResourceID effect_id, |
47 std::vector<EffectParamDesc> *descs) { | 47 std::vector<EffectParamDesc> *descs) { |
48 using effect_param::Desc; | 48 using effect_param::Desc; |
49 DCHECK_NE(effect_id, kInvalidResource); | 49 DCHECK_NE(effect_id, kInvalidResource); |
50 DCHECK(descs); | 50 DCHECK(descs); |
51 descs->clear(); | 51 descs->clear(); |
52 | 52 |
53 // Get the param count. | 53 // Get the param count. |
54 Uint32 *retval = shm_allocator_->AllocTyped<Uint32>(1); | 54 Uint32 *retval = shm_allocator_->AllocTyped<Uint32>(1); |
55 CommandBufferEntry args[4]; | 55 helper_->GetParamCount(effect_id, sizeof(*retval), |
56 args[0].value_uint32 = effect_id; | 56 shm_id_, shm_allocator_->GetOffset(retval)); |
57 args[1].value_uint32 = sizeof(*retval); | |
58 args[2].value_uint32 = shm_id_; | |
59 args[3].value_uint32 = shm_allocator_->GetOffset(retval); | |
60 helper_->AddCommand(GET_PARAM_COUNT, 4, args); | |
61 // Finish has to be called to get the result. | 57 // Finish has to be called to get the result. |
62 helper_->Finish(); | 58 helper_->Finish(); |
63 | 59 |
64 // We could have failed if the effect_id is invalid. | 60 // We could have failed if the effect_id is invalid. |
65 if (helper_->interface()->GetParseError() != | 61 if (helper_->interface()->GetParseError() != |
66 BufferSyncInterface::PARSE_NO_ERROR) { | 62 BufferSyncInterface::kParseNoError) { |
67 shm_allocator_->Free(retval); | 63 shm_allocator_->Free(retval); |
68 return false; | 64 return false; |
69 } | 65 } |
70 unsigned int param_count = *retval; | 66 unsigned int param_count = *retval; |
71 | 67 |
72 shm_allocator_->Free(retval); | 68 shm_allocator_->Free(retval); |
73 unsigned int max_buffer_size = shm_allocator_->GetLargestFreeOrPendingSize(); | 69 unsigned int max_buffer_size = shm_allocator_->GetLargestFreeOrPendingSize(); |
74 if (max_buffer_size < sizeof(Desc)) { // NOLINT | 70 if (max_buffer_size < sizeof(Desc)) { // NOLINT |
75 // Not enough memory to get at least 1 param desc. | 71 // Not enough memory to get at least 1 param desc. |
76 return false; | 72 return false; |
77 } | 73 } |
78 descs->resize(param_count); | 74 descs->resize(param_count); |
79 for (unsigned int i = 0; i < param_count; ++i) { | 75 for (unsigned int i = 0; i < param_count; ++i) { |
80 EffectParamDesc *desc = &((*descs)[i]); | 76 EffectParamDesc *desc = &((*descs)[i]); |
81 desc->id = param_id_allocator_->AllocateID(); | 77 desc->id = param_id_allocator_->AllocateID(); |
82 args[0].value_uint32 = desc->id; | 78 helper_->CreateParam(desc->id, effect_id, i); |
83 args[1].value_uint32 = effect_id; | |
84 args[2].value_uint32 = i; | |
85 helper_->AddCommand(CREATE_PARAM, 3, args); | |
86 } | 79 } |
87 | 80 |
88 // Read param descriptions in batches. We use as much shared memory as | 81 // Read param descriptions in batches. We use as much shared memory as |
89 // possible so that we only call Finish as little as possible. | 82 // possible so that we only call Finish as little as possible. |
90 unsigned int max_param_per_batch = | 83 unsigned int max_param_per_batch = |
91 std::min(static_cast<unsigned>(param_count), | 84 std::min(static_cast<unsigned>(param_count), |
92 static_cast<unsigned>(max_buffer_size / sizeof(Desc))); // NOLINT | 85 static_cast<unsigned>(max_buffer_size / sizeof(Desc))); // NOLINT |
93 Desc *raw_descs = shm_allocator_->AllocTyped<Desc>(max_param_per_batch); | 86 Desc *raw_descs = shm_allocator_->AllocTyped<Desc>(max_param_per_batch); |
94 DCHECK(raw_descs); | 87 DCHECK(raw_descs); |
95 for (unsigned int i = 0; i < param_count; i += max_param_per_batch) { | 88 for (unsigned int i = 0; i < param_count; i += max_param_per_batch) { |
96 unsigned int count = std::min(param_count - i, max_param_per_batch); | 89 unsigned int count = std::min(param_count - i, max_param_per_batch); |
97 for (unsigned int j = 0 ; j < count; ++j) { | 90 for (unsigned int j = 0 ; j < count; ++j) { |
98 EffectParamDesc *desc = &((*descs)[i + j]); | 91 EffectParamDesc *desc = &((*descs)[i + j]); |
99 Desc *raw_desc = raw_descs + j; | 92 Desc *raw_desc = raw_descs + j; |
100 args[0].value_uint32 = desc->id; | 93 helper_->GetParamDesc(desc->id, sizeof(*raw_desc), |
101 args[1].value_uint32 = sizeof(*raw_desc); | 94 shm_id_, |
102 args[2].value_uint32 = shm_id_; | 95 shm_allocator_->GetOffset(raw_desc)); |
103 args[3].value_uint32 = shm_allocator_->GetOffset(raw_desc); | |
104 helper_->AddCommand(GET_PARAM_DESC, 4, args); | |
105 } | 96 } |
106 // Finish to get the results. | 97 // Finish to get the results. |
107 helper_->Finish(); | 98 helper_->Finish(); |
108 DCHECK_EQ(helper_->interface()->GetParseError(), | 99 DCHECK_EQ(helper_->interface()->GetParseError(), |
109 BufferSyncInterface::PARSE_NO_ERROR); | 100 BufferSyncInterface::kParseNoError); |
110 for (unsigned int j = 0 ; j < count; ++j) { | 101 for (unsigned int j = 0 ; j < count; ++j) { |
111 EffectParamDesc *desc = &((*descs)[i + j]); | 102 EffectParamDesc *desc = &((*descs)[i + j]); |
112 Desc *raw_desc = raw_descs + j; | 103 Desc *raw_desc = raw_descs + j; |
113 desc->data_type = raw_desc->data_type; | 104 desc->data_type = raw_desc->data_type; |
114 desc->data_size = raw_desc->data_size; | 105 desc->data_size = raw_desc->data_size; |
115 desc->num_elements = raw_desc->num_elements; | 106 desc->num_elements = raw_desc->num_elements; |
116 desc->cmd_desc_size = raw_desc->size; | 107 desc->cmd_desc_size = raw_desc->size; |
117 } | 108 } |
118 } | 109 } |
119 shm_allocator_->Free(raw_descs); | 110 shm_allocator_->Free(raw_descs); |
120 return true; | 111 return true; |
121 } | 112 } |
122 | 113 |
123 bool EffectHelper::GetParamStrings(EffectParamDesc *desc) { | 114 bool EffectHelper::GetParamStrings(EffectParamDesc *desc) { |
124 using effect_param::Desc; | 115 using effect_param::Desc; |
125 DCHECK(desc); | 116 DCHECK(desc); |
126 DCHECK_NE(desc->id, kInvalidResource); | 117 DCHECK_NE(desc->id, kInvalidResource); |
127 // desc may not have come directly from CreateEffectParameters, so it may be | 118 // desc may not have come directly from CreateEffectParameters, so it may be |
128 // less than the minimum required size. | 119 // less than the minimum required size. |
129 unsigned int size = std::max(static_cast<unsigned>(desc->cmd_desc_size), | 120 unsigned int size = std::max(static_cast<unsigned>(desc->cmd_desc_size), |
130 static_cast<unsigned>(sizeof(Desc))); // NOLINT | 121 static_cast<unsigned>(sizeof(Desc))); // NOLINT |
131 Desc *raw_desc = static_cast<Desc *>(shm_allocator_->Alloc(size)); | 122 Desc *raw_desc = static_cast<Desc *>(shm_allocator_->Alloc(size)); |
132 if (!raw_desc) { | 123 if (!raw_desc) { |
133 // Not enough memory to get the param desc. | 124 // Not enough memory to get the param desc. |
134 return false; | 125 return false; |
135 } | 126 } |
136 CommandBufferEntry args[4]; | 127 helper_->GetParamDesc(desc->id, size, |
137 args[0].value_uint32 = desc->id; | 128 shm_id_, |
138 args[1].value_uint32 = size; | 129 shm_allocator_->GetOffset(raw_desc)); |
139 args[2].value_uint32 = shm_id_; | 130 |
140 args[3].value_uint32 = shm_allocator_->GetOffset(raw_desc); | |
141 helper_->AddCommand(GET_PARAM_DESC, 4, args); | |
142 // Finish to get the results. | 131 // Finish to get the results. |
143 helper_->Finish(); | 132 helper_->Finish(); |
144 | 133 |
145 // We could have failed if the param ID is invalid. | 134 // We could have failed if the param ID is invalid. |
146 if (helper_->interface()->GetParseError() != | 135 if (helper_->interface()->GetParseError() != |
147 BufferSyncInterface::PARSE_NO_ERROR) { | 136 BufferSyncInterface::kParseNoError) { |
148 shm_allocator_->Free(raw_desc); | 137 shm_allocator_->Free(raw_desc); |
149 return false; | 138 return false; |
150 } | 139 } |
151 | 140 |
152 if (raw_desc->size > size) { | 141 if (raw_desc->size > size) { |
153 // We had not allocated enough memory the first time (e.g. if the | 142 // We had not allocated enough memory the first time (e.g. if the |
154 // EffectParamDesc didn't come from CreateEffectParameters, so the user had | 143 // EffectParamDesc didn't come from CreateEffectParameters, so the user had |
155 // no way of knowing what size was needed for the strings), so re-allocate | 144 // no way of knowing what size was needed for the strings), so re-allocate |
156 // and try again. | 145 // and try again. |
157 size = raw_desc->size; | 146 size = raw_desc->size; |
158 desc->cmd_desc_size = size; | 147 desc->cmd_desc_size = size; |
159 shm_allocator_->Free(raw_desc); | 148 shm_allocator_->Free(raw_desc); |
160 raw_desc = static_cast<Desc *>(shm_allocator_->Alloc(size)); | 149 raw_desc = static_cast<Desc *>(shm_allocator_->Alloc(size)); |
161 if (!raw_desc) { | 150 if (!raw_desc) { |
162 // Not enough memory to get the param desc. | 151 // Not enough memory to get the param desc. |
163 return false; | 152 return false; |
164 } | 153 } |
165 args[1].value_uint32 = size; | 154 helper_->GetParamDesc(desc->id, size, |
166 args[3].value_uint32 = shm_allocator_->GetOffset(raw_desc); | 155 shm_id_, |
167 helper_->AddCommand(GET_PARAM_DESC, 4, args); | 156 shm_allocator_->GetOffset(raw_desc)); |
168 // Finish to get the results. | 157 // Finish to get the results. |
169 helper_->Finish(); | 158 helper_->Finish(); |
170 DCHECK_EQ(helper_->interface()->GetParseError(), | 159 DCHECK_EQ(helper_->interface()->GetParseError(), |
171 BufferSyncInterface::PARSE_NO_ERROR); | 160 BufferSyncInterface::kParseNoError); |
172 DCHECK_EQ(raw_desc->size, size); | 161 DCHECK_EQ(raw_desc->size, size); |
173 } | 162 } |
174 | 163 |
175 const char *raw_desc_string = reinterpret_cast<char *>(raw_desc); | 164 const char *raw_desc_string = reinterpret_cast<char *>(raw_desc); |
176 if (raw_desc->name_offset) { | 165 if (raw_desc->name_offset) { |
177 DCHECK_LE(raw_desc->name_offset + raw_desc->name_size, raw_desc->size); | 166 DCHECK_LE(raw_desc->name_offset + raw_desc->name_size, raw_desc->size); |
178 DCHECK_GT(raw_desc->name_size, 0U); | 167 DCHECK_GT(raw_desc->name_size, 0U); |
179 DCHECK_EQ(raw_desc_string[raw_desc->name_offset + raw_desc->name_size - 1], | 168 DCHECK_EQ(raw_desc_string[raw_desc->name_offset + raw_desc->name_size - 1], |
180 0); | 169 0); |
181 desc->name = String(raw_desc_string + raw_desc->name_offset, | 170 desc->name = String(raw_desc_string + raw_desc->name_offset, |
(...skipping 12 matching lines...) Expand all Loading... |
194 raw_desc->semantic_size - 1); | 183 raw_desc->semantic_size - 1); |
195 } else { | 184 } else { |
196 desc->semantic.clear(); | 185 desc->semantic.clear(); |
197 } | 186 } |
198 shm_allocator_->Free(raw_desc); | 187 shm_allocator_->Free(raw_desc); |
199 return true; | 188 return true; |
200 } | 189 } |
201 | 190 |
202 void EffectHelper::DestroyEffectParameters( | 191 void EffectHelper::DestroyEffectParameters( |
203 const std::vector<EffectParamDesc> &descs) { | 192 const std::vector<EffectParamDesc> &descs) { |
204 CommandBufferEntry args[1]; | |
205 for (unsigned int i = 0; i < descs.size(); ++i) { | 193 for (unsigned int i = 0; i < descs.size(); ++i) { |
206 const EffectParamDesc &desc = descs[i]; | 194 const EffectParamDesc &desc = descs[i]; |
207 args[0].value_uint32 = desc.id; | 195 helper_->DestroyParam(desc.id); |
208 helper_->AddCommand(DESTROY_PARAM, 1, args); | |
209 param_id_allocator_->FreeID(desc.id); | 196 param_id_allocator_->FreeID(desc.id); |
210 } | 197 } |
211 } | 198 } |
212 | 199 |
213 bool EffectHelper::GetEffectStreams(ResourceID effect_id, | 200 bool EffectHelper::GetEffectStreams(ResourceID effect_id, |
214 std::vector<EffectStreamDesc> *descs) { | 201 std::vector<EffectStreamDesc> *descs) { |
215 using effect_stream::Desc; | 202 using effect_stream::Desc; |
216 DCHECK_NE(effect_id, kInvalidResource); | 203 DCHECK_NE(effect_id, kInvalidResource); |
217 | 204 |
218 // Get the param count. | 205 // Get the param count. |
219 Uint32 *retval = shm_allocator_->AllocTyped<Uint32>(1); | 206 Uint32 *retval = shm_allocator_->AllocTyped<Uint32>(1); |
220 CommandBufferEntry args[5]; | 207 helper_->GetStreamCount(effect_id, sizeof(*retval), |
221 args[0].value_uint32 = effect_id; | 208 shm_id_, |
222 args[1].value_uint32 = sizeof(*retval); | 209 shm_allocator_->GetOffset(retval)); |
223 args[2].value_uint32 = shm_id_; | |
224 args[3].value_uint32 = shm_allocator_->GetOffset(retval); | |
225 helper_->AddCommand(GET_STREAM_COUNT, 4, args); | |
226 // Finish has to be called to get the result. | 210 // Finish has to be called to get the result. |
227 helper_->Finish(); | 211 helper_->Finish(); |
228 | 212 |
229 // We could have failed if the effect_id is invalid. | 213 // We could have failed if the effect_id is invalid. |
230 if (helper_->interface()->GetParseError() != | 214 if (helper_->interface()->GetParseError() != |
231 BufferSyncInterface::PARSE_NO_ERROR) { | 215 BufferSyncInterface::kParseNoError) { |
232 shm_allocator_->Free(retval); | 216 shm_allocator_->Free(retval); |
233 return false; | 217 return false; |
234 } | 218 } |
235 unsigned int stream_count = *retval; | 219 unsigned int stream_count = *retval; |
236 shm_allocator_->Free(retval); | 220 shm_allocator_->Free(retval); |
237 unsigned int max_buffer_size = shm_allocator_->GetLargestFreeOrPendingSize(); | 221 unsigned int max_buffer_size = shm_allocator_->GetLargestFreeOrPendingSize(); |
238 if (max_buffer_size < sizeof(Desc)) { // NOLINT | 222 if (max_buffer_size < sizeof(Desc)) { // NOLINT |
239 // Not enough memory to get at least 1 stream desc. | 223 // Not enough memory to get at least 1 stream desc. |
240 return false; | 224 return false; |
241 } | 225 } |
242 descs->resize(stream_count); | 226 descs->resize(stream_count); |
243 | 227 |
244 // Read stream descriptions in batches. We use as much shared memory as | 228 // Read stream descriptions in batches. We use as much shared memory as |
245 // possible so that we only call Finish as little as possible. | 229 // possible so that we only call Finish as little as possible. |
246 unsigned int max_stream_per_batch = | 230 unsigned int max_stream_per_batch = |
247 std::min(static_cast<unsigned>(stream_count), | 231 std::min(static_cast<unsigned>(stream_count), |
248 static_cast<unsigned>(max_buffer_size / sizeof(Desc))); // NOLINT | 232 static_cast<unsigned>(max_buffer_size / sizeof(Desc))); // NOLINT |
249 Desc *raw_descs = shm_allocator_->AllocTyped<Desc>(max_stream_per_batch); | 233 Desc *raw_descs = shm_allocator_->AllocTyped<Desc>(max_stream_per_batch); |
250 DCHECK(raw_descs); | 234 DCHECK(raw_descs); |
251 for (unsigned int i = 0; i < stream_count; i += max_stream_per_batch) { | 235 for (unsigned int i = 0; i < stream_count; i += max_stream_per_batch) { |
252 unsigned int count = std::min(stream_count - i, max_stream_per_batch); | 236 unsigned int count = std::min(stream_count - i, max_stream_per_batch); |
253 for (unsigned int j = 0 ; j < count; ++j) { | 237 for (unsigned int j = 0 ; j < count; ++j) { |
254 EffectStreamDesc *desc = &((*descs)[i + j]); | 238 EffectStreamDesc *desc = &((*descs)[i + j]); |
255 Desc *raw_desc = raw_descs + j; | 239 Desc *raw_desc = raw_descs + j; |
256 args[0].value_uint32 = effect_id; | 240 helper_->GetStreamDesc(effect_id, i + j, sizeof(*raw_desc), |
257 args[1].value_uint32 = i+j; | 241 shm_id_, |
258 args[2].value_uint32 = sizeof(*raw_desc); | 242 shm_allocator_->GetOffset(raw_desc)); |
259 args[3].value_uint32 = shm_id_; | |
260 args[4].value_uint32 = shm_allocator_->GetOffset(raw_desc); | |
261 helper_->AddCommand(GET_STREAM_DESC, 5, args); | |
262 } | 243 } |
263 // Finish to get the results. | 244 // Finish to get the results. |
264 helper_->Finish(); | 245 helper_->Finish(); |
265 DCHECK_EQ(helper_->interface()->GetParseError(), | 246 DCHECK_EQ(helper_->interface()->GetParseError(), |
266 BufferSyncInterface::PARSE_NO_ERROR); | 247 BufferSyncInterface::kParseNoError); |
267 for (unsigned int j = 0 ; j < count; ++j) { | 248 for (unsigned int j = 0 ; j < count; ++j) { |
268 EffectStreamDesc *desc = &((*descs)[i + j]); | 249 EffectStreamDesc *desc = &((*descs)[i + j]); |
269 Desc *raw_desc = raw_descs + j; | 250 Desc *raw_desc = raw_descs + j; |
270 desc->semantic = static_cast<vertex_struct::Semantic>(raw_desc->semantic); | 251 desc->semantic = static_cast<vertex_struct::Semantic>(raw_desc->semantic); |
271 desc->semantic_index = raw_desc->semantic_index; | 252 desc->semantic_index = raw_desc->semantic_index; |
272 } | 253 } |
273 } | 254 } |
274 shm_allocator_->Free(raw_descs); | 255 shm_allocator_->Free(raw_descs); |
275 return true; | 256 return true; |
276 } | 257 } |
277 } // namespace command_buffer | 258 } // namespace command_buffer |
278 } // namespace o3d | 259 } // namespace o3d |
OLD | NEW |