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

draw_builtin_shader.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68866ad29031cfaf42bb8b7ec5040aece2657908 (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
168
169
170
171
172
173
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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.
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 */

/** \file draw_builtin_shader.c
 *  \ingroup draw
 * Draw manager versions of #eGPUBuiltinShader, see #GPU_shader_get_builtin_shader.
 *
 * Allows for modifications to shaders (currently only clipping support).
 * Follow GPU_shader.h conventions to avoid annoyance.
 */

#include "BLI_utildefines.h"

#include "GPU_shader.h"

#include "DRW_render.h"

#include "draw_builtin_shader.h"  /* own include */


extern char datatoc_common_world_clip_lib_glsl[];

extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
extern char datatoc_gpu_shader_3D_vert_glsl[];
extern char datatoc_gpu_shader_depth_only_frag_glsl[];
extern char datatoc_gpu_shader_flat_color_frag_glsl[];
extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[];
extern char datatoc_gpu_shader_uniform_color_frag_glsl[];

/* cache of built-in shaders (each is created on first use) */
static struct {
	GPUShader *builtin_shaders[GPU_NUM_BUILTIN_SHADERS];
} g_sh_data[DRW_SHADER_SLOT_LEN - 1] = {{{NULL}}};

static GPUShader *drw_shader_get_builtin_shader_clipped(eGPUBuiltinShader shader_id, bool *r_test_only)
{
	const char *world_clip_lib = datatoc_common_world_clip_lib_glsl;
	const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n";

	if (r_test_only) {
		*r_test_only = true;
	}

	GPUShader *shader = NULL;
	switch (shader_id) {
		case GPU_SHADER_3D_UNIFORM_COLOR:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_3D_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, NULL}});
			break;
		case GPU_SHADER_3D_SMOOTH_COLOR:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_3D_smooth_color_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_3D_smooth_color_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, NULL}});
			break;
		case GPU_SHADER_3D_DEPTH_ONLY:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_3D_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_depth_only_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, NULL}});
			break;
		case GPU_SHADER_CAMERA:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_instance_camera_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, NULL}});
			break;

		case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, "#define UNIFORM_SCALE\n", NULL}});
			break;
		case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE:
			if (r_test_only) {
				break;
			}
			shader = DRW_shader_create_from_arrays({
			        .vert = (const char *[]){world_clip_lib, datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl, NULL},
			        .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
			        .defs = (const char *[]){world_clip_def, NULL}});
			break;
		default:
			/* Unsupported, caller asserts. */
			if (r_test_only) {
				*r_test_only = false;
			}
	}
	return shader;
}

#ifndef NDEBUG
static bool drw_shader_get_builtin_shader_test_all(eGPUBuiltinShader shader_id)
{
	bool test = false;
	drw_shader_get_builtin_shader_clipped(shader_id, &test);
	return test;
}
#endif

GPUShader *DRW_shader_get_builtin_shader(eGPUBuiltinShader shader_id, eDRW_ShaderSlot slot)
{
	BLI_assert(drw_shader_get_builtin_shader_test_all(shader_id));
	if (slot == DRW_SHADER_SLOT_DEFAULT) {
		return GPU_shader_get_builtin_shader(shader_id);
	}

	GPUShader **builtin_shaders = g_sh_data[slot - 1].builtin_shaders;

	if (builtin_shaders[shader_id] != NULL) {
		return builtin_shaders[shader_id];
	}

	if (slot == DRW_SHADER_SLOT_CLIPPED) {
		builtin_shaders[shader_id] = drw_shader_get_builtin_shader_clipped(shader_id, NULL);
		return builtin_shaders[shader_id];
	}
	else {
		BLI_assert(0);
		return NULL;
	}
}

void DRW_shader_free_builtin_shaders(void)
{
	for (int j = 0; j < (DRW_SHADER_SLOT_LEN - 1); j++) {
		GPUShader **builtin_shaders = g_sh_data[j].builtin_shaders;
		for (int i = 0; i < GPU_NUM_BUILTIN_SHADERS; i++) {
			if (builtin_shaders[i]) {
				GPU_shader_free(builtin_shaders[i]);
				builtin_shaders[i] = NULL;
			}
		}
	}
}