Ejemplo n.º 1
0
Archivo: renderman.c Proyecto: fscz/gre
static int tear_down_program(SceneData* sceneData, SceneObject* so, SoData* soData) {

  tear_down_shaders ( sceneData, so, soData );

  char progKey[KEYSIZE];
  snprintf (progKey, KEYSIZE, "%p,%p", &so->vShader, &so->fShader);

  GLuint program = (GLuint)hashmap_find(progKey, sceneData->mapShaderShader2Handle);  
  size_t programUsers = (size_t)hashmap_find ( progKey, sceneData->countProgramUsers );
  programUsers--;

  if ( 0 == programUsers ) {

    glDeleteProgram ( program );

    hashmap_delete ( progKey, sceneData->mapShaderShader2Handle );
    hashmap_delete ( progKey, sceneData->countProgramUsers );
    list_remove_elem ( (void*)program, sceneData->listPrograms );

  } else {
    hashmap_insert ( progKey, (void*)programUsers, sceneData->countProgramUsers );
  }

  return 0;
}
Ejemplo n.º 2
0
Archivo: renderman.c Proyecto: fscz/gre
static int tear_down_index_buffer (SceneData* sceneData, SceneObject* so, SoData* soData) {
  Draw* method = so->draw;

  char bufferKey[KEYSIZE];

  Attribute* indices = method->indices;
  GLuint bufferHandle;
  size_t bufferUsers;

  if ( indices && 1 == indices->useBuffer ) {
    snprintf (bufferKey, KEYSIZE, "%p", indices);
    bufferUsers = (size_t)hashmap_find ( bufferKey, sceneData->countBufferUsers );
    bufferUsers--;

    if ( 0 == bufferUsers ) {

      bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle );
      glDeleteBuffers ( 1, &bufferHandle );
      hashmap_delete ( bufferKey, sceneData->mapBuffer2Handle );
      hashmap_delete ( bufferKey, sceneData->countBufferUsers );
      list_remove_elem ( (void*)bufferHandle, sceneData->listBuffers );

    } else {
      
      hashmap_insert ( bufferKey, (void*)bufferUsers, sceneData->countBufferUsers );
    }

  }

  return 0;
}
Ejemplo n.º 3
0
Archivo: renderman.c Proyecto: fscz/gre
static int setup_program(SceneData* sceneData, SceneObject* so, SoData* soData, GLuint* programHandle) {

  GLuint vShaderHandle;
  GLuint fShaderHandle;
  if ( -1 == setup_shaders( sceneData, so, soData, &vShaderHandle, &fShaderHandle ) ) return -1;

  char progKey[KEYSIZE];
  snprintf (progKey, KEYSIZE, "%p,%p", &so->vShader, &so->fShader);

  GLuint program = (GLuint)hashmap_find(progKey, sceneData->mapShaderShader2Handle);  
  size_t programUsers;
  if (program) {

    *programHandle = program;
    programUsers = (size_t)hashmap_find ( progKey, sceneData->countProgramUsers );
    hashmap_insert ( progKey, (void*)(programUsers+1), sceneData->countProgramUsers );   
  } else {

    program = create_program( so->vShader, so->fShader, vShaderHandle, fShaderHandle );
    if ( -1 == program ) return -1;
    
    list_add ( (void*)program, sceneData->listPrograms );
    hashmap_insert ( progKey, (void*)program, sceneData->mapShaderShader2Handle );
    hashmap_insert ( progKey, (void*)1, sceneData->countProgramUsers );
    
    *programHandle = program;
  }
  return 0;
}
Ejemplo n.º 4
0
Archivo: renderman.c Proyecto: fscz/gre
static int tear_down_textures (SceneData* sceneData, SceneObject* so, SoData* soData) {
  
  Uniform* uniform;
  char uniKey[KEYSIZE];
  GLuint textureHandle;
  size_t textureUsers;

  int i;
  for ( i = 0; i < list_size ( soData->textures ); i++) {
    
    uniform = list_get( i, soData->textures);
    snprintf ( uniKey, KEYSIZE, "%p", uniform );    
    textureHandle = (GLuint)hashmap_find ( uniKey, sceneData->mapTexture2Handle );
    textureUsers = (size_t)hashmap_find ( uniKey, sceneData->countTextureUsers );
    
    textureUsers--;

    if ( 0 == textureUsers ) {
      glDeleteTextures( 1, &textureHandle );
      
      hashmap_delete ( uniKey, sceneData->mapTexture2Handle );
      hashmap_delete ( uniKey, sceneData->countTextureUsers );
      list_remove_elem ( (void*)textureHandle, sceneData->listTextures );

    } else {
      hashmap_insert ( uniKey, (void*)textureUsers, sceneData->countTextureUsers );
    }
  }

  return 0;
}
Ejemplo n.º 5
0
Result parser_value(register Parser *pp, int *value, char const *text)
{
                                                        /* find the counter */
    register HashItem *item = hashmap_find(pp->d_symtab_ptr, text, COUNTER);

    if (item != PFAILED)                                /* got it           */
    {
        *value = counter_value(item);                   /* assign the value */
        if (message_show(MSG_INFO))
            message("%s: counter[%s] == %d", parser_fun(), text, *value);
    }
    else if (sscanf(text, "%d", value) == 1)    /* convert text to number   */
    {
        if (message_show(MSG_INFO))
            message("%s: got value %d\n", parser_fun(),  *value);
    }
    else                                                    /* no number    */
    {
        if (message_show(MSG_ERR))
            message("%s: Number expected at `%s'", parser_fun(), text);
        return FAILED;
    }

    return SUCCESS;
}
Ejemplo n.º 6
0
void hash_test_func(hashmap_t *hmp)
{
	char *val = NULL;
    char key[1024] = {0};
    char buf[1024] = {0};
    int j=0;
    int i=0;
    
    for(; i< 9; i++) {
		j = 0xffffff - i;
        sprintf(key, "key_%x", j);
		sprintf(buf, "val_%x", j);
        
        val = strdup(buf);
        hashmap_insert(hmp, key, strlen(key), val);
	}

	for(i=0; i< 9; i++) {
        j = 0xffffff - i;
        sprintf(buf, "key_%x", j);
        
        printf("find key:%s, val:%s\n", buf, hashmap_find(hmp, buf, strlen(buf)));
        
    }
}
Ejemplo n.º 7
0
void gram_SYMBOLVALUE()
{
    register char *name;
    register HashItem *item;

    parser_push_fun("SYMBOLVALUE");

    name = parser_name_parlist(&parser,  false);
    item = hashmap_find(&symtab, name, SYMBOL);

    if (item == PFAILED && message_show(MSG_ERR))
        message("SYMBOLVALUE: symbol `%s' not defined", name);
    else
    {
        char const *value = symbol_value(item);

        if (message_show(MSG_NOTICE))
            message("SYMBOLVALUE: `%s' = %s", name, value);

        lexer_push_str(&parser.d_lexer, value);
    }

    parser_pop_fun();
    free(name);
}
Ejemplo n.º 8
0
HashItem *construct_preset(char const *setkey, char *rest)
{
    char *key;
    HashItem *mapItem;

    if (!(key = string_firstword(&rest)))               /* get the key      */
    {                                                   /* e.g., title      */
        message_error("`preset ...: missing key");
        return 0;
    }

    if (strcmp(key, "nohtmlfive") == 0)
        global.d_html5 = 0;
    else
    {
        if (!*string_strip(&rest))                      /* get the value    */
            warning("Empty value of symbol `%s'", key); /* e.g., This is... */
    
        if (strcmp(key, "styleopt") == 0)               /* store styleopts  */
            lines_add(&global.d_styleopt, rest);
        else
        {                                                   
                                                        /* look up the key  */
            mapItem = hashmap_find(&global.d_symbol, key, ANY); 
    
            if (mapItem != PFAILED)                     /* reassign         */
                hashitem_set(mapItem, rest, free);      /* existing value   */
            else                                        /* or insert new    */
                hashmap_insert(&global.d_symbol,        /* element          */
                    hashitem_construct(VOIDPTR, key, new_str(rest), free));
        }
    }
    free(key);
    return hashitem_construct(VOIDPTR, "", 0, root_nop);
}
Ejemplo n.º 9
0
/*
 * Get the file appropriate for a given error.
 */
