CMakeLists.txt 9.3 KB
Newer Older
1
2
PROJECT(MaterialLibrary)

3
vtk_module_export_info()
VTK Developers's avatar
VTK Developers committed
4
5
6
7
8
9
10
11
12
13

# FIXME: Provide a reasonable default for this value in MaterialLibrary?
set(VTK_DEFAULT_SHADERS_DIR
  "${VTK_BINARY_DIR}/Utilities/MaterialLibrary/Repository"
  CACHE INTERNAL
  "The directory that code for shaders is stored in.")
set(VTK_MATERIALS_DIRS
  ${VTK_DEFAULT_SHADERS_DIR}
  CACHE STRING
  "A ; separated list of directories to search for materials/shaders.")
14
mark_as_advanced(VTK_MATERIALS_DIRS)
VTK Developers's avatar
VTK Developers committed
15
16
17
18

include_directories("${VTK_SOURCE_DIR}/Common/Core"
  "${VTK_BINARY_DIR}/Common/Core")

Alexander Neundorf's avatar
 
Alexander Neundorf committed
19
IF(NOT CMAKE_CROSSCOMPILING)
20
21
22
  ADD_EXECUTABLE(ProcessShader ProcessShader.cxx)
  TARGET_LINK_LIBRARIES(ProcessShader vtksys)
  vtk_compile_tools_target(ProcessShader)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
23
ENDIF(NOT CMAKE_CROSSCOMPILING)
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
# Macro to obtain the relative path of a file.
MACRO(GET_RELATIVE_PATH __path __file __result)
  STRING(REGEX REPLACE "/" ";" __dirfilelist "${__file}")
  SET(__found)
  SET(__cfile "${__file}")
  SET(__res "")
  FOREACH(__dir ${__dirfilelist})
    IF(NOT __found)
      STRING(REGEX REPLACE ".*/(.*)" "\\1" __lineres "${__cfile}")
      STRING(REGEX REPLACE "(.*)/.*" "\\1" __cfile "${__cfile}")
      SET(__res "${__lineres}/${__res}")
      STRING(COMPARE EQUAL "${__cfile}" "${__path}" __flag)
      IF(__flag)
        SET(__found 1)
      ENDIF(__flag)
    ENDIF(NOT __found)
  ENDFOREACH(__dir)
  STRING(REGEX REPLACE "/$" "" __res "${__res}")
  #MESSAGE("Dir: ${__cfile}")
  #MESSAGE("File: ${__res}")
  SET(${__result} "${__res}")
ENDMACRO(GET_RELATIVE_PATH)

#Macro to copy files over. Copies the entire subtree.
49
50
MACRO(COPY_FILES_TO_DIR src_dir target_dir)
  IF (NOT EXISTS ${target_dir})
51
    FILE(MAKE_DIRECTORY ${target_dir})
52
53
  ENDIF(NOT EXISTS ${target_dir})

