aclnnRasterizer
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Implements rasterization. Based on the given points and faces in 3D space, obtains the minimum depth of each pixel on the screen and the corresponding face index, and calculates the perspective corrective interpolation of the barycentric coordinates of the face.
Formula:
records the face index corresponding to the minimum depth of each pixel, and records the perspective corrective interpolation of the barycentric coordinates of each vertex with respect to the faces recorded in . The Z-Buffer used in the computation records, for each pixel , the minimum depth and the triangle face index corresponding to this depth .
The computation process is as follows: For each triangle face in 3D space:
Convert the three vertex coordinates of to screen coordinates
Compute the bounding rectangle of based on
For each pixel within the rectangle, perform the following operations:
a. Compute the pixel center coordinates b. Compute the barycentric coordinates of with respect to triangle c. Determine whether is inside the triangle based on . If is not inside the triangle, process the next pixel within the rectangle; otherwise, perform the following steps d. Obtain the depth value depth of the current pixel using and e. If the depth prior is enabled, compute the depth threshold depth_thres using the depth prior map; otherwise, proceed directly to the next step "Z-Buffer update"
- Compute the depth threshold depth_thres using the depth prior map
- If depth < depth_thres, process the next pixel within the rectangle; otherwise, perform the following steps
f. Z-Buffer update:
If :
If :
After all triangle patches in the space are processed as described above, for each pixel on the screen of size :
- Obtain the patch index corresponding to in the Z-Buffer,
- Convert the three vertex coordinates of to screen coordinates
- Compute the center point coordinates of
- Compute the barycentric coordinates of with respect to triangle (\alpha, \beta, \gamma)
- Use to compute perspective-corrected interpolation
The specific computation methods involved are as follows:
Convert vertex to screen coordinates
Barycentric coordinates of point with respect to triangle
- Compute the signed areas , and of triangles , , and , respectively.
- If is 0, then , otherwise
Signed area of the triangle formed by vertices , , and
Compute the depth of pixel using the barycentric coordinates and the screen coordinates of the triangle
With the depth map , apply occlusion truncation to compute the depth threshold of point
Use the barycentric coordinates to determine whether the vertex is inside the triangle If and and , the point is inside the triangle (including on the edges of the triangle); otherwise, the point is not inside the triangle.
Use the barycentric coordinates and the three vertex coordinates of the triangle , , and to compute the perspective-corrective interpolation
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnRasterizerGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call the "aclnnRasterizer" API to execute the computation.
aclnnStatus aclnnRasterizerGetWorkspaceSize(
const aclTensor *v,
const aclTensor *f,
const aclTensor *dOptional,
int64_t width,
int64_t height,
double occlusionTruncation,
int64_t useDepthPrior,
const aclTensor *findicesOut,
const aclTensor *barycentricOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRasterizer(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnRasterizerGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor v (aclTensor*) Input Input tensor that specifies the spatial coordinates of vertices. - Empty tensors are not supported.
- The shape is (numVertices, 4), where numVertices denotes the number of vertices and must be a positive integer. Each vertex coordinate is expressed as (x, y, z, w).
FLOAT32 ND 2 × f (aclTensor*) Input Input tensor that specifies the faces in space. - Empty tensors are not supported.
- The shape is (numFaces, 3), where numFaces is the number of faces in the space and must be a positive integer. Each face is a triangle, and each vertex of the triangle is represented as the index of the vertex in v. Therefore, each element in f must be a valid index to an element in v, that is, in the range [0, numVertices-1]. The caller must ensure that the elements in f are valid.
INT32 ND 2 × dOptional (aclTensor*) Input Input tensor of the depth map, used for computing the depth threshold. - Optional input. Empty tensors are supported.
- This parameter does not take effect.
FLOAT32 ND 2 × width (int64_t) Input Screen width. Value range [1, 4096]. - - - - height (int64_t) Input Screen height. Value range [1, 4096]. - - - - occlusionTruncation (double) Input Occlusion truncation, used for depth threshold computation. This parameter does not take effect. - - - - useDepthPrior (int64_t) Input Indicates whether to apply the depth prior. - The value is 0 or 1. 1 indicates that the depth prior is applied, and 0 indicates that it is not.
- The current operator does not support the depth prior. Therefore, the value is fixed to 0.
- - - - findicesOut (aclTensor*) Output Index of the face with the minimum depth for each pixel on the screen. - Empty tensors are not supported.
- The shape is (height, width), where height is the screen height and width is the screen width.
INT32 ND 2 × barycentricOut (aclTensor*) Output Output tensor of the perspective-corrective interpolation of barycentric coordinates of each pixel on the screen relative to the face corresponding to the minimum depth. - Empty tensors are not supported.
- The shape is (height, width, 3), where height is the screen height and width is the screen width.
FLOAT32 ND 3 × workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the computation flow of the operator. - - - - - Return Value
aclnnStatus: return status. For details, see aclnn return codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed-in v, f, findicesOut, or barycentricOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of v, f, findicesOut, or barycentricOut is not in the supported range. ACLNN_ERR_INNER_TILING_ERROR 561002 The shape of v, f, findicesOut, or barycentricOut is not in the supported range. The value of useDepthPrior, height, or width is out of the supported range.
aclnnRasterizer
Parameters
Parameter Input/Output Description workspace Input Memory address of the workspace allocated on the Device side. workspaceSize Input Size of the workspace allocated on the Device side, obtained by the first-phase API aclnnRasterizerGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Only the input use case where useDepthPrior is 0 is supported. Parameters dOptional, occlusionTruncation, and useDepthPrior do not take effect in the actual computation.
Deterministic computation:
- aclnnRasterizer defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_rasterizer.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape)
{
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream)
{
// Boilerplate: initialize resources.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate memory on the device.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy data from the host to the device memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call aclCreateTensor to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) Initialize the device/stream. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Custom construction is required based on the API.
std::vector<int64_t> vShape = {3, 4};
std::vector<int64_t> fShape = {1, 3};
std::vector<int64_t> dShape = {10, 10};
std::vector<int64_t> findicesShape = {10, 10};
std::vector<int64_t> baryShape = {10, 10, 3};
int64_t height = 10;
int64_t width = 10;
float occlusionTruncation = 0.0f;
int64_t useDepthPrior = 0;
std::vector<float> vData = {6.0f, 4.0f, 1.0f, 6.9f, 7.0928106f, 0.3491799f,
3.0046327f, 6.6574745f, 7.308903f, 7.6934705f, 0.1315008f, 3.9899914f};
std::vector<int32_t> fData = {2, 1, 0};
std::vector<float> dData(100, 0.0f);
std::vector<int32_t> findicesData(100, 0);
std::vector<float> baryData(10 * 10 * 3, 0.0f);
void* vDeviceAddr = nullptr;
void* fDeviceAddr = nullptr;
void* dDeviceAddr = nullptr;
void* findicesDeviceAddr = nullptr;
void* baryDeviceAddr = nullptr;
aclTensor* v = nullptr;
aclTensor* f = nullptr;
aclTensor* d = nullptr;
aclTensor* findices = nullptr;
aclTensor* barycentric = nullptr;
ret = CreateAclTensor(vData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(fData, fShape, &fDeviceAddr, aclDataType::ACL_INT32, &f);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dData, dShape, &dDeviceAddr, aclDataType::ACL_FLOAT, &d);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(findicesData, findicesShape, &findicesDeviceAddr, aclDataType::ACL_INT32, &findices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(baryData, baryShape, &baryDeviceAddr, aclDataType::ACL_FLOAT, &barycentric);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnRasterizer.
ret = aclnnRasterizerGetWorkspaceSize(v, f, d, width, height, occlusionTruncation, useDepthPrior, findices,
barycentric, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRasterizerGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnRasterizer.
ret = aclnnRasterizer(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRasterizer failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from the device side to the host side. Modify it based on the specific API definition.
auto findicesSize = GetShapeSize(findicesShape);
std::vector<int32_t> findicesOutData(findicesSize, 0);
ret = aclrtMemcpy(findicesOutData.data(), findicesSize * sizeof(findicesOutData[0]), findicesDeviceAddr,
findicesSize * sizeof(findicesOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
auto barycentricSize = GetShapeSize(baryShape);
std::vector<float> baryOutData(barycentricSize, 0);
ret = aclrtMemcpy(baryOutData.data(), barycentricSize * sizeof(baryOutData[0]), baryDeviceAddr,
barycentricSize * sizeof(baryOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < findicesSize; i++) {
LOG_PRINT("findices[%ld] is: %d\n", i, findicesOutData[i]);
}
for (int64_t i = 0; i < barycentricSize; i++) {
LOG_PRINT("barycentric[%ld] is: %f\n", i, baryOutData[i]);
}
// 6. Release aclTensor. Modify it based on the specific API definition.
aclDestroyTensor(v);
aclDestroyTensor(f);
aclDestroyTensor(d);
aclDestroyTensor(findices);
aclDestroyTensor(barycentric);
// 7. Release device resources. Modify it based on the specific API definition.
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}