static char *get_html_file (unsigned int errornum)
{
        hashmap_iter result_iter;
        char errornbuf[ERRORNUM_BUFSIZE];
        char *key;
        static char *val;

        assert (errornum >= 100 && errornum < 1000);

        if (!config.errorpages)
                return (config.errorpage_undef);

        snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum);

        result_iter = hashmap_find (config.errorpages, errornbuf);

        if (hashmap_is_end (config.errorpages, result_iter))
                return (config.errorpage_undef);

        if (hashmap_return_entry (config.errorpages, result_iter,
                                  &key, (void **) &val) < 0)
                return (config.errorpage_undef);

        return (val);
}
Ejemplo n.º 10
0
static void random_insertions( void )
{
	int res;
	hashmap_t hashmap;
	unsigned int i;

	res = hashmap_init_with_buckets( &hashmap, 8192 /* pow 2 */ );
	assert( 0 == res );

	for( i = 0; i < 100000; ++i )
	{
		char str[10];
		hashmap_value_t val = rand();
		hashmap_value_t retrieved;

		random_string( str, sizeof(str) );

		res = hashmap_insert( &hashmap, str, val );
		assert( 0 == res );

		res = hashmap_find( &hashmap, str, &retrieved );
		assert( 1 == res );
		assert( retrieved == val );
	}

	hashmap_term( &hashmap );
}
Ejemplo n.º 11
0
Archivo: renderman.c Proyecto: fscz/gre
static int tear_down_shaders (SceneData* sceneData, SceneObject* so, SoData* soData) {

  char shaderKey[KEYSIZE];
  snprintf ( shaderKey, KEYSIZE, "%p", &so->vShader );

  GLuint handle = (GLuint)hashmap_find ( shaderKey, sceneData->mapVShader2Handle );
  size_t shaderUsers = (size_t)hashmap_find ( shaderKey, sceneData->countVShaderUsers );
  shaderUsers--;

  if ( 0 == shaderUsers ) {

    glDeleteShader ( handle );

    list_remove_elem ( (void*)handle, sceneData->listVShaders );
    hashmap_delete ( shaderKey, sceneData->mapVShader2Handle );
    hashmap_delete ( shaderKey, sceneData->countVShaderUsers );

  } else {

    hashmap_insert ( shaderKey, (void*)shaderUsers, sceneData->countVShaderUsers );
  }


  snprintf ( shaderKey, KEYSIZE, "%p", &so->fShader );
  handle = (GLuint)hashmap_find ( shaderKey, sceneData->mapFShader2Handle );
  shaderUsers = (GLuint)hashmap_find ( shaderKey, sceneData->countFShaderUsers );
  shaderUsers--;

  if ( 0 == shaderUsers ) {

    glDeleteShader ( handle );

    list_remove_elem ( (void*)handle, sceneData->listFShaders );
    hashmap_delete ( shaderKey, sceneData->mapFShader2Handle );
    hashmap_delete ( shaderKey, sceneData->countFShaderUsers );

  } else {

    hashmap_insert ( shaderKey, (void*)shaderUsers, sceneData->countFShaderUsers );
  }

  return 0;
}
Ejemplo n.º 12
0
var_p_t hashmap_get(var_p_t map, const char *key) {
  var_p_t result;
  Node *node = hashmap_find(map, key);
  if (node != NULL) {
    result = node->value;
  } else {
    result = NULL;
  }
  return result;
}
Ejemplo n.º 13
0
const ini_section_t *ini_get_section(const char *section_name, 
        const ini_context_t *context)
{
    if (section_name == NULL || *section_name == '\0') {
        return &context->global;
    }
    else {
        return (ini_section_t *)hashmap_find(context->sections, 
                (void *)section_name);
    }
}
Ejemplo n.º 14
0
Archivo: renderman.c Proyecto: fscz/gre
static int setup_shaders(SceneData* sceneData, SceneObject* so, SoData* soData, GLuint* vShaderHandle, GLuint* fShaderHandle) {

  // lookup vshader
  char shaderKey[KEYSIZE];
  snprintf (shaderKey, KEYSIZE, "%p", &so->vShader);

  size_t shaderUsers;

  GLuint vHandle = (GLuint)hashmap_find (shaderKey, sceneData->mapVShader2Handle);
  if ( !vHandle ) {
    if ( -1 == compile_shader ( GL_VERTEX_SHADER, so->vShader, &vHandle ) ) return -1;

    hashmap_insert(shaderKey, (void*)vHandle, sceneData->mapVShader2Handle);
    list_add((void*)vHandle, sceneData->listVShaders);
    hashmap_insert (shaderKey, (void*)1, sceneData->countVShaderUsers);
    
  } else {
    shaderUsers = (size_t)hashmap_find(shaderKey, sceneData->countVShaderUsers);
    hashmap_insert (shaderKey, (void*)(shaderUsers+1), sceneData->countVShaderUsers);    
  }
  *vShaderHandle = vHandle;

  // lookup fshader
  snprintf (shaderKey, KEYSIZE, "%p", &so->fShader);
  GLuint fHandle = (GLuint)hashmap_find (shaderKey, sceneData->mapFShader2Handle);
  if ( !fHandle ) {
    if ( -1 == compile_shader ( GL_FRAGMENT_SHADER, so->fShader, &fHandle ) ) return -1;
    
    hashmap_insert(shaderKey, (void*)fHandle, sceneData->mapFShader2Handle);
    list_add((void*)fHandle, sceneData->listFShaders);
    hashmap_insert (shaderKey, (void*)1, sceneData->countFShaderUsers);

  } else {
    shaderUsers = (size_t)hashmap_find(shaderKey, sceneData->countFShaderUsers);
    hashmap_insert (shaderKey, (void*)(shaderUsers+1), sceneData->countFShaderUsers);
  }
  *fShaderHandle = fHandle;

  return 0;
}
Ejemplo n.º 15
0
static void arbitrary( void )
{
	int res, val;
	hashmap_t *hashmap;

	hashmap = hashmap_create();
	assert( hashmap );

	res = hashmap_insert( hashmap, "test", 100 );
	assert( 0 == res );

	res = hashmap_insert( hashmap, "test", 101 );
	assert( 0 == res );

	res = hashmap_insert( hashmap, "test2", 3 );
	assert( 0 == res );

	res = hashmap_find( hashmap, "test2", NULL );
	assert( 1 == res );

	res = hashmap_find( hashmap, "test2", &val );
	assert( 1 == res );
	assert( 3 == val );

	res = hashmap_find( hashmap, "test", &val );
	assert( 1 == res );
	assert( 101 == val );

	hashmap_erase( hashmap, "test" );
	res = hashmap_find( hashmap, "test", NULL );
	assert( 0 == res );
	hashmap_erase( hashmap, "test" );
	res = hashmap_find( hashmap, "test", NULL );
	assert( 0 == res );

	hashmap_destroy( hashmap );
}
Ejemplo n.º 16
0
Archivo: renderman.c Proyecto: fscz/gre
static int setup_index_buffer (SceneData* sceneData, SceneObject* so, SoData* soData) {
  Draw* method = so->draw;

  char bufferKey[KEYSIZE];
  Attribute* indices = method->indices;
  GLuint bufferHandle;
  size_t bufferUsers;

  if ( indices && 1 == indices->useBuffer ) {

    snprintf (bufferKey, KEYSIZE, "%p", indices);
    bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle );

    if ( !bufferHandle ) {
      glGenBuffers(1, &bufferHandle);

      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, bufferHandle);

      glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices->size, indices->data, GL_STATIC_DRAW);

      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);

      // update sceneData
      list_add ( (void*)bufferHandle, sceneData->listBuffers );	
      hashmap_insert ( bufferKey, (void*)bufferHandle, sceneData->mapBuffer2Handle );
      hashmap_insert ( bufferKey, (void*)1, sceneData->countBufferUsers );

    } else { 

      // update sceneData	
      bufferUsers = (size_t)hashmap_find (bufferKey, sceneData->countBufferUsers);
      hashmap_insert (bufferKey, (void*)(bufferUsers+1), sceneData->countBufferUsers);	
    }
  }

  return 0;
}
Ejemplo n.º 17
0
Archivo: renderman.c Proyecto: fscz/gre
static int tear_down_attributes (SceneData* sceneData, SceneObject* so, SoData* soData) {
  Attribute* attribute;
  char bufferKey[KEYSIZE];
  size_t bufferUsers;
  GLuint bufferHandle;

  int i;
  for (i = 0; i < list_size(soData->attributes); i++) {

    attribute = list_get ( i, soData->attributes );

    if ( 1 != attribute->useBuffer ) continue;

    snprintf (bufferKey, KEYSIZE, "%p", attribute);
    bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle );
    
    bufferUsers = (size_t)hashmap_find ( bufferKey, sceneData->countBufferUsers );
    bufferUsers--;
    if ( 0 == bufferUsers) { // free resources

      glDeleteBuffers (1, &bufferHandle);

      list_remove_elem ( (void*)bufferHandle, sceneData->listBuffers );

      hashmap_delete ( bufferKey, sceneData->mapBuffer2Handle );

      hashmap_delete ( bufferKey, sceneData->countBufferUsers );

    } else {

      hashmap_insert ( bufferKey, (void*)bufferUsers, sceneData->countBufferUsers );
    }
  }

  return 0;
}
Ejemplo n.º 18
0
void gram_ADDTOSYMBOL()
{
    register char *name;
    char *text;

    parser_push_fun("ADDTOSYMBOL");

    name = parser_name_parlist(&parser,  true);
    text = parser_parlist(&parser, COLLECT_SET);

    symbol_add(hashmap_find(&symtab, name, SYMBOL), text);
    if (message_show(MSG_NOTICE))
        message("ADDTOSYMBOL tos(%s) += \"%s\"",  name, string_short(text));
    parser_pop_fun();
    free(text);
    free(name);
}
Ejemplo n.º 19
0
/*
 * Look up the value for a variable.
 */
