Welcome to mirror list, hosted at ThFree Co, Russian Federation.

basic_shader.c « basic « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b92406d5c0ba626d50e0320263faae4d0bef524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2019, Blender Foundation.
 */

/** \file
 * \ingroup draw_engine
 */

#include "DRW_render.h"

#include "GPU_shader.h"

#include "basic_private.h"

extern char datatoc_depth_frag_glsl[];
extern char datatoc_depth_vert_glsl[];
extern char datatoc_conservative_depth_geom_glsl[];

extern char datatoc_common_view_lib_glsl[];
extern char datatoc_common_pointcloud_lib_glsl[];

/* Shaders */

typedef struct BASIC_Shaders {
  /* Depth Pre Pass */
  struct GPUShader *depth;
  struct GPUShader *pointcloud_depth;
  struct GPUShader *depth_conservative;
  struct GPUShader *pointcloud_depth_conservative;
} BASIC_Shaders;

static struct {
  BASIC_Shaders sh_data[GPU_SHADER_CFG_LEN];
} e_data = {{{NULL}}}; /* Engine data */

static GPUShader *BASIC_shader_create_depth_sh(const GPUShaderConfigData *sh_cfg)
{
  return GPU_shader_create_from_arrays({
      .vert = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_depth_vert_glsl,
                               NULL},
      .frag = (const char *[]){datatoc_depth_frag_glsl, NULL},
      .defs = (const char *[]){sh_cfg->def, NULL},
  });
}

static GPUShader *BASIC_shader_create_pointcloud_depth_sh(const GPUShaderConfigData *sh_cfg)
{
  return GPU_shader_create_from_arrays({
      .vert = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_common_pointcloud_lib_glsl,
                               datatoc_depth_vert_glsl,
                               NULL},
      .frag = (const char *[]){datatoc_depth_frag_glsl, NULL},
      .defs = (const char *[]){sh_cfg->def,
                               "#define POINTCLOUD\n",
                               "#define INSTANCED_ATTR\n",
                               "#define UNIFORM_RESOURCE_ID\n",
                               NULL},
  });
}

static GPUShader *BASIC_shader_create_depth_conservative_sh(const GPUShaderConfigData *sh_cfg)
{
  return GPU_shader_create_from_arrays({
      .vert = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_depth_vert_glsl,
                               NULL},
      .geom = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_conservative_depth_geom_glsl,
                               NULL},
      .frag = (const char *[]){datatoc_depth_frag_glsl, NULL},
      .defs = (const char *[]){sh_cfg->def, "#define CONSERVATIVE_RASTER\n", NULL},
  });
}

static GPUShader *BASIC_shader_create_pointcloud_depth_conservative_sh(
    const GPUShaderConfigData *sh_cfg)
{
  return GPU_shader_create_from_arrays({
      .vert = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_common_pointcloud_lib_glsl,
                               datatoc_depth_vert_glsl,
                               NULL},
      .geom = (const char *[]){sh_cfg->lib,
                               datatoc_common_view_lib_glsl,
                               datatoc_conservative_depth_geom_glsl,
                               NULL},
      .frag = (const char *[]){datatoc_depth_frag_glsl, NULL},
      .defs = (const char *[]){sh_cfg->def,
                               "#define CONSERVATIVE_RASTER\n",
                               "#define POINTCLOUD\n",
                               "#define INSTANCED_ATTR\n",
                               "#define UNIFORM_RESOURCE_ID\n",
                               NULL},
  });
}

GPUShader *BASIC_shaders_depth_sh_get(eGPUShaderConfig config)
{
  BASIC_Shaders *sh_data = &e_data.sh_data[config];
  const GPUShaderConfigData *sh_cfg = &GPU_shader_cfg_data[config];
  if (sh_data->depth == NULL) {
    sh_data->depth = BASIC_shader_create_depth_sh(sh_cfg);
  }
  return sh_data->depth;
}

GPUShader *BASIC_shaders_pointcloud_depth_sh_get(eGPUShaderConfig config)
{
  BASIC_Shaders *sh_data = &e_data.sh_data[config];
  const GPUShaderConfigData *sh_cfg = &GPU_shader_cfg_data[config];
  if (sh_data->pointcloud_depth == NULL) {
    sh_data->pointcloud_depth = BASIC_shader_create_pointcloud_depth_sh(sh_cfg);
  }
  return sh_data->pointcloud_depth;
}

GPUShader *BASIC_shaders_depth_conservative_sh_get(eGPUShaderConfig config)
{
  BASIC_Shaders *sh_data = &e_data.sh_data[config];
  const GPUShaderConfigData *sh_cfg = &GPU_shader_cfg_data[config];
  if (sh_data->depth_conservative == NULL) {
    sh_data->depth_conservative = BASIC_shader_create_depth_conservative_sh(sh_cfg);
  }
  return sh_data->depth_conservative;
}

GPUShader *BASIC_shaders_pointcloud_depth_conservative_sh_get(eGPUShaderConfig config)
{
  BASIC_Shaders *sh_data = &e_data.sh_data[config];
  const GPUShaderConfigData *sh_cfg = &GPU_shader_cfg_data[config];
  if (sh_data->pointcloud_depth_conservative == NULL) {
    sh_data->pointcloud_depth_conservative = BASIC_shader_create_pointcloud_depth_conservative_sh(
        sh_cfg);
  }
  return sh_data->pointcloud_depth_conservative;
}

void BASIC_shaders_free(void)
{
  for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) {
    GPUShader **sh_data_as_array = (GPUShader **)&e_data.sh_data[i];
    for (int j = 0; j < (sizeof(BASIC_Shaders) / sizeof(GPUShader *)); j++) {
      DRW_SHADER_FREE_SAFE(sh_data_as_array[j]);
    }
  }
}