summaryrefslogtreecommitdiffstats
path: root/o3d/import/mac/collada_conditioner_mac.mm
blob: 97f98fad1d68d65bfb9341b53554d4b99e8824f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright 2009, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//
// This file contains the defintions of the functions in the
// conditioner namespace, which do the bulk of the work of
// conditioning a Collada file for use in O3D.

#include "import/cross/precompile.h"

#include <vector>
#import <Cocoa/Cocoa.h>

#include "base/base_paths.h"
#include "base/file_path.h"
#include "base/path_service.h"
#include "core/cross/error.h"
#include "import/cross/collada.h"
#include "import/cross/collada_conditioner.h"
#include "utils/cross/file_path_utils.h"

namespace o3d {
bool ColladaConditioner::CompileHLSL(const String& shader_source,
                                     const String& vs_entry,
                                     const String& ps_entry) {
  // The HLSL compiler isn't available on Mac.
  return true;
}

// Find cgc executable, and run it on the input so that we can get a
// preprocessed version out.
bool ColladaConditioner::PreprocessShaderFile(const FilePath& in_filename,
                                              const FilePath& out_filename) {
  FilePath executable_path;
  if (!PathService::Get(base::DIR_EXE, &executable_path)) {
    O3D_ERROR(service_locator_) << "Couldn't get executable path.";
    return false;
  }
  executable_path = executable_path.Append(FILE_PATH_LITERAL("cgc"));

  // Have to convert the file paths to UTF-8
  std::string executable_path_utf8 = FilePathToUTF8(executable_path);
  std::string in_filename_utf8 = FilePathToUTF8(in_filename);
  std::string out_filename_utf8 = FilePathToUTF8(out_filename);

  DLOG(INFO) << "Now invoking '"
             << executable_path_utf8 << " -E "
             << in_filename_utf8 << " -o "
             << out_filename_utf8 << "'";

  // Now we have to convert the UTF-8 file paths to NSStrings.
  NSString *executable_path_ns =
      [[[NSString alloc] initWithUTF8String:executable_path_utf8.c_str()]
        autorelease];
  NSString *in_filename_ns =
      [[[NSString alloc] initWithUTF8String:in_filename_utf8.c_str()]
        autorelease];
  NSString *out_filename_ns =
      [[[NSString alloc] initWithUTF8String:out_filename_utf8.c_str()]
        autorelease];

  NSTask *task = [[[NSTask alloc] init] autorelease];
  [task setLaunchPath:executable_path_ns];
  [task setArguments:[NSArray arrayWithObjects:@"-E", in_filename_ns,
                                @"-o", out_filename_ns, nil]];

  // We send the output to a pipe, even though the tool doesn't really
  // output anything.  This is so that the invocation of the command
  // will be synchronous, and we can be sure that the output file has
  // been written by the time we exit this function.
  NSPipe *pipe = [NSPipe pipe];
  [task setStandardOutput:pipe];
  [task launch];
  NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
  return true;
}
}  // end namespace o3d