static char *lookup_variable (struct conn_s *connptr, const char *varname)
{
        hashmap_iter result_iter;
        char *key;
        static char *data;

        result_iter = hashmap_find (connptr->error_variables, varname);

        if (hashmap_is_end (connptr->error_variables, result_iter))
                return (NULL);

        if (hashmap_return_entry (connptr->error_variables, result_iter,
                                  &key, (void **) &data) < 0)
                return (NULL);

        return (data);
}
Ejemplo n.º 20
0
void gram_USECOUNTER()
{
    register char *name;
    register HashItem *item;

    parser_push_fun("USECOUNTER");

    name = parser_name_parlist(&parser,  false);
    item = hashmap_find(&symtab, name, COUNTER);

    counter_add(item, 1);
    if (message_show(MSG_NOTICE))
        message("USECOUNTER: tos(%s)++", name);
    (*parser.d_insert)(&parser, counter_text(item));

    parser_pop_fun();
    free(name);
}
Ejemplo n.º 21
0
/**
 * hashmap_get:
 * returns the value associated to a key within the hashmap, if any
 */
int hashmap_get(hashmap_ptr map, const any_key_t* key, any_value_t** value) {
    if (map) {
        int index;
        hashmap_map* _map = (hashmap_map*) map;

        /* find a suitable bucket, if any */
        index = hashmap_find(_map, key);
        if (index < 0) {
            /* key not found, return error code */
            return index;
        }

        /* save value into output parameter */
        *value = _map->_buckets[index]._value;

        return HASHMAP_OK;
    }
    return HASHMAP_ERR;
}
Ejemplo n.º 22
0
/**
 * hashmap_remove:
 * removes key/value pair from hashmap, if found;
 * stores the value in the output variable on return
 */
