void ShaderSource::insertSourceShader(const std::string &name_of_shader, const std::string &filename, const std::string &program) { /*infostream<<"ShaderSource::insertSourceShader(): " "name_of_shader=\""<<name_of_shader<<"\", " "filename=\""<<filename<<"\""<<std::endl;*/ sanity_check(thr_is_current_thread(m_main_thread)); m_sourcecache.insert(name_of_shader, filename, program, true); }
/* This method generates all the shaders */ u32 ShaderSource::getShaderIdDirect(const std::string &name, const u8 material_type, const u8 drawtype) { //infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl; // Empty name means shader 0 if(name == ""){ infostream<<"getShaderIdDirect(): name is empty"<<std::endl; return 0; } // Check if already have such instance for(u32 i=0; i<m_shaderinfo_cache.size(); i++){ ShaderInfo *info = &m_shaderinfo_cache[i]; if(info->name == name && info->material_type == material_type && info->drawtype == drawtype) return i; } /* Calling only allowed from main thread */ if (!thr_is_current_thread(m_main_thread)) { errorstream<<"ShaderSource::getShaderIdDirect() " "called not from main thread"<<std::endl; return 0; } ShaderInfo info = generate_shader(name, material_type, drawtype, m_device, m_shader_callback, &m_sourcecache); /* Add shader to caches (add dummy shaders too) */ MutexAutoLock lock(m_shaderinfo_cache_mutex); u32 id = m_shaderinfo_cache.size(); m_shaderinfo_cache.push_back(info); infostream<<"getShaderIdDirect(): " <<"Returning id="<<id<<" for name \""<<name<<"\""<<std::endl; return id; }
u32 ShaderSource::getShader(const std::string &name, const u8 material_type, const u8 drawtype) { /* Get shader */ if (thr_is_current_thread(m_main_thread)) { return getShaderIdDirect(name, material_type, drawtype); } else { /*errorstream<<"getShader(): Queued: name=\""<<name<<"\""<<std::endl;*/ // We're gonna ask the result to be put into here static ResultQueue<std::string, u32, u8, u8> result_queue; // Throw a request in m_get_shader_queue.add(name, 0, 0, &result_queue); /* infostream<<"Waiting for shader from main thread, name=\"" <<name<<"\""<<std::endl;*/ while(true) { GetResult<std::string, u32, u8, u8> result = result_queue.pop_frontNoEx(); if (result.key == name) { return result.item; } else { errorstream << "Got shader with invalid name: " << result.key << std::endl; } } } infostream<<"getShader(): Failed"<<std::endl; return 0; }
ClientCached* getClientCached(const std::string &name, IGameDef *gamedef) const { ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; if(thr_is_current_thread(m_main_thread)) { return createClientCachedDirect(name, gamedef); } else { // We're gonna ask the result to be put into here static ResultQueue<std::string, ClientCached*, u8, u8> result_queue; // Throw a request in m_get_clientcached_queue.add(name, 0, 0, &result_queue); try{ while(true) { // Wait result for a second GetResult<std::string, ClientCached*, u8, u8> result = result_queue.pop_front(1000); if (result.key == name) { return result.item; } } } catch(ItemNotFoundException &e) { errorstream<<"Waiting for clientcached " << name << " timed out."<<std::endl; return &m_dummy_clientcached; } } }
ClientCached* createClientCachedDirect(const std::string &name, IGameDef *gamedef) const { infostream<<"Lazily creating item texture and mesh for \"" <<name<<"\""<<std::endl; // This is not thread-safe sanity_check(thr_is_current_thread(m_main_thread)); // Skip if already in cache ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; ITextureSource *tsrc = gamedef->getTextureSource(); const ItemDefinition &def = get(name); // Create new ClientCached cc = new ClientCached(); // Create an inventory texture cc->inventory_texture = NULL; if(def.inventory_image != "") cc->inventory_texture = tsrc->getTexture(def.inventory_image); ItemStack item = ItemStack(); item.name = def.name; scene::IMesh *mesh = getItemMesh(gamedef, item); cc->wield_mesh = mesh; // Put in cache m_clientcached.set(name, cc); return cc; }
bool Thread::isCurrentThread() { return thr_is_current_thread(m_thread_id); }