Skip to content

Commit

Permalink
reverse cube face
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeySmolenchuk committed Dec 14, 2023
1 parent 9a5b5d9 commit 666ff0b
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 181 deletions.
122 changes: 61 additions & 61 deletions example04_firstTriangleMesh/SampleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace osc {
xfm.l.vz = vec3f(0.f,0.f,size.z);
addUnitCube(xfm);
}

/*! add a unit cube (subject to given xfm matrix) to the current
triangleMesh */
void TriangleMesh::addUnitCube(const affine3f &xfm)
Expand All @@ -77,7 +77,7 @@ namespace osc {
vertex.push_back(xfmPoint(xfm,vec3f(1.f,1.f,1.f)));


int indices[] = {0,1,3, 2,3,0,
int indices[] = {0,1,3, 2,0,3,
5,7,6, 5,6,4,
0,4,5, 0,5,1,
2,3,7, 2,7,6,
Expand All @@ -89,17 +89,17 @@ namespace osc {
indices[3*i+1],
indices[3*i+2]));
}


/*! constructor - performs all setup, including initializing
optix, creates module, pipeline, programs, SBT, etc. */
SampleRenderer::SampleRenderer(const TriangleMesh &model)
{
initOptix();

std::cout << "#osc: creating optix context ..." << std::endl;
createContext();

std::cout << "#osc: setting up module ..." << std::endl;
createModule();

Expand All @@ -111,7 +111,7 @@ namespace osc {
createHitgroupPrograms();

launchParams.traversable = buildAccel(model);

std::cout << "#osc: setting up optix pipeline ..." << std::endl;
createPipeline();

Expand All @@ -131,9 +131,9 @@ namespace osc {
// upload the model to the device: the builder
vertexBuffer.alloc_and_upload(model.vertex);
indexBuffer.alloc_and_upload(model.index);

OptixTraversableHandle asHandle { 0 };

// ==================================================================
// triangle inputs
// ==================================================================
Expand All @@ -145,38 +145,38 @@ namespace osc {
// device pointers
CUdeviceptr d_vertices = vertexBuffer.d_pointer();
CUdeviceptr d_indices = indexBuffer.d_pointer();

triangleInput.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3;
triangleInput.triangleArray.vertexStrideInBytes = sizeof(vec3f);
triangleInput.triangleArray.numVertices = (int)model.vertex.size();
triangleInput.triangleArray.vertexBuffers = &d_vertices;

triangleInput.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3;
triangleInput.triangleArray.indexStrideInBytes = sizeof(vec3i);
triangleInput.triangleArray.numIndexTriplets = (int)model.index.size();
triangleInput.triangleArray.indexBuffer = d_indices;

uint32_t triangleInputFlags[1] = { 0 };

// in this example we have one SBT entry, and no per-primitive
// materials:
triangleInput.triangleArray.flags = triangleInputFlags;
triangleInput.triangleArray.numSbtRecords = 1;
triangleInput.triangleArray.sbtIndexOffsetBuffer = 0;
triangleInput.triangleArray.sbtIndexOffsetSizeInBytes = 0;
triangleInput.triangleArray.sbtIndexOffsetStrideInBytes = 0;
triangleInput.triangleArray.sbtIndexOffsetBuffer = 0;
triangleInput.triangleArray.sbtIndexOffsetSizeInBytes = 0;
triangleInput.triangleArray.sbtIndexOffsetStrideInBytes = 0;

// ==================================================================
// BLAS setup
// ==================================================================

OptixAccelBuildOptions accelOptions = {};
accelOptions.buildFlags = OPTIX_BUILD_FLAG_NONE
| OPTIX_BUILD_FLAG_ALLOW_COMPACTION
;
accelOptions.motionOptions.numKeys = 1;
accelOptions.operation = OPTIX_BUILD_OPERATION_BUILD;

OptixAccelBufferSizes blasBufferSizes;
OPTIX_CHECK(optixAccelComputeMemoryUsage
(optixContext,
Expand All @@ -185,51 +185,51 @@ namespace osc {
1, // num_build_inputs
&blasBufferSizes
));

// ==================================================================
// prepare compaction
// ==================================================================

CUDABuffer compactedSizeBuffer;
compactedSizeBuffer.alloc(sizeof(uint64_t));

OptixAccelEmitDesc emitDesc;
emitDesc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
emitDesc.result = compactedSizeBuffer.d_pointer();

// ==================================================================
// execute build (main stage)
// ==================================================================

CUDABuffer tempBuffer;
tempBuffer.alloc(blasBufferSizes.tempSizeInBytes);

CUDABuffer outputBuffer;
outputBuffer.alloc(blasBufferSizes.outputSizeInBytes);

OPTIX_CHECK(optixAccelBuild(optixContext,
/* stream */0,
&accelOptions,
&triangleInput,
1,
1,
tempBuffer.d_pointer(),
tempBuffer.sizeInBytes,

outputBuffer.d_pointer(),
outputBuffer.sizeInBytes,

&asHandle,

&emitDesc,1
));
CUDA_SYNC_CHECK();

// ==================================================================
// perform compaction
// ==================================================================
uint64_t compactedSize;
compactedSizeBuffer.download(&compactedSize,1);

asBuffer.alloc(compactedSize);
OPTIX_CHECK(optixAccelCompact(optixContext,
/*stream:*/0,
Expand All @@ -238,7 +238,7 @@ namespace osc {
asBuffer.sizeInBytes,
&asHandle));
CUDA_SYNC_CHECK();

// ==================================================================
// aaaaaand .... clean up
// ==================================================================
Expand All @@ -248,12 +248,12 @@ namespace osc {

return asHandle;
}

/*! helper function that initializes optix and checks for errors */
void SampleRenderer::initOptix()
{
std::cout << "#osc: initializing optix..." << std::endl;

// -------------------------------------------------------
// check for available optix7 capable devices
// -------------------------------------------------------
Expand Down Expand Up @@ -289,14 +289,14 @@ namespace osc {
const int deviceID = 0;
CUDA_CHECK(SetDevice(deviceID));
CUDA_CHECK(StreamCreate(&stream));

cudaGetDeviceProperties(&deviceProps, deviceID);
std::cout << "#osc: running on device: " << deviceProps.name << std::endl;

CUresult cuRes = cuCtxGetCurrent(&cudaContext);
if( cuRes != CUDA_SUCCESS )
if( cuRes != CUDA_SUCCESS )
fprintf( stderr, "Error querying current context: error code %d\n", cuRes );

OPTIX_CHECK(optixDeviceContextCreate(cudaContext, 0, &optixContext));
OPTIX_CHECK(optixDeviceContextSetLogCallback
(optixContext,context_log_cb,nullptr,4));
Expand All @@ -320,11 +320,11 @@ namespace osc {
pipelineCompileOptions.numAttributeValues = 2;
pipelineCompileOptions.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE;
pipelineCompileOptions.pipelineLaunchParamsVariableName = "optixLaunchParams";

pipelineLinkOptions.maxTraceDepth = 2;

const std::string ptxCode = embedded_ptx_code;

char log[2048];
size_t sizeof_log = sizeof( log );
#if OPTIX_VERSION >= 70700
Expand All @@ -349,19 +349,19 @@ namespace osc {
#endif
if (sizeof_log > 1) PRINT(log);
}



/*! does all setup for the raygen program(s) we are going to use */
void SampleRenderer::createRaygenPrograms()
{
// we do a single ray gen program in this example:
raygenPGs.resize(1);

OptixProgramGroupOptions pgOptions = {};
OptixProgramGroupDesc pgDesc = {};
pgDesc.kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
pgDesc.raygen.module = module;
pgDesc.raygen.module = module;
pgDesc.raygen.entryFunctionName = "__raygen__renderFrame";

// OptixProgramGroup raypg;
Expand All @@ -376,17 +376,17 @@ namespace osc {
));
if (sizeof_log > 1) PRINT(log);
}

/*! does all setup for the miss program(s) we are going to use */
void SampleRenderer::createMissPrograms()
{
// we do a single ray gen program in this example:
missPGs.resize(1);

OptixProgramGroupOptions pgOptions = {};
OptixProgramGroupDesc pgDesc = {};
pgDesc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
pgDesc.miss.module = module;
pgDesc.miss.module = module;
pgDesc.miss.entryFunctionName = "__miss__radiance";

// OptixProgramGroup raypg;
Expand All @@ -401,19 +401,19 @@ namespace osc {
));
if (sizeof_log > 1) PRINT(log);
}

/*! does all setup for the hitgroup program(s) we are going to use */
void SampleRenderer::createHitgroupPrograms()
{
// for this simple example, we set up a single hit group
hitgroupPGs.resize(1);

OptixProgramGroupOptions pgOptions = {};
OptixProgramGroupDesc pgDesc = {};
pgDesc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
pgDesc.hitgroup.moduleCH = module;
pgDesc.hitgroup.moduleCH = module;
pgDesc.hitgroup.entryFunctionNameCH = "__closesthit__radiance";
pgDesc.hitgroup.moduleAH = module;
pgDesc.hitgroup.moduleAH = module;
pgDesc.hitgroup.entryFunctionNameAH = "__anyhit__radiance";

char log[2048];
Expand All @@ -427,7 +427,7 @@ namespace osc {
));
if (sizeof_log > 1) PRINT(log);
}


/*! assembles the full pipeline of all programs */
void SampleRenderer::createPipeline()
Expand All @@ -439,7 +439,7 @@ namespace osc {
programGroups.push_back(pg);
for (auto pg : hitgroupPGs)
programGroups.push_back(pg);

char log[2048];
size_t sizeof_log = sizeof( log );
OPTIX_CHECK(optixPipelineCreate(optixContext,
Expand All @@ -454,12 +454,12 @@ namespace osc {

OPTIX_CHECK(optixPipelineSetStackSize
(/* [in] The pipeline to configure the stack size for */
pipeline,
pipeline,
/* [in] The direct stack size requirement for direct
callables invoked from IS or AH. */
2*1024,
/* [in] The direct stack size requirement for direct
callables invoked from RG, MS, or CH. */
callables invoked from RG, MS, or CH. */
2*1024,
/* [in] The continuation stack requirement. */
2*1024,
Expand Down Expand Up @@ -531,9 +531,9 @@ namespace osc {
// sanity check: make sure we launch only after first resize is
// already done:
if (launchParams.frame.size.x == 0) return;

launchParamsBuffer.upload(&launchParams,1);

OPTIX_CHECK(optixLaunch(/*! pipeline we're launching launch: */
pipeline,stream,
/*! parameters and SBT */
Expand Down Expand Up @@ -567,13 +567,13 @@ namespace osc {
= cosFovy * normalize(cross(launchParams.camera.horizontal,
launchParams.camera.direction));
}

/*! resize frame buffer to given resolution */
void SampleRenderer::resize(const vec2i &newSize)
{
// if window minimized
if (newSize.x == 0 | newSize.y == 0) return;

// resize our cuda frame buffer
colorBuffer.resize(newSize.x*newSize.y*sizeof(uint32_t));

Expand All @@ -592,5 +592,5 @@ namespace osc {
colorBuffer.download(h_pixels,
launchParams.frame.size.x*launchParams.frame.size.y);
}

} // ::osc
Loading

0 comments on commit 666ff0b

Please sign in to comment.