This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
基础
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
统一的Unigine着色器语言 UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Unigine::CPUShader Class

Header: #include <UnigineThread.h>

This class is used to create a custom CPU shader, for example, to implement multi-threaded mesh cluster update. Your custom CPU-shader must be inherited from this class. The corresponding sample is included in the SDK(/samples/Api/Systems/CPUShader/). Engine::swap() always waits for CPU shaders, therefore they cannot be used as tasks. CPU shader is updated with the current framerate of the application.

Here is an example of implementing a custom CPU shader:

Source code (C++)
struct UpdateClustersCPUShader: public CPUShader
{
	UpdateClustersCPUShader() {}
	virtual ~UpdateClustersCPUShader() 
	{ 
		// wait until all asynchronous operations are completed
		wait(); 
	}

	Vector<AsyncCluster> clusters;

	volatile int counter{0};
	
	// overriding the process method to performa our calculations
	void process(int thread_num, int threads_count) override
	{
		UNIGINE_PROFILER_FUNCTION;

		while (true)
		{
			int num = AtomicAdd(&counter, 1);
			if (num >= clusters.size())
				break;
			clusters[num].update();
		}
	}

	void run()
	{
		for (auto &c : clusters)
			c.swap();

		counter = 0;
		
		// running shader code in asynchronous mode
		runAsync();
	}
};

CPUShader Class

Members


static CPUShaderPtr create ( ) #

Default CPUShader class constructor.

void runSync ( int threads_count ) #

Runs CPU shader code synchronously. This method is blocking. No additional threads are created, as the Engine's thread pool is used.

Arguments

  • int threads_count - Number of threads to be used. The default value of -1 sets an optimum number of threads calculated for the particular PC.

void runAsync ( int threads_count ) #

Runs CPU shader code asynchronously. This method is non-blocking. No additional threads are created, as the Engine's thread pool is used.

Arguments

  • int threads_count - Number of threads to be used. The default value of -1 sets an optimum number of threads calculated for the particular PC.

void wait ( ) #

Waits for running asynchronous shader code execution.

int isRunning ( ) #

Returns a value indicating if the CPU shader code is currently executed.

Return value

1 if the shader code is currently executed; otherwise, 0.

int getNumThreads ( ) #

Returns the currently used number of threads.

Return value

Number of currently used threads.

virtual void process ( int thread_num, int threads_count ) #

Override this method to implement calculations.

Arguments

  • int thread_num - Current thread number. This number is not a thread ID, it just a virtual number.
  • int threads_count - Number of threads to be used.
Last update: 2024-04-19
Build: ()