bool LuaScript::LoadRawFile(const String& fileName) { PROFILE(LoadRawFile); LOGINFO("Finding Lua file on file system: " + fileName); ResourceCache* cache = GetSubsystem<ResourceCache>(); String filePath = cache->GetResourceFileName(fileName); if (filePath.Empty()) { LOGINFO("Lua file not found: " + fileName); return false; } filePath = GetNativePath(filePath); LOGINFO("Loading Lua file from file system: " + filePath); if (luaL_loadfile(luaState_, filePath.CString())) { const char* message = lua_tostring(luaState_, -1); LOGERRORF("Load Lua file failed: %s", message); lua_pop(luaState_, 1); return false; } LOGINFO("Lua file loaded: " + filePath); return true; }
void ShaderVariation::SaveByteCode(const String& binaryShaderName) { ResourceCache* cache = owner_->GetSubsystem<ResourceCache>(); FileSystem* fileSystem = owner_->GetSubsystem<FileSystem>(); // Filename may or may not be inside the resource system String fullName = binaryShaderName; if (!IsAbsolutePath(fullName)) { // If not absolute, use the resource dir of the shader String shaderFileName = cache->GetResourceFileName(owner_->GetName()); if (shaderFileName.Empty()) return; fullName = shaderFileName.Substring(0, shaderFileName.Find(owner_->GetName())) + binaryShaderName; } String path = GetPath(fullName); if (!fileSystem->DirExists(path)) fileSystem->CreateDir(path); SharedPtr<File> file(new File(owner_->GetContext(), fullName, FILE_WRITE)); if (!file->IsOpen()) return; file->WriteFileID("USHD"); file->WriteShort((unsigned short)type_); file->WriteShort(3); file->WriteUInt(parameters_.Size()); for (HashMap<StringHash, ShaderParameter>::ConstIterator i = parameters_.Begin(); i != parameters_.End(); ++i) { file->WriteString(i->second_.name_); file->WriteUByte((unsigned char)i->second_.register_); file->WriteUByte((unsigned char)i->second_.regCount_); } unsigned usedTextureUnits = 0; for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i) { if (useTextureUnit_[i]) ++usedTextureUnits; } file->WriteUInt(usedTextureUnits); for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i) { if (useTextureUnit_[i]) { file->WriteString(graphics_->GetTextureUnitName((TextureUnit)i)); file->WriteUByte((unsigned char)i); } } unsigned dataSize = byteCode_.Size(); file->WriteUInt(dataSize); if (dataSize) file->Write(&byteCode_[0], dataSize); }
bool Shader::ProcessSource(String& code, Deserializer& source) { ResourceCache* cache = GetSubsystem<ResourceCache>(); // If the source if a non-packaged file, store the timestamp File* file = dynamic_cast<File*>(&source); if (file && !file->IsPackaged()) { FileSystem* fileSystem = GetSubsystem<FileSystem>(); String fullName = cache->GetResourceFileName(file->GetName()); unsigned fileTimeStamp = fileSystem->GetLastModifiedTime(fullName); if (fileTimeStamp > timeStamp_) timeStamp_ = fileTimeStamp; } // Store resource dependencies for includes so that we know to reload if any of them changes if (source.GetName() != GetName()) cache->StoreResourceDependency(this, source.GetName()); while (!source.IsEof()) { String line = source.ReadLine(); if (line.StartsWith("#include")) { String includeFileName = GetPath(source.GetName()) + line.Substring(9).Replaced("\"", "").Trimmed(); SharedPtr<File> includeFile = cache->GetFile(includeFileName); if (!includeFile) return false; // Add the include file into the current code recursively if (!ProcessSource(code, *includeFile)) return false; } else { code += line; code += "\n"; } } // Finally insert an empty line to mark the space between files code += "\n"; return true; }
bool ShaderVariation::LoadByteCode(const String& binaryShaderName) { ResourceCache* cache = owner_->GetSubsystem<ResourceCache>(); if (!cache->Exists(binaryShaderName)) return false; FileSystem* fileSystem = owner_->GetSubsystem<FileSystem>(); unsigned sourceTimeStamp = owner_->GetTimeStamp(); // If source code is loaded from a package, its timestamp will be zero. Else check that binary is not older // than source if (sourceTimeStamp && fileSystem->GetLastModifiedTime(cache->GetResourceFileName(binaryShaderName)) < sourceTimeStamp) return false; SharedPtr<File> file = cache->GetFile(binaryShaderName); if (!file || file->ReadFileID() != "USHD") { ATOMIC_LOGERROR(binaryShaderName + " is not a valid shader bytecode file"); return false; } /// \todo Check that shader type and model match /*unsigned short shaderType = */file->ReadUShort(); /*unsigned short shaderModel = */file->ReadUShort(); unsigned numParameters = file->ReadUInt(); for (unsigned i = 0; i < numParameters; ++i) { String name = file->ReadString(); unsigned reg = file->ReadUByte(); unsigned regCount = file->ReadUByte(); ShaderParameter parameter; parameter.type_ = type_; parameter.name_ = name; parameter.register_ = reg; parameter.regCount_ = regCount; parameters_[StringHash(name)] = parameter; } unsigned numTextureUnits = file->ReadUInt(); for (unsigned i = 0; i < numTextureUnits; ++i) { /*String unitName = */file->ReadString(); unsigned reg = file->ReadUByte(); if (reg < MAX_TEXTURE_UNITS) useTextureUnit_[reg] = true; } unsigned byteCodeSize = file->ReadUInt(); if (byteCodeSize) { byteCode_.Resize(byteCodeSize); file->Read(&byteCode_[0], byteCodeSize); if (type_ == VS) ATOMIC_LOGDEBUG("Loaded cached vertex shader " + GetFullName()); else ATOMIC_LOGDEBUG("Loaded cached pixel shader " + GetFullName()); return true; } else { ATOMIC_LOGERROR(binaryShaderName + " has zero length bytecode"); return false; } }