Difference between revisions of "Implementing a Texture"

From LuxCoreRender Wiki
Jump to navigation Jump to search
(OpenCL Code)
Line 69: Line 69:
 
== OpenCL Code ==
 
== OpenCL Code ==
  
TODO
+
Files that need to be edited:
 +
* include/slg/textures/texture_funcs.cl
 +
* include/slg/textures/texture_types.cl
 +
* src/slg/engines/pathoclbase/compiletextures.cpp
 +
* src/slg/engines/pathoclbase/pathoclbaseoclthreadkernels.cpp
 +
 
 +
==== include/slg/textures/texture_funcs.cl ====
 +
 
 +
<pre>
 +
//------------------------------------------------------------------------------
 +
// Your texture
 +
//------------------------------------------------------------------------------
 +
 
 +
#if defined(PARAM_ENABLE_TEX_YOURS)
 +
 
 +
OPENCL_FORCE_NOT_INLINE float YourTexture_ConstEvaluateFloat(__global HitPoint *hitPoint,
 +
const float tex1, const float tex2) {
 +
// The actual work is done here (this function is executed when a float value of your texture is requested)
 +
return tex1 * tex2;
 +
}
 +
 
 +
OPENCL_FORCE_NOT_INLINE float3 YourTexture_ConstEvaluateSpectrum(__global HitPoint *hitPoint,
 +
const float3 tex1, const float3 tex2) {
 +
// The actual work is done here (this function is executed when a color value of your texture is requested)
 +
return tex1 * tex2;
 +
}
 +
 
 +
#endif
 +
</pre>
 +
 
 +
==== include/slg/textures/texture_types.cl ====
 +
 
 +
Add a new texture type for your texture to the <code>TextureType</code> enum.
 +
 
 +
If your texture has input parameters, create a new struct in the form
 +
<pre>
 +
typedef struct {
 +
unsigned int tex1Index, tex2Index;
 +
} YourTexParam;
 +
</pre>
 +
and add it to the big union in the <code>Texture</code> struct.
 +
 
 +
==== src/slg/engines/pathoclbase/compiletextures.cpp ====
 +
 
 +
Include the C++ header of your texture.
 +
 
 +
Add a new <code>case</code> for your function to the <code>CompiledScene::CompileTextures()</code> method.
 +
 
 +
Add a new <code>case</code> for your function to the <code>CompiledScene::GetTexturesEvaluationSourceCode()</code> method.
 +
 
 +
==== src/slg/engines/pathoclbase/pathoclbaseoclthreadkernels.cpp ====
 +
 
 +
Add a new <code>if</code> statement for your texture to the <code>PathOCLBaseOCLRenderThread::InitKernels()</code> method.

Revision as of 16:16, 20 March 2019

C++ Code

Files that need to be created:

  • Header: include/slg/textures/yourtexturename.h
  • Source: src/slg/textures/yourtexturename.cpp

Files that need to be edited:

  • Base texture header: include/slg/textures/texture.h
  • Sceneparse: src/slg/scene/parsetextures.cpp
  • CMakeLists.txt: src/slg/CMakeLists.txt

Commit with example: adding the "Divide" texture: https://github.com/LuxCoreRender/LuxCore/commit/ce6e23c244b1bb426c1dcd13099a41982e565b88

Creating the header file

Creating the source file

TODO: Description of the methods that need to be implemented:

  • GetFloatValue
  • GetSpectrumValue
  • Filter
  • Y
  • Bump

etc.

What do they do, what should they return?

Modifying the base texture header

Open the file include/slg/textures/texture.h
Add your new texture type to the TextureType enum.

Modifying Sceneparse

Open src/slg/scene/parsetextures.cpp
Include the new header file you created for your texture (the list is alphabetic).

Next, go to the method Scene::CreateTexture, scroll down until the end of the big if/else statement and add a new else if for your texture.

Modifying CMakeLists.txt

Open src/slg/CMakeLists.txt
Search for set(SLG_CORE_SRCS and add your source filepath among the other textures (they are sorted alphabetically).

Compile and Test

To test your plugin, compile LuxCore: Compiling_LuxCore

After the compilation succeeds, you will need a scene to test your plugin. The easiest method is to edit one of the .scn files in the scenes/luxball directory.

TODO Example

Now, run luxcoreui from the root LuxCore directory with

./bin/luxcoreui ./scenes/luxball/luxball-sunset.cfg

TODO result example image

OpenCL Code

Files that need to be edited:

  • include/slg/textures/texture_funcs.cl
  • include/slg/textures/texture_types.cl
  • src/slg/engines/pathoclbase/compiletextures.cpp
  • src/slg/engines/pathoclbase/pathoclbaseoclthreadkernels.cpp

include/slg/textures/texture_funcs.cl

//------------------------------------------------------------------------------
// Your texture
//------------------------------------------------------------------------------

#if defined(PARAM_ENABLE_TEX_YOURS)

OPENCL_FORCE_NOT_INLINE float YourTexture_ConstEvaluateFloat(__global HitPoint *hitPoint,
		const float tex1, const float tex2) {
	// The actual work is done here (this function is executed when a float value of your texture is requested)
	return tex1 * tex2;
}

OPENCL_FORCE_NOT_INLINE float3 YourTexture_ConstEvaluateSpectrum(__global HitPoint *hitPoint,
		const float3 tex1, const float3 tex2) {
	// The actual work is done here (this function is executed when a color value of your texture is requested)
	return tex1 * tex2;
}

#endif

include/slg/textures/texture_types.cl

Add a new texture type for your texture to the TextureType enum.

If your texture has input parameters, create a new struct in the form

typedef struct {
	unsigned int tex1Index, tex2Index;
} YourTexParam;

and add it to the big union in the Texture struct.

src/slg/engines/pathoclbase/compiletextures.cpp

Include the C++ header of your texture.

Add a new case for your function to the CompiledScene::CompileTextures() method.

Add a new case for your function to the CompiledScene::GetTexturesEvaluationSourceCode() method.

src/slg/engines/pathoclbase/pathoclbaseoclthreadkernels.cpp

Add a new if statement for your texture to the PathOCLBaseOCLRenderThread::InitKernels() method.