2011-07-27 6 views
1

나는이 성가신 것을 알았지 만, Cg 쉐이더에서 구조체 파라미터를 검색하는 더 좋은 방법이 있습니까?Cg 쉐이더에서 struct 파라미터를 가져 오는 것

의 Cg 쉐이더 : 다음 11_two_light_with_structs 예 (OpenGL을)에서

는 쉐이더에서 구조체 재질있다

// From page 128 
struct Material { 
    float3 Ke; 
    float3 Ka; 
    float3 Kd; 
    float3 Ks; 
    float shininess; 
}; 

다음 코드에서 그들은이 수행

C 코드를 :

myCgVertexParam_material_Ke = cgGetNamedParameter(myCgVertexProgram, "material.Ke"); 
myCgVertexParam_material_Ka = cgGetNamedParameter(myCgVertexProgram, "material.Ka"); 
myCgVertexParam_material_Kd = cgGetNamedParameter(myCgVertexProgram, "material.Kd"); 
myCgVertexParam_material_Ks = cgGetNamedParameter(myCgVertexProgram, "material.Ks"); 

여기

cgSetParameterValuefr(myCgVertexParam_materialALL, 13, brassMat) ;//! no. 

내가 배열로 구조체를 치료하기 위해 노력하고있어,하지만 마지막 명령하지만 작동하지 않습니다 지루한 MS, 당신은 그런 식으로 뭔가를

myCgVertexParam_materialALL = cgGetNamedParameter(myCgVertexProgram, "material") ; 

할 수 없습니다 "매개 변수가 숫자 형식이 아닙니다."라는 오류 메시지가 나타납니다.

답변

0

Cg가 바로 이런 식으로 그대로입니다.

HLSL/D3D11은 CBUFFERS의 개념으로이를 향상시킵니다. 결국 당신의 쉐이더에있는 모든 매개 변수를 반영하는 C 스타일의 구조체를 C++ 코드로 작성하고 바보 같은 변수 이름을 사용하지 않고 gpu로 한번에 플러시합니다 :

C++ 코드 :

struct GPUCDATA 
{ 
    // the concatenated modelview-projection matrix 
    float modelViewProj[16] ; // column major 
    float diffuseLightPos[4] ; 
    float diffuseLightColor[4] ; 
    float specularLightPos[4] ; 
    float specularLightColor[4] ; 
    float eyePos[4] ; 
} ; 

hlslShader.vsh ++ C에서 그런

cbuffer Globals : register(b0) 
{ 
    // the concatenated modelview-projection matrix 
    float4x4 modelViewProj ; 
    float4 diffuseLightPos ; 
    float4 diffuseLightColor ; 
    float4 specularLightPos ; 
    float4 specularLightColor ; 
    float4 eyePos ; 
} ; 

:

// gpu is the ID3D11DeviceContext 
D3D11_MAPPED_SUBRESOURCE mappedRes ; 
CHECK_HRESULT(gpu->Map(gpuCDataBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedRes), "Map gpucdatabuffer"); 
GPUCDATA* gpuMems = (GPUCDATA*)mappedRes.pData ; 
memcpy(gpuMems, &gpuCData, sizeof(GPUCDATA)) ; // write to the gpu mems. 
gpu->Unmap(gpuCDataBuffer, 0) ; 
gpu->VSSetConstantBuffers(0, 1, &gpuCDataBuffer); 
gpu->PSSetConstantBuffers(0, 1, &gpuCDataBuffer); 

그리고 그게 다야!