Quake II RTX doxygen  1.0 dev
material.h File Reference
#include "shared/shared.h"
#include "refresh/refresh.h"

Go to the source code of this file.

Classes

struct  pbr_material_s
 

Macros

#define MAX_PBR_MATERIALS   4096
 

Typedefs

typedef struct image_s image_t
 
typedef struct pbr_material_s pbr_material_t
 

Functions

int MAT_GetPBRMaterialIndex (pbr_material_t const *mat)
 
qerror_t MAT_RegisterPBRMaterial (pbr_material_t *mat, image_t *image_diffuse, image_t *image_normals, image_t *image_emissive)
 
void MAT_UpdateRegistration (pbr_material_t *mat)
 
const pbr_material_tMAT_UpdatePBRMaterialSkin (image_t *image_diffuse)
 
qerror_t MAT_SetPBRMaterialAttribute (pbr_material_t *mat, char const *token, char const *value)
 
qerror_t MAT_InitializePBRmaterials ()
 
qerror_t MAT_ResetUnused ()
 
const pbr_material_tMAT_GetPBRMaterialsTable ()
 
int MAT_GetNumPBRMaterials ()
 
pbr_material_tMAT_GetPBRMaterial (int index)
 
pbr_material_tMAT_FindPBRMaterial (char const *name)
 
qerror_t MAT_ReloadPBRMaterials ()
 
qerror_t MAT_SavePBRMaterials ()
 
void MAT_PrintMaterialProperties (pbr_material_t const *mat)
 
uint32_t MAT_SetKind (uint32_t material, uint32_t kind)
 
qboolean MAT_IsKind (uint32_t material, uint32_t kind)
 

Macro Definition Documentation

◆ MAX_PBR_MATERIALS

#define MAX_PBR_MATERIALS   4096

Definition at line 25 of file material.h.

Typedef Documentation

◆ image_t

typedef struct image_s image_t

Definition at line 27 of file material.h.

◆ pbr_material_t

Function Documentation

◆ MAT_FindPBRMaterial()

pbr_material_t* MAT_FindPBRMaterial ( char const *  name)

Definition at line 465 of file material.c.

466 {
467  char name_copy[MAX_QPATH];
468  int len = truncateExtension(name, name_copy);
469  assert(len>0);
470 
472 
473  // note : key comparison must be case insensitive
474 
475  int search_start = 0;
476 
477  if (table->alpha_sorted)
478  {
479  // binary search if names are sorted in alpha order
480  int left = 0, right = table->num_materials - 1;
481 
482  while (left <= right)
483  {
484  int middle = floor((left + right) / 2);
485  pbr_material_t * mat = &table->materials[middle];
486  int cmp = Q_strcasecmp(name_copy, mat->name);
487  if (cmp < 0)
488  right = middle - 1;
489  else if (cmp > 0)
490  left = middle + 1;
491  else
492  return mat;
493  }
494 
495  search_start = pbr_materials_table.num_materials;
496  }
497 
498  // brute-force search if the table is not sorted or if the material is not found in the main table
499  for (int i = search_start; i < table->num_materials + table->num_custom_materials; ++i)
500  {
501  pbr_material_t * mat = &table->materials[i];
502  if (Q_strcasecmp(mat->name, name_copy) == 0)
503  return mat;
504  }
505 
506  // not found - create a material entry
507  int index = table->num_materials + table->num_custom_materials;
508  table->num_custom_materials++;
509  pbr_material_t * mat = &table->materials[index];
510  MAT_Reset(mat, index);
511  strcpy(mat->name, name_copy);
512  Com_Printf("Created a material entry %d for unknown material %s\n", index, name_copy);
513 
514  return mat;
515 }

Referenced by bsp_mesh_register_textures(), MAT_UpdatePBRMaterialSkin(), MOD_LoadMD2_RTX(), vkpt_print_material(), and vkpt_set_material().

◆ MAT_GetNumPBRMaterials()

int MAT_GetNumPBRMaterials ( )

Definition at line 144 of file material.c.

Referenced by vkpt_light_buffer_upload_to_staging().

◆ MAT_GetPBRMaterial()

pbr_material_t* MAT_GetPBRMaterial ( int  index)

Definition at line 457 of file material.c.

458 {
460  return &pbr_materials_table.materials[index];
461  return NULL;
462 }

Referenced by compute_world_tangents(), and process_render_feedback().

◆ MAT_GetPBRMaterialIndex()

int MAT_GetPBRMaterialIndex ( pbr_material_t const *  mat)

Definition at line 149 of file material.c.

150 {
151  return mat->flags & MATERIAL_INDEX_MASK;
152 }

◆ MAT_GetPBRMaterialsTable()

const pbr_material_t* MAT_GetPBRMaterialsTable ( )

Definition at line 139 of file material.c.