54
55
56
57
58
59
60
61
62
63
64
65
66
  FILE (GLOB_RECURSE files_to_copy1 ${src_dir}/*.*)

  FOREACH (src_file ${files_to_copy1})
    GET_RELATIVE_PATH( ${src_dir} ${src_file} rel_file )
    GET_FILENAME_COMPONENT(src_path "${rel_file}" PATH)
    GET_FILENAME_COMPONENT(filename "${rel_file}" NAME)
    STRING(REGEX REPLACE "/" ";" dirlist "${src_path}")

    # Ensure that the tree structure is identical on the destination side.
    SET (path ${target_dir})
    FOREACH(subdir ${dirlist})
      SET(path "${path}/${subdir}")
      IF (NOT EXISTS ${path})
67
        FILE(MAKE_DIRECTORY ${path})
68
69
70
71
      ENDIF (NOT EXISTS ${path})
    ENDFOREACH(subdir)

    # Now configure the file over.
72
    CONFIGURE_FILE(${src_file} "${path}/${filename}" COPYONLY)
73
  ENDFOREACH (src_file)
74
75
76
77
78
79
80
81
82
ENDMACRO(COPY_FILES_TO_DIR)

# Shaders/Materials provided in Utilities/MaterialLibrary/Repository
# are copied over to the binary dir.
IF (NOT "${VTK_BINARY_DIR}" MATCHES "^${VTK_SOURCE_DIR}$")
  # Ensure that the Source and Build dir are not same.
  # In that case we don't need to copy anything.
  COPY_FILES_TO_DIR(${MaterialLibrary_SOURCE_DIR}/Repository
    ${VTK_DEFAULT_SHADERS_DIR})
83

84
85
ENDIF (NOT "${VTK_BINARY_DIR}" MATCHES "^${VTK_SOURCE_DIR}$")

86
87
# Build the ShaderCodes Library.
SET (CgCodes
88
89
90
91
92
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/FragmentTexture.cg
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/VertTexPassThrough.cg
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/VertPassThrough.cg
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/FragmentIsotropicTorranceSparrow.cg
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/cg_sinewave.cg
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
93
94
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/VertexLighting.cg
  ${MaterialLibrary_SOURCE_DIR}/CgShaders/Twisted.cg
95
96
97
  )

SET (GLSLCodes
98
99
100
101
102
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/TestAppVarFrag.glsl
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/TestVertex.glsl
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/TestVtkPropertyFrag.glsl
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/TestMatrixFrag.glsl
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/TestScalarVectorFrag.glsl
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
103
  ${MaterialLibrary_SOURCE_DIR}/GLSLShaders/Twisted.glsl
104
105
106
107
108
109
  )

SET (ShaderLibraryDependencies)
SET (ShaderLibraryHeaders)
SET (ShaderLibraryModules)

110
# Macro to obtain the Module names from filenames.
111
112
113
114
115
116
117
MACRO(get_modules modules prefix files )
  FOREACH(file ${files})
    STRING(REGEX REPLACE ".*[\\\\/]([^./\\\\]+)\\.(cg|glsl|xml)$" "\\1" module "${file}")
    SET(${modules} ${${modules}} "${prefix}${module}")
  ENDFOREACH(file)
ENDMACRO(get_modules)

118
SET(CMD_DEP ProcessShader)
119
SET(CMD ProcessShader)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
120

121
122
123
IF (VTK_USE_CG_SHADERS)
  # Create the Cg library.
  ADD_CUSTOM_COMMAND(
124
    OUTPUT ${MaterialLibrary_BINARY_DIR}/vtkCgShaderLibrary.h
125
    DEPENDS ${CgCodes}
Alexander Neundorf's avatar
 
Alexander Neundorf committed
126
    ${CMD_DEP}
127
    COMMAND ${CMD}
128
    ARGS ${MaterialLibrary_BINARY_DIR}/vtkCgShaderLibrary.h
129
130
    vtkShaderCG Code GetCode
    ${CgCodes})
131

132
133
134
  SET (ShaderLibraryHeaders ${ShaderLibraryHeaders}
    vtkCgShaderLibrary.h)
  SET (ShaderLibraryDependencies ${ShaderLibraryDependencies}
135
    ${MaterialLibrary_BINARY_DIR}/vtkCgShaderLibrary.h)
136
137
138
139
140
141
142

  get_modules(ShaderLibraryModules "CG" "${CgCodes}")
ENDIF (VTK_USE_CG_SHADERS)

IF (VTK_USE_GLSL_SHADERS)
  # Create the GLSL library
  ADD_CUSTOM_COMMAND(
143
    OUTPUT ${MaterialLibrary_BINARY_DIR}/vtkGLSLShaderLibrary.h
144
    DEPENDS ${GLSLCodes}
Alexander Neundorf's avatar
 
Alexander Neundorf committed
145
    ${CMD_DEP}
146
    COMMAND ${CMD}
147
    ARGS ${MaterialLibrary_BINARY_DIR}/vtkGLSLShaderLibrary.h
148
149
150
151
152
    vtkShaderGLSL Code GetCode
    ${GLSLCodes})
  SET (ShaderLibraryHeaders ${ShaderLibraryHeaders}
    vtkGLSLShaderLibrary.h)
  SET (ShaderLibraryDependencies ${ShaderLibraryDependencies}
153
    ${MaterialLibrary_BINARY_DIR}/vtkGLSLShaderLibrary.h)
154
155
156
157
158
159

  get_modules(ShaderLibraryModules "GLSL" "${GLSLCodes}")
ENDIF (VTK_USE_GLSL_SHADERS)

# Write the vtkShaderCodeLibraryMacro header file.
SET (VTK_SHADER_CODE_LIBRARY_MACRO)
160
SET (VTK_SHADER_CODE_LIBRARY_LIST)
161
FOREACH(file ${ShaderLibraryHeaders})
162
  SET (VTK_SHADER_CODE_LIBRARY_MACRO
163
164
165
166
167
168
169
    "${VTK_SHADER_CODE_LIBRARY_MACRO}#include \"${file}\"\n")
ENDFOREACH(file)

SET (VTK_SHADER_CODE_LIBRARY_MACRO "${VTK_SHADER_CODE_LIBRARY_MACRO}
#define vtkShaderCodeLibraryMacro(name) \\\n")
FOREACH (module ${ShaderLibraryModules})
  # get the module name.
170
  SET(VTK_SHADER_CODE_LIBRARY_MACRO
171
172
    "${VTK_SHADER_CODE_LIBRARY_MACRO} if (strcmp(name, \"${module}\") == 0)\\
        {\\
173
        return vtkShader${module}GetCode();\\
174
        }\\\n")
175
176
177
  SET(VTK_SHADER_CODE_LIBRARY_LIST
    "${VTK_SHADER_CODE_LIBRARY_LIST}
    \"${module}\",")
178
179
ENDFOREACH(module)

180
181
182
CONFIGURE_FILE(${MaterialLibrary_SOURCE_DIR}/vtkShaderCodeLibraryMacro.h.in
  ${MaterialLibrary_BINARY_DIR}/vtkShaderCodeLibraryMacro.h
  @ONLY)
183
184
185
186
187

# Build the Material library.
SET (MaterialXMLs)

IF (VTK_USE_CG_SHADERS)
188
# # Add Materials using Cg Shaders.
189
  SET (MaterialXMLs ${MaterialXMLs}
190
    ${MaterialLibrary_SOURCE_DIR}/Materials/CgSinewave.xml
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
191
192
    ${MaterialLibrary_SOURCE_DIR}/Materials/CgLighting.xml
    ${MaterialLibrary_SOURCE_DIR}/Materials/CgTwisted.xml
193
194
195
196
    )
ENDIF (VTK_USE_CG_SHADERS)

IF (VTK_USE_GLSL_SHADERS)
197
  # Add Materials using GLSL Shaders.
198
  SET (MaterialXMLs ${MaterialXMLs}
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
199
    ${MaterialLibrary_SOURCE_DIR}/Materials/GLSLTwisted.xml
200
201
202
    )
ENDIF (VTK_USE_GLSL_SHADERS)

203
204
205
206
# Add the Materials that don't use Cg/GLSL.
SET (Materials ${MaterialXMLs}
  )

207
208
209
210
CONFIGURE_FILE(
  ${MaterialLibrary_SOURCE_DIR}/vtkMaterialXMLLibrary.h.forcebuild.in
  ${MaterialLibrary_BINARY_DIR}/vtkMaterialXMLLibrary.h.forcebuild
  @ONLY)
211

212
213
214
215
216
217
218
219
SET (VTK_MATERIAL_LIBRARY_MACRO "")
# materials_exist is variable to indicate if there are any materials.
# if not we can reduce our work by not trying to build the headers at
# all.
SET (materials_exist 0)
FOREACH(material ${Materials})
  SET (materials_exist 1)
ENDFOREACH(material ${Materials})
220

221
222
223
224
225
SET(vtkMaterialLibraryConfiguredFilesDependencies
  ${ShaderLibraryDependencies}
  ${MaterialLibrary_BINARY_DIR}/vtkShaderCodeLibraryMacro.h
  ${MaterialLibrary_BINARY_DIR}/vtkMaterialLibraryMacro.h)

226
227
228
IF (${materials_exist})
  ADD_CUSTOM_COMMAND(
    OUTPUT ${MaterialLibrary_BINARY_DIR}/vtkMaterialXMLLibrary.h
229
    DEPENDS ${MaterialXMLs}
Alexander Neundorf's avatar
 
Alexander Neundorf committed
230
      ${CMD_DEP}
231
232
233
234
235
236
      ${MaterialLibrary_BINARY_DIR}/vtkMaterialXMLLibrary.h.forcebuild
    COMMAND ${CMD}
    ARGS ${MaterialLibrary_BINARY_DIR}/vtkMaterialXMLLibrary.h
    vtkMaterial XML GetXML
    ${MaterialXMLs}
    )
237

238
  SET (VTK_MATERIAL_LIBRARY_MACRO "#include \"vtkMaterialXMLLibrary.h\"\n")
239
240
241

  # Add a dependency on the vtkMaterialXMLLibrary.h file for the target that generates all
  # the configured files.
242
  SET(vtkMaterialLibraryConfiguredFilesDependencies
243
244
    ${vtkMaterialLibraryConfiguredFilesDependencies}
    ${MaterialLibrary_BINARY_DIR}/vtkMaterialXMLLibrary.h)
245
ENDIF (${materials_exist})
246
247

# Write the MaterialLibraryMacro header file.
248
SET (VTK_MATERIAL_LIBRARY_LIST)
249
250
251
SET (VTK_MATERIAL_LIBRARY_MACRO "${VTK_MATERIAL_LIBRARY_MACRO}
#define vtkMaterialLibraryMacro(name) \\\n")

252

253
254
255
256
257
258
SET (MaterialModules)
get_modules(MaterialModules "" "${MaterialXMLs}")
FOREACH(module ${MaterialModules})
  SET (VTK_MATERIAL_LIBRARY_MACRO
    "${VTK_MATERIAL_LIBRARY_MACRO} if (strcmp(name, \"${module}\") == 0)\\
      {\\
259
      return vtkMaterial${module}GetXML();\\
260
      }\\\n")
261
262
  SET (VTK_MATERIAL_LIBRARY_LIST
    "${VTK_MATERIAL_LIBRARY_LIST}
263
    \"${module}\",")
264
265
ENDFOREACH(module)

266
267
268
CONFIGURE_FILE(
  ${MaterialLibrary_SOURCE_DIR}/vtkMaterialLibraryMacro.h.in
  ${MaterialLibrary_BINARY_DIR}/vtkMaterialLibraryMacro.h)
269

270
ADD_CUSTOM_TARGET(vtkMaterialLibraryConfiguredFiles ALL DEPENDS
271
272
  ${vtkMaterialLibraryConfiguredFilesDependencies}
  )
273

274

275
276
277
SET(MATERIAL_LIBRARY_SHADER_MACRO_HEADER
  ${MaterialLibrary_BINARY_DIR}/vtkShaderCodeLibraryMacro.h
  CACHE INTERNAL "Path to vtkShaderCodeLibraryMacro.h")
278

279
280
281
SET(MATERIAL_LIBRARY_MATERIAL_MACRO_HEADER
  ${MaterialLibrary_BINARY_DIR}/vtkMaterialLibraryMacro.h
  CACHE INTERNAL "Path to vtkMaterialLibraryMacro.h")
282

283