GPU编程系列之(一)——环境配置

          最近在学习GPU编程,蛮有感触的,期间也遇到很多困难,在这里我将它们记下来,也算是一次总结吧。

         开始学的时候有人说康玉之写的那本GPU编程之下里巴人与阳春白雪不错,我看了一段时间,感觉不太适合自己,就在图书馆借了《CG教程——可编程实时图像权威指南》,这本书写的很好,翻译么,还行。反正不影响阅读。推荐大家阅读。因为是配置编程环境,我先将我的编程环境说出来,方便大家参考。 编译器visual studio 2008.由于GPU编程嘛,可定要用到显卡,不过不要太担心,一般显卡就成,本人的显卡么,不好意思说(intel x3100),将就了。呵呵...

        既然是编译程序,就要用到编译器,因为CG没有集成在VS中,需要自己下载。http://developer.nvidia.com/object/cg_download.html 从这可以下载到最新的编译器,接下来就是安装了,选者默认路径。相信只要不是很老的机器,安装一般都会通过。这就好了吗?没呢,才开始。

       学习编程的捷径就是阅读源码,好在CGC自带了大量的例子,对于入门学习很是方便。因为本篇是配置环境,语言细节就就留在以后。用VS新建一个工程,将 C:/Program Files/NVIDIA Corporation/Cg/examples/OpenGL/basic/01_vertex_program中的01_vertex_program.c敲进自己所建的工程中。接着再建一个cpp文件,将C2E1v_green.cg中代码也敲进去,完毕后将后缀改为cg。请确保自己的代码无语发错误。下面我们开始参数的设置。

//01_vertex_program.c

#ifdef _MSC_VER
#pragma comment( lib, "cg.lib" )
#pragma comment( lib, "cgGL.lib" )
#define GLUT_DISABLE_ATEXIT_HACK
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <glut.h>
#endif
#include <Cg/cg.h>
#include <Cg/cgGL.h>

static CGcontext   myCgContext;
static CGprofile   myCgVertexProfile;
static CGprogram   myCgVertexProgram;

static const char *myProgramName = "01_vertex_program",
                  *myVertexProgramFileName = "C2E1v_green.cg",
                  *myVertexProgramName = "main";

static void checkForCgError(const char *situation)
{
  CGerror error;
  const char *string = cgGetLastErrorString(&error);

  if (error != CG_NO_ERROR) {
    printf("%s: %s: %s/n",
      myProgramName, situation, string);
    if (error == CG_COMPILER_ERROR) {
      printf("%s/n", cgGetLastListing(myCgContext));
    }
    exit(1);
  }
}
static void display(void);
static void keyboard(unsigned char c, int x, int y);

int main(int argc, char **argv)
{
  glutInitWindowSize(640, 640);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInit(&argc, argv);

  glutCreateWindow(myProgramName);
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

  glClearColor(0.5, 0.3, 0.1, 0.0);  /* Blue background */

  myCgContext = cgCreateContext();
  checkForCgError("creating context");
  cgGLSetDebugMode(CG_FALSE);
  cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);

  myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
  cgGLSetOptimalOptions(myCgVertexProfile);
  checkForCgError("selecting vertex profile");

  myCgVertexProgram =
    cgCreateProgramFromFile(
      myCgContext,             

      CG_SOURCE,              
      myVertexProgramFileName, 
      myCgVertexProfile,     
      myVertexProgramName,    
      NULL);                 
  checkForCgError("creating vertex program from file");
  cgGLLoadProgram(myCgVertexProgram);
  checkForCgError("loading vertex program");

  glutMainLoop();
  return 0;
}

static void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  cgGLBindProgram(myCgVertexProgram);
  checkForCgError("binding vertex program");

  cgGLEnableProfile(myCgVertexProfile);
  checkForCgError("enabling vertex profile");

  glBegin(GL_TRIANGLES);
    glVertex2f(-0.6, 0.8);
    glVertex2f(0.8, 0.5);
    glVertex2f(0.0, -0.4);
  glEnd();

  cgGLDisableProfile(myCgVertexProfile);
  checkForCgError("disabling vertex profile");

  glutSwapBuffers();
}

static void keyboard(unsigned char c, int x, int y)
{
  switch (c) {
  case 27:  /* Esc key */
    /* Demonstrate proper deallocation of Cg runtime data structures.
       Not strictly necessary if we are simply going to exit. */
    cgDestroyProgram(myCgVertexProgram);
    cgDestroyContext(myCgContext);
    exit(0);
    break;
  }
}
 

//C2E1v_green。cg

struct C2E1v_Output {
  float4 position : POSITION;
  float3 color    : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)

  C2E1v_Output OUT;

  OUT.position = float4(position,0,1);
  OUT.color = float3(0.3,0.5,0.8);

  return OUT; 
}

  1. 右键单击工程中的C2E1v_green.cg,在属性里面可以自定义一个编译规则:

命令行:"$(CG_BIN_PATH)/cgc.exe" "$(InputPath)" -profile arbvp1 -o out.vp

输出:$(OutDir) 然后保存。

2 现在才开始编译。选择C2E1v_green.cg,右键选择编译,出现如下错误

1>(0) : error C3001: no program defined
1>17 lines, 1 errors.

这是为什么么呢?原来该顶点程序的入口错了,原来是C2E1v_green,改为main(cgc默认的)

这次编译就通过了。呵呵,注意,将01_vertex_program.c中 *myVertexProgramName = "C2E1v_green";改为 *myVertexProgramName = "main";。至此,vs与编译器的链接全部完成。接下来就是编译之程序了。下面就是程序的运行结果。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值