140 {
141  return &pbr_materials_table.materials[0];
142 }

Referenced by vkpt_light_buffer_upload_to_staging().

◆ MAT_InitializePBRmaterials()

qerror_t MAT_InitializePBRmaterials ( )

Definition at line 341 of file material.c.

Referenced by R_Init_RTX().

◆ MAT_IsKind()

qboolean MAT_IsKind ( uint32_t  material,
uint32_t  kind 
)

◆ MAT_PrintMaterialProperties()

void MAT_PrintMaterialProperties ( pbr_material_t const *  mat)

Definition at line 549 of file material.c.

550 {
551  Com_Printf("{ %s\n", mat->name);
552  Com_Printf(" image_diffuse = '%s'\n", mat->image_diffuse ? mat->image_diffuse->name : "");
553  Com_Printf(" image_normals = '%s'\n", mat->image_normals ? mat->image_normals->name : "");
554  Com_Printf(" image_emissive = '%s'\n", mat->image_emissive ? mat->image_emissive->name : "");
555  Com_Printf(" bump_scale = %f,\n", mat->bump_scale);
556  Com_Printf(" rough_override = %f,\n", mat->rough_override);
557  Com_Printf(" specular_scale = %f,\n", mat->specular_scale);
558  Com_Printf(" emissive_scale = %f,\n", mat->emissive_scale);
559  char const * kind = getMaterialKindName(mat->flags);
560  Com_Printf(" kind = '%s',\n", kind ? kind : "");
561  Com_Printf(" light = %d,\n", (mat->flags & MATERIAL_FLAG_LIGHT) ? (mat->enable_light_styles ? 1 : 2) : 0);
562  Com_Printf(" correct_albedo = %d\n", (mat->flags & MATERIAL_FLAG_CORRECT_ALBEDO) != 0);
563  Com_Printf("}\n");
564 }

Referenced by vkpt_print_material().

◆ MAT_RegisterPBRMaterial()

qerror_t MAT_RegisterPBRMaterial ( pbr_material_t mat,
image_t image_diffuse,
image_t image_normals,
image_t image_emissive 
)

Definition at line 397 of file material.c.

398 {
399  if (!mat)
400  {
401  //Com_Error(ERR_FATAL, "%s: bad pbr material", __func__);
402  return Q_ERR_FAILURE;
403  }
404 
405  // already registered ?
407  return Q_ERR_SUCCESS;
408 
410 
411  mat->image_diffuse = image_diffuse;
412  mat->image_normals = image_normals;
413  mat->image_emissive = image_emissive;
414 
415  //if (mat->image_diffuse == R_NOTEXTURE)
416  // mat->flags &= ~(MATERIAL_FLAG_VALID);
417 
418  return Q_ERR_SUCCESS;
419 }

Referenced by bsp_mesh_register_textures(), and MOD_LoadMD2_RTX().

◆ MAT_ReloadPBRMaterials()

qerror_t MAT_ReloadPBRMaterials ( )

Definition at line 347 of file material.c.

348 {
349  pbr_materials_table_t newtable;
350 
352  * new_table = &newtable;
353 
354  if (parseMaterialsTable(materials_filename, new_table) == Q_ERR_SUCCESS)
355  {
356  if (new_table->num_materials != old_table->num_materials)
357  {
358  Com_EPrintf("Cannot reload materials table : table has different material count, please restart the game\n");
359  return Q_ERR_FAILURE;
360  }
361 
362  if (new_table->alpha_sorted != old_table->alpha_sorted)
363  {
364  Com_EPrintf("Cannot reload materials table : table has different material keys, please check your edits\n");
365  return Q_ERR_FAILURE;
366  }
367 
368  for (int i = 0; i < old_table->num_materials; ++i)
369  {
370  pbr_material_t * old_mat = &old_table->materials[i],
371  * new_mat = &new_table->materials[i];
372 
373  if (strncmp(old_mat->name, new_mat->name, MAX_QPATH) != 0)
374  {
375  Com_EPrintf("Cannot update material '%s' : incorrect name, please check your edits\n", new_mat->name);
376  continue;
377  }
378 
379  // update CSV values
380  old_mat->bump_scale = new_mat->bump_scale;
381  old_mat->rough_override = new_mat->rough_override;
382  old_mat->specular_scale = new_mat->specular_scale;
383  old_mat->emissive_scale = new_mat->emissive_scale;
384  old_mat->flags = new_mat->flags;
385  }
386  Com_Printf("Reloaded '%s' : %d materials updated\n", materials_filename, old_table->num_materials);
387  }
388  return Q_ERR_FAILURE;
389 }

Referenced by vkpt_reload_materials().

◆ MAT_ResetUnused()

qerror_t MAT_ResetUnused ( )

Definition at line 433 of file material.c.

