this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Removed GPU_BlitBatch() and GPU_BlitBatchSeparate(). Implementations of these based on GPU_TriangleBatch() can still be found in the blit-batch-test. They've been removed so the batching input format can be kept simple, all the batch conversion code can be removed, and the batching feature set can be focused better.

Replaced GPU_BlitFlagEnum with GPU_BatchFlagEnum, which has different values and semantics.

Changed implementation of GPU_TriangleBatch() to support the new flags for more flexibility within a more concrete format. This also allows for 3D geometry batching.

+867 -1294
+20 -30
include/SDL_gpu.h
··· 424 424 static const Uint32 GPU_NONE = 0x0; 425 425 426 426 /*! \ingroup Rendering 427 - * Bit flags for the blit batch functions. 428 - * \see GPU_BlitBatch() 429 - * \see GPU_BlitBatchSeparate() 427 + * Bit flags for geometry batching. 428 + * \see GPU_TriangleBatch() 430 429 */ 431 - typedef Uint32 GPU_BlitFlagEnum; 432 - static const GPU_BlitFlagEnum GPU_PASSTHROUGH_VERTICES = 0x1; 433 - static const GPU_BlitFlagEnum GPU_PASSTHROUGH_TEXCOORDS = 0x2; 434 - static const GPU_BlitFlagEnum GPU_PASSTHROUGH_COLORS = 0x4; 435 - static const GPU_BlitFlagEnum GPU_USE_DEFAULT_POSITIONS = 0x8; 436 - static const GPU_BlitFlagEnum GPU_USE_DEFAULT_SRC_RECTS = 0x10; 437 - static const GPU_BlitFlagEnum GPU_USE_DEFAULT_COLORS = 0x20; 430 + typedef Uint32 GPU_BatchFlagEnum; 431 + static const GPU_BatchFlagEnum GPU_BATCH_XY = 0x1; 432 + static const GPU_BatchFlagEnum GPU_BATCH_XYZ = 0x2; 433 + static const GPU_BatchFlagEnum GPU_BATCH_ST = 0x4; 434 + static const GPU_BatchFlagEnum GPU_BATCH_RGB = 0x8; 435 + static const GPU_BatchFlagEnum GPU_BATCH_RGBA = 0x10; 438 436 439 - #define GPU_PASSTHROUGH_ALL (GPU_PASSTHROUGH_VERTICES | GPU_PASSTHROUGH_TEXCOORDS | GPU_PASSTHROUGH_COLORS) 437 + #define GPU_BATCH_XY_ST (GPU_BATCH_XY | GPU_BATCH_ST) 438 + #define GPU_BATCH_XYZ_ST (GPU_BATCH_XYZ | GPU_BATCH_ST) 439 + #define GPU_BATCH_XY_RGB (GPU_BATCH_XY | GPU_BATCH_RGB) 440 + #define GPU_BATCH_XYZ_RGB (GPU_BATCH_XYZ | GPU_BATCH_RGB) 441 + #define GPU_BATCH_XY_RGBA (GPU_BATCH_XY | GPU_BATCH_RGBA) 442 + #define GPU_BATCH_XYZ_RGBA (GPU_BATCH_XYZ | GPU_BATCH_RGBA) 443 + #define GPU_BATCH_XY_ST_RGBA (GPU_BATCH_XY | GPU_BATCH_ST | GPU_BATCH_RGBA) 444 + #define GPU_BATCH_XYZ_ST_RGBA (GPU_BATCH_XYZ | GPU_BATCH_ST | GPU_BATCH_RGBA) 440 445 441 446 /*! \ingroup ShaderInterface 442 447 * Type enumeration for GPU_AttributeFormat specifications. ··· 1184 1189 * \param matrix3x3 3x3 matrix in column-major order (index = row + column*numColumns) */ 1185 1190 DECLSPEC void SDLCALL GPU_BlitTransformMatrix(GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y, float* matrix3x3); 1186 1191 1187 - /*! Performs 'num_sprites' blits of the given image to the given target. 1188 - * Note: GPU_BlitBatch() cannot interpret a mix of normal values and "passthrough" values due to format ambiguity. 1189 - * \param values A tightly-packed array of position (x,y), src_rect (x,y,w,h) values in image coordinates, and color (r,g,b,a) values with a range from 0-255. Pass NULL to render with only custom shader attributes. 1190 - * \param flags Bit flags to control the interpretation of the array parameters. The only passthrough option accepted is GPU_PASSTHROUGH_ALL. 1191 - */ 1192 - DECLSPEC void SDLCALL GPU_BlitBatch(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, GPU_BlitFlagEnum flags); 1193 - 1194 - /*! Performs 'num_sprites' blits of the given image to the given target. 1195 - * \param positions A tightly-packed array of (x,y) values 1196 - * \param src_rects A tightly-packed array of (x,y,w,h) values in image coordinates 1197 - * \param colors A tightly-packed array of (r,g,b,a) values with a range from 0-255 1198 - * \param flags Bit flags to control the interpretation of the array parameters 1199 - */ 1200 - DECLSPEC void SDLCALL GPU_BlitBatchSeparate(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* positions, float* src_rects, float* colors, GPU_BlitFlagEnum flags); 1201 - 1202 - /*! Renders triangles from the given set of vertices. This lets you render arbitrary 2D geometry. 1203 - * \param values A tightly-packed array of vertex position (x,y), image coordinates (s,t), and color (r,g,b,a) values with a range from 0-255. Pass NULL to render with only custom shader attributes. 1192 + /*! Renders triangles from the given set of vertices. This lets you render arbitrary 2D geometry. It is a direct path to the GPU, so the format is different than typical SDL_gpu calls. 1193 + * \param values A tightly-packed array of vertex position (e.g. x,y), texture coordinates (e.g. s,t), and color (e.g. r,g,b,a) values. Texture coordinates and color values are expected to be already normalized to 0.0 - 1.0. Pass NULL to render with only custom shader attributes. 1204 1194 * \param indices If not NULL, this is used to specify which vertices to use and in what order (i.e. it indexes the vertices in the 'values' array). 1205 - * \param flags Bit flags to control the interpretation of the array parameters. Since 'values' contains per-vertex data, GPU_PASSTHROUGH_VERTICES is ignored. Texture coordinates are scaled down using the image dimensions and color components are normalized to [0.0, 1.0]. 1195 + * \param flags Bit flags to control the interpretation of the 'values' array parameters. 1206 1196 */ 1207 - DECLSPEC void SDLCALL GPU_TriangleBatch(GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BlitFlagEnum flags); 1197 + DECLSPEC void SDLCALL GPU_TriangleBatch(GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BatchFlagEnum flags); 1208 1198 1209 1199 /*! Send all buffered blitting data to the current context target. */ 1210 1200 DECLSPEC void SDLCALL GPU_FlushBlitBuffer(void);
+1 -4
include/SDL_gpu_RendererImpl.h
··· 118 118 /*! \see GPU_BlitTransformMatrix() */ 119 119 void (SDLCALL *BlitTransformMatrix)(GPU_Renderer* renderer, GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y, float* matrix3x3); 120 120 121 - /*! \see GPU_BlitBatch() */ 122 - void (SDLCALL *BlitBatch)(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, GPU_BlitFlagEnum flags); 123 - 124 121 /*! \see GPU_TriangleBatch() */ 125 - void (SDLCALL *TriangleBatch)(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BlitFlagEnum flags); 122 + void (SDLCALL *TriangleBatch)(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BatchFlagEnum flags); 126 123 127 124 /*! \see GPU_GenerateMipmaps() */ 128 125 void (SDLCALL *GenerateMipmaps)(GPU_Renderer* renderer, GPU_Image* image);
+2 -762
src/SDL_gpu.c
··· 1126 1126 current_renderer->impl->BlitTransformMatrix(current_renderer, image, src_rect, target, x, y, matrix3x3); 1127 1127 } 1128 1128 1129 - void GPU_BlitBatch(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, GPU_BlitFlagEnum flags) 1130 - { 1131 - int src_position_floats_per_sprite; 1132 - int src_rect_floats_per_sprite; 1133 - int src_color_floats_per_sprite; 1134 - 1135 - Uint8 no_positions; 1136 - Uint8 no_rects; 1137 - Uint8 no_colors; 1138 - Uint8 pass_vertices; 1139 - Uint8 pass_texcoords; 1140 - Uint8 pass_colors; 1141 - 1142 - int src_floats_per_sprite; 1143 - 1144 - int size; 1145 - float* new_values; 1146 - 1147 - unsigned int n; // The sprite number iteration variable. 1148 - // Source indices (per sprite) 1149 - int pos_n; 1150 - int rect_n; 1151 - int color_n; 1152 - // Dest indices 1153 - int vert_i; 1154 - int texcoord_i; 1155 - int color_i; 1156 - // Dest float stride 1157 - int floats_per_vertex; 1158 - 1159 - float w2; // texcoord helpers for position expansion 1160 - float h2; 1161 - 1162 - Uint32 tex_w; 1163 - Uint32 tex_h; 1164 - 1165 - if(!CHECK_RENDERER) 1166 - RETURN_ERROR(GPU_ERROR_USER_ERROR, "NULL renderer"); 1167 - if(!CHECK_CONTEXT) 1168 - RETURN_ERROR(GPU_ERROR_USER_ERROR, "NULL context"); 1169 - 1170 - if(image == NULL) 1171 - RETURN_ERROR(GPU_ERROR_NULL_ARGUMENT, "image"); 1172 - if(target == NULL) 1173 - RETURN_ERROR(GPU_ERROR_NULL_ARGUMENT, "target"); 1174 - 1175 - if(num_sprites == 0) 1176 - return; 1177 - 1178 - // Is it already in the right format? 1179 - if((flags & GPU_PASSTHROUGH_ALL) == GPU_PASSTHROUGH_ALL || values == NULL) 1180 - { 1181 - current_renderer->impl->BlitBatch(current_renderer, image, target, num_sprites, values, flags); 1182 - return; 1183 - } 1184 - 1185 - // Conversion time... 1186 - 1187 - // Convert condensed interleaved format into full interleaved format for the renderer to use. 1188 - // Condensed: Each vertex has 2 pos, 4 rect, 4 color 1189 - 1190 - // Default values: Each sprite is defined by a position, a rect, and a color. 1191 - src_position_floats_per_sprite = 2; 1192 - src_rect_floats_per_sprite = 4; 1193 - src_color_floats_per_sprite = 4; 1194 - 1195 - no_positions = (Uint8)(flags & GPU_USE_DEFAULT_POSITIONS); 1196 - no_rects = (Uint8)(flags & GPU_USE_DEFAULT_SRC_RECTS); 1197 - no_colors = (Uint8)(flags & GPU_USE_DEFAULT_COLORS); 1198 - pass_vertices = (Uint8)(flags & GPU_PASSTHROUGH_VERTICES); 1199 - pass_texcoords = (Uint8)(flags & GPU_PASSTHROUGH_TEXCOORDS); 1200 - pass_colors = (Uint8)(flags & GPU_PASSTHROUGH_COLORS); 1201 - 1202 - // Passthrough data is per-vertex. Non-passthrough is per-sprite. They can't interleave cleanly. 1203 - if(flags & GPU_PASSTHROUGH_ALL && (flags & GPU_PASSTHROUGH_ALL) != GPU_PASSTHROUGH_ALL) 1204 - { 1205 - GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Cannot interpret interleaved data using partial passthrough"); 1206 - return; 1207 - } 1208 - 1209 - if(pass_vertices) 1210 - src_position_floats_per_sprite = 8; // 4 vertices of x, y 1211 - if(pass_texcoords) 1212 - src_rect_floats_per_sprite = 8; // 4 vertices of s, t 1213 - if(pass_colors) 1214 - src_color_floats_per_sprite = 16; // 4 vertices of r, g, b, a 1215 - if(no_positions) 1216 - src_position_floats_per_sprite = 0; 1217 - if(no_rects) 1218 - src_rect_floats_per_sprite = 0; 1219 - if(no_colors) 1220 - src_color_floats_per_sprite = 0; 1221 - 1222 - src_floats_per_sprite = src_position_floats_per_sprite + src_rect_floats_per_sprite + src_color_floats_per_sprite; 1223 - 1224 - size = num_sprites*(8 + 8 + 16); 1225 - new_values = (float*)malloc(sizeof(float)*size); 1226 - 1227 - // Source indices (per sprite) 1228 - pos_n = 0; 1229 - rect_n = src_position_floats_per_sprite; 1230 - color_n = src_position_floats_per_sprite + src_rect_floats_per_sprite; 1231 - // Dest indices 1232 - vert_i = 0; 1233 - texcoord_i = 2; 1234 - color_i = 4; 1235 - // Dest float stride 1236 - floats_per_vertex = 8; 1237 - 1238 - w2 = 0.5f*image->w; // texcoord helpers for position expansion 1239 - h2 = 0.5f*image->h; 1240 - 1241 - tex_w = image->texture_w; 1242 - tex_h = image->texture_h; 1243 - 1244 - for(n = 0; n < num_sprites; n++) 1245 - { 1246 - if(no_rects) 1247 - { 1248 - new_values[texcoord_i] = 0.0f; 1249 - new_values[texcoord_i+1] = 0.0f; 1250 - texcoord_i += floats_per_vertex; 1251 - new_values[texcoord_i] = 1.0f; 1252 - new_values[texcoord_i+1] = 0.0f; 1253 - texcoord_i += floats_per_vertex; 1254 - new_values[texcoord_i] = 1.0f; 1255 - new_values[texcoord_i+1] = 1.0f; 1256 - texcoord_i += floats_per_vertex; 1257 - new_values[texcoord_i] = 0.0f; 1258 - new_values[texcoord_i+1] = 1.0f; 1259 - texcoord_i += floats_per_vertex; 1260 - } 1261 - else 1262 - { 1263 - if(!pass_texcoords) 1264 - { 1265 - float s1 = values[rect_n]/tex_w; 1266 - float t1 = values[rect_n+1]/tex_h; 1267 - float s3 = s1 + values[rect_n+2]/tex_w; 1268 - float t3 = t1 + values[rect_n+3]/tex_h; 1269 - rect_n += src_floats_per_sprite; 1270 - 1271 - new_values[texcoord_i] = s1; 1272 - new_values[texcoord_i+1] = t1; 1273 - texcoord_i += floats_per_vertex; 1274 - new_values[texcoord_i] = s3; 1275 - new_values[texcoord_i+1] = t1; 1276 - texcoord_i += floats_per_vertex; 1277 - new_values[texcoord_i] = s3; 1278 - new_values[texcoord_i+1] = t3; 1279 - texcoord_i += floats_per_vertex; 1280 - new_values[texcoord_i] = s1; 1281 - new_values[texcoord_i+1] = t3; 1282 - texcoord_i += floats_per_vertex; 1283 - 1284 - if(!pass_vertices) 1285 - { 1286 - w2 = 0.5f*(s3-s1)*image->w; 1287 - h2 = 0.5f*(t3-t1)*image->h; 1288 - } 1289 - } 1290 - else 1291 - { 1292 - // 4 vertices all in a row 1293 - float s1, t1, s3, t3; 1294 - s1 = new_values[texcoord_i] = values[rect_n]; 1295 - t1 = new_values[texcoord_i+1] = values[rect_n+1]; 1296 - texcoord_i += floats_per_vertex; 1297 - new_values[texcoord_i] = values[rect_n+2]; 1298 - new_values[texcoord_i+1] = values[rect_n+3]; 1299 - texcoord_i += floats_per_vertex; 1300 - s3 = new_values[texcoord_i] = values[rect_n+4]; 1301 - t3 = new_values[texcoord_i+1] = values[rect_n+5]; 1302 - texcoord_i += floats_per_vertex; 1303 - new_values[texcoord_i] = values[rect_n+6]; 1304 - new_values[texcoord_i+1] = values[rect_n+7]; 1305 - texcoord_i += floats_per_vertex; 1306 - rect_n += src_floats_per_sprite; 1307 - 1308 - if(!pass_vertices) 1309 - { 1310 - w2 = 0.5f*(s3-s1)*image->w; 1311 - h2 = 0.5f*(t3-t1)*image->h; 1312 - } 1313 - } 1314 - } 1315 - 1316 - if(no_positions) 1317 - { 1318 - new_values[vert_i] = 0.0f; 1319 - new_values[vert_i+1] = 0.0f; 1320 - vert_i += floats_per_vertex; 1321 - new_values[vert_i] = 0.0f; 1322 - new_values[vert_i+1] = 0.0f; 1323 - vert_i += floats_per_vertex; 1324 - new_values[vert_i] = 0.0f; 1325 - new_values[vert_i+1] = 0.0f; 1326 - vert_i += floats_per_vertex; 1327 - new_values[vert_i] = 0.0f; 1328 - new_values[vert_i+1] = 0.0f; 1329 - vert_i += floats_per_vertex; 1330 - } 1331 - else 1332 - { 1333 - if(!pass_vertices) 1334 - { 1335 - // Expand vertices from the position and dimensions 1336 - float x = values[pos_n]; 1337 - float y = values[pos_n+1]; 1338 - pos_n += src_floats_per_sprite; 1339 - 1340 - new_values[vert_i] = x - w2; 1341 - new_values[vert_i+1] = y - h2; 1342 - vert_i += floats_per_vertex; 1343 - new_values[vert_i] = x + w2; 1344 - new_values[vert_i+1] = y - h2; 1345 - vert_i += floats_per_vertex; 1346 - new_values[vert_i] = x + w2; 1347 - new_values[vert_i+1] = y + h2; 1348 - vert_i += floats_per_vertex; 1349 - new_values[vert_i] = x - w2; 1350 - new_values[vert_i+1] = y + h2; 1351 - vert_i += floats_per_vertex; 1352 - } 1353 - else 1354 - { 1355 - // 4 vertices all in a row 1356 - new_values[vert_i] = values[pos_n]; 1357 - new_values[vert_i+1] = values[pos_n+1]; 1358 - vert_i += floats_per_vertex; 1359 - new_values[vert_i] = values[pos_n+2]; 1360 - new_values[vert_i+1] = values[pos_n+3]; 1361 - vert_i += floats_per_vertex; 1362 - new_values[vert_i] = values[pos_n+4]; 1363 - new_values[vert_i+1] = values[pos_n+5]; 1364 - vert_i += floats_per_vertex; 1365 - new_values[vert_i] = values[pos_n+6]; 1366 - new_values[vert_i+1] = values[pos_n+7]; 1367 - vert_i += floats_per_vertex; 1368 - pos_n += src_floats_per_sprite; 1369 - } 1370 - } 1371 - 1372 - if(no_colors) 1373 - { 1374 - new_values[color_i] = 1.0f; 1375 - new_values[color_i+1] = 1.0f; 1376 - new_values[color_i+2] = 1.0f; 1377 - new_values[color_i+3] = 1.0f; 1378 - color_i += floats_per_vertex; 1379 - new_values[color_i] = 1.0f; 1380 - new_values[color_i+1] = 1.0f; 1381 - new_values[color_i+2] = 1.0f; 1382 - new_values[color_i+3] = 1.0f; 1383 - color_i += floats_per_vertex; 1384 - new_values[color_i] = 1.0f; 1385 - new_values[color_i+1] = 1.0f; 1386 - new_values[color_i+2] = 1.0f; 1387 - new_values[color_i+3] = 1.0f; 1388 - color_i += floats_per_vertex; 1389 - new_values[color_i] = 1.0f; 1390 - new_values[color_i+1] = 1.0f; 1391 - new_values[color_i+2] = 1.0f; 1392 - new_values[color_i+3] = 1.0f; 1393 - color_i += floats_per_vertex; 1394 - } 1395 - else 1396 - { 1397 - if(!pass_colors) 1398 - { 1399 - float r = values[color_n]/255.0f; 1400 - float g = values[color_n+1]/255.0f; 1401 - float b = values[color_n+2]/255.0f; 1402 - float a = values[color_n+3]/255.0f; 1403 - color_n += src_floats_per_sprite; 1404 - 1405 - new_values[color_i] = r; 1406 - new_values[color_i+1] = g; 1407 - new_values[color_i+2] = b; 1408 - new_values[color_i+3] = a; 1409 - color_i += floats_per_vertex; 1410 - new_values[color_i] = r; 1411 - new_values[color_i+1] = g; 1412 - new_values[color_i+2] = b; 1413 - new_values[color_i+3] = a; 1414 - color_i += floats_per_vertex; 1415 - new_values[color_i] = r; 1416 - new_values[color_i+1] = g; 1417 - new_values[color_i+2] = b; 1418 - new_values[color_i+3] = a; 1419 - color_i += floats_per_vertex; 1420 - new_values[color_i] = r; 1421 - new_values[color_i+1] = g; 1422 - new_values[color_i+2] = b; 1423 - new_values[color_i+3] = a; 1424 - color_i += floats_per_vertex; 1425 - } 1426 - else 1427 - { 1428 - // 4 vertices all in a row 1429 - new_values[color_i] = values[color_n]; 1430 - new_values[color_i+1] = values[color_n+1]; 1431 - new_values[color_i+2] = values[color_n+2]; 1432 - new_values[color_i+3] = values[color_n+3]; 1433 - color_i += floats_per_vertex; 1434 - new_values[color_i] = values[color_n+4]; 1435 - new_values[color_i+1] = values[color_n+5]; 1436 - new_values[color_i+2] = values[color_n+6]; 1437 - new_values[color_i+3] = values[color_n+7]; 1438 - color_i += floats_per_vertex; 1439 - new_values[color_i] = values[color_n+8]; 1440 - new_values[color_i+1] = values[color_n+9]; 1441 - new_values[color_i+2] = values[color_n+10]; 1442 - new_values[color_i+3] = values[color_n+11]; 1443 - color_i += floats_per_vertex; 1444 - new_values[color_i] = values[color_n+12]; 1445 - new_values[color_i+1] = values[color_n+13]; 1446 - new_values[color_i+2] = values[color_n+14]; 1447 - new_values[color_i+3] = values[color_n+15]; 1448 - color_i += floats_per_vertex; 1449 - color_n += src_floats_per_sprite; 1450 - } 1451 - } 1452 - } 1453 - 1454 - current_renderer->impl->BlitBatch(current_renderer, image, target, num_sprites, new_values, flags | GPU_PASSTHROUGH_ALL); 1455 - 1456 - free(new_values); 1457 - } 1458 - 1459 - void GPU_BlitBatchSeparate(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* positions, float* src_rects, float* colors, GPU_BlitFlagEnum flags) 1460 - { 1461 - Uint8 pass_vertices; 1462 - Uint8 pass_texcoords; 1463 - Uint8 pass_colors; 1464 - 1465 - int size; // 4 vertices of x, y... s, t... r, g, b, a 1466 - float* values; 1467 - 1468 - unsigned int n; // The sprite number iteration variable. 1469 - // Source indices 1470 - int pos_n; 1471 - int rect_n; 1472 - int color_n; 1473 - // Dest indices 1474 - int vert_i; 1475 - int texcoord_i; 1476 - int color_i; 1477 - // Dest float stride 1478 - int floats_per_vertex; 1479 - 1480 - float w2; // texcoord helpers for position expansion 1481 - float h2; 1482 - 1483 - Uint32 tex_w; 1484 - Uint32 tex_h; 1485 - 1486 - if(!CHECK_RENDERER) 1487 - RETURN_ERROR(GPU_ERROR_USER_ERROR, "NULL renderer"); 1488 - if(!CHECK_CONTEXT) 1489 - RETURN_ERROR(GPU_ERROR_USER_ERROR, "NULL context"); 1490 - 1491 - if(image == NULL) 1492 - RETURN_ERROR(GPU_ERROR_NULL_ARGUMENT, "image"); 1493 - if(target == NULL) 1494 - RETURN_ERROR(GPU_ERROR_NULL_ARGUMENT, "target"); 1495 - 1496 - if(num_sprites == 0) 1497 - return; 1498 - 1499 - // No data to repack? Skip it. 1500 - if(positions == NULL && src_rects == NULL && colors == NULL) 1501 - { 1502 - current_renderer->impl->BlitBatch(current_renderer, image, target, num_sprites, NULL, flags); 1503 - return; 1504 - } 1505 - 1506 - // Repack the given arrays into an interleaved array for more efficient access 1507 - // Default values: Each sprite is defined by a position, a rect, and a color. 1508 - 1509 - pass_vertices = (Uint8)(flags & GPU_PASSTHROUGH_VERTICES); 1510 - pass_texcoords = (Uint8)(flags & GPU_PASSTHROUGH_TEXCOORDS); 1511 - pass_colors = (Uint8)(flags & GPU_PASSTHROUGH_COLORS); 1512 - 1513 - size = num_sprites*(8 + 8 + 16); // 4 vertices of x, y... s, t... r, g, b, a 1514 - values = (float*)malloc(sizeof(float)*size); 1515 - 1516 - // Source indices 1517 - pos_n = 0; 1518 - rect_n = 0; 1519 - color_n = 0; 1520 - // Dest indices 1521 - vert_i = 0; 1522 - texcoord_i = 2; 1523 - color_i = 4; 1524 - // Dest float stride 1525 - floats_per_vertex = 8; 1526 - 1527 - w2 = 0.5f*image->w; // texcoord helpers for position expansion 1528 - h2 = 0.5f*image->h; 1529 - 1530 - tex_w = image->texture_w; 1531 - tex_h = image->texture_h; 1532 - 1533 - for(n = 0; n < num_sprites; n++) 1534 - { 1535 - // Unpack the arrays 1536 - 1537 - if(src_rects == NULL) 1538 - { 1539 - values[texcoord_i] = 0.0f; 1540 - values[texcoord_i+1] = 0.0f; 1541 - texcoord_i += floats_per_vertex; 1542 - values[texcoord_i] = 1.0f; 1543 - values[texcoord_i+1] = 0.0f; 1544 - texcoord_i += floats_per_vertex; 1545 - values[texcoord_i] = 1.0f; 1546 - values[texcoord_i+1] = 1.0f; 1547 - texcoord_i += floats_per_vertex; 1548 - values[texcoord_i] = 0.0f; 1549 - values[texcoord_i+1] = 1.0f; 1550 - texcoord_i += floats_per_vertex; 1551 - } 1552 - else 1553 - { 1554 - if(!pass_texcoords) 1555 - { 1556 - float s1 = src_rects[rect_n++]/tex_w; 1557 - float t1 = src_rects[rect_n++]/tex_h; 1558 - float s3 = s1 + src_rects[rect_n++]/tex_w; 1559 - float t3 = t1 + src_rects[rect_n++]/tex_h; 1560 - 1561 - values[texcoord_i] = s1; 1562 - values[texcoord_i+1] = t1; 1563 - texcoord_i += floats_per_vertex; 1564 - values[texcoord_i] = s3; 1565 - values[texcoord_i+1] = t1; 1566 - texcoord_i += floats_per_vertex; 1567 - values[texcoord_i] = s3; 1568 - values[texcoord_i+1] = t3; 1569 - texcoord_i += floats_per_vertex; 1570 - values[texcoord_i] = s1; 1571 - values[texcoord_i+1] = t3; 1572 - texcoord_i += floats_per_vertex; 1573 - 1574 - if(!pass_vertices) 1575 - { 1576 - w2 = 0.5f*(s3-s1)*image->w; 1577 - h2 = 0.5f*(t3-t1)*image->h; 1578 - } 1579 - } 1580 - else 1581 - { 1582 - // 4 vertices all in a row 1583 - float s1, t1, s3, t3; 1584 - s1 = values[texcoord_i] = src_rects[rect_n++]; 1585 - t1 = values[texcoord_i+1] = src_rects[rect_n++]; 1586 - texcoord_i += floats_per_vertex; 1587 - values[texcoord_i] = src_rects[rect_n++]; 1588 - values[texcoord_i+1] = src_rects[rect_n++]; 1589 - texcoord_i += floats_per_vertex; 1590 - s3 = values[texcoord_i] = src_rects[rect_n++]; 1591 - t3 = values[texcoord_i+1] = src_rects[rect_n++]; 1592 - texcoord_i += floats_per_vertex; 1593 - values[texcoord_i] = src_rects[rect_n++]; 1594 - values[texcoord_i+1] = src_rects[rect_n++]; 1595 - texcoord_i += floats_per_vertex; 1596 - 1597 - if(!pass_vertices) 1598 - { 1599 - w2 = 0.5f*(s3-s1)*image->w; 1600 - h2 = 0.5f*(t3-t1)*image->h; 1601 - } 1602 - } 1603 - } 1604 - 1605 - if(positions == NULL) 1606 - { 1607 - values[vert_i] = 0.0f; 1608 - values[vert_i+1] = 0.0f; 1609 - vert_i += floats_per_vertex; 1610 - values[vert_i] = 0.0f; 1611 - values[vert_i+1] = 0.0f; 1612 - vert_i += floats_per_vertex; 1613 - values[vert_i] = 0.0f; 1614 - values[vert_i+1] = 0.0f; 1615 - vert_i += floats_per_vertex; 1616 - values[vert_i] = 0.0f; 1617 - values[vert_i+1] = 0.0f; 1618 - vert_i += floats_per_vertex; 1619 - } 1620 - else 1621 - { 1622 - if(!pass_vertices) 1623 - { 1624 - // Expand vertices from the position and dimensions 1625 - float x = positions[pos_n++]; 1626 - float y = positions[pos_n++]; 1627 - values[vert_i] = x - w2; 1628 - values[vert_i+1] = y - h2; 1629 - vert_i += floats_per_vertex; 1630 - values[vert_i] = x + w2; 1631 - values[vert_i+1] = y - h2; 1632 - vert_i += floats_per_vertex; 1633 - values[vert_i] = x + w2; 1634 - values[vert_i+1] = y + h2; 1635 - vert_i += floats_per_vertex; 1636 - values[vert_i] = x - w2; 1637 - values[vert_i+1] = y + h2; 1638 - vert_i += floats_per_vertex; 1639 - } 1640 - else 1641 - { 1642 - // 4 vertices all in a row 1643 - values[vert_i] = positions[pos_n++]; 1644 - values[vert_i+1] = positions[pos_n++]; 1645 - vert_i += floats_per_vertex; 1646 - values[vert_i] = positions[pos_n++]; 1647 - values[vert_i+1] = positions[pos_n++]; 1648 - vert_i += floats_per_vertex; 1649 - values[vert_i] = positions[pos_n++]; 1650 - values[vert_i+1] = positions[pos_n++]; 1651 - vert_i += floats_per_vertex; 1652 - values[vert_i] = positions[pos_n++]; 1653 - values[vert_i+1] = positions[pos_n++]; 1654 - vert_i += floats_per_vertex; 1655 - } 1656 - } 1657 - 1658 - if(colors == NULL) 1659 - { 1660 - values[color_i] = 1.0f; 1661 - values[color_i+1] = 1.0f; 1662 - values[color_i+2] = 1.0f; 1663 - values[color_i+3] = 1.0f; 1664 - color_i += floats_per_vertex; 1665 - values[color_i] = 1.0f; 1666 - values[color_i+1] = 1.0f; 1667 - values[color_i+2] = 1.0f; 1668 - values[color_i+3] = 1.0f; 1669 - color_i += floats_per_vertex; 1670 - values[color_i] = 1.0f; 1671 - values[color_i+1] = 1.0f; 1672 - values[color_i+2] = 1.0f; 1673 - values[color_i+3] = 1.0f; 1674 - color_i += floats_per_vertex; 1675 - values[color_i] = 1.0f; 1676 - values[color_i+1] = 1.0f; 1677 - values[color_i+2] = 1.0f; 1678 - values[color_i+3] = 1.0f; 1679 - color_i += floats_per_vertex; 1680 - } 1681 - else 1682 - { 1683 - if(!pass_colors) 1684 - { 1685 - float r = colors[color_n++]/255.0f; 1686 - float g = colors[color_n++]/255.0f; 1687 - float b = colors[color_n++]/255.0f; 1688 - float a = colors[color_n++]/255.0f; 1689 - 1690 - values[color_i] = r; 1691 - values[color_i+1] = g; 1692 - values[color_i+2] = b; 1693 - values[color_i+3] = a; 1694 - color_i += floats_per_vertex; 1695 - values[color_i] = r; 1696 - values[color_i+1] = g; 1697 - values[color_i+2] = b; 1698 - values[color_i+3] = a; 1699 - color_i += floats_per_vertex; 1700 - values[color_i] = r; 1701 - values[color_i+1] = g; 1702 - values[color_i+2] = b; 1703 - values[color_i+3] = a; 1704 - color_i += floats_per_vertex; 1705 - values[color_i] = r; 1706 - values[color_i+1] = g; 1707 - values[color_i+2] = b; 1708 - values[color_i+3] = a; 1709 - color_i += floats_per_vertex; 1710 - } 1711 - else 1712 - { 1713 - // 4 vertices all in a row 1714 - values[color_i] = colors[color_n++]; 1715 - values[color_i+1] = colors[color_n++]; 1716 - values[color_i+2] = colors[color_n++]; 1717 - values[color_i+3] = colors[color_n++]; 1718 - color_i += floats_per_vertex; 1719 - values[color_i] = colors[color_n++]; 1720 - values[color_i+1] = colors[color_n++]; 1721 - values[color_i+2] = colors[color_n++]; 1722 - values[color_i+3] = colors[color_n++]; 1723 - color_i += floats_per_vertex; 1724 - values[color_i] = colors[color_n++]; 1725 - values[color_i+1] = colors[color_n++]; 1726 - values[color_i+2] = colors[color_n++]; 1727 - values[color_i+3] = colors[color_n++]; 1728 - color_i += floats_per_vertex; 1729 - values[color_i] = colors[color_n++]; 1730 - values[color_i+1] = colors[color_n++]; 1731 - values[color_i+2] = colors[color_n++]; 1732 - values[color_i+3] = colors[color_n++]; 1733 - color_i += floats_per_vertex; 1734 - } 1735 - } 1736 - } 1737 - 1738 - current_renderer->impl->BlitBatch(current_renderer, image, target, num_sprites, values, flags | GPU_PASSTHROUGH_ALL); 1739 - free(values); 1740 - } 1741 - 1742 - void GPU_TriangleBatch(GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BlitFlagEnum flags) 1129 + void GPU_TriangleBatch(GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BatchFlagEnum flags) 1743 1130 { 1744 - int src_position_floats_per_vertex; 1745 - int src_texcoord_floats_per_vertex; 1746 - int src_color_floats_per_vertex; 1747 - 1748 - Uint8 no_positions; 1749 - Uint8 no_texcoords; 1750 - Uint8 no_colors; 1751 - Uint8 pass_texcoords; 1752 - Uint8 pass_colors; 1753 - 1754 - int src_floats_per_vertex; 1755 - 1756 - int size; 1757 - float* new_values; 1758 - 1759 - unsigned int n; // Vertex number iteration variable 1760 - // Source indices 1761 - int pos_n; 1762 - int texcoord_n; 1763 - int color_n; 1764 - // Dest indices 1765 - int vert_i; 1766 - 1767 - Uint32 tex_w; 1768 - Uint32 tex_h; 1769 - 1770 - Uint8 using_texture = (image != NULL); 1771 - 1772 1131 if(!CHECK_RENDERER) 1773 1132 RETURN_ERROR(GPU_ERROR_USER_ERROR, "NULL renderer"); 1774 1133 if(!CHECK_CONTEXT) ··· 1780 1139 if(num_vertices == 0) 1781 1140 return; 1782 1141 1783 - // Is it already in the right format? 1784 - if((flags & GPU_PASSTHROUGH_ALL) == GPU_PASSTHROUGH_ALL || values == NULL) 1785 - { 1786 - current_renderer->impl->TriangleBatch(current_renderer, image, target, num_vertices, values, num_indices, indices, flags); 1787 - return; 1788 - } 1789 - 1790 - // Conversion time... 1791 - 1792 - // Convert texcoords and colors for the renderer to use. 1793 - // Condensed: Each vertex has 2 pos, 2 texcoords, 4 color components 1794 - 1795 - // Default values: Each vertex is defined by a position, texcoords, and a color. 1796 - src_position_floats_per_vertex = 2; 1797 - src_texcoord_floats_per_vertex = 2; 1798 - src_color_floats_per_vertex = 4; 1799 - 1800 - no_positions = (Uint8)(flags & GPU_USE_DEFAULT_POSITIONS); 1801 - no_texcoords = (Uint8)(flags & GPU_USE_DEFAULT_SRC_RECTS) || !using_texture; 1802 - no_colors = (Uint8)(flags & GPU_USE_DEFAULT_COLORS); 1803 - pass_texcoords = (Uint8)(flags & GPU_PASSTHROUGH_TEXCOORDS); 1804 - pass_colors = (Uint8)(flags & GPU_PASSTHROUGH_COLORS); 1805 - 1806 - // Vertex position passthrough is ignored (we're not positioning triangles, we're positioning vertices already) 1807 - src_position_floats_per_vertex = 2; // x, y 1808 - if(pass_texcoords) 1809 - src_texcoord_floats_per_vertex = 2; // s, t 1810 - if(pass_colors) 1811 - src_color_floats_per_vertex = 4; // r, g, b, a 1812 - if(no_positions) 1813 - src_position_floats_per_vertex = 0; 1814 - if(no_texcoords) 1815 - src_texcoord_floats_per_vertex = 0; 1816 - if(no_colors) 1817 - src_color_floats_per_vertex = 0; 1818 1142 1819 - src_floats_per_vertex = src_position_floats_per_vertex + src_texcoord_floats_per_vertex + src_color_floats_per_vertex; 1820 - 1821 - size = num_vertices*(2 + 2 + 4); 1822 - new_values = (float*)malloc(sizeof(float)*size); 1823 - 1824 - // Source indices 1825 - pos_n = 0; 1826 - texcoord_n = src_position_floats_per_vertex; 1827 - color_n = src_position_floats_per_vertex + src_texcoord_floats_per_vertex; 1828 - // Dest indices 1829 - vert_i = 0; 1830 - 1831 - if(using_texture) 1832 - { 1833 - tex_w = image->texture_w; 1834 - tex_h = image->texture_h; 1835 - } 1836 - 1837 - for(n = 0; n < num_vertices; n++) 1838 - { 1839 - // 2 floats from position 1840 - if(no_positions) 1841 - { 1842 - new_values[vert_i++] = 0.0f; 1843 - new_values[vert_i++] = 0.0f; 1844 - } 1845 - else 1846 - { 1847 - new_values[vert_i++] = values[pos_n]; 1848 - new_values[vert_i++] = values[pos_n+1]; 1849 - pos_n += src_floats_per_vertex; 1850 - } 1851 - 1852 - // 2 floats from texcoords 1853 - if(no_texcoords) 1854 - { 1855 - new_values[vert_i++] = 0.0f; 1856 - new_values[vert_i++] = 0.0f; 1857 - } 1858 - else 1859 - { 1860 - if(!pass_texcoords && using_texture) 1861 - { 1862 - new_values[vert_i++] = values[texcoord_n]/tex_w; 1863 - new_values[vert_i++] = values[texcoord_n+1]/tex_h; 1864 - texcoord_n += src_floats_per_vertex; 1865 - } 1866 - else 1867 - { 1868 - new_values[vert_i++] = values[texcoord_n]; 1869 - new_values[vert_i++] = values[texcoord_n+1]; 1870 - } 1871 - } 1872 - 1873 - if(no_colors) 1874 - { 1875 - new_values[vert_i++] = 1.0f; 1876 - new_values[vert_i++] = 1.0f; 1877 - new_values[vert_i++] = 1.0f; 1878 - new_values[vert_i++] = 1.0f; 1879 - } 1880 - else 1881 - { 1882 - if(!pass_colors) 1883 - { 1884 - new_values[vert_i++] = values[color_n]/255.0f; 1885 - new_values[vert_i++] = values[color_n+1]/255.0f; 1886 - new_values[vert_i++] = values[color_n+2]/255.0f; 1887 - new_values[vert_i++] = values[color_n+3]/255.0f; 1888 - color_n += src_floats_per_vertex; 1889 - } 1890 - else 1891 - { 1892 - new_values[vert_i++] = values[color_n]; 1893 - new_values[vert_i++] = values[color_n+1]; 1894 - new_values[vert_i++] = values[color_n+2]; 1895 - new_values[vert_i++] = values[color_n+3]; 1896 - color_n += src_floats_per_vertex; 1897 - } 1898 - } 1899 - } 1900 - 1901 - current_renderer->impl->TriangleBatch(current_renderer, image, target, num_vertices, new_values, num_indices, indices, flags | GPU_PASSTHROUGH_ALL); 1902 - 1903 - free(new_values); 1143 + current_renderer->impl->TriangleBatch(current_renderer, image, target, num_vertices, values, num_indices, indices, flags); 1904 1144 } 1905 1145 1906 1146
+108 -259
src/renderer_GL_common.inl
··· 4103 4103 #endif 4104 4104 } 4105 4105 4106 - 4107 4106 // Assumes the right format 4108 - static void BlitBatch(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, GPU_BlitFlagEnum flags) 4109 - { 4110 - GPU_CONTEXT_DATA* cdata; 4111 - unsigned short* index_buffer; 4112 - unsigned int i; 4113 - (void)flags; 4114 - 4115 - if(image == NULL) 4116 - { 4117 - GPU_PushErrorCode("GPU_BlitBatch", GPU_ERROR_NULL_ARGUMENT, "image"); 4118 - return; 4119 - } 4120 - if(target == NULL) 4121 - { 4122 - GPU_PushErrorCode("GPU_BlitBatch", GPU_ERROR_NULL_ARGUMENT, "target"); 4123 - return; 4124 - } 4125 - if(renderer != image->renderer || renderer != target->renderer) 4126 - { 4127 - GPU_PushErrorCode("GPU_BlitBatch", GPU_ERROR_USER_ERROR, "Mismatched renderer"); 4128 - return; 4129 - } 4130 - 4131 - makeContextCurrent(renderer, target); 4132 - 4133 - // Bind the texture to which subsequent calls refer 4134 - bindTexture(renderer, image); 4135 - 4136 - // Bind the FBO 4137 - if(!bindFramebuffer(renderer, target)) 4138 - { 4139 - GPU_PushErrorCode("GPU_BlitBatch", GPU_ERROR_BACKEND_ERROR, "Failed to bind framebuffer."); 4140 - return; 4141 - } 4142 - 4143 - prepareToRenderToTarget(renderer, target); 4144 - prepareToRenderImage(renderer, target, image); 4145 - changeViewport(target); 4146 - changeCamera(target); 4147 - 4148 - changeTexturing(renderer, 1); 4149 - 4150 - setClipRect(renderer, target); 4151 - 4152 - #ifdef SDL_GPU_APPLY_TRANSFORMS_TO_GL_STACK 4153 - if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER)) 4154 - applyTransforms(); 4155 - #endif 4156 - 4157 - 4158 - cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data; 4159 - 4160 - renderer->impl->FlushBlitBuffer(renderer); 4161 - // Only need to check the blit buffer because of the VBO storage 4162 - if(cdata->blit_buffer_num_vertices + num_sprites*4 >= cdata->blit_buffer_max_num_vertices) 4163 - { 4164 - if(!growBlitBuffer(cdata, cdata->blit_buffer_num_vertices + num_sprites*4)) 4165 - { 4166 - // Can't do all of these sprites! Only do some of them... 4167 - num_sprites = (cdata->blit_buffer_max_num_vertices - cdata->blit_buffer_num_vertices)/4; 4168 - } 4169 - } 4170 - if(cdata->index_buffer_num_vertices + num_sprites*6 >= cdata->index_buffer_max_num_vertices) 4171 - { 4172 - if(!growIndexBuffer(cdata, cdata->index_buffer_num_vertices + num_sprites*6)) 4173 - { 4174 - // Can't do all of these sprites! Only do some of them... 4175 - num_sprites = (cdata->index_buffer_max_num_vertices - cdata->index_buffer_num_vertices)/6; 4176 - } 4177 - } 4178 - 4179 - index_buffer = cdata->index_buffer; 4180 - 4181 - #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4182 - refresh_attribute_data(cdata); 4183 - #endif 4184 - 4185 - // Triangle indices 4186 - for(i = 0; i < num_sprites; i++) 4187 - { 4188 - int buffer_num_vertices = i*4; 4189 - // First tri 4190 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices; // 0 4191 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices+1; // 1 4192 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices+2; // 2 4193 - 4194 - // Second tri 4195 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices; // 0 4196 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices+2; // 2 4197 - index_buffer[cdata->index_buffer_num_vertices++] = buffer_num_vertices+3; // 3 4198 - } 4199 - 4200 - 4201 - #ifdef SDL_GPU_USE_ARRAY_PIPELINE 4202 - 4203 - glEnableClientState(GL_VERTEX_ARRAY); 4204 - glEnableClientState(GL_TEXTURE_COORD_ARRAY); 4205 - glEnableClientState(GL_COLOR_ARRAY); 4206 - 4207 - { 4208 - int stride = 8*sizeof(float); 4209 - glVertexPointer(2, GL_FLOAT, stride, values + GPU_BLIT_BUFFER_VERTEX_OFFSET); 4210 - glTexCoordPointer(2, GL_FLOAT, stride, values + GPU_BLIT_BUFFER_TEX_COORD_OFFSET); 4211 - glColorPointer(4, GL_FLOAT, stride, values + GPU_BLIT_BUFFER_COLOR_OFFSET); 4212 - } 4213 - 4214 - glDrawElements(GL_TRIANGLES, cdata->index_buffer_num_vertices, GL_UNSIGNED_SHORT, cdata->index_buffer); 4215 - 4216 - glDisableClientState(GL_COLOR_ARRAY); 4217 - glDisableClientState(GL_TEXTURE_COORD_ARRAY); 4218 - glDisableClientState(GL_VERTEX_ARRAY); 4219 - #endif 4220 - 4221 - 4222 - 4223 - #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 4224 - if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER)) 4225 - #endif 4226 - #ifdef SDL_GPU_USE_FIXED_FUNCTION_PIPELINE 4227 - { 4228 - if(values != NULL) 4229 - { 4230 - unsigned int num_indices = cdata->index_buffer_num_vertices; 4231 - unsigned int index; 4232 - float* vertex_pointer = values; 4233 - float* texcoord_pointer = values + GPU_BLIT_BUFFER_TEX_COORD_OFFSET; 4234 - float* color_pointer = values + GPU_BLIT_BUFFER_COLOR_OFFSET; 4235 - 4236 - glBegin(GL_TRIANGLES); 4237 - for(i = 0; i < num_indices; i++) 4238 - { 4239 - index = index_buffer[i]*GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4240 - glColor4f( color_pointer[index], color_pointer[index+1], color_pointer[index+2], color_pointer[index+3] ); 4241 - glTexCoord2f( texcoord_pointer[index], texcoord_pointer[index+1] ); 4242 - glVertex3f( vertex_pointer[index], vertex_pointer[index+1], 0.0f ); 4243 - } 4244 - glEnd(); 4245 - } 4246 - } 4247 - #endif 4248 - #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 4249 - else 4250 - #endif 4251 - 4252 - 4253 - 4254 - #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4255 - { 4256 - // Upload our modelviewprojection matrix 4257 - if(cdata->current_shader_block.modelViewProjection_loc >= 0) 4258 - { 4259 - float mvp[16]; 4260 - GPU_GetModelViewProjection(mvp); 4261 - glUniformMatrix4fv(cdata->current_shader_block.modelViewProjection_loc, 1, 0, mvp); 4262 - } 4263 - 4264 - // Update the vertex array object's buffers 4265 - #if !defined(SDL_GPU_NO_VAO) 4266 - glBindVertexArray(cdata->blit_VAO); 4267 - #endif 4268 - 4269 - if(values != NULL) 4270 - { 4271 - // Upload blit buffer to a single buffer object 4272 - glBindBuffer(GL_ARRAY_BUFFER, cdata->blit_VBO[cdata->blit_VBO_flop]); 4273 - cdata->blit_VBO_flop = !cdata->blit_VBO_flop; 4274 - 4275 - // Copy the whole blit buffer to the GPU 4276 - submit_buffer_data(GPU_BLIT_BUFFER_STRIDE * (num_sprites*4), values); // Fills GPU buffer with data. 4277 - 4278 - // Specify the formatting of the blit buffer 4279 - if(cdata->current_shader_block.position_loc >= 0) 4280 - { 4281 - glEnableVertexAttribArray(cdata->current_shader_block.position_loc); // Tell GL to use client-side attribute data 4282 - glVertexAttribPointer(cdata->current_shader_block.position_loc, 2, GL_FLOAT, GL_FALSE, GPU_BLIT_BUFFER_STRIDE, 0); // Tell how the data is formatted 4283 - } 4284 - if(cdata->current_shader_block.texcoord_loc >= 0) 4285 - { 4286 - glEnableVertexAttribArray(cdata->current_shader_block.texcoord_loc); 4287 - glVertexAttribPointer(cdata->current_shader_block.texcoord_loc, 2, GL_FLOAT, GL_FALSE, GPU_BLIT_BUFFER_STRIDE, (void*)(GPU_BLIT_BUFFER_TEX_COORD_OFFSET * sizeof(float))); 4288 - } 4289 - if(cdata->current_shader_block.color_loc >= 0) 4290 - { 4291 - glEnableVertexAttribArray(cdata->current_shader_block.color_loc); 4292 - glVertexAttribPointer(cdata->current_shader_block.color_loc, 4, GL_FLOAT, GL_FALSE, GPU_BLIT_BUFFER_STRIDE, (void*)(GPU_BLIT_BUFFER_COLOR_OFFSET * sizeof(float))); 4293 - } 4294 - } 4295 - 4296 - upload_attribute_data(cdata, num_sprites*4); 4297 - 4298 - glDrawElements(GL_TRIANGLES, cdata->index_buffer_num_vertices, GL_UNSIGNED_SHORT, cdata->index_buffer); 4299 - 4300 - // Disable the vertex arrays again 4301 - if(cdata->current_shader_block.position_loc >= 0) 4302 - glDisableVertexAttribArray(cdata->current_shader_block.position_loc); 4303 - if(cdata->current_shader_block.texcoord_loc >= 0) 4304 - glDisableVertexAttribArray(cdata->current_shader_block.texcoord_loc); 4305 - if(cdata->current_shader_block.color_loc >= 0) 4306 - glDisableVertexAttribArray(cdata->current_shader_block.color_loc); 4307 - 4308 - disable_attribute_data(cdata); 4309 - 4310 - #if !defined(SDL_GPU_NO_VAO) 4311 - glBindVertexArray(0); 4312 - #endif 4313 - } 4314 - #endif 4315 - 4316 - 4317 - cdata->blit_buffer_num_vertices = 0; 4318 - cdata->index_buffer_num_vertices = 0; 4319 - 4320 - unsetClipRect(renderer, target); 4321 - } 4322 - 4323 - // Assumes the right format 4324 - static void TriangleBatch(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BlitFlagEnum flags) 4107 + static void TriangleBatch(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BatchFlagEnum flags) 4325 4108 { 4326 4109 GPU_CONTEXT_DATA* cdata; 4327 - int stride; 4110 + int stride, offset_texcoords, offset_colors; 4111 + int size_vertices, size_texcoords, size_colors; 4112 + 4328 4113 Uint8 using_texture = (image != NULL); 4329 - (void)flags; 4114 + Uint8 use_vertices = (flags & (GPU_BATCH_XY | GPU_BATCH_XYZ)); 4115 + Uint8 use_texcoords = (flags & GPU_BATCH_ST); 4116 + Uint8 use_colors = (flags & (GPU_BATCH_RGB | GPU_BATCH_RGBA)); 4117 + Uint8 use_z = (flags & GPU_BATCH_XYZ); 4118 + Uint8 use_a = (flags & GPU_BATCH_RGBA); 4330 4119 4331 4120 if(num_vertices == 0) 4332 4121 return; ··· 4409 4198 refresh_attribute_data(cdata); 4410 4199 #endif 4411 4200 4412 - stride = GPU_BLIT_BUFFER_STRIDE; 4413 - (void)stride; 4414 4201 if(indices == NULL) 4415 4202 num_indices = num_vertices; 4416 4203 4204 + (void)stride; 4205 + (void)offset_texcoords; 4206 + (void)offset_colors; 4207 + (void)size_vertices; 4208 + (void)size_texcoords; 4209 + (void)size_colors; 4210 + 4211 + stride = 0; 4212 + offset_texcoords = offset_colors = 0; 4213 + size_vertices = size_texcoords = size_colors = 0; 4214 + 4215 + // Determine stride, size, and offsets 4216 + if(use_vertices) 4217 + { 4218 + if(use_z) 4219 + size_vertices = 3; 4220 + else 4221 + size_vertices = 2; 4222 + 4223 + stride += size_vertices; 4224 + 4225 + offset_texcoords = stride; 4226 + offset_colors = stride; 4227 + } 4228 + 4229 + if(use_texcoords) 4230 + { 4231 + size_texcoords = 2; 4232 + 4233 + stride += size_texcoords; 4234 + 4235 + offset_colors = stride; 4236 + } 4237 + 4238 + if(use_colors) 4239 + { 4240 + if(use_a) 4241 + size_colors = 4; 4242 + else 4243 + size_colors = 3; 4244 + 4245 + stride += size_colors; 4246 + } 4247 + 4248 + // Convert to a number of bytes 4249 + stride *= sizeof(float); 4250 + 4417 4251 4418 4252 #ifdef SDL_GPU_USE_ARRAY_PIPELINE 4419 - 4420 - glEnableClientState(GL_VERTEX_ARRAY); 4421 - if(using_texture) 4422 - glEnableClientState(GL_TEXTURE_COORD_ARRAY); 4423 - glEnableClientState(GL_COLOR_ARRAY); 4424 4253 4425 - glVertexPointer(2, GL_FLOAT, stride, values); 4426 - if(using_texture) 4427 4254 { 4428 - glTexCoordPointer(2, GL_FLOAT, stride, values + 2); 4429 - glColorPointer(4, GL_FLOAT, stride, values + 4); 4430 - } 4431 - else 4432 - glColorPointer(4, GL_FLOAT, stride, values + 4); 4255 + // Enable 4256 + if(use_vertices) 4257 + glEnableClientState(GL_VERTEX_ARRAY); 4258 + if(use_texcoords) 4259 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); 4260 + if(use_colors) 4261 + glEnableClientState(GL_COLOR_ARRAY); 4262 + 4263 + // Set pointers 4264 + if(use_vertices) 4265 + glVertexPointer(size_vertices, GL_FLOAT, stride, values); 4266 + if(use_texcoords) 4267 + glTexCoordPointer(size_texcoords, GL_FLOAT, stride, values + offset_texcoords); 4268 + if(use_colors) 4269 + glColorPointer(size_colors, GL_FLOAT, stride, values + offset_colors); 4433 4270 4434 - if(indices == NULL) 4435 - glDrawArrays(GL_TRIANGLES, 0, num_indices); 4436 - else 4437 - glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices); 4271 + // Upload 4272 + if(indices == NULL) 4273 + glDrawArrays(GL_TRIANGLES, 0, num_indices); 4274 + else 4275 + glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices); 4438 4276 4439 - glDisableClientState(GL_COLOR_ARRAY); 4440 - if(using_texture) 4441 - glDisableClientState(GL_TEXTURE_COORD_ARRAY); 4442 - glDisableClientState(GL_VERTEX_ARRAY); 4277 + // Disable 4278 + if(use_colors) 4279 + glDisableClientState(GL_COLOR_ARRAY); 4280 + if(use_texcoords) 4281 + glDisableClientState(GL_TEXTURE_COORD_ARRAY); 4282 + if(use_vertices) 4283 + glDisableClientState(GL_VERTEX_ARRAY); 4284 + } 4443 4285 #endif 4444 4286 4445 4287 ··· 4454 4296 unsigned int i; 4455 4297 unsigned int index; 4456 4298 float* vertex_pointer = values; 4457 - float* texcoord_pointer = values + 2; 4458 - float* color_pointer = values + 4; 4459 - if(using_texture) 4460 - color_pointer = values + 2; 4299 + float* texcoord_pointer = values + offset_texcoords; 4300 + float* color_pointer = values + offset_colors; 4461 4301 4462 4302 glBegin(GL_TRIANGLES); 4463 4303 for(i = 0; i < num_indices; i++) ··· 4466 4306 index = i*GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4467 4307 else 4468 4308 index = indices[i]*GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4469 - glColor4f( color_pointer[index], color_pointer[index+1], color_pointer[index+2], color_pointer[index+3] ); 4470 - if(using_texture) 4309 + if(use_colors) 4310 + glColor4f(color_pointer[index], color_pointer[index+1], color_pointer[index+2], (use_a? color_pointer[index+3] : 1.0f)); 4311 + if(use_texcoords) 4471 4312 glTexCoord2f( texcoord_pointer[index], texcoord_pointer[index+1] ); 4472 - glVertex3f( vertex_pointer[index], vertex_pointer[index+1], 0.0f ); 4313 + if(use_vertices) 4314 + glVertex3f( vertex_pointer[index], vertex_pointer[index+1], (use_z? vertex_pointer[index+2] : 0.0f) ); 4473 4315 } 4474 4316 glEnd(); 4475 4317 } ··· 4483 4325 4484 4326 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4485 4327 { 4328 + // Skip uploads if we have no attribute location 4329 + if(cdata->current_shader_block.position_loc < 0) 4330 + use_vertices = 0; 4331 + if(cdata->current_shader_block.texcoord_loc < 0) 4332 + use_texcoords = 0; 4333 + if(cdata->current_shader_block.color_loc < 0) 4334 + use_colors = 0; 4335 + 4486 4336 // Upload our modelviewprojection matrix 4487 4337 if(cdata->current_shader_block.modelViewProjection_loc >= 0) 4488 4338 { ··· 4506 4356 submit_buffer_data(stride * num_vertices, values); // Fills GPU buffer with data. 4507 4357 4508 4358 // Specify the formatting of the blit buffer 4509 - if(cdata->current_shader_block.position_loc >= 0) 4359 + if(use_vertices) 4510 4360 { 4511 4361 glEnableVertexAttribArray(cdata->current_shader_block.position_loc); // Tell GL to use client-side attribute data 4512 - glVertexAttribPointer(cdata->current_shader_block.position_loc, 2, GL_FLOAT, GL_FALSE, stride, 0); // Tell how the data is formatted 4362 + glVertexAttribPointer(cdata->current_shader_block.position_loc, size_vertices, GL_FLOAT, GL_FALSE, stride, 0); // Tell how the data is formatted 4513 4363 } 4514 - if(using_texture && cdata->current_shader_block.texcoord_loc >= 0) 4364 + if(use_texcoords) 4515 4365 { 4516 4366 glEnableVertexAttribArray(cdata->current_shader_block.texcoord_loc); 4517 - glVertexAttribPointer(cdata->current_shader_block.texcoord_loc, 2, GL_FLOAT, GL_FALSE, stride, (void*)(2 * sizeof(float))); 4367 + glVertexAttribPointer(cdata->current_shader_block.texcoord_loc, size_texcoords, GL_FLOAT, GL_FALSE, stride, (void*)(offset_texcoords * sizeof(float))); 4518 4368 } 4519 - if(cdata->current_shader_block.color_loc >= 0) 4369 + if(use_colors) 4520 4370 { 4521 4371 glEnableVertexAttribArray(cdata->current_shader_block.color_loc); 4522 - glVertexAttribPointer(cdata->current_shader_block.color_loc, 4, GL_FLOAT, GL_FALSE, stride, using_texture? (void*)(4 * sizeof(float)) : (void*)(2 * sizeof(float))); 4372 + glVertexAttribPointer(cdata->current_shader_block.color_loc, size_colors, GL_FLOAT, GL_FALSE, stride, (void*)(offset_colors * sizeof(float))); 4523 4373 } 4524 4374 } 4525 4375 ··· 4531 4381 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices); 4532 4382 4533 4383 // Disable the vertex arrays again 4534 - if(cdata->current_shader_block.position_loc >= 0) 4384 + if(use_vertices) 4535 4385 glDisableVertexAttribArray(cdata->current_shader_block.position_loc); 4536 - if(using_texture && cdata->current_shader_block.texcoord_loc >= 0) 4386 + if(use_texcoords) 4537 4387 glDisableVertexAttribArray(cdata->current_shader_block.texcoord_loc); 4538 - if(cdata->current_shader_block.color_loc >= 0) 4388 + if(use_colors) 4539 4389 glDisableVertexAttribArray(cdata->current_shader_block.color_loc); 4540 4390 4541 4391 disable_attribute_data(cdata); ··· 6246 6096 impl->BlitTransform = &BlitTransform; \ 6247 6097 impl->BlitTransformX = &BlitTransformX; \ 6248 6098 impl->BlitTransformMatrix = &BlitTransformMatrix; \ 6249 - impl->BlitBatch = &BlitBatch; \ 6250 6099 impl->TriangleBatch = &TriangleBatch; \ 6251 6100 \ 6252 6101 impl->GenerateMipmaps = &GenerateMipmaps; \
+640 -3
tests/blit-batch/main.c
··· 2 2 #include "SDL_gpu.h" 3 3 #include "common.h" 4 4 5 + 6 + typedef Uint32 BlitFlagEnum; 7 + static const BlitFlagEnum PASSTHROUGH_VERTICES = 0x1; 8 + static const BlitFlagEnum PASSTHROUGH_TEXCOORDS = 0x2; 9 + static const BlitFlagEnum PASSTHROUGH_COLORS = 0x4; 10 + static const BlitFlagEnum USE_DEFAULT_POSITIONS = 0x8; 11 + static const BlitFlagEnum USE_DEFAULT_SRC_RECTS = 0x10; 12 + static const BlitFlagEnum USE_DEFAULT_COLORS = 0x20; 13 + 14 + #define PASSTHROUGH_ALL (PASSTHROUGH_VERTICES | PASSTHROUGH_TEXCOORDS | PASSTHROUGH_COLORS) 15 + 16 + void BlitBatch(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, BlitFlagEnum flags) 17 + { 18 + int src_position_floats_per_sprite; 19 + int src_rect_floats_per_sprite; 20 + int src_color_floats_per_sprite; 21 + 22 + Uint8 no_positions; 23 + Uint8 no_rects; 24 + Uint8 no_colors; 25 + Uint8 pass_vertices; 26 + Uint8 pass_texcoords; 27 + Uint8 pass_colors; 28 + 29 + int src_floats_per_sprite; 30 + 31 + int size; 32 + float* new_values; 33 + unsigned short* indices; 34 + unsigned int num_indices; 35 + 36 + unsigned int current_index; 37 + 38 + unsigned int n; // The sprite number iteration variable. 39 + // Source indices (per sprite) 40 + int pos_n; 41 + int rect_n; 42 + int color_n; 43 + // Dest indices 44 + int vert_i; 45 + int texcoord_i; 46 + int color_i; 47 + // Dest float stride 48 + int floats_per_vertex; 49 + 50 + float w2; // texcoord helpers for position expansion 51 + float h2; 52 + 53 + Uint32 tex_w; 54 + Uint32 tex_h; 55 + 56 + if(image == NULL) 57 + return; 58 + if(target == NULL) 59 + return; 60 + 61 + if(num_sprites == 0) 62 + return; 63 + 64 + // Conversion time... 65 + 66 + // Convert condensed interleaved format into full interleaved format for the renderer to use. 67 + // Condensed: Each vertex has 2 pos, 4 rect, 4 color 68 + 69 + // Default values: Each sprite is defined by a position, a rect, and a color. 70 + src_position_floats_per_sprite = 2; 71 + src_rect_floats_per_sprite = 4; 72 + src_color_floats_per_sprite = 4; 73 + 74 + no_positions = (Uint8)(flags & USE_DEFAULT_POSITIONS) || (values == NULL); 75 + no_rects = (Uint8)(flags & USE_DEFAULT_SRC_RECTS) || (values == NULL); 76 + no_colors = (Uint8)(flags & USE_DEFAULT_COLORS) || (values == NULL); 77 + pass_vertices = (Uint8)(flags & PASSTHROUGH_VERTICES); 78 + pass_texcoords = (Uint8)(flags & PASSTHROUGH_TEXCOORDS); 79 + pass_colors = (Uint8)(flags & PASSTHROUGH_COLORS); 80 + 81 + // Passthrough data is per-vertex. Non-passthrough is per-sprite. They can't interleave cleanly. 82 + if(flags & PASSTHROUGH_ALL && (flags & PASSTHROUGH_ALL) != PASSTHROUGH_ALL) 83 + { 84 + GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Cannot interpret interleaved data using partial passthrough"); 85 + return; 86 + } 87 + 88 + if(pass_vertices) 89 + src_position_floats_per_sprite = 8; // 4 vertices of x, y 90 + if(pass_texcoords) 91 + src_rect_floats_per_sprite = 8; // 4 vertices of s, t 92 + if(pass_colors) 93 + src_color_floats_per_sprite = 16; // 4 vertices of r, g, b, a 94 + if(no_positions) 95 + src_position_floats_per_sprite = 0; 96 + if(no_rects) 97 + src_rect_floats_per_sprite = 0; 98 + if(no_colors) 99 + src_color_floats_per_sprite = 0; 100 + 101 + src_floats_per_sprite = src_position_floats_per_sprite + src_rect_floats_per_sprite + src_color_floats_per_sprite; 102 + 103 + size = num_sprites*(8 + 8 + 16); 104 + new_values = (float*)malloc(sizeof(float)*size); 105 + num_indices = num_sprites * 6; 106 + indices = (unsigned short*)malloc(sizeof(unsigned short)*num_indices); 107 + current_index = 0; 108 + 109 + // Source indices (per sprite) 110 + pos_n = 0; 111 + rect_n = src_position_floats_per_sprite; 112 + color_n = src_position_floats_per_sprite + src_rect_floats_per_sprite; 113 + // Dest indices 114 + vert_i = 0; 115 + texcoord_i = 2; 116 + color_i = 4; 117 + // Dest float stride 118 + floats_per_vertex = 8; 119 + 120 + w2 = 0.5f*image->w; // texcoord helpers for position expansion 121 + h2 = 0.5f*image->h; 122 + 123 + tex_w = image->texture_w; 124 + tex_h = image->texture_h; 125 + 126 + for(n = 0; n < num_sprites; n++) 127 + { 128 + int i_n = n*6; 129 + indices[i_n] = current_index; 130 + indices[i_n+1] = current_index+1; 131 + indices[i_n+2] = current_index+2; 132 + indices[i_n+3] = current_index+2; 133 + indices[i_n+4] = current_index+3; 134 + indices[i_n+5] = current_index; 135 + current_index += 4; 136 + 137 + if(no_rects) 138 + { 139 + new_values[texcoord_i] = 0.0f; 140 + new_values[texcoord_i+1] = 0.0f; 141 + texcoord_i += floats_per_vertex; 142 + new_values[texcoord_i] = 1.0f; 143 + new_values[texcoord_i+1] = 0.0f; 144 + texcoord_i += floats_per_vertex; 145 + new_values[texcoord_i] = 1.0f; 146 + new_values[texcoord_i+1] = 1.0f; 147 + texcoord_i += floats_per_vertex; 148 + new_values[texcoord_i] = 0.0f; 149 + new_values[texcoord_i+1] = 1.0f; 150 + texcoord_i += floats_per_vertex; 151 + } 152 + else 153 + { 154 + if(!pass_texcoords) 155 + { 156 + float s1 = values[rect_n]/tex_w; 157 + float t1 = values[rect_n+1]/tex_h; 158 + float s3 = s1 + values[rect_n+2]/tex_w; 159 + float t3 = t1 + values[rect_n+3]/tex_h; 160 + rect_n += src_floats_per_sprite; 161 + 162 + new_values[texcoord_i] = s1; 163 + new_values[texcoord_i+1] = t1; 164 + texcoord_i += floats_per_vertex; 165 + new_values[texcoord_i] = s3; 166 + new_values[texcoord_i+1] = t1; 167 + texcoord_i += floats_per_vertex; 168 + new_values[texcoord_i] = s3; 169 + new_values[texcoord_i+1] = t3; 170 + texcoord_i += floats_per_vertex; 171 + new_values[texcoord_i] = s1; 172 + new_values[texcoord_i+1] = t3; 173 + texcoord_i += floats_per_vertex; 174 + 175 + if(!pass_vertices) 176 + { 177 + w2 = 0.5f*(s3-s1)*image->w; 178 + h2 = 0.5f*(t3-t1)*image->h; 179 + } 180 + } 181 + else 182 + { 183 + // 4 vertices all in a row 184 + float s1, t1, s3, t3; 185 + s1 = new_values[texcoord_i] = values[rect_n]; 186 + t1 = new_values[texcoord_i+1] = values[rect_n+1]; 187 + texcoord_i += floats_per_vertex; 188 + new_values[texcoord_i] = values[rect_n+2]; 189 + new_values[texcoord_i+1] = values[rect_n+3]; 190 + texcoord_i += floats_per_vertex; 191 + s3 = new_values[texcoord_i] = values[rect_n+4]; 192 + t3 = new_values[texcoord_i+1] = values[rect_n+5]; 193 + texcoord_i += floats_per_vertex; 194 + new_values[texcoord_i] = values[rect_n+6]; 195 + new_values[texcoord_i+1] = values[rect_n+7]; 196 + texcoord_i += floats_per_vertex; 197 + rect_n += src_floats_per_sprite; 198 + 199 + if(!pass_vertices) 200 + { 201 + w2 = 0.5f*(s3-s1)*image->w; 202 + h2 = 0.5f*(t3-t1)*image->h; 203 + } 204 + } 205 + } 206 + 207 + if(no_positions) 208 + { 209 + new_values[vert_i] = 0.0f; 210 + new_values[vert_i+1] = 0.0f; 211 + vert_i += floats_per_vertex; 212 + new_values[vert_i] = 0.0f; 213 + new_values[vert_i+1] = 0.0f; 214 + vert_i += floats_per_vertex; 215 + new_values[vert_i] = 0.0f; 216 + new_values[vert_i+1] = 0.0f; 217 + vert_i += floats_per_vertex; 218 + new_values[vert_i] = 0.0f; 219 + new_values[vert_i+1] = 0.0f; 220 + vert_i += floats_per_vertex; 221 + } 222 + else 223 + { 224 + if(!pass_vertices) 225 + { 226 + // Expand vertices from the position and dimensions 227 + float x = values[pos_n]; 228 + float y = values[pos_n+1]; 229 + pos_n += src_floats_per_sprite; 230 + 231 + new_values[vert_i] = x - w2; 232 + new_values[vert_i+1] = y - h2; 233 + vert_i += floats_per_vertex; 234 + new_values[vert_i] = x + w2; 235 + new_values[vert_i+1] = y - h2; 236 + vert_i += floats_per_vertex; 237 + new_values[vert_i] = x + w2; 238 + new_values[vert_i+1] = y + h2; 239 + vert_i += floats_per_vertex; 240 + new_values[vert_i] = x - w2; 241 + new_values[vert_i+1] = y + h2; 242 + vert_i += floats_per_vertex; 243 + } 244 + else 245 + { 246 + // 4 vertices all in a row 247 + new_values[vert_i] = values[pos_n]; 248 + new_values[vert_i+1] = values[pos_n+1]; 249 + vert_i += floats_per_vertex; 250 + new_values[vert_i] = values[pos_n+2]; 251 + new_values[vert_i+1] = values[pos_n+3]; 252 + vert_i += floats_per_vertex; 253 + new_values[vert_i] = values[pos_n+4]; 254 + new_values[vert_i+1] = values[pos_n+5]; 255 + vert_i += floats_per_vertex; 256 + new_values[vert_i] = values[pos_n+6]; 257 + new_values[vert_i+1] = values[pos_n+7]; 258 + vert_i += floats_per_vertex; 259 + pos_n += src_floats_per_sprite; 260 + } 261 + } 262 + 263 + if(no_colors) 264 + { 265 + new_values[color_i] = 1.0f; 266 + new_values[color_i+1] = 1.0f; 267 + new_values[color_i+2] = 1.0f; 268 + new_values[color_i+3] = 1.0f; 269 + color_i += floats_per_vertex; 270 + new_values[color_i] = 1.0f; 271 + new_values[color_i+1] = 1.0f; 272 + new_values[color_i+2] = 1.0f; 273 + new_values[color_i+3] = 1.0f; 274 + color_i += floats_per_vertex; 275 + new_values[color_i] = 1.0f; 276 + new_values[color_i+1] = 1.0f; 277 + new_values[color_i+2] = 1.0f; 278 + new_values[color_i+3] = 1.0f; 279 + color_i += floats_per_vertex; 280 + new_values[color_i] = 1.0f; 281 + new_values[color_i+1] = 1.0f; 282 + new_values[color_i+2] = 1.0f; 283 + new_values[color_i+3] = 1.0f; 284 + color_i += floats_per_vertex; 285 + } 286 + else 287 + { 288 + if(!pass_colors) 289 + { 290 + float r = values[color_n]/255.0f; 291 + float g = values[color_n+1]/255.0f; 292 + float b = values[color_n+2]/255.0f; 293 + float a = values[color_n+3]/255.0f; 294 + color_n += src_floats_per_sprite; 295 + 296 + new_values[color_i] = r; 297 + new_values[color_i+1] = g; 298 + new_values[color_i+2] = b; 299 + new_values[color_i+3] = a; 300 + color_i += floats_per_vertex; 301 + new_values[color_i] = r; 302 + new_values[color_i+1] = g; 303 + new_values[color_i+2] = b; 304 + new_values[color_i+3] = a; 305 + color_i += floats_per_vertex; 306 + new_values[color_i] = r; 307 + new_values[color_i+1] = g; 308 + new_values[color_i+2] = b; 309 + new_values[color_i+3] = a; 310 + color_i += floats_per_vertex; 311 + new_values[color_i] = r; 312 + new_values[color_i+1] = g; 313 + new_values[color_i+2] = b; 314 + new_values[color_i+3] = a; 315 + color_i += floats_per_vertex; 316 + } 317 + else 318 + { 319 + // 4 vertices all in a row 320 + new_values[color_i] = values[color_n]; 321 + new_values[color_i+1] = values[color_n+1]; 322 + new_values[color_i+2] = values[color_n+2]; 323 + new_values[color_i+3] = values[color_n+3]; 324 + color_i += floats_per_vertex; 325 + new_values[color_i] = values[color_n+4]; 326 + new_values[color_i+1] = values[color_n+5]; 327 + new_values[color_i+2] = values[color_n+6]; 328 + new_values[color_i+3] = values[color_n+7]; 329 + color_i += floats_per_vertex; 330 + new_values[color_i] = values[color_n+8]; 331 + new_values[color_i+1] = values[color_n+9]; 332 + new_values[color_i+2] = values[color_n+10]; 333 + new_values[color_i+3] = values[color_n+11]; 334 + color_i += floats_per_vertex; 335 + new_values[color_i] = values[color_n+12]; 336 + new_values[color_i+1] = values[color_n+13]; 337 + new_values[color_i+2] = values[color_n+14]; 338 + new_values[color_i+3] = values[color_n+15]; 339 + color_i += floats_per_vertex; 340 + color_n += src_floats_per_sprite; 341 + } 342 + } 343 + } 344 + 345 + GPU_TriangleBatch(image, target, num_sprites*4, new_values, num_indices, indices, GPU_BATCH_XY_ST_RGBA); 346 + 347 + free(indices); 348 + free(new_values); 349 + } 350 + 351 + void BlitBatchSeparate(GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* positions, float* src_rects, float* colors, BlitFlagEnum flags) 352 + { 353 + Uint8 pass_vertices; 354 + Uint8 pass_texcoords; 355 + Uint8 pass_colors; 356 + 357 + int size; // 4 vertices of x, y... s, t... r, g, b, a 358 + float* values; 359 + unsigned short* indices; 360 + unsigned int num_indices; 361 + 362 + unsigned int current_index; 363 + 364 + unsigned int n; // The sprite number iteration variable. 365 + // Source indices 366 + int pos_n; 367 + int rect_n; 368 + int color_n; 369 + // Dest indices 370 + int vert_i; 371 + int texcoord_i; 372 + int color_i; 373 + // Dest float stride 374 + int floats_per_vertex; 375 + 376 + float w2; // texcoord helpers for position expansion 377 + float h2; 378 + 379 + Uint32 tex_w; 380 + Uint32 tex_h; 381 + 382 + if(image == NULL) 383 + return; 384 + if(target == NULL) 385 + return; 386 + 387 + if(num_sprites == 0) 388 + return; 389 + 390 + // Repack the given arrays into an interleaved array for more efficient access 391 + // Default values: Each sprite is defined by a position, a rect, and a color. 392 + 393 + pass_vertices = (Uint8)(flags & PASSTHROUGH_VERTICES); 394 + pass_texcoords = (Uint8)(flags & PASSTHROUGH_TEXCOORDS); 395 + pass_colors = (Uint8)(flags & PASSTHROUGH_COLORS); 396 + 397 + size = num_sprites*(8 + 8 + 16); // 4 vertices of x, y... s, t... r, g, b, a 398 + values = (float*)malloc(sizeof(float)*size); 399 + num_indices = num_sprites * 6; 400 + indices = (unsigned short*)malloc(sizeof(unsigned short)*num_indices); 401 + current_index = 0; 402 + 403 + // Source indices 404 + pos_n = 0; 405 + rect_n = 0; 406 + color_n = 0; 407 + // Dest indices 408 + vert_i = 0; 409 + texcoord_i = 2; 410 + color_i = 4; 411 + // Dest float stride 412 + floats_per_vertex = 8; 413 + 414 + w2 = 0.5f*image->w; // texcoord helpers for position expansion 415 + h2 = 0.5f*image->h; 416 + 417 + tex_w = image->texture_w; 418 + tex_h = image->texture_h; 419 + 420 + for(n = 0; n < num_sprites; n++) 421 + { 422 + int i_n = n*6; 423 + indices[i_n] = current_index; 424 + indices[i_n+1] = current_index+1; 425 + indices[i_n+2] = current_index+2; 426 + indices[i_n+3] = current_index+2; 427 + indices[i_n+4] = current_index+3; 428 + indices[i_n+5] = current_index; 429 + current_index += 4; 430 + 431 + // Unpack the arrays 432 + 433 + if(src_rects == NULL) 434 + { 435 + values[texcoord_i] = 0.0f; 436 + values[texcoord_i+1] = 0.0f; 437 + texcoord_i += floats_per_vertex; 438 + values[texcoord_i] = 1.0f; 439 + values[texcoord_i+1] = 0.0f; 440 + texcoord_i += floats_per_vertex; 441 + values[texcoord_i] = 1.0f; 442 + values[texcoord_i+1] = 1.0f; 443 + texcoord_i += floats_per_vertex; 444 + values[texcoord_i] = 0.0f; 445 + values[texcoord_i+1] = 1.0f; 446 + texcoord_i += floats_per_vertex; 447 + } 448 + else 449 + { 450 + if(!pass_texcoords) 451 + { 452 + float s1 = src_rects[rect_n++]/tex_w; 453 + float t1 = src_rects[rect_n++]/tex_h; 454 + float s3 = s1 + src_rects[rect_n++]/tex_w; 455 + float t3 = t1 + src_rects[rect_n++]/tex_h; 456 + 457 + values[texcoord_i] = s1; 458 + values[texcoord_i+1] = t1; 459 + texcoord_i += floats_per_vertex; 460 + values[texcoord_i] = s3; 461 + values[texcoord_i+1] = t1; 462 + texcoord_i += floats_per_vertex; 463 + values[texcoord_i] = s3; 464 + values[texcoord_i+1] = t3; 465 + texcoord_i += floats_per_vertex; 466 + values[texcoord_i] = s1; 467 + values[texcoord_i+1] = t3; 468 + texcoord_i += floats_per_vertex; 469 + 470 + if(!pass_vertices) 471 + { 472 + w2 = 0.5f*(s3-s1)*image->w; 473 + h2 = 0.5f*(t3-t1)*image->h; 474 + } 475 + } 476 + else 477 + { 478 + // 4 vertices all in a row 479 + float s1, t1, s3, t3; 480 + s1 = values[texcoord_i] = src_rects[rect_n++]; 481 + t1 = values[texcoord_i+1] = src_rects[rect_n++]; 482 + texcoord_i += floats_per_vertex; 483 + values[texcoord_i] = src_rects[rect_n++]; 484 + values[texcoord_i+1] = src_rects[rect_n++]; 485 + texcoord_i += floats_per_vertex; 486 + s3 = values[texcoord_i] = src_rects[rect_n++]; 487 + t3 = values[texcoord_i+1] = src_rects[rect_n++]; 488 + texcoord_i += floats_per_vertex; 489 + values[texcoord_i] = src_rects[rect_n++]; 490 + values[texcoord_i+1] = src_rects[rect_n++]; 491 + texcoord_i += floats_per_vertex; 492 + 493 + if(!pass_vertices) 494 + { 495 + w2 = 0.5f*(s3-s1)*image->w; 496 + h2 = 0.5f*(t3-t1)*image->h; 497 + } 498 + } 499 + } 500 + 501 + if(positions == NULL) 502 + { 503 + values[vert_i] = 0.0f; 504 + values[vert_i+1] = 0.0f; 505 + vert_i += floats_per_vertex; 506 + values[vert_i] = 0.0f; 507 + values[vert_i+1] = 0.0f; 508 + vert_i += floats_per_vertex; 509 + values[vert_i] = 0.0f; 510 + values[vert_i+1] = 0.0f; 511 + vert_i += floats_per_vertex; 512 + values[vert_i] = 0.0f; 513 + values[vert_i+1] = 0.0f; 514 + vert_i += floats_per_vertex; 515 + } 516 + else 517 + { 518 + if(!pass_vertices) 519 + { 520 + // Expand vertices from the position and dimensions 521 + float x = positions[pos_n++]; 522 + float y = positions[pos_n++]; 523 + values[vert_i] = x - w2; 524 + values[vert_i+1] = y - h2; 525 + vert_i += floats_per_vertex; 526 + values[vert_i] = x + w2; 527 + values[vert_i+1] = y - h2; 528 + vert_i += floats_per_vertex; 529 + values[vert_i] = x + w2; 530 + values[vert_i+1] = y + h2; 531 + vert_i += floats_per_vertex; 532 + values[vert_i] = x - w2; 533 + values[vert_i+1] = y + h2; 534 + vert_i += floats_per_vertex; 535 + } 536 + else 537 + { 538 + // 4 vertices all in a row 539 + values[vert_i] = positions[pos_n++]; 540 + values[vert_i+1] = positions[pos_n++]; 541 + vert_i += floats_per_vertex; 542 + values[vert_i] = positions[pos_n++]; 543 + values[vert_i+1] = positions[pos_n++]; 544 + vert_i += floats_per_vertex; 545 + values[vert_i] = positions[pos_n++]; 546 + values[vert_i+1] = positions[pos_n++]; 547 + vert_i += floats_per_vertex; 548 + values[vert_i] = positions[pos_n++]; 549 + values[vert_i+1] = positions[pos_n++]; 550 + vert_i += floats_per_vertex; 551 + } 552 + } 553 + 554 + if(colors == NULL) 555 + { 556 + values[color_i] = 1.0f; 557 + values[color_i+1] = 1.0f; 558 + values[color_i+2] = 1.0f; 559 + values[color_i+3] = 1.0f; 560 + color_i += floats_per_vertex; 561 + values[color_i] = 1.0f; 562 + values[color_i+1] = 1.0f; 563 + values[color_i+2] = 1.0f; 564 + values[color_i+3] = 1.0f; 565 + color_i += floats_per_vertex; 566 + values[color_i] = 1.0f; 567 + values[color_i+1] = 1.0f; 568 + values[color_i+2] = 1.0f; 569 + values[color_i+3] = 1.0f; 570 + color_i += floats_per_vertex; 571 + values[color_i] = 1.0f; 572 + values[color_i+1] = 1.0f; 573 + values[color_i+2] = 1.0f; 574 + values[color_i+3] = 1.0f; 575 + color_i += floats_per_vertex; 576 + } 577 + else 578 + { 579 + if(!pass_colors) 580 + { 581 + float r = colors[color_n++]/255.0f; 582 + float g = colors[color_n++]/255.0f; 583 + float b = colors[color_n++]/255.0f; 584 + float a = colors[color_n++]/255.0f; 585 + 586 + values[color_i] = r; 587 + values[color_i+1] = g; 588 + values[color_i+2] = b; 589 + values[color_i+3] = a; 590 + color_i += floats_per_vertex; 591 + values[color_i] = r; 592 + values[color_i+1] = g; 593 + values[color_i+2] = b; 594 + values[color_i+3] = a; 595 + color_i += floats_per_vertex; 596 + values[color_i] = r; 597 + values[color_i+1] = g; 598 + values[color_i+2] = b; 599 + values[color_i+3] = a; 600 + color_i += floats_per_vertex; 601 + values[color_i] = r; 602 + values[color_i+1] = g; 603 + values[color_i+2] = b; 604 + values[color_i+3] = a; 605 + color_i += floats_per_vertex; 606 + } 607 + else 608 + { 609 + // 4 vertices all in a row 610 + values[color_i] = colors[color_n++]; 611 + values[color_i+1] = colors[color_n++]; 612 + values[color_i+2] = colors[color_n++]; 613 + values[color_i+3] = colors[color_n++]; 614 + color_i += floats_per_vertex; 615 + values[color_i] = colors[color_n++]; 616 + values[color_i+1] = colors[color_n++]; 617 + values[color_i+2] = colors[color_n++]; 618 + values[color_i+3] = colors[color_n++]; 619 + color_i += floats_per_vertex; 620 + values[color_i] = colors[color_n++]; 621 + values[color_i+1] = colors[color_n++]; 622 + values[color_i+2] = colors[color_n++]; 623 + values[color_i+3] = colors[color_n++]; 624 + color_i += floats_per_vertex; 625 + values[color_i] = colors[color_n++]; 626 + values[color_i+1] = colors[color_n++]; 627 + values[color_i+2] = colors[color_n++]; 628 + values[color_i+3] = colors[color_n++]; 629 + color_i += floats_per_vertex; 630 + } 631 + } 632 + } 633 + 634 + GPU_TriangleBatch(image, target, num_sprites*4, values, num_indices, indices, GPU_BATCH_XY_ST_RGBA); 635 + 636 + free(indices); 637 + free(values); 638 + } 639 + 5 640 int do_interleaved(GPU_Target* screen) 6 641 { 7 642 GPU_Image* image; ··· 128 763 129 764 GPU_Clear(screen); 130 765 131 - GPU_BlitBatch(image, screen, numSprites, sprite_values, 0); 766 + BlitBatch(image, screen, numSprites, sprite_values, 0); 132 767 133 768 GPU_Flip(screen); 134 769 ··· 274 909 275 910 GPU_Clear(screen); 276 911 277 - GPU_BlitBatchSeparate(image, screen, numSprites, positions, NULL, colors, 0); 912 + BlitBatchSeparate(image, screen, numSprites, positions, NULL, colors, 0); 278 913 279 914 GPU_Flip(screen); 280 915 ··· 298 933 299 934 return return_value; 300 935 } 936 + 937 + 301 938 302 939 303 940 int do_attributes(GPU_Target* screen) ··· 510 1147 GPU_SetAttributeSource(numSprites*4, attributes[0]); 511 1148 GPU_SetAttributeSource(numSprites*4, attributes[1]); 512 1149 GPU_SetAttributeSource(numSprites*4, attributes[2]); 513 - GPU_BlitBatch(image, screen, numSprites, NULL, 0); 1150 + BlitBatch(image, screen, numSprites, NULL, 0); 514 1151 515 1152 GPU_Flip(screen); 516 1153
-7
tests/renderer/main.c
··· 737 737 } 738 738 739 739 740 - static void BlitBatch(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned int num_sprites, float* values, GPU_BlitFlagEnum flags) 741 - { 742 - GPU_Log(" %s (dummy)\n", __func__); 743 - } 744 - 745 - 746 740 static void TriangleBatch(GPU_Renderer* renderer, GPU_Image* image, GPU_Target* target, unsigned short num_vertices, float* values, unsigned int num_indices, unsigned short* indices, GPU_BlitFlagEnum flags) 747 741 { 748 742 GPU_Log(" %s (dummy)\n", __func__); ··· 1224 1218 impl->BlitTransform = &BlitTransform; 1225 1219 impl->BlitTransformX = &BlitTransformX; 1226 1220 impl->BlitTransformMatrix = &BlitTransformMatrix; 1227 - impl->BlitBatch = &BlitBatch; 1228 1221 impl->TriangleBatch = &TriangleBatch; 1229 1222 1230 1223 impl->GenerateMipmaps = &GenerateMipmaps;
+96 -229
tests/triangle-batch/main.c
··· 2 2 #include "SDL_gpu.h" 3 3 #include "common.h" 4 4 5 + void fill_vertex_values(float* vertex_values, float* velx, float* vely, unsigned int max_vertices, GPU_Target* screen, GPU_Image* image) 6 + { 7 + unsigned int i; 8 + int val_n = 0; 9 + Uint16 w, h; 10 + Uint16 tex_w, tex_h; 11 + 12 + if(image != NULL) 13 + { 14 + w = image->w; 15 + h = image->h; 16 + 17 + tex_w = image->texture_w; 18 + tex_h = image->texture_h; 19 + } 20 + else 21 + w = h = tex_w = tex_h = 80; 22 + 23 + for(i = 0; i < max_vertices; i+=3) 24 + { 25 + float offset_x1, offset_x2, offset_x3; 26 + float offset_y1, offset_y2, offset_y3; 27 + float x1, y1; 28 + 29 + offset_x1 = rand()%(w/2); 30 + offset_y1 = rand()%(h/2); 31 + x1 = vertex_values[val_n++] = rand()%screen->w + offset_x1; 32 + y1 = vertex_values[val_n++] = rand()%screen->h + offset_y1; 33 + if(image != NULL) 34 + { 35 + vertex_values[val_n++] = offset_x1/tex_w; 36 + vertex_values[val_n++] = offset_y1/tex_h; 37 + } 38 + vertex_values[val_n++] = (rand()%256)/255.0f; 39 + vertex_values[val_n++] = (rand()%256)/255.0f; 40 + vertex_values[val_n++] = (rand()%256)/255.0f; 41 + vertex_values[val_n++] = (rand()%256)/255.0f; 42 + 43 + offset_x2 = 5 + rand()%(w/2); 44 + offset_y2 = 5 + rand()%(h/2); 45 + vertex_values[val_n++] = x1 + offset_x2; 46 + vertex_values[val_n++] = y1 + offset_y2; 47 + if(image != NULL) 48 + { 49 + vertex_values[val_n++] = (offset_x1 + offset_x2)/tex_w; 50 + vertex_values[val_n++] = (offset_y1 + offset_y2)/tex_h; 51 + } 52 + vertex_values[val_n++] = (rand()%256)/255.0f; 53 + vertex_values[val_n++] = (rand()%256)/255.0f; 54 + vertex_values[val_n++] = (rand()%256)/255.0f; 55 + vertex_values[val_n++] = (rand()%256)/255.0f; 56 + 57 + offset_x3 = -5 - rand()%(w/2); 58 + offset_y3 = 5 + rand()%(h/2); 59 + vertex_values[val_n++] = x1 + offset_x3; 60 + vertex_values[val_n++] = y1 + offset_y3; 61 + if(image != NULL) 62 + { 63 + vertex_values[val_n++] = (offset_x1 + offset_x3)/tex_w; 64 + vertex_values[val_n++] = (offset_y1 + offset_y3)/tex_h; 65 + } 66 + vertex_values[val_n++] = (rand()%256)/255.0f; 67 + vertex_values[val_n++] = (rand()%256)/255.0f; 68 + vertex_values[val_n++] = (rand()%256)/255.0f; 69 + vertex_values[val_n++] = (rand()%256)/255.0f; 70 + 71 + // Once per triangle 72 + { 73 + int n = i/3; 74 + velx[n] = 10 + rand()%screen->w/10; 75 + vely[n] = 10 + rand()%screen->h/10; 76 + if(rand()%2) 77 + velx[n] = -velx[n]; 78 + if(rand()%2) 79 + vely[n] = -vely[n]; 80 + } 81 + } 82 + } 83 + 5 84 int do_interleaved(GPU_Target* screen) 6 85 { 7 86 Uint32 startTime; ··· 21 100 float* velx = (float*)malloc(sizeof(float)*max_vertices/3); 22 101 float* vely = (float*)malloc(sizeof(float)*max_vertices/3); 23 102 int i; 24 - int val_n = 0; 25 103 26 104 GPU_Image* image = GPU_LoadImage("data/test3.png"); 27 105 GPU_LogError("do_interleaved()\n"); ··· 31 109 startTime = SDL_GetTicks(); 32 110 frameCount = 0; 33 111 34 - for(i = 0; i < max_vertices; i+=3) 35 - { 36 - float offset_x1, offset_x2, offset_x3; 37 - float offset_y1, offset_y2, offset_y3; 38 - float x1, y1; 39 - 40 - offset_x1 = rand()%(image->w/2); 41 - offset_y1 = rand()%(image->h/2); 42 - x1 = vertex_values[val_n++] = rand()%screen->w + offset_x1; 43 - y1 = vertex_values[val_n++] = rand()%screen->h + offset_y1; 44 - vertex_values[val_n++] = offset_x1; 45 - vertex_values[val_n++] = offset_y1; 46 - vertex_values[val_n++] = rand()%256; 47 - vertex_values[val_n++] = rand()%256; 48 - vertex_values[val_n++] = rand()%256; 49 - vertex_values[val_n++] = rand()%256; 50 - 51 - offset_x2 = 5 + rand()%(image->w/2); 52 - offset_y2 = 5 + rand()%(image->h/2); 53 - vertex_values[val_n++] = x1 + offset_x2; 54 - vertex_values[val_n++] = y1 + offset_y2; 55 - vertex_values[val_n++] = offset_x1 + offset_x2; 56 - vertex_values[val_n++] = offset_y1 + offset_y2; 57 - vertex_values[val_n++] = rand()%256; 58 - vertex_values[val_n++] = rand()%256; 59 - vertex_values[val_n++] = rand()%256; 60 - vertex_values[val_n++] = rand()%256; 61 - 62 - offset_x3 = -5 - rand()%(image->w/2); 63 - offset_y3 = 5 + rand()%(image->h/2); 64 - vertex_values[val_n++] = x1 + offset_x3; 65 - vertex_values[val_n++] = y1 + offset_y3; 66 - vertex_values[val_n++] = offset_x1 + offset_x3; 67 - vertex_values[val_n++] = offset_y1 + offset_y3; 68 - vertex_values[val_n++] = rand()%256; 69 - vertex_values[val_n++] = rand()%256; 70 - vertex_values[val_n++] = rand()%256; 71 - vertex_values[val_n++] = rand()%256; 72 - if(i%3 == 0) // Once per triangle 73 - { 74 - int n = i/3; 75 - velx[n] = 10 + rand()%screen->w/10; 76 - vely[n] = 10 + rand()%screen->h/10; 77 - if(rand()%2) 78 - velx[n] = -velx[n]; 79 - if(rand()%2) 80 - vely[n] = -vely[n]; 81 - } 82 - } 112 + fill_vertex_values(vertex_values, velx, vely, max_vertices, screen, image); 83 113 84 114 85 115 done = 0; ··· 124 154 for(i = 0; i < num_vertices; i++) 125 155 { 126 156 int n = i/3; 127 - val_n = floats_per_vertex*i; 157 + int val_n = floats_per_vertex*i; 128 158 vertex_values[val_n] += velx[n]*dt; 129 159 vertex_values[val_n+1] += vely[n]*dt; 130 160 if(vertex_values[val_n] < 0) ··· 152 182 153 183 GPU_Clear(screen); 154 184 155 - GPU_TriangleBatch(image, screen, num_vertices, vertex_values, 0, NULL, 0); 185 + GPU_TriangleBatch(image, screen, num_vertices, vertex_values, 0, NULL, GPU_BATCH_XY_ST_RGBA); 156 186 157 187 GPU_Flip(screen); 158 188 ··· 196 226 float* vely = (float*)malloc(sizeof(float)*max_vertices/3); 197 227 unsigned short* indices = (unsigned short*)malloc(sizeof(unsigned short)*max_vertices); 198 228 int i; 199 - int val_n = 0; 200 229 201 230 GPU_Image* image = GPU_LoadImage("data/test3.png"); 202 231 GPU_LogError("do_indexed()\n"); ··· 205 234 206 235 startTime = SDL_GetTicks(); 207 236 frameCount = 0; 208 - for(i = 0; i < max_vertices; i+=3) 209 - { 210 - float offset_x1, offset_x2, offset_x3; 211 - float offset_y1, offset_y2, offset_y3; 212 - float x1, y1; 213 - 214 - offset_x1 = rand()%(image->w/2); 215 - offset_y1 = rand()%(image->h/2); 216 - x1 = vertex_values[val_n++] = rand()%screen->w + offset_x1; 217 - y1 = vertex_values[val_n++] = rand()%screen->h + offset_y1; 218 - vertex_values[val_n++] = offset_x1; 219 - vertex_values[val_n++] = offset_y1; 220 - vertex_values[val_n++] = rand()%256; 221 - vertex_values[val_n++] = rand()%256; 222 - vertex_values[val_n++] = rand()%256; 223 - vertex_values[val_n++] = rand()%256; 224 - indices[i] = i; 225 - 226 - offset_x2 = 5 + rand()%(image->w/2); 227 - offset_y2 = 5 + rand()%(image->h/2); 228 - vertex_values[val_n++] = x1 + offset_x2; 229 - vertex_values[val_n++] = y1 + offset_y2; 230 - vertex_values[val_n++] = offset_x1 + offset_x2; 231 - vertex_values[val_n++] = offset_y1 + offset_y2; 232 - vertex_values[val_n++] = rand()%256; 233 - vertex_values[val_n++] = rand()%256; 234 - vertex_values[val_n++] = rand()%256; 235 - vertex_values[val_n++] = rand()%256; 236 - indices[i+1] = i+1; 237 - 238 - offset_x3 = -5 - rand()%(image->w/2); 239 - offset_y3 = 5 + rand()%(image->h/2); 240 - vertex_values[val_n++] = x1 + offset_x3; 241 - vertex_values[val_n++] = y1 + offset_y3; 242 - vertex_values[val_n++] = offset_x1 + offset_x3; 243 - vertex_values[val_n++] = offset_y1 + offset_y3; 244 - vertex_values[val_n++] = rand()%256; 245 - vertex_values[val_n++] = rand()%256; 246 - vertex_values[val_n++] = rand()%256; 247 - vertex_values[val_n++] = rand()%256; 248 - indices[i+2] = i+2; 249 - 250 - if(i%3 == 0) // Once per triangle 251 - { 252 - int n = i/3; 253 - velx[n] = 10 + rand()%screen->w/10; 254 - vely[n] = 10 + rand()%screen->h/10; 255 - if(rand()%2) 256 - velx[n] = -velx[n]; 257 - if(rand()%2) 258 - vely[n] = -vely[n]; 259 - } 260 - } 237 + 238 + fill_vertex_values(vertex_values, velx, vely, max_vertices, screen, image); 239 + // Setup indices 240 + for(i = 0; i < max_vertices; i++) 241 + indices[i] = i; 261 242 262 243 263 244 done = 0; ··· 302 283 for(i = 0; i < num_vertices; i++) 303 284 { 304 285 int n = i/3; 305 - val_n = floats_per_vertex*i; 286 + int val_n = floats_per_vertex*i; 306 287 vertex_values[val_n] += velx[n]*dt; 307 288 vertex_values[val_n+1] += vely[n]*dt; 308 289 if(vertex_values[val_n] < 0) ··· 330 311 331 312 GPU_Clear(screen); 332 313 333 - GPU_TriangleBatch(image, screen, num_vertices, vertex_values, num_vertices, indices, 0); 314 + GPU_TriangleBatch(image, screen, num_vertices, vertex_values, num_vertices, indices, GPU_BATCH_XY_ST_RGBA); 334 315 335 316 GPU_Flip(screen); 336 317 ··· 371 352 float* velx = (float*)malloc(sizeof(float)*max_vertices/3); 372 353 float* vely = (float*)malloc(sizeof(float)*max_vertices/3); 373 354 int i; 374 - int val_n = 0; 375 355 376 356 // 2 pos floats per vertex, 2 texcoords, 4 color components 377 357 int floats_per_vertex = 2 + 2 + 4; ··· 389 369 startTime = SDL_GetTicks(); 390 370 frameCount = 0; 391 371 372 + fill_vertex_values(vertex_values, velx, vely, max_vertices, screen, image); 373 + 392 374 // Load attributes for the textured shader 393 375 program_object = 0; 394 376 GPU_ActivateShaderProgram(program_object, NULL); ··· 406 388 GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 4 * sizeof(float))); 407 389 408 390 409 - for(i = 0; i < max_vertices; i+=3) 410 - { 411 - float offset_x1, offset_x2, offset_x3; 412 - float offset_y1, offset_y2, offset_y3; 413 - float x1, y1; 414 - 415 - offset_x1 = rand()%(image->w/2); 416 - offset_y1 = rand()%(image->h/2); 417 - x1 = vertex_values[val_n++] = rand()%screen->w + offset_x1; 418 - y1 = vertex_values[val_n++] = rand()%screen->h + offset_y1; 419 - vertex_values[val_n++] = offset_x1/image->texture_w; 420 - vertex_values[val_n++] = offset_y1/image->texture_h; 421 - vertex_values[val_n++] = rand()%101/100.0f; 422 - vertex_values[val_n++] = rand()%101/100.0f; 423 - vertex_values[val_n++] = rand()%101/100.0f; 424 - vertex_values[val_n++] = rand()%101/100.0f; 425 - 426 - offset_x2 = 5 + rand()%(image->w/2); 427 - offset_y2 = 5 + rand()%(image->h/2); 428 - vertex_values[val_n++] = x1 + offset_x2; 429 - vertex_values[val_n++] = y1 + offset_y2; 430 - vertex_values[val_n++] = (offset_x1 + offset_x2)/image->texture_w; 431 - vertex_values[val_n++] = (offset_y1 + offset_y2)/image->texture_h; 432 - vertex_values[val_n++] = rand()%101/100.0f; 433 - vertex_values[val_n++] = rand()%101/100.0f; 434 - vertex_values[val_n++] = rand()%101/100.0f; 435 - vertex_values[val_n++] = rand()%101/100.0f; 436 - 437 - offset_x3 = -5 - rand()%(image->w/2); 438 - offset_y3 = 5 + rand()%(image->h/2); 439 - vertex_values[val_n++] = x1 + offset_x3; 440 - vertex_values[val_n++] = y1 + offset_y3; 441 - vertex_values[val_n++] = (offset_x1 + offset_x3)/image->texture_w; 442 - vertex_values[val_n++] = (offset_y1 + offset_y3)/image->texture_h; 443 - vertex_values[val_n++] = rand()%101/100.0f; 444 - vertex_values[val_n++] = rand()%101/100.0f; 445 - vertex_values[val_n++] = rand()%101/100.0f; 446 - vertex_values[val_n++] = rand()%101/100.0f; 447 - 448 - if(i%3 == 0) // Once per triangle 449 - { 450 - int n = i/3; 451 - velx[n] = 10 + rand()%screen->w/10; 452 - vely[n] = 10 + rand()%screen->h/10; 453 - if(rand()%2) 454 - velx[n] = -velx[n]; 455 - if(rand()%2) 456 - vely[n] = -vely[n]; 457 - } 458 - } 459 - 460 - 461 391 done = 0; 462 392 while(!done) 463 393 { ··· 502 432 for(i = 0; i < num_vertices; i++) 503 433 { 504 434 int n = i/3; 505 - val_n = floats_per_vertex*i; 435 + int val_n = floats_per_vertex*i; 506 436 vertex_values[val_n] += velx[n]*dt; 507 437 vertex_values[val_n+1] += vely[n]*dt; 508 438 if(vertex_values[val_n] < 0) ··· 531 461 GPU_SetAttributeSource(num_vertices, attributes[0]); 532 462 GPU_SetAttributeSource(num_vertices, attributes[1]); 533 463 GPU_SetAttributeSource(num_vertices, attributes[2]); 534 - GPU_TriangleBatch(image, screen, num_vertices, NULL, 0, NULL, 0); 464 + GPU_TriangleBatch(image, screen, num_vertices, NULL, 0, NULL, GPU_NONE); 535 465 536 466 GPU_Flip(screen); 537 467 ··· 581 511 int floats_per_vertex = 2 + 4; 582 512 float* vertex_values = (float*)malloc(sizeof(float)*max_vertices*floats_per_vertex); 583 513 584 - Uint32 program_object; 585 - GPU_Attribute attributes[2]; 586 - 587 514 GPU_LogError("do_untextured()\n"); 588 515 589 516 startTime = SDL_GetTicks(); 590 517 frameCount = 0; 591 518 592 - // Load attributes for the textured shader 593 - program_object = GPU_GetContextTarget()->context->default_untextured_shader_program; 594 - GPU_ActivateShaderProgram(program_object, NULL); 595 - // Disable the default shader's attributes (not a typical thing to do...) 596 - { 597 - GPU_ShaderBlock block = {-1,-1,-1,GPU_GetUniformLocation(program_object, "gpu_ModelViewProjectionMatrix")}; 598 - GPU_ActivateShaderProgram(program_object, &block); 599 - } 519 + fill_vertex_values(vertex_values, velx, vely, max_vertices, screen, NULL); 600 520 601 - attributes[0] = GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Vertex"), vertex_values, 602 - GPU_MakeAttributeFormat(2, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 0)); 603 - attributes[1] = GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Color"), vertex_values, 604 - GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 2 * sizeof(float))); 605 - 606 - 607 - for(i = 0; i < max_vertices; i+=3) 608 - { 609 - float offset_x1, offset_x2, offset_x3; 610 - float offset_y1, offset_y2, offset_y3; 611 - float x1, y1; 612 - 613 - offset_x1 = rand()%(40); 614 - offset_y1 = rand()%(40); 615 - x1 = vertex_values[val_n++] = rand()%screen->w + offset_x1; 616 - y1 = vertex_values[val_n++] = rand()%screen->h + offset_y1; 617 - vertex_values[val_n++] = rand()%101/100.0f; 618 - vertex_values[val_n++] = rand()%101/100.0f; 619 - vertex_values[val_n++] = rand()%101/100.0f; 620 - vertex_values[val_n++] = rand()%101/100.0f; 621 - 622 - offset_x2 = 5 + rand()%(40); 623 - offset_y2 = 5 + rand()%(40); 624 - vertex_values[val_n++] = x1 + offset_x2; 625 - vertex_values[val_n++] = y1 + offset_y2; 626 - vertex_values[val_n++] = rand()%101/100.0f; 627 - vertex_values[val_n++] = rand()%101/100.0f; 628 - vertex_values[val_n++] = rand()%101/100.0f; 629 - vertex_values[val_n++] = rand()%101/100.0f; 630 - 631 - offset_x3 = -5 - rand()%(40); 632 - offset_y3 = 5 + rand()%(40); 633 - vertex_values[val_n++] = x1 + offset_x3; 634 - vertex_values[val_n++] = y1 + offset_y3; 635 - vertex_values[val_n++] = rand()%101/100.0f; 636 - vertex_values[val_n++] = rand()%101/100.0f; 637 - vertex_values[val_n++] = rand()%101/100.0f; 638 - vertex_values[val_n++] = rand()%101/100.0f; 639 - 640 - if(i%3 == 0) // Once per triangle 641 - { 642 - int n = i/3; 643 - velx[n] = 10 + rand()%screen->w/10; 644 - vely[n] = 10 + rand()%screen->h/10; 645 - if(rand()%2) 646 - velx[n] = -velx[n]; 647 - if(rand()%2) 648 - vely[n] = -vely[n]; 649 - } 650 - } 651 - 652 521 653 522 done = 0; 654 523 while(!done) ··· 719 588 vely[n] = -vely[n]; 720 589 } 721 590 } 722 - 723 - GPU_SetAttributeSource(num_vertices, attributes[0]); 724 - GPU_SetAttributeSource(num_vertices, attributes[1]); 725 - GPU_TriangleBatch(NULL, screen, num_vertices, NULL, 0, NULL, 0); 591 + 592 + GPU_TriangleBatch(NULL, screen, num_vertices, vertex_values, 0, NULL, GPU_BATCH_XY | GPU_BATCH_RGBA); 726 593 727 594 GPU_Flip(screen); 728 595