static void test_value(int c) { int i; pmemset(buf, c, 10); for (i = 0; i < 10; i++) if ((unsigned char)buf[i] != (unsigned char)c) { t_error("memset(%d) failed: got %d\n", c, buf[i]); break; } }
static void test_align(int align, int len) { char *s = aligned(buf); char *want = aligned(buf + 256); char *p; int i; if (align + len > N) abort(); for (i = 0; i < N; i++) s[i] = want[i] = ' '; for (i = 0; i < len; i++) want[align+i] = '#'; p = pmemset(s+align, '#', len); if (p != s+align) t_error("memset(%p,...) returned %p\n", s+align, p); for (i = 0; i < N; i++) if (s[i] != want[i]) { t_error("memset(align %d, '#', %d) failed\n", align, len); t_printf("got : %.*s\n", align+len+1, s); t_printf("want: %.*s\n", align+len+1, want); break; } }
PMaterialParameter::PMaterialParameter(const pchar *name, const pchar *uniformName, PGlShaderUniformTypeEnum type, puint32 size) : PAbstractRenderParameter(name, uniformName) , m_size(size) { puint32 dataTypeSizes[] = { 4, // P_GLSHADERUNIFORM_FLOAT, 8, // P_GLSHADERUNIFORM_FLOAT2, 12, // P_GLSHADERUNIFORM_FLOAT3, 16, // P_GLSHADERUNIFORM_FLOAT4, 4, // P_GLSHADERUNIFORM_INT, 8, // P_GLSHADERUNIFORM_INT2, 12, // P_GLSHADERUNIFORM_INT3, 16, // P_GLSHADERUNIFORM_INT4, 4, // P_GLSHADERUNIFORM_BOOL, 8, // P_GLSHADERUNIFORM_BOOL2, 12, // P_GLSHADERUNIFORM_BOOL3, 16, // P_GLSHADERUNIFORM_BOOL4, 16, // P_GLSHADERUNIFORM_FLOATMATRIX2X2, 36, // P_GLSHADERUNIFORM_FLOATMATRIX3X3, 64, // P_GLSHADERUNIFORM_FLOATMATRIX4X4, 4, // P_GLSHADERUNIFORM_SAMPLER2D, 4, // P_GLSHADERUNIFORM_SAMPLERCUBE, }; if ((type != P_GLSHADERUNIFORM_UNKNOWN && type != P_GLSHADERUNIFORM_FLOAT && type != P_GLSHADERUNIFORM_INT && type != P_GLSHADERUNIFORM_BOOL && type != P_GLSHADERUNIFORM_SAMPLER2D && type != P_GLSHADERUNIFORM_SAMPLERCUBE) || m_size > 1) { m_value.v = PNEWARRAY(puint8, dataTypeSizes[type] * m_size); pmemset(m_value.v, 0, dataTypeSizes[type] * m_size); } else { m_value.i = 0; } m_uniformType = type; if (m_uniformType == P_GLSHADERUNIFORM_UNKNOWN) { m_upload = P_NULL; } else if (m_size > 1) { UploadFuncPointer functions[] = { &PMaterialParameter::uploadFloatArray, &PMaterialParameter::uploadFloat2Array, &PMaterialParameter::uploadFloat3Array, &PMaterialParameter::uploadFloat4Array, &PMaterialParameter::uploadIntArray, &PMaterialParameter::uploadInt2Array, &PMaterialParameter::uploadInt3Array, &PMaterialParameter::uploadInt4Array, &PMaterialParameter::uploadIntArray, &PMaterialParameter::uploadInt2Array, &PMaterialParameter::uploadInt3Array, &PMaterialParameter::uploadInt4Array, &PMaterialParameter::uploadMatrix2x2Array, &PMaterialParameter::uploadMatrix3x3Array, &PMaterialParameter::uploadMatrix4x4Array, }; m_upload = functions[m_uniformType]; } else { UploadFuncPointer functions[] = { &PMaterialParameter::uploadFloat, &PMaterialParameter::uploadFloat2, &PMaterialParameter::uploadFloat3, &PMaterialParameter::uploadFloat4, &PMaterialParameter::uploadInt, &PMaterialParameter::uploadInt2, &PMaterialParameter::uploadInt3, &PMaterialParameter::uploadInt4, &PMaterialParameter::uploadInt, &PMaterialParameter::uploadInt2, &PMaterialParameter::uploadInt3, &PMaterialParameter::uploadInt4, &PMaterialParameter::uploadMatrix2x2, &PMaterialParameter::uploadMatrix3x3, &PMaterialParameter::uploadMatrix4x4, &PMaterialParameter::uploadTexture, &PMaterialParameter::uploadTexture, }; m_upload = functions[m_uniformType]; } }
PDir::PDir(const pchar *dir) { // Check if the directory exists. DWORD dwAttrib = GetFileAttributesA(dir); m_exists = ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // Fetch the item names in that directory. WIN32_FIND_DATAA fdFile; HANDLE hFind = NULL; pchar path[MAX_PATH]; m_items = P_NULL; m_numberOfItems = 0; puint32 capacity = 32; puint8 *itemData = PNEWARRAY(puint8, capacity * sizeof(PDirItem)); pmemset(itemData, 0, sizeof(capacity * sizeof(PDirItem))); // Specify a file mask. *.* = We want everything! psprintf(path, MAX_PATH, "%s\\*.*", dir); if ((hFind = FindFirstFileA(path, &fdFile)) == INVALID_HANDLE_VALUE) { PLOG_ERROR("Failed to read directory %s.", dir); PDELETEARRAY(itemData); } else { do { // Find first file will always return "." and ".." // as the first two directories. if (pstrcmp(fdFile.cFileName, ".") != 0 && pstrcmp(fdFile.cFileName, "..") != 0) { //Build up our file path using the passed in // [sDir] and the file/foldername we just found: psprintf(path, MAX_PATH, "%s\\%s", dir, fdFile.cFileName); //Is the entity a File or Folder? if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // TODO: use the absolute path. new (itemData + m_numberOfItems * sizeof(PDirItem)) PDirItem(P_DIR_ITEM_DIRECTORY, path); m_numberOfItems++; } else if ((fdFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (fdFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)) { // TODO: use the absolute path. new (itemData + m_numberOfItems * sizeof(PDirItem)) PDirItem(P_DIR_ITEM_FILE, path); m_numberOfItems++; } else { PLOG_WARNING("%s is not seen supported.", path); } if (m_numberOfItems > capacity) { resizeItemArray(itemData, capacity, 2 * capacity); capacity *= 2; } } } while (FindNextFileA(hFind, &fdFile)); // Find the next file. FindClose(hFind); //Always, Always, clean things up! } m_items = reinterpret_cast<PDirItem*>(itemData); }
PDir::PDir(const pchar *dir) { // Check if the directory exists. struct stat s; if (stat(dir, &s) != 0 || !S_ISDIR(s.st_mode)) { PLOG_ERROR("%s is not a valid directory.", dir); m_exists = false; } else { m_exists = true; // Fetch the item names in that directory. DIR* dirObject; dirent* dirEntry; m_items = P_NULL; m_numberOfItems = 0; puint32 capacity = 32; puint8* itemData = PNEWARRAY(puint8, capacity * sizeof(PDirItem)); pmemset(itemData, 0, sizeof(capacity * sizeof(PDirItem))); if ((dirObject = opendir(dir)) == NULL) { PLOG_ERROR("Failed to read directory %s.", dir); PDELETEARRAY(itemData); } else { char path[4096]; while ((dirEntry = readdir(dirObject)) != NULL) { // Find first file will always return "." and ".." // as the first two directories. if (pstrcmp(dirEntry->d_name, ".") != 0 && pstrcmp(dirEntry->d_name, "..") != 0) { //Build up our file path using the passed in // [sDir] and the file/foldername we just found: psprintf(path, 4096, "%s\\%s", dir, dirEntry->d_name); //Is the entity a File or Folder? if (dirEntry->d_type == DT_DIR) { // TODO: use the absolute path. new (itemData + m_numberOfItems * sizeof(PDirItem)) PDirItem(P_DIR_ITEM_DIRECTORY, path); m_numberOfItems++; } else if (dirEntry->d_type == DT_REG) { // TODO: use the absolute path. new (itemData + m_numberOfItems * sizeof(PDirItem)) PDirItem(P_DIR_ITEM_FILE, path); m_numberOfItems++; } else { PLOG_WARNING("%s is not supported.", path); } if (m_numberOfItems > capacity) { resizeItemArray(itemData, capacity, 2 * capacity); capacity *= 2; } } } closedir(dirObject); //Always, Always, clean things up! m_items = reinterpret_cast<PDirItem*>(itemData); } } }