434 {
436 
437  for (int i = 0; i < table->num_materials + table->num_custom_materials; ++i)
438  {
439  pbr_material_t * mat = table->materials;
440 
442  continue;
443 
444  if (!mat->registration_sequence)
445  continue;
446 
447  mat->image_diffuse = R_NOTEXTURE;
448  mat->image_normals = R_NOTEXTURE;
449  mat->image_emissive = R_NOTEXTURE;
450 
451  mat->registration_sequence = 0;
452  }
453  return Q_ERR_SUCCESS;
454 }

Referenced by R_EndRegistration_RTX().

◆ MAT_SavePBRMaterials()

qerror_t MAT_SavePBRMaterials ( )

Definition at line 391 of file material.c.

Referenced by vkpt_save_materials().

◆ MAT_SetKind()

uint32_t MAT_SetKind ( uint32_t  material,
uint32_t  kind 
)

Definition at line 660 of file material.c.

661 {
662  return (material & ~MATERIAL_KIND_MASK) | kind;
663 }

Referenced by collect_surfaces(), fill_model_instance(), MAT_SetPBRMaterialAttribute(), and parseMaterialsTable().

◆ MAT_SetPBRMaterialAttribute()

qerror_t MAT_SetPBRMaterialAttribute ( pbr_material_t mat,
char const *  token,
char const *  value 
)

Definition at line 570 of file material.c.

571 {
572  assert(mat);
573 
574  // valid token-value pairs
575 
576  enum TokenType { TOKEN_BOOL, TOKEN_FLOAT, TOKEN_STRING };
577 
578  static struct Token {
579  int index;
580  char const * name;
581  enum TokenType type;
582  } tokens[] = {
583  {0, "bump_scale", TOKEN_FLOAT},
584  {1, "roughness_override", TOKEN_FLOAT},
585  {2, "specular_scale", TOKEN_FLOAT},
586  {3, "emissive_scale", TOKEN_FLOAT},
587  {4, "kind", TOKEN_STRING},
588  {5, "light_flag", TOKEN_BOOL},
589  {6, "correct_albedo_flag", TOKEN_BOOL} };
590 
591  static int ntokens = sizeof(tokens) / sizeof(struct Token);
592 
593  if (token == NULL || value == NULL)
594  goto usage;
595 
596  struct Token const * t = NULL;
597  for (int i = 0; i < ntokens; ++i)
598  {
599  if (strcmp(token, tokens[i].name) == 0)
600  t = &tokens[i];
601  }
602  if (!t)
603  {
604  Com_EPrintf("Unknown material token '%s'\n", Cmd_Argv(1));
605  goto usage;
606  }
607 
608  float fvalue = 0.f; qboolean bvalue = qfalse; char const * svalue = NULL;
609  switch (t->type)
610  {
611  case TOKEN_BOOL: bvalue = atoi(value) == 0 ? qfalse : qtrue; break;
612  case TOKEN_FLOAT: fvalue = (float)atof(value); break;
613  case TOKEN_STRING: svalue = value; break;
614  default:
615  assert("unknown PBR MAT attribute token type");
616  }
617 
618  // set material
619 
620  switch (t->index)
621  {
622  case 0: mat->bump_scale = fvalue; break;
623  case 1: mat->rough_override = fvalue; break;
624  case 2: mat->specular_scale = fvalue; break;
625  case 3: mat->emissive_scale = fvalue; break;
626  case 4: {
627  uint32_t kind = getMaterialKind(svalue);
628  if (kind != 0)
629  mat->flags = MAT_SetKind(mat->flags, kind);
630  else
631  {
632  Com_EPrintf("Unknown material kind '%s'\n", svalue);
633  return Q_ERR_FAILURE;
634  }
635  } break;
636  case 5: mat->flags = bvalue == qtrue ? mat->flags | MATERIAL_FLAG_LIGHT : mat->flags & ~(MATERIAL_FLAG_LIGHT); break;
637  case 6: mat->flags = bvalue == qtrue ? mat->flags | MATERIAL_FLAG_CORRECT_ALBEDO : mat->flags & ~(MATERIAL_FLAG_CORRECT_ALBEDO); break;
638  }
639 
640  return Q_ERR_SUCCESS;
641 
642 usage:
643  Com_Printf("Usage : set_material <token> <value>\n");
644  for (int i = 0; i < ntokens; ++i)
645  {
646  struct Token const * t = &tokens[i];
647 
648  char const * typename = "(undefined)";
649  switch (t->type)
650  {
651  case TOKEN_BOOL: typename = "bool [0,1]"; break;
652  case TOKEN_FLOAT: typename = "float"; break;
653  case TOKEN_STRING: typename = "string"; break;
654  }
655  Com_Printf(" %s (%s)\n", t->name, typename);
656  }
657  return Q_ERR_FAILURE;
658 }

Referenced by vkpt_set_material().