int hashmap_remove(hashmap_ptr map, const any_key_t* key, any_value_t** value) {
    if (map) {
        int index;
        hashmap_map* _map = (hashmap_map*) map;

        /* find a suitable bucket, if any */
        index = hashmap_find(_map, key);
        if (index < 0) {
            /* key not found, return error code */
            return index;
        }

        /* set bucket as not used */
        _map->_buckets[index]._in_use = 0;
        _map->_cur_size--;
        _map->_changed = 1;
        if (value)
            *value = _map->_buckets[index]._value;

        return HASHMAP_OK;
    }
    return HASHMAP_ERR;
}
Ejemplo n.º 23
0
void handle_solvelink(long offset, HashItem *item)
{
    LabelInfo *lp;
    char const *label = hashitem_key(item);
                                    /* look up the label    */
    HashItem *info = hashmap_find(&symtab, label, ANY);

    file_copy2offset(global.d_out, postqueue_istream(), offset);

    if (info == PFAILED)
    {
        message_show(MSG_WARNING);
        message("Unresolved label: %s", label);
        fputs("??", global.d_out);
        return;
    }

    lp = (LabelInfo *)hashitem_value(info);

    if (lp->d_filenr)
    {
        fprintf(global.d_out, "%s%02u.%s#%s",
                    global.d_noext,
                    (unsigned)lp->d_filenr,
                    global.d_ext,
                    label);
    }
    else
    {
        fprintf(global.d_out, "%s.%s#%s",
                    global.d_noext,
                    global.d_ext,
                    label);
    }

}
Ejemplo n.º 24
0
Archivo: renderman.c Proyecto: fscz/gre
static int setup_textures (SceneData* sceneData, SceneObject* so, SoData* soData, GLuint program) {
  
  GLint num;

  GLint size_i;
  GLenum type_i;
  char name_i[MAX_NAME_LENGTH];
  GLint location_i;
  Uniform* uniform;
  GLuint textureHandle;
  size_t textureUsers;
  char uniKey[KEYSIZE];

  uint i;
  glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &num);

  for (i = 0; i < num; i++) {
    glGetActiveUniform(program, i, MAX_NAME_LENGTH, NULL, &size_i, &type_i, name_i);
    location_i = glGetUniformLocation(program, name_i);

    if ( -1 == location_i) {
      log_message ("error: could not find name: [%s] in shader program.");
      return -1;
    }
    
    uniform = hashmap_find(name_i, so->uniforms);
    if ( !uniform ) {
      log_message ("error: could not find [%s] in scene object's uniform map", name_i);
      return -1;
    }

    if ( GL_SAMPLER_CUBE == type_i ) {
      log_message ( "error: cube maps are not supported" );
      return -1;
    }

    if ( GL_SAMPLER_2D != type_i) {
      continue;
    }

    // type_i == GL_SAMPLER_2D

    snprintf ( uniKey, KEYSIZE, "%p", uniform );
    textureHandle = (GLuint)hashmap_find ( uniKey, sceneData->mapTexture2Handle );


    if ( textureHandle ) { // texture already exists

      textureUsers = (size_t)hashmap_find ( uniKey, sceneData->countTextureUsers );
      hashmap_insert ( uniKey, (void*)(textureUsers+1), sceneData->countTextureUsers );

    } else { // texture does not exist

      if ( 0 != uniform->textureHandle ) { // just map to handle

	if ( TEXTURE_HANDLE_GBUFFER == uniform->textureHandle ) {
	  textureHandle = sceneData->tbos[0];
	}
	else if ( TEXTURE_HANDLE_LIGHTBUFFER == uniform->textureHandle ) {
	  textureHandle = sceneData->tbos[1];
	}
	else {
	  textureHandle = uniform->textureHandle;	
	}

      } else { // load texture and map
      
	glGenTextures ( 1, &textureHandle );

	glBindTexture ( GL_TEXTURE_2D, textureHandle );

	glTexImage2D( GL_TEXTURE_2D, 0, uniform->textureFormat, uniform->width, uniform->height, 0, uniform->textureFormat, uniform->textureType, uniform->data);       

	glGenerateMipmap ( GL_TEXTURE_2D );

	glBindTexture ( GL_TEXTURE_2D, 0 );

	hashmap_insert ( uniKey, (void*)textureHandle, sceneData->mapTexture2Handle );
      }

      hashmap_insert ( uniKey, (void*)textureHandle, sceneData->mapTexture2Handle );
      hashmap_insert ( uniKey, (void*)1, sceneData->countTextureUsers );
      list_add ( (void*)textureHandle, sceneData->listTextures );
    }
    
    if ( TEXTURE_HANDLE_GBUFFER != uniform->textureHandle && TEXTURE_HANDLE_LIGHTBUFFER != uniform->textureHandle ) {
      list_add ( uniform, soData->textures );
    }
  }


  return 0;
}
Ejemplo n.º 25
0
Archivo: renderman.c Proyecto: fscz/gre
static int setup_attributes (SceneData* sceneData, SceneObject* so, SoData* soData, GLuint program) {

  GLint num;
  char name_i[MAX_NAME_LENGTH];
  GLenum type_i;
  GLint size_i;

  Attribute* attribute;
  char bufferKey[KEYSIZE]; 
  size_t bufferUsers;
  GLuint bufferHandle;
 
  size_t numAttrComponents;


  glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &num);
      
  int i;
  for (i = 0; i < num; i++) {
    glGetActiveAttrib(program, i, MAX_NAME_LENGTH, NULL, &size_i, &type_i, name_i);

    numAttrComponents = get_num_attrib_components(type_i);

    if ( -1 == numAttrComponents ) {
      log_message ("error: matrix 3x3 and matrix 4x4 attributes are not supported");
      return -1;
    }

    attribute = hashmap_find(name_i, so->attributes);

    if ( !attribute ) {
      log_message ("error: shader attribute [%s] not found in so attribute map.", name_i);
      return -1;
    }

    list_add( attribute, soData->attributes );

    if ( 1 == attribute->useBuffer ) { // BUFFERED ATTRIBUTES    
            
      snprintf (bufferKey, KEYSIZE, "%p", attribute);
      bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle );      

      if ( !bufferHandle ) { // create new vbo
	glGenBuffers(1, &bufferHandle);	

	glBindBuffer (GL_ARRAY_BUFFER, bufferHandle);

	glBufferData (GL_ARRAY_BUFFER, attribute->size, attribute->data, GL_STATIC_DRAW);

	glBindBuffer (GL_ARRAY_BUFFER, 0);

	// update sceneData
	list_add ( (void*)bufferHandle, sceneData->listBuffers );	
	hashmap_insert ( bufferKey, (void*)bufferHandle, sceneData->mapBuffer2Handle );
	hashmap_insert ( bufferKey, (void*)1, sceneData->countBufferUsers );

      } else { 

	// update sceneData	
	bufferUsers = (size_t)hashmap_find (bufferKey, sceneData->countBufferUsers);
	hashmap_insert (bufferKey, (void*)(bufferUsers+1), sceneData->countBufferUsers);	
      }


    } else { // UNBUFFERD ATTRIBUTES

      // do nothing

    }

  }

  //if ( -1 == link_program ( program, so->vShader, so->fShader ) ) return -1; // binding requires relinking

  return 0;
}
Ejemplo n.º 26
0
Archivo: renderman.c Proyecto: fscz/gre
static SceneData* find_scene_data (Scene* scene) {
  char key[KEYSIZE];
  snprintf (key, KEYSIZE, "%p", scene);

  return hashmap_find (key, mapSceneData);
}
Ejemplo n.º 27
0
Archivo: renderman.c Proyecto: fscz/gre
static SoData* find_so_data (SceneData* sceneData, SceneObject* so) {
  char key[KEYSIZE];
  snprintf (key, KEYSIZE, "%p", so);

  return hashmap_find (key, sceneData->mapSo2SoData);
}
Ejemplo n.º 28
0
Archivo: renderman.c Proyecto: fscz/gre
static void free_scene_data (Scene* scene) {

  char sdkey[KEYSIZE];
  snprintf (sdkey, KEYSIZE, "%p", scene);
  
  SceneData* sceneData = hashmap_find (sdkey, mapSceneData);

  if (sceneData) {    
      
    GLuint handle;
    
    int k;
    for (k = 0; k < list_size(sceneData->listBuffers); k++) {

      handle = (GLuint)list_get(k, sceneData->listBuffers);
      glDeleteBuffers (1, &handle);
    }

    for ( k = 0; k < list_size(sceneData->listPrograms); k++) {
      
      handle = (GLuint)list_get(k, sceneData->listPrograms);
      glDeleteProgram ( handle );
    }

    for ( k = 0; k < list_size(sceneData->listVShaders); k++) {
      
      handle = (GLuint)list_get(k, sceneData->listVShaders);
      glDeleteShader ( handle );
    }

    for ( k = 0; k < list_size(sceneData->listFShaders); k++) {
      
      handle = (GLuint)list_get(k, sceneData->listFShaders);
      glDeleteShader ( handle );
    }

    for ( k = 0; k < list_size(sceneData->listTextures); k++) {
      
      handle = (GLuint)list_get(k, sceneData->listTextures);
      glDeleteTextures ( 1, &handle );
    }

    list_free (sceneData->listPrograms);
    list_free (sceneData->listVShaders);
    list_free (sceneData->listFShaders);
    list_free (sceneData->listBuffers);
    list_free (sceneData->listTextures);

    hashmap_free (sceneData->mapVShader2Handle);
    hashmap_free (sceneData->mapFShader2Handle);
    hashmap_free (sceneData->mapShaderShader2Handle);
    hashmap_free (sceneData->mapBuffer2Handle);
    hashmap_free (sceneData->mapTexture2Handle);

    hashmap_free (sceneData->countBufferUsers);
    hashmap_free (sceneData->countVShaderUsers);
    hashmap_free (sceneData->countFShaderUsers);
    hashmap_free (sceneData->countProgramUsers);
    hashmap_free (sceneData->countTextureUsers);

    hashmap_free (sceneData->mapSo2SoData);

    list_free (sceneData->stageGbuffer);
    list_free (sceneData->stageLight);
    list_free (sceneData->stageGeometry);
    list_free (sceneData->stageParticle);
    list_free (sceneData->stageOverlay);
    
    hashmap_delete (sdkey, mapSceneData);
  }
}
Ejemplo n.º 29
0
Archivo: renderman.c Proyecto: fscz/gre
static int find_and_pass_uniforms(SceneData* sceneData, SceneObject* so, GLuint program) {

  GLint num;

  GLint size_i;
  GLenum type_i;
  char name_i[MAX_NAME_LENGTH];
  GLint location_i;
  Uniform* uniform = NULL;
  void* data = NULL;
  char texKey[KEYSIZE];
  GLuint textureHandle;

  size_t textureIndex = 0;

  uint i;

  glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &num);

  for (i = 0; i < num; i++) {
    glGetActiveUniform(program, i, MAX_NAME_LENGTH, NULL, &size_i, &type_i, name_i);
    location_i = glGetUniformLocation(program, name_i);

    if ( -1 == location_i) {
      log_message ("error: could not find name: [%s] in shader program.", name_i);
      return -1;
    }

    uniform = hashmap_find(name_i, so->uniforms);
    if ( !uniform ) {
      log_message ("error: could not find [%s] in scene object's uniform map", name_i);
      return -1;
    }

    data = uniform->data;


    switch (type_i) {
    case GL_BOOL:
      if (  1 == size_i ) glUniform1i(location_i, ((GLint*)data)[0]);
      else glUniform1iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_BOOL_VEC2:
      if ( 1 == size_i ) glUniform2i(location_i, ((GLint*)data)[0], ((GLint*)data)[1]);
      else glUniform2iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_BOOL_VEC3:
      if ( 1 == size_i ) glUniform3i(location_i, ((GLint*)data)[0], ((GLint*)data)[1], ((GLint*)data)[2]);
      else glUniform3iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_BOOL_VEC4:
      if ( 1 == size_i ) glUniform4i(location_i, ((GLint*)data)[0], ((GLint*)data)[1], ((GLint*)data)[2], ((GLint*)data)[3]);
      else glUniform4iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_INT:
      if ( 1 == size_i ) glUniform1i(location_i, (GLint)data);
      else glUniform1iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_INT_VEC2:
      if ( 1 == size_i ) glUniform2i(location_i, ((GLint*)data)[0], ((GLint*)data)[1]);
      else glUniform2iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_INT_VEC3:
      if ( 1 == size_i ) glUniform3i(location_i, ((GLint*)data)[0], ((GLint*)data)[1], ((GLint*)data)[2]);
      else glUniform3iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_INT_VEC4:
      if ( 1 == size_i ) glUniform4i(location_i, ((GLint*)data)[0], ((GLint*)data)[1], ((GLint*)data)[2], ((GLint*)data)[3]);
      else glUniform4iv(location_i, size_i, ((const GLint*)data));
      break;

    case GL_FLOAT:
      if ( 1 == size_i ) glUniform1f(location_i, ((GLfloat*)data)[0]);
      else glUniform1fv(location_i, size_i, ((const GLfloat*)data));
      break;

    case GL_FLOAT_VEC2:
      if ( 1 == size_i ) glUniform2f(location_i, ((GLfloat*)data)[0], ((GLfloat*)data)[1]);
      else glUniform2fv(location_i, size_i, ((const GLfloat*)data));
      break;

    case GL_FLOAT_VEC3:
      if ( 1 == size_i ) glUniform3f(location_i, ((GLfloat*)data)[0], ((GLfloat*)data)[1], ((GLfloat*)data)[2]);
      else glUniform3fv(location_i, size_i, ((const GLfloat*)data));
      break;

    case GL_FLOAT_VEC4:
      if ( 1 == size_i ) glUniform4f(location_i, ((GLfloat*)data)[0], ((GLfloat*)data)[1], ((GLfloat*)data)[2], ((GLfloat*)data)[3]);      
      else glUniform4fv(location_i, size_i, ((const GLfloat*)data));
      break;

    case GL_FLOAT_MAT2:
      glUniformMatrix2fv(location_i, size_i, GL_FALSE, (const GLfloat*)data);
      break;

    case GL_FLOAT_MAT3:
      glUniformMatrix3fv(location_i, size_i, GL_FALSE, (const GLfloat*)data);
      break;
      
    case GL_FLOAT_MAT4:
      glUniformMatrix4fv(location_i, size_i, GL_FALSE, (const GLfloat*)data);
      break;

    case GL_SAMPLER_2D:

      snprintf( texKey, KEYSIZE, "%p", uniform );      
      textureHandle = (GLuint)hashmap_find ( texKey, sceneData->mapTexture2Handle );

      if ( !textureHandle ) {
	log_message ( "error: could not bind texture. handle not found");
	return -1;
      }
      
      if (textureIndex > 7) {
	log_message ( "error: cannot use more than 8 texture units per scene object" );
	return -1;
      }

      
      glActiveTexture(get_texture_unit(textureIndex));

      glBindTexture (GL_TEXTURE_2D, textureHandle);

      glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, uniform->sampleMode );
      glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, uniform->sampleMode );

      
      glUniform1i ( location_i, textureIndex );
      
      textureIndex++;

      break;

    case GL_SAMPLER_CUBE:
      log_message ( "error: cube maps are not supported" );
      break;
    }
  }

  return 0;
}
Ejemplo n.º 30
0
Archivo: renderman.c Proyecto: fscz/gre
int renderman_rendertask_repeat(RepeatInfo* info, RenderContext* ctx) {

  SceneData* sceneData = find_scene_data (ctx->scene);

  if ( !sceneData ) {
    log_message("error: scene data is missing");
    return -1;
  }

  SceneObject* so;
  SoData* soData;

  char progKey[KEYSIZE];

  Attribute* attribute;
  size_t numAttributes;

  GLuint program;

  char bufferKey[KEYSIZE];
  GLuint bufferHandle;
  char iboKey[KEYSIZE];
  GLuint iboHandle;

  Draw* method;
  
  int i;
  int k;
  
  //////////////////////////////////////////////////
  //////////////////////// STAGE Gbuffer
  //////////////////////////////////////////////////
  glBindFramebuffer(GL_FRAMEBUFFER, sceneData->fbos[0]);  
  glEnable(GL_DEPTH_TEST);  
  glEnable(GL_CULL_FACE);
  glDisable(GL_BLEND);
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  for (i = list_size(sceneData->stageGbuffer) - 1; i >= 0; i--) {   

    so = list_get(i, sceneData->stageGbuffer);    

    soData = find_so_data ( sceneData, so );

    if ( !soData ) {
      log_message ( "error: scene object has not been setup correctly - removing." ); 
      tear_down_so ( sceneData, so );
      list_remove_elem( so, sceneData->stageGbuffer );
      continue;
    }

    program = (GLuint)hashmap_find( "gbuffer", staticPrograms );

    glUseProgram ( program );

    if ( -1 == find_and_pass_uniforms (sceneData, so, (GLuint)program)) {
      tear_down_so ( sceneData, so );
      list_remove_elem( so, sceneData->stageGbuffer );
      continue;
    }

    for (k = 0; k < 2; k++) { // position and normal must be first 2 shader attributes

      attribute = list_get ( k, soData->attributes );

      if ( 1 == attribute->useBuffer ) {
	snprintf ( bufferKey, KEYSIZE, "%p", attribute );
	bufferHandle = (GLuint)hashmap_find ( bufferKey, sceneData->mapBuffer2Handle );

	glBindBuffer (GL_ARRAY_BUFFER, bufferHandle);
	  
	glVertexAttribPointer (k, attribute->numComponents, attribute->dataType, GL_FALSE, 0, 0);	
	glEnableVertexAttribArray(k);

      } else {

	glVertexAttribPointer (k, attribute->numComponents, attribute->dataType, GL_FALSE, 0, attribute->data);
	glEnableVertexAttribArray(k);

      }
    }

    method = so->draw;
    if ( NULL == method->indices ) { 	
      glDrawArrays ( method->type, method->start, method->count );
    } else {

      snprintf( iboKey, KEYSIZE, "%p", method->indices );
      iboHandle = (GLuint)hashmap_find ( iboKey, sceneData->mapBuffer2Handle );
	
      if ( 1 == method->indices->useBuffer ) {
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iboHandle );
      }

      glDrawElements ( method->type, method->count, method->indices->dataType, 1 == method->indices->useBuffer ? 0 : method->indices->data );
    }

    
    for (k = 0; k < 2; k++) {
      glDisableVertexAttribArray(k);
    }

    glBindBuffer (GL_ARRAY_BUFFER, 0);
    glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindTexture (GL_TEXTURE_2D, 0);
  }
  
  //////////////////////////////////////////////////
  //////////////////////// STAGE Light
  //////////////////////////////////////////////////
  glBindFramebuffer(GL_FRAMEBUFFER, sceneData->fbos[1]);  
  glDisable(GL_CULL_FACE);
  glDisable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);
  glClear(GL_COLOR_BUFFER_BIT);

  render_stage(sceneData->stageLight, sceneData, so, soData, progKey, attribute, numAttributes, program, bufferKey, bufferHandle, iboKey, iboHandle, method, i, k);  

  //////////////////////////////////////////////////
  //////////////////////// STAGE Geometry
  //////////////////////////////////////////////////  
  glBindFramebuffer(GL_FRAMEBUFFER, 0);  
  glEnable(GL_DEPTH_TEST);  
  glEnable(GL_CULL_FACE);
  glDisable(GL_BLEND);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  render_stage(sceneData->stageGeometry, sceneData, so, soData, progKey, attribute, numAttributes, program, bufferKey, bufferHandle, iboKey, iboHandle, method, i, k);

  //////////////////////////////////////////////////
  //////////////////////// STAGE Particle
  //////////////////////////////////////////////////

  //////////////////////////////////////////////////
  //////////////////////// STAGE Overlay
  //////////////////////////////////////////////////
  glDisable(GL_DEPTH_TEST);  
  glDisable(GL_CULL_FACE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  render_stage(sceneData->stageOverlay, sceneData, so, soData, progKey, attribute, numAttributes, program, bufferKey, bufferHandle, iboKey, iboHandle, method, i, k);  

  //////////////////////////////////////////////////
  //////////////////////// STAGE Postprocessing
  //////////////////////////////////////////////////


  //////////////////////////////////////////////////
  //////////////////////// STAGE Update
  //////////////////////////////////////////////////

  eglSwapBuffers(ctx->esContext->display, ctx->esContext->surface);

  if ( ctx->updateFunc ) {
    ctx->updateFunc(ctx->userData, info->overruns);
  }
 
  return 0;
}