◆ MAT_UpdatePBRMaterialSkin()

const pbr_material_t* MAT_UpdatePBRMaterialSkin ( image_t image_diffuse)

Definition at line 517 of file material.c.

518 {
519  pbr_material_t const * material = MAT_FindPBRMaterial(image_diffuse->name);
520  assert(material);
521 
522  if (material->image_diffuse == image_diffuse)
523  return material;
524 
525  {
526  pbr_material_t * mat = (pbr_material_t *)material;
527  //MAT_Reset(mat, MAT_GetPBRMaterialIndex(mat));
528 
529  mat->image_diffuse = image_diffuse;
530 
531  // update registration sequence of material and its textures
532  int registration_sequence = image_diffuse->registration_sequence;
533 
534  if (mat->image_normals)
535  mat->image_normals->registration_sequence = registration_sequence;
536 
537  if (mat->image_emissive)
538  mat->image_emissive->registration_sequence = registration_sequence;
539 
541  }
542  return material;
543 }

Referenced by get_mesh_material().

◆ MAT_UpdateRegistration()

void MAT_UpdateRegistration ( pbr_material_t mat)

Definition at line 421 of file material.c.

422 {
423  if (!mat)
424  return;
425 
427  if (mat->image_diffuse) mat->image_diffuse->registration_sequence = registration_sequence;
428  if (mat->image_normals) mat->image_normals->registration_sequence = registration_sequence;
429  if (mat->image_emissive) mat->image_emissive->registration_sequence = registration_sequence;
430 }

Referenced by MOD_Reference_RTX().

pbr_materials_table_s::alpha_sorted
qboolean alpha_sorted
Definition: material.c:133
pbr_material_s::name
char name[MAX_QPATH]
Definition: material.h:34
MATERIAL_FLAG_CORRECT_ALBEDO
#define MATERIAL_FLAG_CORRECT_ALBEDO
Definition: constants.h:74
materials_filename
static const char * materials_filename
Definition: material.c:338
pbr_material_s::image_normals
image_t * image_normals
Definition: material.h:36
pbr_material_s::image_emissive
image_t * image_emissive
Definition: material.h:37
pbr_material_s::flags
uint32_t flags
Definition: material.h:42
registration_sequence
int registration_sequence
Definition: main.c:34
MATERIAL_FLAG_LIGHT
#define MATERIAL_FLAG_LIGHT
Definition: constants.h:73
Cmd_Argv
char * Cmd_Argv(int arg)
Definition: cmd.c:899
writeMaterialsTable
static qerror_t writeMaterialsTable(char const *filename, pbr_materials_table_t const *table)
Definition: material.c:229
pbr_material_s::specular_scale
float specular_scale
Definition: material.h:40
pbr_material_s::emissive_scale
float emissive_scale
Definition: material.h:41
pbr_material_s::bump_scale
float bump_scale
Definition: material.h:38
pbr_material_s
Definition: material.h:33
MAT_Reset
static void MAT_Reset(pbr_material_t *mat, int mat_index)
Definition: material.c:154
MATERIAL_KIND_MASK
#define MATERIAL_KIND_MASK
Definition: constants.h:57
pbr_material_s::image_diffuse
image_t * image_diffuse
Definition: material.h:35
pbr_materials_table_s::materials
pbr_material_t materials[MAX_PBR_MATERIALS]
Definition: material.c:131
pbr_materials_table_s::num_custom_materials
int num_custom_materials
Definition: material.c:134
pbr_materials_table_s
Definition: material.c:129
Q_strcasecmp
int Q_strcasecmp(const char *s1, const char *s2)
Definition: shared.c:666
right
static vec3_t right
Definition: p_view.c:27
pbr_materials_table_s::num_materials
int num_materials
Definition: material.c:132
pbr_material_s::registration_sequence
int registration_sequence
Definition: material.h:43
pbr_materials_table
static pbr_materials_table_t pbr_materials_table
Definition: material.c:137
getMaterialKindName
static const char * getMaterialKindName(uint32_t flag)
Definition: material.c:217
getMaterialKind
static uint32_t getMaterialKind(char const *kindname)
Definition: material.c:209
pbr_material_s::rough_override
float rough_override
Definition: material.h:39
MATERIAL_INDEX_MASK
#define MATERIAL_INDEX_MASK
Definition: constants.h:86
truncateExtension
static size_t truncateExtension(char const *src, char *dest)
Definition: material.c:111
parseMaterialsTable
static qerror_t parseMaterialsTable(char const *filename, pbr_materials_table_t *table)
Definition: material.c:261
MAT_SetKind
uint32_t MAT_SetKind(uint32_t material, uint32_t kind)
Definition: material.c:660
MAT_FindPBRMaterial
pbr_material_t * MAT_FindPBRMaterial(char const *name)
Definition: material.c:465